|
|
|
@ -60,7 +60,6 @@ public class ContainerProfileManager extends ContainerHandler {
|
|
|
|
|
private static final String PUBLISHED_PROP = "published";
|
|
|
|
|
private List<Publisher> publishers;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onStart(ContainerLifecycleEvent.Start e) {
|
|
|
|
|
|
|
|
|
@ -80,13 +79,11 @@ public class ContainerProfileManager extends ContainerHandler {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void activated(){
|
|
|
|
|
publishers = context.configuration().mode()!=Mode.offline?
|
|
|
|
|
ProviderFactory.provider().publishers():
|
|
|
|
|
Collections.emptyList();
|
|
|
|
|
registerObservers();
|
|
|
|
|
schedulePeriodicUpdates();
|
|
|
|
|
private void activated() {
|
|
|
|
|
publishers = context.configuration().mode() != Mode.offline ? ProviderFactory.provider().publishers()
|
|
|
|
|
: Collections.emptyList();
|
|
|
|
|
registerObservers();
|
|
|
|
|
schedulePeriodicUpdates();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void registerObservers() {
|
|
|
|
@ -94,59 +91,62 @@ public class ContainerProfileManager extends ContainerHandler {
|
|
|
|
|
@Observes({ activation, part_activation, shutdown, stop, failure })
|
|
|
|
|
void onChanged(ContainerLifecycle lc) {
|
|
|
|
|
|
|
|
|
|
// since we do not know the observers, they will deal with failures and their consequences
|
|
|
|
|
// since we do not know the observers, they will deal with failures and their
|
|
|
|
|
// consequences
|
|
|
|
|
// any that comes back will be logged in this event thread
|
|
|
|
|
context.events().fire(context, changed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Observes(value = changed, kind = critical)
|
|
|
|
|
void publishAfterChange(ContainerContext context) {
|
|
|
|
|
log.info("Publish after profile Change event called");
|
|
|
|
|
//if we've failed before first publication do not try to publish
|
|
|
|
|
//(we may well have failed there)
|
|
|
|
|
if (!context.properties().contains(PUBLISHED_PROP)) {
|
|
|
|
|
context.properties().add(new Property(PUBLISHED_PROP, true));
|
|
|
|
|
log.info("publishing container for the first time");
|
|
|
|
|
if (context.lifecycle().state() != ContainerState.failed) {
|
|
|
|
|
publishers.forEach(p -> {
|
|
|
|
|
log.info("Publish after profile Change event called -- contains published prop? {}",context.properties().contains(PUBLISHED_PROP));
|
|
|
|
|
// if we've failed before first publication do not try to publish
|
|
|
|
|
// (we may well have failed there)
|
|
|
|
|
if (context.lifecycle().state() != ContainerState.failed) {
|
|
|
|
|
if (!context.properties().contains(PUBLISHED_PROP)) {
|
|
|
|
|
context.properties().add(new Property(PUBLISHED_PROP, true));
|
|
|
|
|
log.info("publishing container for the first time");
|
|
|
|
|
|
|
|
|
|
publishers.parallelStream().forEach(p -> {
|
|
|
|
|
try {
|
|
|
|
|
p.create(context,
|
|
|
|
|
context.authorizationProvider().getContexts());
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
log.error("cannot publish container for first time with publisher type {} (see details)", p.getClass().getCanonicalName(), e);
|
|
|
|
|
p.create(context, context.authorizationProvider().getContexts());
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
|
log.error(
|
|
|
|
|
"cannot publish container for first time with publisher type {} (see details)",
|
|
|
|
|
p.getClass().getCanonicalName(), e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else
|
|
|
|
|
publishers.parallelStream().forEach(p -> {
|
|
|
|
|
try {
|
|
|
|
|
p.update(context);
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
|
log.error("cannot publish container with publisher type {} (see details)",
|
|
|
|
|
p.getClass().getCanonicalName(), e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
publishers.forEach(p -> {
|
|
|
|
|
try {
|
|
|
|
|
p.update(context);
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
log.error("cannot publish container with publisher type {} (see details)", p.getClass().getCanonicalName(), e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Observes(value = addToContext)
|
|
|
|
|
void addTo(String scope) {
|
|
|
|
|
log.info("add_to_context event arrived in container");
|
|
|
|
|
for (Publisher publisher: publishers)
|
|
|
|
|
for (Publisher publisher : publishers)
|
|
|
|
|
try {
|
|
|
|
|
log.trace("publishing container within new scope");
|
|
|
|
|
publisher.create(context,
|
|
|
|
|
Collections.singleton(scope));
|
|
|
|
|
publisher.create(context, Collections.singleton(scope));
|
|
|
|
|
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
|
|
log.error("cannot add container to {} with publisher type {} (see details)",scope, publisher.getClass().getCanonicalName(), e);
|
|
|
|
|
log.error("cannot add container to {} with publisher type {} (see details)", scope,
|
|
|
|
|
publisher.getClass().getCanonicalName(), e);
|
|
|
|
|
|
|
|
|
|
// since we've failed no published event is fired and profile
|
|
|
|
|
// will not be stored.
|
|
|
|
|
// we do it manually to ensure we leave some local trace of the
|
|
|
|
|
// changed profile.
|
|
|
|
|
//TODO: CHECK --- store(profile);
|
|
|
|
|
// TODO: CHECK --- store(profile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -154,67 +154,66 @@ public class ContainerProfileManager extends ContainerHandler {
|
|
|
|
|
@Observes(value = removeFromContext)
|
|
|
|
|
void removeFrom(String scope) {
|
|
|
|
|
log.info("remove_from_context event arrived in container");
|
|
|
|
|
for (Publisher publisher: publishers)
|
|
|
|
|
for (Publisher publisher : publishers)
|
|
|
|
|
try {
|
|
|
|
|
log.trace("unpublishing container from context {}", scope);
|
|
|
|
|
publisher.remove(context,
|
|
|
|
|
Collections.singleton(scope));
|
|
|
|
|
publisher.remove(context, Collections.singleton(scope));
|
|
|
|
|
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
|
|
log.error("cannot remove container from {} with publisher type {} (see details)",scope, publisher.getClass().getCanonicalName(), e);
|
|
|
|
|
log.error("cannot remove container from {} with publisher type {} (see details)", scope,
|
|
|
|
|
publisher.getClass().getCanonicalName(), e);
|
|
|
|
|
|
|
|
|
|
// since we've failed no published event is fired and profile
|
|
|
|
|
// will not be stored.
|
|
|
|
|
// we do it manually to ensure we leave some local trace of the
|
|
|
|
|
// changed profile.
|
|
|
|
|
//TODO: CHECK --- store(profile);
|
|
|
|
|
// TODO: CHECK --- store(profile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void schedulePeriodicUpdates() {
|
|
|
|
|
// register to cancel updates
|
|
|
|
|
context.events().subscribe(
|
|
|
|
|
new Object() {
|
|
|
|
|
// we register it in response to lifecycle events so that we can stop and resume along with application
|
|
|
|
|
@Observes(value = { activation, part_activation }, kind = resilient)
|
|
|
|
|
synchronized void restartPeriodicUpdates(ContainerLifecycle lc) {
|
|
|
|
|
//already running
|
|
|
|
|
if (periodicUpdates!=null)
|
|
|
|
|
return;
|
|
|
|
|
if (lc.state()==active)
|
|
|
|
|
log.info("scheduling periodic updates of container profile");
|
|
|
|
|
else
|
|
|
|
|
log.info("resuming periodic updates of container profile");
|
|
|
|
|
final Runnable updateTask = new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
context.events().fire(context,changed);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
periodicUpdates = Utils.scheduledServicePool.scheduleAtFixedRate(updateTask, 3, context.configuration()
|
|
|
|
|
.publicationFrequency(), SECONDS);
|
|
|
|
|
context.events().subscribe(new Object() {
|
|
|
|
|
// we register it in response to lifecycle events so that we can stop and resume
|
|
|
|
|
// along with application
|
|
|
|
|
@Observes(value = { activation, part_activation }, kind = resilient)
|
|
|
|
|
synchronized void restartPeriodicUpdates(ContainerLifecycle lc) {
|
|
|
|
|
// already running
|
|
|
|
|
if (periodicUpdates != null)
|
|
|
|
|
return;
|
|
|
|
|
if (lc.state() == active)
|
|
|
|
|
log.info("scheduling periodic updates of container profile");
|
|
|
|
|
else
|
|
|
|
|
log.info("resuming periodic updates of container profile");
|
|
|
|
|
final Runnable updateTask = new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
context.events().fire(context, changed);
|
|
|
|
|
}
|
|
|
|
|
@Observes(value = { stop, failure, shutdown }, kind = resilient)
|
|
|
|
|
synchronized void cancelPeriodicUpdates(ContainerLifecycle ignore) {
|
|
|
|
|
if (periodicUpdates != null){
|
|
|
|
|
log.trace("stopping periodic updates of container profile");
|
|
|
|
|
try {
|
|
|
|
|
periodicUpdates.cancel(true);
|
|
|
|
|
periodicUpdates=null;
|
|
|
|
|
}
|
|
|
|
|
catch(Exception e) {
|
|
|
|
|
log.warn("could not stop periodic updates of container profile",e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
periodicUpdates = Utils.scheduledServicePool.scheduleAtFixedRate(updateTask, 3,
|
|
|
|
|
context.configuration().publicationFrequency(), SECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Observes(value = { stop, failure, shutdown }, kind = resilient)
|
|
|
|
|
synchronized void cancelPeriodicUpdates(ContainerLifecycle ignore) {
|
|
|
|
|
if (periodicUpdates != null) {
|
|
|
|
|
log.trace("stopping periodic updates of container profile");
|
|
|
|
|
try {
|
|
|
|
|
periodicUpdates.cancel(true);
|
|
|
|
|
periodicUpdates = null;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.warn("could not stop periodic updates of container profile", e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return profile_management;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|