Merge "Bug 1637: Change Rpc actor calls to async"
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / ActorSystemFactory.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.remote.rpc;
10
11 import akka.actor.ActorSystem;
12 import akka.osgi.BundleDelegatingClassLoader;
13 import org.opendaylight.controller.remote.rpc.utils.AkkaConfigurationReader;
14 import org.osgi.framework.BundleContext;
15
16
17 public class ActorSystemFactory {
18
19     public static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-rpc";
20     public static final String CONFIGURATION_NAME = "odl-cluster-rpc";
21
22     private static volatile ActorSystem actorSystem = null;
23
24   public static final ActorSystem getInstance(){
25      return actorSystem;
26   }
27
28   /**
29    * This method should be called only once during initialization
30    *
31    * @param bundleContext
32    */
33   public static final void createInstance(final BundleContext bundleContext, AkkaConfigurationReader akkaConfigurationReader) {
34     if(actorSystem == null) {
35       // Create an OSGi bundle classloader for actor system
36       BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
37           Thread.currentThread().getContextClassLoader());
38       synchronized (ActorSystemFactory.class) {
39         // Double check
40         if (actorSystem == null) {
41           ActorSystem system = ActorSystem.create(ACTOR_SYSTEM_NAME,
42               akkaConfigurationReader.read().getConfig(CONFIGURATION_NAME), classLoader);
43           actorSystem = system;
44         }
45       }
46     } else {
47       throw new IllegalStateException("Actor system should be created only once. Use getInstance method to access existing actor system");
48     }
49   }
50
51 }