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