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.spi.config.ds;
17  
18  import java.io.IOException;
19  import java.io.ObjectInputStream;
20  import java.sql.PreparedStatement;
21  import java.sql.SQLException;
22  
23  import javax.naming.InitialContext;
24  import javax.naming.NamingException;
25  import javax.sql.DataSource;
26  
27  /**
28   * A vendor-neutral implementation using JNDI to access a data source.
29   */
30  public final class JndiDataSourceProxy implements DataSourceProxy
31  {
32    // ********************************* Fields *********************************
33  
34    // --- constants ------------------------------------------------------------
35  
36    /**
37     * The class version identifier.
38     */
39    private static final long serialVersionUID = 1L;
40  
41    // --- members --------------------------------------------------------------
42  
43    /**
44     * The JNDI name to access the data source.
45     *
46     * @serial
47     */
48    private final String mappedName;
49  
50    /**
51     * The cached data source.
52     */
53    private transient DataSource dataSource;
54  
55    // ****************************** Initializer *******************************
56  
57    // ****************************** Constructors ******************************
58  
59    /**
60     * Default constructor.
61     *
62     * @param mappedName the JNDI name to access the data source.
63     * @throws NamingException if the data source cannot be fetched by the given
64     *           name.
65     */
66    public JndiDataSourceProxy(final String mappedName) throws NamingException
67    {
68      this.mappedName = mappedName;
69      this.dataSource = fetchDataSource(mappedName);
70    }
71  
72    // ****************************** Inner Classes *****************************
73  
74    private static DataSource fetchDataSource(final String mappedName)
75      throws NamingException
76    {
77      final InitialContext context = new InitialContext();
78      final DataSource dataSource = (DataSource) context.lookup(mappedName);
79      return dataSource;
80    }
81  
82    // ********************************* Methods ********************************
83  
84    // --- init -----------------------------------------------------------------
85  
86    // --- get&set --------------------------------------------------------------
87  
88    @Override
89    public String getDataSourceId()
90    {
91      return mappedName;
92    }
93  
94    @Override
95    public DataSource getDataSource()
96    {
97      return dataSource;
98    }
99  
100   @Override
101   public String getCreateTableSqlTemplate()
102   {
103     return null;
104   }
105 
106   @Override
107   public String getInsertOrUpdateSqlTemplate()
108   {
109     return null;
110   }
111 
112   @Override
113   public void setInsertOrUpdate(final PreparedStatement statement,
114       final String config, final String name, final String value)
115     throws SQLException
116   {
117   }
118 
119   // --- business -------------------------------------------------------------
120 
121   // --- object basics --------------------------------------------------------
122 
123   /**
124    * Reads the object from the given stream.
125    *
126    * @param in the stream to read from.
127    * @throws IOException on read problems.
128    * @throws ClassNotFoundException if a class cannot be found.
129    */
130   private void readObject(final ObjectInputStream in) throws IOException,
131     ClassNotFoundException
132   {
133     in.defaultReadObject();
134 
135     try
136     {
137       this.dataSource = fetchDataSource(mappedName);
138     }
139     catch (final NamingException e)
140     {
141       throw new IOException(
142           "Cannot restore data source from JNDI context with mapped name '"
143               + mappedName + "'.", e);
144     }
145   }
146 
147   /**
148    * Returns the string representation of the object.
149    *
150    * @return the string representation of the object.
151    */
152   @Override
153   public String toString()
154   {
155     final StringBuilder buffer = new StringBuilder();
156 
157     buffer.append("Data Source Mapped Name: ").append(mappedName);
158 
159     return buffer.toString();
160   }
161 }