|
|
|
@ -18,6 +18,7 @@ import org.gcube.common.events.Observes;
|
|
|
|
|
import org.gcube.common.events.Observes.Kind;
|
|
|
|
|
import org.gcube.smartgears.Constants;
|
|
|
|
|
import org.gcube.smartgears.configuration.Mode;
|
|
|
|
|
import org.gcube.smartgears.context.Property;
|
|
|
|
|
import org.gcube.smartgears.context.application.ApplicationContext;
|
|
|
|
|
import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent;
|
|
|
|
|
import org.gcube.smartgears.handlers.application.ApplicationLifecycleHandler;
|
|
|
|
@ -46,8 +47,6 @@ import org.slf4j.LoggerFactory;
|
|
|
|
|
* </ul>
|
|
|
|
|
*
|
|
|
|
|
* @author Fabio Simeoni
|
|
|
|
|
* @see ProfileBuilder
|
|
|
|
|
* @see ProfilePublisherImpl
|
|
|
|
|
*/
|
|
|
|
|
public class ApplicationProfileManager extends ApplicationLifecycleHandler {
|
|
|
|
|
|
|
|
|
@ -55,7 +54,7 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler {
|
|
|
|
|
|
|
|
|
|
private ApplicationContext context;
|
|
|
|
|
private ScheduledFuture<?> periodicUpdates;
|
|
|
|
|
|
|
|
|
|
private static final String PUBLISHED_PROP = "published";
|
|
|
|
|
private List<Publisher> publishers = ProviderFactory.provider().publishers();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -80,12 +79,12 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void activated(){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
publishers = context.container().configuration().mode()!=Mode.offline?
|
|
|
|
|
ProviderFactory.provider().publishers():
|
|
|
|
|
Collections.emptyList();
|
|
|
|
|
registerObservers();
|
|
|
|
|
schedulePeriodicUpdates();
|
|
|
|
|
registerObservers();
|
|
|
|
|
schedulePeriodicUpdates();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// helpers
|
|
|
|
@ -118,19 +117,28 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler {
|
|
|
|
|
|
|
|
|
|
//if we've failed before first publication do not try to publish
|
|
|
|
|
//(we may well have failed there)
|
|
|
|
|
for (Publisher publisher: publishers)
|
|
|
|
|
try {
|
|
|
|
|
publisher.update(context);
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
|
|
|
|
|
log.error("cannot publish {} with publisher type {} (see details)",context.name(), 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);
|
|
|
|
|
if (context.properties().contains(PUBLISHED_PROP)) {
|
|
|
|
|
log.info("publishing application for the first time");
|
|
|
|
|
context.properties().add(new Property(PUBLISHED_PROP, true));
|
|
|
|
|
if (context.lifecycle().state() != ApplicationState.failed) {
|
|
|
|
|
publishers.forEach(p -> {
|
|
|
|
|
try {
|
|
|
|
|
p.create(context,
|
|
|
|
|
context.container().configuration().authorizationProvider().getContexts());
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
log.error("cannot publish {} for first time with publisher type {} (see details)",context.name(), p.getClass().getCanonicalName(), e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
publishers.forEach(p -> {
|
|
|
|
|
try {
|
|
|
|
|
p.update(context);
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
log.error("cannot publish {} with publisher type {} (see details)",context.name(), p.getClass().getCanonicalName(), e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -158,20 +166,20 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler {
|
|
|
|
|
@Observes(value = removeFromContext)
|
|
|
|
|
void removeFrom(String scope) {
|
|
|
|
|
for (Publisher publisher: publishers)
|
|
|
|
|
try {
|
|
|
|
|
log.debug("unpublishing application from scope {}", scope);
|
|
|
|
|
publisher.remove(context,
|
|
|
|
|
Collections.singleton(scope));
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
|
|
|
|
|
log.error("cannot remove scope {} 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);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
log.debug("unpublishing application from scope {}", scope);
|
|
|
|
|
publisher.remove(context,
|
|
|
|
|
Collections.singleton(scope));
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
|
|
|
|
|
log.error("cannot remove scope {} 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
@ -206,7 +214,7 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler {
|
|
|
|
|
context.events().fire(context,changed);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
periodicUpdates = Utils.scheduledServicePool.scheduleAtFixedRate(updateTask,
|
|
|
|
|
Constants.application_republish_frequency_in_minutes,
|
|
|
|
|
Constants.application_republish_frequency_in_minutes , TimeUnit.MINUTES);
|
|
|
|
|