Merge "BUG-1690: catch wildcard InstanceIdentifiers"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / 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.cluster.datastore;
10
11 import akka.actor.ActorSystem;
12 import akka.actor.Props;
13 import akka.osgi.BundleDelegatingClassLoader;
14 import com.google.common.base.Preconditions;
15 import com.typesafe.config.Config;
16 import com.typesafe.config.ConfigFactory;
17 import org.osgi.framework.BundleContext;
18
19 import java.io.File;
20
21 public class ActorSystemFactory {
22
23     public static final String AKKA_CONF_PATH = "./configuration/initial/akka.conf";
24     public static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
25     public static final String CONFIGURATION_NAME = "odl-cluster-data";
26
27     private static volatile ActorSystem actorSystem = null;
28
29     public static final ActorSystem getInstance(){
30         return actorSystem;
31     }
32
33     /**
34      * This method should be called only once during initialization
35      *
36      * @param bundleContext
37      */
38     public static final ActorSystem createInstance(final BundleContext bundleContext) {
39         if(actorSystem == null) {
40             // Create an OSGi bundle classloader for actor system
41             BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
42                 Thread.currentThread().getContextClassLoader());
43             synchronized (ActorSystemFactory.class) {
44                 // Double check
45
46                 if (actorSystem == null) {
47                     ActorSystem system = ActorSystem.create(ACTOR_SYSTEM_NAME,
48                         ConfigFactory.load(readAkkaConfiguration()).getConfig(CONFIGURATION_NAME), classLoader);
49                     system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
50                     actorSystem = system;
51                 }
52             }
53         }
54
55         return actorSystem;
56     }
57
58
59     private static final Config readAkkaConfiguration(){
60         File defaultConfigFile = new File(AKKA_CONF_PATH);
61         Preconditions.checkState(defaultConfigFile.exists(), "akka.conf is missing");
62         return ConfigFactory.parseFile(defaultConfigFile);
63     }
64 }