BUG-8143: issue a JVM restart
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / config / yang / config / actor_system_provider / impl / ActorSystemProviderImpl.java
index 5970ea47e5413d4c76c19e212632589f60341351..5b6e447d0b09e165e66178bb596bf6d813bf5d7d 100644 (file)
@@ -9,10 +9,11 @@ package org.opendaylight.controller.config.yang.config.actor_system_provider.imp
 
 import akka.actor.ActorSystem;
 import akka.actor.Props;
-import akka.japi.Effect;
 import akka.osgi.BundleDelegatingClassLoader;
 import com.typesafe.config.Config;
 import com.typesafe.config.ConfigFactory;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.concurrent.TimeUnit;
 import org.opendaylight.controller.cluster.ActorSystemProvider;
 import org.opendaylight.controller.cluster.ActorSystemProviderListener;
@@ -26,6 +27,7 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import scala.concurrent.Await;
 import scala.concurrent.duration.Duration;
 
 public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseable {
@@ -40,8 +42,9 @@ public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseab
 
         final Bundle bundle = bundleContext.getBundle();
 
-        final BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundle, Thread.currentThread()
-                .getContextClassLoader());
+        final BundleDelegatingClassLoader classLoader = AccessController.doPrivileged(
+            (PrivilegedAction<BundleDelegatingClassLoader>) () ->
+                new BundleDelegatingClassLoader(bundle, Thread.currentThread().getContextClassLoader()));
 
         final AkkaConfigurationReader configurationReader = new FileAkkaConfigurationReader();
         final Config akkaConfig = ConfigFactory.load(configurationReader.read()).getConfig(CONFIGURATION_NAME);
@@ -50,17 +53,12 @@ public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseab
 
         actorSystem.actorOf(Props.create(TerminationMonitor.class), TerminationMonitor.ADDRESS);
 
-        actorSystem.actorOf(QuarantinedMonitorActor.props(new Effect() {
-
-            @Override
-            public void apply() throws Exception {
-                // restart the entire karaf container
-                LOG.warn("Restarting karaf container");
-                System.setProperty("karaf.restart", "true");
-                bundleContext.getBundle(0).stop();
-            }
+        actorSystem.actorOf(QuarantinedMonitorActor.props(() -> {
+            // restart the entire karaf container
+            LOG.warn("Restarting karaf container");
+            System.setProperty("karaf.restart.jvm", "true");
+            bundleContext.getBundle(0).stop();
         }), QuarantinedMonitorActor.ADDRESS);
-
     }
 
     @Override
@@ -70,17 +68,17 @@ public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseab
 
     @Override
     public ListenerRegistration<ActorSystemProviderListener> registerActorSystemProviderListener(
-            ActorSystemProviderListener listener) {
+            final ActorSystemProviderListener listener) {
         return listeners.register(listener);
     }
 
     @Override
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void close() {
         LOG.info("Shutting down ActorSystem");
 
-        actorSystem.shutdown();
         try {
-            actorSystem.awaitTermination(Duration.create(10, TimeUnit.SECONDS));
+            Await.result(actorSystem.terminate(), Duration.create(10, TimeUnit.SECONDS));
         } catch (Exception e) {
             LOG.warn("Error awaiting actor termination", e);
         }