View Javadoc

1   // SECTION-START[License Header]
2   // <editor-fold defaultstate="collapsed" desc=" Generated License ">
3   /*
4    *   Copyright (c) 2010 The JOMC Project
5    *   Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net>
6    *   All rights reserved.
7    *
8    *   Redistribution and use in source and binary forms, with or without
9    *   modification, are permitted provided that the following conditions
10   *   are met:
11   *
12   *     o Redistributions of source code must retain the above copyright
13   *       notice, this list of conditions and the following disclaimer.
14   *
15   *     o Redistributions in binary form must reproduce the above copyright
16   *       notice, this list of conditions and the following disclaimer in
17   *       the documentation and/or other materials provided with the
18   *       distribution.
19   *
20   *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
21   *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22   *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23   *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
24   *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25   *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26   *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27   *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29   *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30   *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31   *
32   *   $Id: DataSourceContextFactory.java 2242 2010-06-29 07:54:56Z schulte2005 $
33   *
34   */
35  // </editor-fold>
36  // SECTION-END
37  package org.jomc.standalone.ri.naming.support;
38  
39  import java.lang.reflect.InvocationTargetException;
40  import java.lang.reflect.Method;
41  import java.math.BigDecimal;
42  import java.math.BigInteger;
43  import java.util.Hashtable;
44  import java.util.Map;
45  import javax.naming.Context;
46  import javax.naming.NamingException;
47  import org.jomc.standalone.ri.StandaloneEnvironment;
48  
49  // SECTION-START[Documentation]
50  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
51  /**
52   * Standalone data source context factory.
53   * <p><b>Specifications</b><ul>
54   * <li>{@code 'javax.naming.spi.InitialContextFactory'} {@code (javax.naming.spi.InitialContextFactory)} {@code Multiton}</li>
55   * </ul></p>
56   *
57   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
58   * @version $Id: DataSourceContextFactory.java 2242 2010-06-29 07:54:56Z schulte2005 $
59   */
60  // </editor-fold>
61  // SECTION-END
62  // SECTION-START[Annotations]
63  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
64  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
65  // </editor-fold>
66  // SECTION-END
67  public final class DataSourceContextFactory extends AbstractContextFactory
68  {
69      // SECTION-START[InitialContextFactory]
70  
71      public Context getInitialContext( final Hashtable environment ) throws NamingException
72      {
73          try
74          {
75              this.bindDataSource();
76              return null;
77          }
78          catch ( final ClassNotFoundException e )
79          {
80              throw (NamingException) new NamingException( e.getMessage() ).initCause( e );
81          }
82          catch ( final InstantiationException e )
83          {
84              throw (NamingException) new NamingException( e.getMessage() ).initCause( e );
85          }
86          catch ( final IllegalAccessException e )
87          {
88              throw (NamingException) new NamingException( e.getMessage() ).initCause( e );
89          }
90          catch ( final InvocationTargetException e )
91          {
92              throw (NamingException) new NamingException( e.getMessage() ).initCause( e );
93          }
94      }
95  
96      // SECTION-END
97      // SECTION-START[DataSourceContextFactory]
98      private static final Class[] NO_CLASSES =
99      {
100     };
101 
102     private void bindDataSource()
103         throws ClassNotFoundException, InstantiationException, IllegalAccessException, NamingException,
104                InvocationTargetException
105     {
106         final Object dataSource = Class.forName(
107             this.getStandaloneEnvironment().getDataSourceClassName() ).newInstance();
108 
109         this.setupDataSource( dataSource );
110         this.getStandaloneContext().bind( this.getStandaloneEnvironment().getDataSourceJndiName(), dataSource );
111         this.getStandaloneContext().bind( this.getStandaloneEnvironment().getJtaDataSourceJndiName(), dataSource );
112     }
113 
114     private void setupDataSource( final Object dataSource )
115         throws NamingException, IllegalAccessException, InvocationTargetException
116     {
117         for ( Map.Entry p : this.getStandaloneEnvironment().getProperties().entrySet() )
118         {
119             if ( p.getKey().toString().startsWith( StandaloneEnvironment.DATA_SOURCE_PREFIX )
120                  && !p.getKey().toString().equals( StandaloneEnvironment.DATA_SOURCE_JNDI_NAME )
121                  && !p.getKey().toString().equals( StandaloneEnvironment.DATA_SOURCE_CLASS_NAME )
122                  && !p.getKey().toString().equals( StandaloneEnvironment.DATA_SOURCE_CONTEXT_FACTORY_NAME ) )
123             {
124                 final char[] chars =
125                     p.getKey().toString().substring( StandaloneEnvironment.DATA_SOURCE_PREFIX.length() ).toCharArray();
126 
127                 chars[0] = Character.toUpperCase( chars[0] );
128                 final String propertyName = String.valueOf( chars );
129 
130                 Method getter = this.getMethod( dataSource.getClass(), "get" + propertyName, NO_CLASSES );
131                 if ( getter == null )
132                 {
133                     getter = this.getMethod( dataSource.getClass(), "is" + propertyName, NO_CLASSES );
134                 }
135 
136                 Class propertyType = String.class;
137                 if ( getter != null )
138                 {
139                     propertyType = getter.getReturnType();
140                 }
141 
142                 final Method setter = this.getMethod( dataSource.getClass(), "set" + propertyName, new Class[]
143                     {
144                         propertyType
145                     } );
146 
147                 if ( setter != null )
148                 {
149                     if ( propertyType == Boolean.class || propertyType == Boolean.TYPE )
150                     {
151                         setter.invoke( dataSource, Boolean.valueOf( p.getValue().toString() ) );
152                     }
153                     else if ( propertyType == Byte.class || propertyType == Byte.TYPE )
154                     {
155                         setter.invoke( dataSource, Byte.valueOf( p.getValue().toString() ) );
156                     }
157                     else if ( propertyType == Character.class || propertyType == Character.TYPE )
158                     {
159                         setter.invoke( dataSource, Character.valueOf( p.getValue().toString().charAt( 0 ) ) );
160                     }
161                     else if ( propertyType == Double.class || propertyType == Double.TYPE )
162                     {
163                         setter.invoke( dataSource, Double.valueOf( p.getValue().toString() ) );
164                     }
165                     else if ( propertyType == Float.class || propertyType == Float.TYPE )
166                     {
167                         setter.invoke( dataSource, Float.valueOf( p.getValue().toString() ) );
168                     }
169                     else if ( propertyType == Integer.class || propertyType == Integer.TYPE )
170                     {
171                         setter.invoke( dataSource, Integer.valueOf( p.getValue().toString() ) );
172                     }
173                     else if ( propertyType == Long.class || propertyType == Long.TYPE )
174                     {
175                         setter.invoke( dataSource, Long.valueOf( p.getValue().toString() ) );
176                     }
177                     else if ( propertyType == Short.class || propertyType == Short.TYPE )
178                     {
179                         setter.invoke( dataSource, Short.valueOf( p.getValue().toString() ) );
180                     }
181                     else if ( propertyType == BigInteger.class )
182                     {
183                         setter.invoke( dataSource, new BigInteger( p.getValue().toString() ) );
184                     }
185                     else if ( propertyType == BigDecimal.class )
186                     {
187                         setter.invoke( dataSource, new BigDecimal( p.getValue().toString() ) );
188                     }
189                     else if ( propertyType == String.class )
190                     {
191                         setter.invoke( dataSource, p.getValue().toString() );
192                     }
193                 }
194             }
195         }
196     }
197 
198     private Method getMethod( final Class clazz, final String name, final Class[] arguments ) throws NamingException
199     {
200         try
201         {
202             return clazz.getMethod( name, arguments );
203         }
204         catch ( final NoSuchMethodException e )
205         {
206             return null;
207         }
208         catch ( final SecurityException e )
209         {
210             throw (NamingException) new NamingException( e.getMessage() ).initCause( e );
211         }
212     }
213 
214     // SECTION-END
215     // SECTION-START[Constructors]
216     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
217 
218     /** Creates a new {@code DataSourceContextFactory} instance. */
219     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
220     public DataSourceContextFactory()
221     {
222         // SECTION-START[Default Constructor]
223         super();
224         // SECTION-END
225     }
226     // </editor-fold>
227     // SECTION-END
228     // SECTION-START[Dependencies]
229     // SECTION-END
230     // SECTION-START[Properties]
231     // SECTION-END
232     // SECTION-START[Messages]
233     // SECTION-END
234 }