Initial Configuration for Clustering
[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     if(actorSystem == null) {
31       // Create an OSGi bundle classloader for actor system
32       BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
33           Thread.currentThread().getContextClassLoader());
34       synchronized (ActorSystemFactory.class) {
35         // Double check
36         if (actorSystem == null) {
37           ActorSystem system = ActorSystem.create("opendaylight-cluster-rpc",
38               ConfigFactory.load().getConfig("odl-cluster-rpc"), classLoader);
39           actorSystem = system;
40         }
41       }
42     } else {
43       throw new IllegalStateException("Actor system should be created only once. Use getInstance method to access existing actor system");
44     }
45   }
46 }