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.impl.config.domain.key.envapp;
17  
18  import java.net.InetAddress;
19  
20  import de.smartics.properties.api.config.domain.key.ApplicationId;
21  import de.smartics.properties.api.config.domain.key.ApplicationIdLoader;
22  import de.smartics.properties.api.config.domain.key.ConfigurationKeyHelper;
23  import de.smartics.properties.api.config.domain.key.EnvironmentId;
24  
25  /**
26   * Helper to construct configuration keys by analyzing the runtime environment.
27   */
28  public final class EnvAppConfigurationKeyHelper implements ConfigurationKeyHelper
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    /**
35     * The class version identifier.
36     */
37    private static final long serialVersionUID = 1L;
38  
39    /**
40     * The name of the system property that stores the name of the environment.
41     * <p>
42     * The value of this constant is {@value}.
43     * </p>
44     */
45    public static final String SYSTEM_PROPERTY_ENVIRONMENT_NAME =
46        "properties.environment.name";
47  
48    /**
49     * The name of the system property that stores the node name of the
50     * environment.
51     * <p>
52     * The value of this constant is {@value}.
53     * </p>
54     */
55    public static final String SYSTEM_PROPERTY_ENVIRONMENT_NODE =
56        "properties.environment.node";
57  
58    // --- members --------------------------------------------------------------
59  
60    /**
61     * The flag to signal that the manifest of a EAR file (with the extension
62     * <code>ear</code>) is preferred if present.
63     *
64     * @serial
65     */
66    private final boolean preferEarManifest;
67  
68    // ****************************** Initializer *******************************
69  
70    // ****************************** Constructors ******************************
71  
72    /**
73     * Convenience constructor preferring manifest information from EARs.
74     */
75    public EnvAppConfigurationKeyHelper()
76    {
77      this(true);
78    }
79  
80    /**
81     * Default constructor.
82     *
83     * @param preferEarManifest the flag to signal that the manifest of a EAR file
84     *          (with the extension <code>ear</code>) is preferred if present.
85     */
86    public EnvAppConfigurationKeyHelper(final boolean preferEarManifest)
87    {
88      this.preferEarManifest = preferEarManifest;
89    }
90  
91    // ****************************** Inner Classes *****************************
92  
93    // ********************************* Methods ********************************
94  
95    // --- init -----------------------------------------------------------------
96  
97    // --- get&set --------------------------------------------------------------
98  
99    // --- business -------------------------------------------------------------
100 
101   /**
102    * Loads the application identifier from the manifest file pointed at by the
103    * given {@code locator} class.
104    *
105    * @param locator the class to locate the manifest file to load. It is the
106    *          manifest file of the archive this class is part of.
107    * @return the application identifier.
108    * @throws IllegalStateException if the application identifier cannot be read
109    *           from the manifest.
110    * @see #load(EnvironmentId, Class)
111    */
112   public EnvAppConfigurationKey load(final Class<?> locator)
113     throws IllegalStateException
114   {
115     return load((String) null, locator);
116   }
117 
118   /**
119    * Loads the application identifier from the manifest file pointed at by the
120    * given {@code locator} class.
121    *
122    * @param environmentName the name of the environment of use. May be
123    *          <code>null</code> if the value of the system property
124    *          {@link #SYSTEM_PROPERTY_ENVIRONMENT_NAME} should be used (along
125    *          with the node name defined by
126    *          {@link #SYSTEM_PROPERTY_ENVIRONMENT_NODE}). If not specified, the
127    *          name of the host of this runtime is determined.
128    * @param locator the class to locate the manifest file to load. It is the
129    *          manifest file of the archive this class is part of.
130    * @return the application identifier.
131    * @throws IllegalStateException if the application identifier cannot be read
132    *           from the manifest.
133    * @see #load(EnvironmentId, Class)
134    */
135   public EnvAppConfigurationKey load(final String environmentName,
136       final Class<?> locator) throws IllegalStateException
137   {
138     final EnvironmentId environmentId = loadEnvironmentId(environmentName);
139     final ApplicationIdLoader loader = createLoader();
140     final ApplicationId applicationId = loader.load(locator);
141 
142     final EnvAppConfigurationKey configurationId =
143         new EnvAppConfigurationKey(environmentId, applicationId);
144     return configurationId;
145   }
146 
147   private static EnvironmentId loadEnvironmentId(final String environmentName)
148   {
149     final String name =
150         environmentName != null ? environmentName : System.getProperty(
151             SYSTEM_PROPERTY_ENVIRONMENT_NAME, "development");
152     final String node = calcEnvironmentNode();
153 
154     final EnvironmentId environmentId = new EnvironmentId(name, node);
155     return environmentId;
156   }
157 
158   private static String calcEnvironmentNode()
159   {
160     final String node = System.getProperty(SYSTEM_PROPERTY_ENVIRONMENT_NODE);
161 
162     if (node == null)
163     {
164       try
165       {
166         final InetAddress address = InetAddress.getLocalHost();
167         final String hostName = address.getCanonicalHostName();
168         return hostName;
169       }
170       catch (final Exception e)
171       {
172         return null;
173       }
174     }
175     return node;
176   }
177 
178   /**
179    * Loads the application identifier from the manifest file pointed at by the
180    * given {@code locator} class.
181    *
182    * @param environmentId the environment part of the configuration key to
183    *          construct.
184    * @param locator the class to locate the manifest file to load. It is the
185    *          manifest file of the archive this class is part of.
186    * @return the application identifier.
187    * @throws IllegalStateException if the application identifier cannot be read
188    *           from the manifest.
189    * @see #load(Class)
190    * @see #load(String, Class)
191    */
192   public EnvAppConfigurationKey load(final EnvironmentId environmentId,
193       final Class<?> locator) throws IllegalStateException
194   {
195     final ApplicationIdLoader loader = createLoader();
196     final ApplicationId applicationId = loader.load(locator);
197 
198     final EnvAppConfigurationKey configurationId =
199         new EnvAppConfigurationKey(environmentId, applicationId);
200     return configurationId;
201   }
202 
203   /**
204    * Loads the configuration key using the given applicationId and determines
205    * the environmentId through system properties or defaults (null for the
206    * environment and hostname for the node).
207    *
208    * @param applicationId the application part of the configuration key to
209    *          construct.
210    * @return the configuration key.
211    */
212   public EnvAppConfigurationKey load(final ApplicationId applicationId)
213   {
214     final EnvironmentId environmentId = loadEnvironmentId(null);
215 
216     final EnvAppConfigurationKey configurationId =
217         new EnvAppConfigurationKey(environmentId, applicationId);
218 
219     return configurationId;
220   }
221 
222   private ApplicationIdLoader createLoader()
223   {
224     return new ApplicationIdLoader(preferEarManifest);
225   }
226 
227   // --- object basics --------------------------------------------------------
228 
229 }