1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.api.core.security;
17
18 import static de.smartics.properties.api.core.security.SecurityConfigurationLoader.SECURITY_ID;
19
20 import java.io.BufferedInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.Serializable;
24 import java.net.URL;
25 import java.util.Collections;
26 import java.util.Enumeration;
27 import java.util.List;
28 import java.util.Properties;
29
30 import org.apache.commons.io.IOUtils;
31
32 import de.smartics.properties.api.core.domain.PropertiesContext;
33
34
35
36
37
38 class SecurityConfigurationPropertiesLoader implements Serializable
39 {
40
41
42
43
44
45
46
47
48
49
50 private static final long serialVersionUID = 1L;
51
52
53
54
55 static final String CLASSPATH_LOCATION =
56 PropertiesContext.BOOT_PROPERTIES_HOME + "/security.properties";
57
58
59
60
61
62
63
64
65
66
67 SecurityConfigurationPropertiesLoader()
68 {
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 Properties load() throws IllegalStateException
89 {
90 try
91 {
92 final Properties properties = loadProperties();
93
94 return properties;
95 }
96 catch (final IOException e)
97 {
98 throw new IllegalStateException(String.format(
99 "Cannot configure the access to a security module: %s",
100 e.getMessage(), e));
101 }
102 }
103
104 private Properties loadProperties() throws IOException
105 {
106 final Enumeration<URL> urlsEnum =
107 Thread.currentThread().getContextClassLoader()
108 .getResources(CLASSPATH_LOCATION);
109 final List<URL> urls = Collections.list(urlsEnum);
110 final int count = urls.size();
111 if (count == 1)
112 {
113 final URL url = urls.get(0);
114 final Properties properties = new Properties();
115
116 final InputStream in = new BufferedInputStream(url.openStream());
117 try
118 {
119 properties.load(in);
120 }
121 finally
122 {
123 IOUtils.closeQuietly(in);
124 }
125
126 properties.setProperty(SECURITY_ID, url.toExternalForm());
127
128 return properties;
129 }
130 else if (count == 0)
131 {
132 throw new IllegalStateException(String.format(
133 "Cannot find '%s' to configure the access to security module.",
134 CLASSPATH_LOCATION));
135 }
136 else
137 {
138 throw new IllegalStateException(
139 String
140 .format(
141 "Found more than one configuration for '%s' to access a security module: %s",
142 CLASSPATH_LOCATION, urls));
143 }
144 }
145
146
147
148 }