--- /dev/null
+/*
+ * Copyright (c) 2018 Inocybe Technologies and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.aaa;
+
+import java.util.Hashtable;
+import java.util.concurrent.CompletableFuture;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.blueprint.container.BlueprintEvent;
+import org.osgi.service.blueprint.container.BlueprintListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Bundle Activator.
+ *
+ * @author Thomas Pantelis
+ */
+public class AAAShiroActivator implements BundleActivator, BlueprintListener {
+ private static final Logger LOG = LoggerFactory.getLogger(AAAShiroActivator.class);
+
+ private ServiceRegistration<?> eventHandlerReg;
+ private Bundle thisBundle;
+
+ @Override
+ public void start(BundleContext context) {
+ thisBundle = context.getBundle();
+ eventHandlerReg = context.registerService(BlueprintListener.class.getName(), this, new Hashtable<>());
+ }
+
+ @Override
+ public void stop(BundleContext context) {
+ possiblyFailAAAShiroProviderFuture(String.format("%s bundle stopped", thisBundle));
+
+ if (eventHandlerReg != null) {
+ try {
+ eventHandlerReg.unregister();
+ } catch (IllegalStateException e) {
+ // This can be safely ignored
+ }
+ }
+ }
+
+ @Override
+ public void blueprintEvent(BlueprintEvent event) {
+ if (!thisBundle.equals(event.getBundle())) {
+ return;
+ }
+
+ LOG.debug("Blueprint container event for bundle {}: {}", thisBundle, event.getType());
+
+ if (event.getType() == BlueprintEvent.DESTROYING) {
+ possiblyFailAAAShiroProviderFuture(String.format("Blueprint container for bundle %s is being destroyed",
+ thisBundle));
+ }
+ }
+
+ private static void possiblyFailAAAShiroProviderFuture(String message) {
+ CompletableFuture<AAAShiroProvider> instanceFuture = AAAShiroProvider.getInstanceFuture();
+ if (!instanceFuture.isDone()) {
+ instanceFuture.completeExceptionally(new RuntimeException(message));
+ }
+ }
+}
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.List;
+import java.util.concurrent.CompletableFuture;
import javax.servlet.ServletException;
import org.opendaylight.aaa.api.AuthenticationService;
import org.opendaylight.aaa.api.CredentialAuth;
private static final Logger LOG = LoggerFactory.getLogger(AAAShiroProvider.class);
+ private static final CompletableFuture<AAAShiroProvider> INSTANCE_FUTURE = new CompletableFuture();
private static volatile AAAShiroProvider INSTANCE;
private static IIDMStore iidmStore;
final String dbPassword) {
INSTANCE = new AAAShiroProvider(dataBroker, certificateManager, credentialAuth, shiroConfiguration,
httpService, moonEndpointPath, oauth2EndpointPath, datastoreConfig, dbUsername, dbPassword);
+ INSTANCE_FUTURE.complete(INSTANCE);
return INSTANCE;
}
return INSTANCE;
}
+ public static CompletableFuture<AAAShiroProvider> getInstanceFuture() {
+ return INSTANCE_FUTURE;
+ }
+
/**
* Get IDM data store.
*
package org.opendaylight.aaa.shiro.web.env;
import java.util.List;
+import java.util.concurrent.ExecutionException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.config.Ini;
import org.apache.shiro.config.IniSecurityManagerFactory;
@Override
public void init() {
- while (AAAShiroProvider.getInstance() == null) {
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- throw new RuntimeException("Interrupted waiting for b", e);
- }
- }
+ try {
+ AAAShiroProvider provider = AAAShiroProvider.getInstanceFuture().get();
- // Initialize the Shiro environment from clustered-app-config
- final Ini ini = createIniFromClusteredAppConfig(AAAShiroProvider.getInstance().getShiroConfiguration());
- setIni(ini);
- super.init();
+ // Initialize the Shiro environment from clustered-app-config
+ final Ini ini = createIniFromClusteredAppConfig(provider.getShiroConfiguration());
+ setIni(ini);
+ super.init();
+ } catch (InterruptedException | ExecutionException e) {
+ throw new RuntimeException("Error obtaining AAAShiroProvider", e);
+ }
}
}