Merge "Fix failing IT tests"
[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 com.typesafe.config.ConfigFactory;
14 import org.osgi.framework.BundleContext;
15
16
17 public class ActorSystemFactory {
18  private static volatile ActorSystem actorSystem = null;
19
20   public static final ActorSystem getInstance(){
21      return actorSystem;
22   }
23
24   /**
25    * This method should be called only once during initialization
26    *
27    * @param bundleContext
28    */
29   public static final void createInstance(final BundleContext bundleContext) {
30
31     if(actorSystem == null) {
32       // Create an OSGi bundle classloader for actor system
33       BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
34           Thread.currentThread().getContextClassLoader());
35       synchronized (ActorSystemFactory.class) {
36         // Double check
37         if (actorSystem == null) {
38           ActorSystem system = ActorSystem.create("opendaylight-rpc",
39               ConfigFactory.load().getConfig("odl-cluster"), classLoader);
40           actorSystem = system;
41         }
42       }
43     } else {
44       throw new IllegalStateException("Actor system should be created only once. Use getInstance method to access existing actor system");
45     }
46   }
47 }