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.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   * Responsible to read configuration properties from a properties file to secure
36   * property values.
37   */
38  class SecurityConfigurationPropertiesLoader implements Serializable
39  {
40    // ********************************* Fields *********************************
41  
42    // --- constants ------------------------------------------------------------
43  
44    /**
45     * The class version identifier.
46     * <p>
47     * The value of this constant is {@value}.
48     * </p>
49     */
50    private static final long serialVersionUID = 1L;
51  
52    /**
53     * The location within the classpath to find the configuration file to load.
54     */
55    static final String CLASSPATH_LOCATION =
56        PropertiesContext.BOOT_PROPERTIES_HOME + "/security.properties";
57  
58    // --- members --------------------------------------------------------------
59  
60    // ****************************** Initializer *******************************
61  
62    // ****************************** Constructors ******************************
63  
64    /**
65     * Default constructor.
66     */
67    SecurityConfigurationPropertiesLoader()
68    {
69    }
70  
71    // ****************************** Inner Classes *****************************
72  
73    // ********************************* Methods ********************************
74  
75    // --- init -----------------------------------------------------------------
76  
77    // --- get&set --------------------------------------------------------------
78  
79    // --- business -------------------------------------------------------------
80  
81    /**
82     * Loads the security configuration.
83     *
84     * @return the security configuration.
85     * @throws IllegalStateException on any problem reading the security
86     *           properties.
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   // --- object basics --------------------------------------------------------
147 
148 }