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  /**
19   * Provides access to the manager and configuration key implementations.
20   */
21  public final class ConfigurationKeyContextManager
22  {
23    // ********************************* Fields *********************************
24  
25    // --- constants ------------------------------------------------------------
26  
27    /**
28     * The singleton to provide access to the manager and configuration key
29     * implementations.
30     */
31    public static final ConfigurationKeyContextManager INSTANCE =
32        new ConfigurationKeyContextManager();
33  
34    // --- members --------------------------------------------------------------
35  
36    /**
37     * The configured context instance to access configuration key helpers.
38     */
39    private final ConfigurationKeyContext context; // NOPMD
40  
41    // ****************************** Initializer *******************************
42  
43    // ****************************** Constructors ******************************
44  
45    /**
46     * Default constructor.
47     */
48    private ConfigurationKeyContextManager()
49    {
50      this.context = createContext();
51    }
52  
53    // ****************************** Inner Classes *****************************
54  
55    // ********************************* Methods ********************************
56  
57    // --- init -----------------------------------------------------------------
58  
59    @SuppressWarnings("unchecked")
60    private ConfigurationKeyContext createContext()
61    {
62      final String defaultTypeName =
63          "de.smartics.properties.impl.config.domain.key.envapp.EnvAppConfigurationKeyContext";
64      try
65      {
66        final Class<ConfigurationKeyContext> defaultType =
67            (Class<ConfigurationKeyContext>) Class.forName(defaultTypeName);
68        final ConfigurationKeyContextFactory factory =
69            new ConfigurationKeyContextFactory();
70  
71        final ConfigurationKeyContext instance = factory.create(defaultType);
72        return instance;
73      }
74      catch (final Exception e)
75      {
76        throw new IllegalStateException(
77            String
78                .format(
79                    "Cannot find configuration key context and cannot create default: %s",
80                    defaultTypeName));
81      }
82    }
83  
84    // --- get&set --------------------------------------------------------------
85  
86    /**
87     * Returns the configured context instance to access configuration key
88     * helpers.
89     *
90     * @return the configured context instance to access configuration key
91     *         helpers.
92     */
93    public ConfigurationKeyContext context()
94    {
95      return context;
96    }
97  
98    // --- business -------------------------------------------------------------
99  
100   // --- object basics --------------------------------------------------------
101 
102 }