View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.spi.config.domain.key;
17  
18  import java.util.ServiceLoader;
19  
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  /**
24   * Provides access to the manager and configuration key implementations.
25   */
26  public final class ConfigurationKeyContextManager
27  {
28    // ********************************* Fields *********************************
29  
30    // --- constants ------------------------------------------------------------
31  
32    /**
33     * The singleton to provide access to the manager and configuration key
34     * implementations.
35     */
36    public static final ConfigurationKeyContextManager INSTANCE =
37        new ConfigurationKeyContextManager();
38  
39    // --- members --------------------------------------------------------------
40  
41    /**
42     * Reference to the logger for this instance.
43     */
44    private final Logger log = LoggerFactory
45        .getLogger(ConfigurationKeyContextManager.class);
46  
47    /**
48     * The configured context instance to access configuration key helpers.
49     */
50    private final ConfigurationKeyContext context; // NOPMD
51  
52    // ****************************** Initializer *******************************
53  
54    // ****************************** Constructors ******************************
55  
56    /**
57     * Default constructor.
58     */
59    private ConfigurationKeyContextManager()
60    {
61      this.context = createContext();
62    }
63  
64    // ****************************** Inner Classes *****************************
65  
66    // ********************************* Methods ********************************
67  
68    // --- init -----------------------------------------------------------------
69  
70    @SuppressWarnings("unchecked")
71    private ConfigurationKeyContext createContext()
72    {
73      final ServiceLoader<ConfigurationKeyContext> loader =
74          ServiceLoader.load(ConfigurationKeyContext.class);
75  
76      ConfigurationKeyContext current = null;
77      for (final ConfigurationKeyContext context : loader)
78      {
79        if (current == null)
80        {
81          current = context;
82        }
83        else
84        {
85          log.debug(String.format(
86              "Multiple instances. Using '%s', found instance of '%s'.", current
87                  .getClass().getName(), context.getClass().getName()));
88        }
89      }
90  
91      if (current == null)
92      {
93        final String defaultTypeName =
94            "de.smartics.properties.impl.config.domain.key.envapp.EnvAppConfigurationKeyContext";
95        try
96        {
97          final Class<ConfigurationKeyContext> defaultType =
98              (Class<ConfigurationKeyContext>) Class.forName(defaultTypeName);
99          current = defaultType.newInstance();
100       }
101       catch (final Exception e)
102       {
103         throw new IllegalStateException(
104             String
105                 .format(
106                     "Cannot find configuration key context and cannot create default: %s",
107                     defaultTypeName));
108       }
109     }
110 
111     return current;
112   }
113 
114   // --- get&set --------------------------------------------------------------
115 
116   /**
117    * Returns the configured context instance to access configuration key
118    * helpers.
119    *
120    * @return the configured context instance to access configuration key
121    *         helpers.
122    */
123   public ConfigurationKeyContext context()
124   {
125     return context;
126   }
127 
128   // --- business -------------------------------------------------------------
129 
130   // --- object basics --------------------------------------------------------
131 
132 }