1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
65
66
67 public final class DataSourceContextFactory extends AbstractContextFactory
68 {
69
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
97
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
215
216
217
218
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
223 super();
224
225 }
226
227
228
229
230
231
232
233
234 }