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.ds;
17  
18  import java.io.BufferedInputStream;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.Serializable;
22  import java.net.URL;
23  import java.util.Collections;
24  import java.util.Enumeration;
25  import java.util.List;
26  import java.util.Properties;
27  
28  import org.apache.commons.io.IOUtils;
29  
30  import de.smartics.properties.api.config.ds.DataSourceConfiguration;
31  import de.smartics.properties.api.core.domain.PropertiesContext;
32  
33  /**
34   * Responsible to read configuration properties to connect to a data source
35   * stored via JNDI.
36   */
37  public class DataSourceConfigurationPropertiesLoader implements Serializable
38  {
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    /**
44     * The class version identifier.
45     * <p>
46     * The value of this constant is {@value}.
47     * </p>
48     */
49    private static final long serialVersionUID = 1L;
50  
51    /**
52     * The location within the classpath to find the configuration file to load.
53     */
54    public static final String CLASSPATH_LOCATION =
55        PropertiesContext.BOOT_PROPERTIES_HOME + "/datasource.properties";
56  
57    // --- members --------------------------------------------------------------
58  
59    // ****************************** Initializer *******************************
60  
61    // ****************************** Constructors ******************************
62  
63    /**
64     * Default constructor.
65     */
66    public DataSourceConfigurationPropertiesLoader()
67    {
68    }
69  
70    // ****************************** Inner Classes *****************************
71  
72    // ********************************* Methods ********************************
73  
74    // --- init -----------------------------------------------------------------
75  
76    // --- get&set --------------------------------------------------------------
77  
78    // --- business -------------------------------------------------------------
79  
80    /**
81     * Loads the data source configuration.
82     *
83     * @return the data source configuration.
84     * @throws IllegalStateException if the configuration cannot be loaded.
85     */
86    public DataSourceConfiguration load() throws IllegalStateException
87    {
88      try
89      {
90        final Properties properties = loadProperties();
91  
92        final DataSourceConfiguration config =
93            new DataSourceConfiguration(properties);
94        return config;
95      }
96      catch (final IOException e)
97      {
98        throw new IllegalStateException(
99            String.format("Cannot configure the access to a data source: %s",
100               e.getMessage(), e));
101     }
102   }
103 
104   private Properties loadProperties() throws IOException, IllegalStateException
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       final InputStream in = new BufferedInputStream(url.openStream());
116       try
117       {
118         properties.load(in);
119       }
120       finally
121       {
122         IOUtils.closeQuietly(in);
123       }
124       properties.setProperty(DataSourceConfiguration.CONFIG_SOURCE_ID,
125           url.toExternalForm());
126       return properties;
127     }
128     else if (count == 0)
129     {
130       throw new IllegalStateException(String.format(
131           "Cannot find '%s' to configure the access to a data source.",
132           CLASSPATH_LOCATION));
133     }
134     else
135     {
136       throw new IllegalStateException(
137           String
138               .format(
139                   "Found more than one configuration for '%s' to access a data source: %s",
140                   CLASSPATH_LOCATION, urls));
141     }
142   }
143 
144   // --- object basics --------------------------------------------------------
145 
146 }