View Javadoc

1   /*
2    * Copyright 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.admin.domain.service;
17  
18  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.ARCHIVE_TYPE;
19  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.ARTIFACT_ID;
20  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.CLASSIFIER;
21  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.GROUP_ID;
22  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.VERSION;
23  
24  import java.io.BufferedInputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.Serializable;
28  import java.net.URL;
29  import java.util.Collections;
30  import java.util.Enumeration;
31  import java.util.List;
32  import java.util.Properties;
33  
34  import org.apache.commons.io.IOUtils;
35  import org.apache.commons.lang.StringUtils;
36  
37  import de.smartics.properties.api.core.domain.PropertiesContext;
38  import de.smartics.properties.resource.domain.ArtifactId;
39  
40  /**
41   * Responsible to read application identifier properties from the classpath.
42   */
43  public class ApplicationIdConfigurationPropertiesLoader implements Serializable
44  {
45    // ********************************* Fields *********************************
46  
47    // --- constants ------------------------------------------------------------
48  
49    /**
50     * The class version identifier.
51     * <p>
52     * The value of this constant is {@value}.
53     * </p>
54     */
55    private static final long serialVersionUID = 1L;
56  
57    /**
58     * The location within the classpath to find the configuration file to load.
59     */
60    public static final String CLASSPATH_LOCATION =
61        PropertiesContext.BOOT_PROPERTIES_HOME + "/application-id.properties";
62  
63    // --- members --------------------------------------------------------------
64  
65    // ****************************** Initializer *******************************
66  
67    // ****************************** Constructors ******************************
68  
69    /**
70     * Default constructor.
71     */
72    public ApplicationIdConfigurationPropertiesLoader()
73    {
74    }
75  
76    // ****************************** Inner Classes *****************************
77  
78    // ********************************* Methods ********************************
79  
80    // --- init -----------------------------------------------------------------
81  
82    // --- get&set --------------------------------------------------------------
83  
84    // --- business -------------------------------------------------------------
85  
86    /**
87     * Loads the application identifier configuration.
88     *
89     * @return the application identifier configuration.
90     */
91    public ArtifactId load()
92    {
93      try
94      {
95        final Properties properties = loadProperties();
96  
97        final String classifier = readClassifier(properties);
98        final ArtifactId applicationId =
99            new ArtifactId.Builder()
100               .withGroupId(properties.getProperty(GROUP_ID))
101               .withName(properties.getProperty(ARTIFACT_ID))
102               .withVersion(properties.getProperty(VERSION))
103               .withArchiveType(properties.getProperty(ARCHIVE_TYPE))
104               .withClassifier(classifier).build();
105 
106       return applicationId;
107     }
108     catch (final IOException e)
109     {
110       throw new IllegalStateException(String.format(
111           "Cannot define the application identifier: %s", e.getMessage(), e));
112     }
113   }
114 
115   private static String readClassifier(final Properties properties)
116   {
117     final String classifier = properties.getProperty(CLASSIFIER);
118     if (StringUtils.isBlank(classifier))
119     {
120       return null;
121     }
122     return classifier;
123   }
124 
125   private Properties loadProperties() throws IOException
126   {
127     final Enumeration<URL> urlsEnum =
128         Thread.currentThread().getContextClassLoader()
129             .getResources(CLASSPATH_LOCATION);
130     final List<URL> urls = Collections.list(urlsEnum);
131     final int count = urls.size();
132     if (count == 1)
133     {
134       final URL url = urls.get(0);
135       final Properties properties = new Properties();
136 
137       final InputStream in = new BufferedInputStream(url.openStream());
138       try
139       {
140         properties.load(in);
141       }
142       finally
143       {
144         IOUtils.closeQuietly(in);
145       }
146       return properties;
147     }
148     else if (count == 0)
149     {
150       throw new IllegalStateException(String.format(
151           "Cannot find '%s' to configure the application identifier.",
152           CLASSPATH_LOCATION));
153     }
154     else
155     {
156       throw new IllegalStateException(String.format(
157           "Found more than one configuration for '%s' to define the"
158               + " application identifier: %s", CLASSPATH_LOCATION, urls));
159     }
160   }
161 
162   // --- object basics --------------------------------------------------------
163 
164 }