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 javax.naming.Context;
21  import javax.naming.InitialContext;
22  
23  import de.smartics.properties.api.config.ds.DataSourceConfiguration;
24  import de.smartics.properties.api.core.app.JndiContext;
25  
26  /**
27   * Responsible to read JNDI configuration properties to connect to a data source
28   * stored via JNDI.
29   */
30  public class DataSourceConfigurationJndiLoader implements Serializable
31  {
32    // ********************************* Fields *********************************
33  
34    // --- constants ------------------------------------------------------------
35  
36    /**
37     * The class version identifier.
38     * <p>
39     * The value of this constant is {@value}.
40     * </p>
41     */
42    private static final long serialVersionUID = 1L;
43  
44    // --- members --------------------------------------------------------------
45  
46    // ****************************** Initializer *******************************
47  
48    // ****************************** Constructors ******************************
49  
50    /**
51     * Default constructor.
52     */
53    public DataSourceConfigurationJndiLoader()
54    {
55    }
56  
57    // ****************************** Inner Classes *****************************
58  
59    // ********************************* Methods ********************************
60  
61    // --- init -----------------------------------------------------------------
62  
63    // --- get&set --------------------------------------------------------------
64  
65    // --- business -------------------------------------------------------------
66  
67    /**
68     * Loads the data source configuration.
69     *
70     * @return the data source configuration.
71     * @throws IllegalStateException if the configuration cannot be loaded.
72     */
73    public DataSourceConfiguration load() throws IllegalStateException
74    {
75      try
76      {
77        final Context context = new InitialContext();
78        final JndiContext lookup = JndiContext.boot(context);
79        final String jndiName = lookup.lookup(DataSourceConfiguration.JNDI_NAME);
80  
81        if (jndiName == null)
82        {
83          throw new IllegalStateException(String.format(
84              "Cannot find '%s' in the JNDI context to configure the"
85                  + " access to a data source.",
86              DataSourceConfiguration.JNDI_NAME));
87        }
88  
89        final boolean createTable =
90            lookup.lookupBoolean(DataSourceConfiguration.CREATE_TABLE);
91        final boolean dropTable =
92            lookup.lookupBoolean(DataSourceConfiguration.DROP_TABLE);
93  
94        final DataSourceConfigurationBuilder builder =
95            new DataSourceConfigurationBuilder();
96        builder.setConfigSourceId(lookup.getSourceId());
97        builder.setJndiName(jndiName);
98        builder.setCreateTable(createTable);
99        builder.setDropTable(dropTable);
100 
101       final DataSourceConfiguration config = builder.build();
102       return config;
103     }
104     catch (final Exception e)
105     {
106       throw new IllegalStateException(
107           String.format("Cannot access JNDI context to configure the access to"
108                         + " a data source."), e);
109     }
110   }
111 
112   // --- object basics --------------------------------------------------------
113 
114 }