1a331b7e99d28216dbad982d36916d1b5fa3d4d0
[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 import java.util.HashMap;
15 import java.util.Map;
16
17 public abstract class AbstractConfig implements UnifiedConfig {
18
19     private final Config config;
20
21     public AbstractConfig(Config config){
22         this.config = config;
23     }
24
25     @Override
26     public Config get() {
27         return config;
28     }
29
30     public static abstract class Builder<T extends Builder<T>> {
31         protected Map<String, Object> configHolder;
32         protected Config fallback;
33
34         private final String actorSystemName;
35
36         public Builder(String actorSystemName){
37             Preconditions.checkArgument(actorSystemName != null, "Actor system name must not be null");
38             this.actorSystemName = actorSystemName;
39             configHolder = new HashMap<>();
40         }
41
42         public T withConfigReader(AkkaConfigurationReader reader){
43             fallback = reader.read().getConfig(actorSystemName);
44             return (T)this;
45         }
46
47         protected Config merge() {
48             Config config = ConfigFactory.parseMap(configHolder);
49             if (fallback != null) {
50                 config = config.withFallback(fallback);
51             }
52
53             return config;
54         }
55     }
56 }