diff --git a/src/main/java/com/dotcms/saml/osgi/Activator.java b/src/main/java/com/dotcms/saml/osgi/Activator.java index cd61285..14b4639 100644 --- a/src/main/java/com/dotcms/saml/osgi/Activator.java +++ b/src/main/java/com/dotcms/saml/osgi/Activator.java @@ -3,27 +3,26 @@ import java.util.HashMap; import java.util.Hashtable; import java.util.Map; - +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; +import org.osgi.framework.startlevel.BundleStartLevel; import com.dotcms.auth.providers.saml.v1.DotSamlResource; import com.dotcms.filters.interceptor.FilterWebInterceptorProvider; import com.dotcms.filters.interceptor.WebInterceptorDelegate; import com.dotcms.filters.interceptor.saml.SamlWebInterceptor; import com.dotcms.rest.config.RestServiceUtil; import com.dotcms.saml.DotSamlProxyFactory; +import com.dotcms.saml.SamlServiceBuilder; +import com.dotcms.saml.service.impl.SamlServiceBuilderImpl; +import com.dotcms.saml.service.init.Initializer; +import com.dotcms.saml.service.init.SamlInitializer; import com.dotcms.security.apps.AppSecretSavedEvent; import com.dotmarketing.business.APILocator; import com.dotmarketing.filters.AutoLoginFilter; +import com.dotmarketing.osgi.GenericBundleActivator; import com.dotmarketing.util.Config; import com.dotmarketing.util.Logger; import io.vavr.control.Try; -import org.opensaml.core.config.InitializationService; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; -import com.dotcms.saml.SamlServiceBuilder; -import com.dotcms.saml.service.impl.SamlServiceBuilderImpl; -import com.dotcms.saml.service.init.Initializer; -import com.dotcms.saml.service.init.SamlInitializer; -import com.dotmarketing.osgi.GenericBundleActivator; /** * This activator will register the {@link SamlServiceBuilder} this class will provide the main @@ -38,89 +37,125 @@ public class Activator extends GenericBundleActivator { private ServiceRegistration samlServiceBuilder; private String interceptorName; + private final String DOT_SAML_STATE = "DOT_SAML_STATE"; + private final String STARTED = "STARTED"; + private final String STOPPED = "STOPPED"; private final Class clazz = DotSamlResource.class; private final String VERSION = "21.04"; - private final long buildNumber = 1; + private final long buildNumber = 1; @SuppressWarnings("unchecked") public void start(final BundleContext context) throws Exception { + + BundleStartLevel bundleStartLevel = context.getBundle().adapt(BundleStartLevel.class); + + + Logger.warn(this.getClass().getName(), " "); + Logger.warn(this.getClass().getName(), "dotSAML startlevel = " + bundleStartLevel.getStartLevel()); + Logger.warn(this.getClass().getName(), "dotSAML persistentlyStarted = " + bundleStartLevel.isPersistentlyStarted()); + Logger.warn(this.getClass().getName(), "dotSAML activationPolicyUsed = " + bundleStartLevel.isActivationPolicyUsed()); + Logger.warn(this.getClass().getName(), " "); + + try { + activate(context); + } catch (Exception e) { + Logger.warn(this.getClass().getName(), "dotSAML failed to activate:" + e.getMessage(), e); + System.setProperty(DOT_SAML_STATE, STOPPED); + } + } + + private void activate(final BundleContext context) { - System.out.println("SAML OSGI STARTING INIT....."); - System.out.println("SAML version: " + VERSION + ", build number: " + buildNumber); - final Map contextMap = new HashMap<>(); - final Initializer initializer = new SamlInitializer(); + if (STARTED.equals(System.getProperty(DOT_SAML_STATE))) { + Logger.warn(this.getClass().getName(), "dotSAML already activated, returning"); + return; + } - initializer.init(contextMap); + synchronized (Config.class) { - final SamlServiceBuilderImpl samlServiceBuilderImpl = new SamlServiceBuilderImpl(); - samlServiceBuilderImpl.setInitializer(initializer); + if (STARTED.equals(System.getProperty(DOT_SAML_STATE))) { + Logger.warn(this.getClass().getName(), "dotSAML already activated, returning"); + return; + } - // Register the TikaServiceBuilder as a OSGI service - this.samlServiceBuilder = context.registerService(SamlServiceBuilder.class.getName(), samlServiceBuilderImpl, - new Hashtable<>()); + System.out.println("SAML OSGI STARTING INIT....."); + System.out.println("SAML version: " + VERSION + ", build number: " + buildNumber); - Logger.info(this.getClass().getName(), "Adding the SAML Web Filter"); + final Map contextMap = new HashMap<>(); + final Initializer initializer = new SamlInitializer(); - addSamlWebInterceptor(); + initializer.init(contextMap); + final SamlServiceBuilderImpl samlServiceBuilderImpl = new SamlServiceBuilderImpl(); + samlServiceBuilderImpl.setInitializer(initializer); - Logger.info(this.getClass().getName(), "Adding the SAML Web Service: " + clazz.getName()); - RestServiceUtil.addResource(clazz); + // Register the TikaServiceBuilder as a OSGI service + this.samlServiceBuilder = context.registerService(SamlServiceBuilder.class.getName(), + samlServiceBuilderImpl, new Hashtable<>()); - Logger.info(this.getClass().getName(), "Subscribing to AppSecretSavedEvent events"); - Try.run (()-> APILocator.getLocalSystemEventsAPI(). - subscribe(AppSecretSavedEvent.class, DotSamlProxyFactory.getInstance())) - .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); + Logger.info(this.getClass().getName(), "Adding the SAML Web Filter"); + addSamlWebInterceptor(); - System.out.println("SAML OSGI STARTED....."); + Logger.info(this.getClass().getName(), "Adding the SAML Web Service: " + clazz.getName()); + RestServiceUtil.addResource(clazz); + Logger.info(this.getClass().getName(), "Subscribing to AppSecretSavedEvent events"); + Try.run(() -> APILocator.getLocalSystemEventsAPI().subscribe(AppSecretSavedEvent.class, + DotSamlProxyFactory.getInstance())) + .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); + + System.out.println("SAML OSGI STARTED....."); + + System.setProperty(DOT_SAML_STATE, STARTED); + } } private void addSamlWebInterceptor() { final FilterWebInterceptorProvider filterWebInterceptorProvider = - FilterWebInterceptorProvider.getInstance(Config.CONTEXT); + FilterWebInterceptorProvider.getInstance(Config.CONTEXT); - final WebInterceptorDelegate delegate = - filterWebInterceptorProvider.getDelegate(AutoLoginFilter.class); + final WebInterceptorDelegate delegate = filterWebInterceptorProvider.getDelegate(AutoLoginFilter.class); final SamlWebInterceptor samlWebInterceptor = new SamlWebInterceptor(); this.interceptorName = samlWebInterceptor.getName(); - // in old versions of dotcms may still have the interceptor on core, so lets remove it before add a new one - Try.run(()->delegate.remove(this.interceptorName, true)) - .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); + // in old versions of dotcms may still have the interceptor on core, so lets remove it before add a + // new one + Try.run(() -> delegate.remove(this.interceptorName, true)) + .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); delegate.add(samlWebInterceptor); } - public void stop(final BundleContext context) throws Exception { Logger.info(this.getClass().getName(), "UnSubscribing to AppSecretSavedEvent events."); - Try.run (()-> APILocator.getLocalSystemEventsAPI(). - unsubscribe(AppSecretSavedEvent.class, DotSamlProxyFactory.getInstance().getClass().getName())) - .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); + Try.run(() -> APILocator.getLocalSystemEventsAPI().unsubscribe(AppSecretSavedEvent.class, + DotSamlProxyFactory.getInstance().getClass().getName())) + .onFailure(e -> Logger.error(this.getClass().getName(), e.getMessage())); Logger.info(this.getClass().getName(), "Removing the SAML Web Service"); RestServiceUtil.removeResource(clazz); Logger.info(this.getClass().getName(), "Removing the SAML Web Filter"); final FilterWebInterceptorProvider filterWebInterceptorProvider = - FilterWebInterceptorProvider.getInstance(Config.CONTEXT); + FilterWebInterceptorProvider.getInstance(Config.CONTEXT); - final WebInterceptorDelegate delegate = - filterWebInterceptorProvider.getDelegate(AutoLoginFilter.class); + final WebInterceptorDelegate delegate = filterWebInterceptorProvider.getDelegate(AutoLoginFilter.class); delegate.remove(this.interceptorName, true); // Unregister the registered services this.samlServiceBuilder.unregister(); + + System.setProperty(DOT_SAML_STATE, STOPPED); + } }