1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43 public class ApplicationIdConfigurationPropertiesLoader implements Serializable
44 {
45
46
47
48
49
50
51
52
53
54
55 private static final long serialVersionUID = 1L;
56
57
58
59
60 public static final String CLASSPATH_LOCATION =
61 PropertiesContext.BOOT_PROPERTIES_HOME + "/application-id.properties";
62
63
64
65
66
67
68
69
70
71
72 public ApplicationIdConfigurationPropertiesLoader()
73 {
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
163
164 }