0f2ac854cebf3735461658b56a4767abb271eb32
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / AbstractConfig.java
1 /*
2  * Copyright (c) 2014, 2015 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.common.actor;
10
11 import com.google.common.base.Preconditions;
12 import com.typesafe.config.Config;
13 import com.typesafe.config.ConfigFactory;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 public abstract class AbstractConfig implements UnifiedConfig {
19
20     private Config config;
21
22     public AbstractConfig(Config config){
23         this.config = config;
24     }
25
26     @Override
27     public Config get() {
28         return config;
29     }
30
31     public static abstract class Builder<T extends Builder<T>> {
32         protected Map<String, Object> configHolder;
33         protected Config fallback;
34
35         private final String actorSystemName;
36
37         public Builder(String actorSystemName){
38             Preconditions.checkArgument(actorSystemName != null, "Actor system name must not be null");
39             this.actorSystemName = actorSystemName;
40             configHolder = new HashMap<>();
41         }
42
43         public T withConfigReader(AkkaConfigurationReader reader){
44             fallback = reader.read().getConfig(actorSystemName);
45             return (T)this;
46         }
47
48         protected Config merge(){
49             if (fallback == null)
50                 fallback = ConfigFactory.load().getConfig(actorSystemName);
51
52             return ConfigFactory.parseMap(configHolder).withFallback(fallback);
53         }
54     }
55 }