View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.caravan.io.http.impl;
21  
22  import org.apache.commons.configuration.AbstractConfiguration;
23  import org.apache.commons.configuration.Configuration;
24  import org.apache.commons.configuration.ConfigurationException;
25  import org.apache.commons.configuration.PropertiesConfiguration;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import com.netflix.config.ConcurrentCompositeConfiguration;
30  import com.netflix.config.ConcurrentMapConfiguration;
31  import com.netflix.config.ConfigurationManager;
32  
33  /**
34   * Initialize static archaius context.
35   */
36  public final class ArchaiusConfig {
37  
38    private static final String DEFAULT_CONFIGURATION = "/config.properties";
39  
40    private static final ConcurrentMapConfiguration OSGI_CONFIG = new ConcurrentMapConfiguration();
41  
42    private static volatile boolean initialized;
43  
44    private static final Logger log = LoggerFactory.getLogger(ArchaiusConfig.class);
45  
46    private ArchaiusConfig() {
47      // static methods only
48    }
49  
50    /**
51     * Initialize archaius configuration. This is done only once, if called again nothing is done.
52     */
53    public static void initialize() {
54      if (initialized) {
55        return;
56      }
57      synchronized (ArchaiusConfig.class) {
58        if (initialized) {
59          return;
60        }
61        try {
62          initializeArchaius();
63          initialized = true;
64        }
65        catch (ConfigurationException ex) {
66          log.error("Initializing archaius configuration failed.", ex);
67        }
68      }
69    }
70  
71    private static void initializeArchaius() throws ConfigurationException {
72      // Default configuration from classpath of this bundle
73      AbstractConfiguration defaultConfig = new PropertiesConfiguration(ArchaiusConfig.class.getResource(DEFAULT_CONFIGURATION));
74  
75      ConcurrentCompositeConfiguration finalConfig = new ConcurrentCompositeConfiguration();
76  
77      // inject OSGi config which is filled via {@link ArchaiusOsgiProperties}
78      finalConfig.addConfiguration(OSGI_CONFIG);
79  
80      finalConfig.addConfiguration(defaultConfig);
81  
82      ConfigurationManager.install(finalConfig);
83  
84      log.debug("Initialized archaius configuration.");
85    }
86  
87    /**
88     * @return Archaius configuration
89     */
90    public static Configuration getConfiguration() {
91      return OSGI_CONFIG;
92    }
93  
94  }