|
|
|
@ -54,8 +54,8 @@ public class ContainerManager {
|
|
|
|
|
this.context = context;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (context.configuration().mode()!=Mode.offline)
|
|
|
|
|
validateContainer(context);
|
|
|
|
|
|
|
|
|
@ -65,18 +65,17 @@ public class ContainerManager {
|
|
|
|
|
|
|
|
|
|
log.trace("managing container lifecycle with {}", handlers);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
startHandlers(handlers);
|
|
|
|
|
|
|
|
|
|
context.lifecycle().moveTo(active);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
catch(RuntimeException e) {
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
|
|
|
|
|
log.error("cannot manage container (see cause)",e);
|
|
|
|
|
log.error("cannot manage container (see cause)", e);
|
|
|
|
|
|
|
|
|
|
if (context!=null)
|
|
|
|
|
if (context != null)
|
|
|
|
|
context.lifecycle().moveTo(failed);
|
|
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
@ -84,12 +83,11 @@ public class ContainerManager {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void saveContainerState() {
|
|
|
|
|
File file = context.persistenceWriter().file(container_profile_file_path);
|
|
|
|
|
try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))){
|
|
|
|
|
File file = context.persistenceWriter().file(container_profile_file_path);
|
|
|
|
|
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
|
|
|
|
|
oos.writeObject(context.id());
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("error serializing cointainer state");
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
@ -97,40 +95,40 @@ public class ContainerManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void validateContainer(ContainerContext context) {
|
|
|
|
|
//List<String> tokensToRemove = new ArrayList<String>();
|
|
|
|
|
// List<String> tokensToRemove = new ArrayList<String>();
|
|
|
|
|
context.configuration().validate();
|
|
|
|
|
Set<String> foundContexts;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
foundContexts = context.authorizationProvider().getContexts();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("error authorizing container",e);
|
|
|
|
|
throw new RuntimeException("error authorizing container, moving the container to failed",e);
|
|
|
|
|
}
|
|
|
|
|
log.error("error authorizing container", e);
|
|
|
|
|
throw new RuntimeException("error authorizing container, moving the container to failed", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (foundContexts.isEmpty()){
|
|
|
|
|
if (foundContexts.isEmpty()) {
|
|
|
|
|
log.error("no valid contexts found, moving the container to failed");
|
|
|
|
|
throw new RuntimeException("no valid contexts found, moving the container to failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void manage(ApplicationContext app) {
|
|
|
|
|
app.events().subscribe(this);
|
|
|
|
|
app.events().subscribe(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Observes(value={ApplicationLifecycle.failure,ApplicationLifecycle.stop},kind=Kind.critical)
|
|
|
|
|
@Observes(value = { ApplicationLifecycle.failure, ApplicationLifecycle.stop }, kind = Kind.critical)
|
|
|
|
|
void monitorApplication(ApplicationLifecycle lifecycle) {
|
|
|
|
|
context.lifecycle().tryMoveTo(ContainerState.partActive);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Observes(value=ContextEvents.ADD_CONTEXT_TO_CONTAINER,kind=Kind.critical)
|
|
|
|
|
@Observes(value = ContextEvents.ADD_CONTEXT_TO_CONTAINER, kind = Kind.critical)
|
|
|
|
|
void addContext(String scope) {
|
|
|
|
|
log.info("adding context {} event send", context);
|
|
|
|
|
context.events().fire(scope, ProfileEvents.addToContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Observes(value=ContextEvents.REMOVE_CONTEXT_FROM_CONTAINER,kind=Kind.critical)
|
|
|
|
|
@Observes(value = ContextEvents.REMOVE_CONTEXT_FROM_CONTAINER, kind = Kind.critical)
|
|
|
|
|
void removeContext(String scope) {
|
|
|
|
|
log.info("removing context {} event send", context);
|
|
|
|
|
context.events().fire(scope, ProfileEvents.removeFromContext);
|
|
|
|
@ -145,18 +143,20 @@ public class ContainerManager {
|
|
|
|
|
stop(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stops container management on remote request or container shutdown.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public void stop(boolean shutdown) {
|
|
|
|
|
|
|
|
|
|
//two cases: stop-on-shutdown and stop-on-request, some listeners will be selective about this,
|
|
|
|
|
// two cases: stop-on-shutdown and stop-on-request, some listeners will be
|
|
|
|
|
// selective about this,
|
|
|
|
|
|
|
|
|
|
//shutdown is triggered by probe app, which is notified among other apps
|
|
|
|
|
//if other app have been already notified, the container may already be part-active.
|
|
|
|
|
//apps still to notify will listen only on stop, hence won't react to this but will go down when their turn arrives.
|
|
|
|
|
// shutdown is triggered by probe app, which is notified among other apps
|
|
|
|
|
// if other app have been already notified, the container may already be
|
|
|
|
|
// part-active.
|
|
|
|
|
// apps still to notify will listen only on stop, hence won't react to this but
|
|
|
|
|
// will go down when their turn arrives.
|
|
|
|
|
|
|
|
|
|
if (context == null)
|
|
|
|
|
return;
|
|
|
|
@ -165,26 +165,23 @@ public class ContainerManager {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
context.lifecycle().tryMoveTo(shutdown?down:stopped);
|
|
|
|
|
|
|
|
|
|
context.lifecycle().tryMoveTo(shutdown ? down : stopped);
|
|
|
|
|
|
|
|
|
|
stopHandlers();
|
|
|
|
|
|
|
|
|
|
//no further reactions
|
|
|
|
|
// no further reactions
|
|
|
|
|
log.info("stopping container events");
|
|
|
|
|
context.events().stop();
|
|
|
|
|
Utils.scheduledServicePool.shutdownNow();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (RuntimeException e) {
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
|
|
|
|
|
log.warn("cannot stop container management (see cause)", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//helpers
|
|
|
|
|
// helpers
|
|
|
|
|
|
|
|
|
|
private void startHandlers(List<ContainerHandler> handlers) {
|
|
|
|
|
|
|
|
|
@ -201,7 +198,6 @@ public class ContainerManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void stopHandlers() {
|
|
|
|
|
|
|
|
|
|
if (pipeline == null)
|
|
|
|
@ -214,46 +210,32 @@ public class ContainerManager {
|
|
|
|
|
returnPipeline.forward(new ContainerLifecycleEvent.Stop(context));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
private void loadKeyForToken(List<String> tokens) {
|
|
|
|
|
String initialToken = SecurityTokenProvider.instance.get();
|
|
|
|
|
|
|
|
|
|
//TODO: change this
|
|
|
|
|
String filePath = "/tmp/keys";
|
|
|
|
|
File PathDirs = new File(filePath+"/");
|
|
|
|
|
PathDirs.mkdirs();
|
|
|
|
|
try{
|
|
|
|
|
for (String token : tokens) {
|
|
|
|
|
try{
|
|
|
|
|
SecurityTokenProvider.instance.set(token);
|
|
|
|
|
File key = authProvider.getSymmKey(filePath);
|
|
|
|
|
log.trace("loading key {} file name ",key.getAbsolutePath());
|
|
|
|
|
log.trace("loaded key {} file name ",key.getAbsolutePath());
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
log.warn("error loading key for token {}", token, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
loadFileIntoClasspath(PathDirs);
|
|
|
|
|
}finally{
|
|
|
|
|
SecurityTokenProvider.instance.set(initialToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void loadFileIntoClasspath(File file){
|
|
|
|
|
try {
|
|
|
|
|
URL url = file.toURI().toURL();
|
|
|
|
|
|
|
|
|
|
ClassLoader currentClassloader = Thread.currentThread().getContextClassLoader().getParent()==null?
|
|
|
|
|
Thread.currentThread().getContextClassLoader() : Thread.currentThread().getContextClassLoader().getParent();
|
|
|
|
|
|
|
|
|
|
URLClassLoader classLoader = (URLClassLoader)currentClassloader;
|
|
|
|
|
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
|
|
|
|
|
method.setAccessible(true);
|
|
|
|
|
method.invoke(classLoader, url);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
log.error("error loading file into classpath",ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
* private void loadKeyForToken(List<String> tokens) { String initialToken =
|
|
|
|
|
* SecurityTokenProvider.instance.get();
|
|
|
|
|
*
|
|
|
|
|
* //TODO: change this String filePath = "/tmp/keys"; File PathDirs = new
|
|
|
|
|
* File(filePath+"/"); PathDirs.mkdirs(); try{ for (String token : tokens) {
|
|
|
|
|
* try{ SecurityTokenProvider.instance.set(token); File key =
|
|
|
|
|
* authProvider.getSymmKey(filePath);
|
|
|
|
|
* log.trace("loading key {} file name ",key.getAbsolutePath());
|
|
|
|
|
* log.trace("loaded key {} file name ",key.getAbsolutePath()); }catch(Exception
|
|
|
|
|
* e){ log.warn("error loading key for token {}", token, e); } }
|
|
|
|
|
* loadFileIntoClasspath(PathDirs); }finally{
|
|
|
|
|
* SecurityTokenProvider.instance.set(initialToken); } }
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* private void loadFileIntoClasspath(File file){ try { URL url =
|
|
|
|
|
* file.toURI().toURL();
|
|
|
|
|
*
|
|
|
|
|
* ClassLoader currentClassloader =
|
|
|
|
|
* Thread.currentThread().getContextClassLoader().getParent()==null?
|
|
|
|
|
* Thread.currentThread().getContextClassLoader() :
|
|
|
|
|
* Thread.currentThread().getContextClassLoader().getParent();
|
|
|
|
|
*
|
|
|
|
|
* URLClassLoader classLoader = (URLClassLoader)currentClassloader; Method
|
|
|
|
|
* method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
|
|
|
|
|
* method.setAccessible(true); method.invoke(classLoader, url); } catch
|
|
|
|
|
* (Exception ex) { log.error("error loading file into classpath",ex); } }
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|