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.Serializable;
19  
20  import de.smartics.properties.api.config.ds.DataSourceConfiguration;
21  import de.smartics.properties.api.core.domain.PropertiesContext;
22  import de.smartics.properties.spi.config.config.ConfigurationJndiLookupLoader;
23  import de.smartics.properties.spi.config.config.PropertiesConfiguration;
24  
25  /**
26   * Responsible to read configuration properties to connect to a data source
27   * stored via JNDI.
28   */
29  public class DataSourceConfigurationLoader implements Serializable
30  {
31    // ********************************* Fields *********************************
32  
33    // --- constants ------------------------------------------------------------
34  
35    /**
36     * The class version identifier.
37     * <p>
38     * The value of this constant is {@value}.
39     * </p>
40     */
41    private static final long serialVersionUID = 1L;
42  
43    /**
44     * The location within the classpath to find the configuration file to load.
45     */
46    public static final String CLASSPATH_LOCATION =
47        PropertiesContext.META_INF_HOME + "/datasource.properties";
48  
49    // --- members --------------------------------------------------------------
50  
51    // ****************************** Initializer *******************************
52  
53    // ****************************** Constructors ******************************
54  
55    /**
56     * Default constructor.
57     */
58    public DataSourceConfigurationLoader()
59    {
60    }
61  
62    // ****************************** Inner Classes *****************************
63  
64    // ********************************* Methods ********************************
65  
66    // --- init -----------------------------------------------------------------
67  
68    // --- get&set --------------------------------------------------------------
69  
70    // --- business -------------------------------------------------------------
71  
72    /**
73     * Loads the data source configuration.
74     *
75     * @return the data source configuration.
76     */
77    public DataSourceConfiguration load()
78    {
79      try
80      {
81        try
82        {
83          final DataSourceConfigurationPropertiesLoader propertiesLoader =
84              new DataSourceConfigurationPropertiesLoader();
85          final DataSourceConfiguration config = propertiesLoader.load();
86          return config;
87        }
88        catch (final IllegalStateException e)
89        {
90          return loadConfigFromJndi();
91        }
92      }
93      catch (final IllegalStateException e)
94      {
95        throw new IllegalStateException(
96            String.format("Cannot configure the access to a data source: %s",
97                e.getMessage(), e));
98      }
99    }
100 
101   private DataSourceConfiguration loadConfigFromJndi()
102   {
103     try
104     {
105       final DataSourceConfigurationJndiLoader jndiLoader =
106           new DataSourceConfigurationJndiLoader();
107       final DataSourceConfiguration config = jndiLoader.load();
108       return config;
109     }
110     catch (final IllegalStateException e)
111     {
112       final ConfigurationJndiLookupLoader loader =
113           new ConfigurationJndiLookupLoader();
114       final PropertiesConfiguration allConfig = loader.load();
115       final DataSourceConfiguration config =
116           allConfig.getDataSourceConfiguration();
117       return config;
118     }
119   }
120 
121   // --- object basics --------------------------------------------------------
122 
123 }