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: StandaloneContextFactory.java 2270 2010-06-29 13:25:34Z schulte2005 $
33 *
34 */
35 // </editor-fold>
36 // SECTION-END
37 package org.jomc.standalone.ri.naming;
38
39 import java.lang.reflect.InvocationTargetException;
40 import java.lang.reflect.Method;
41 import java.util.Hashtable;
42 import javax.naming.Context;
43 import javax.naming.NamingException;
44 import javax.naming.spi.InitialContextFactory;
45 import org.jomc.standalone.ri.StandaloneEnvironment;
46
47 // SECTION-START[Documentation]
48 // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
49 /**
50 * Standalone {@code InitialContextFactory} implementation.
51 * <p><b>Specifications</b><ul>
52 * <li>{@code 'javax.naming.spi.InitialContextFactory'} {@code (javax.naming.spi.InitialContextFactory)} {@code Multiton}</li>
53 * </ul></p>
54 *
55 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
56 * @version $Id: StandaloneContextFactory.java 2270 2010-06-29 13:25:34Z schulte2005 $
57 */
58 // </editor-fold>
59 // SECTION-END
60 // SECTION-START[Annotations]
61 // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
62 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
63 // </editor-fold>
64 // SECTION-END
65 public class StandaloneContextFactory implements InitialContextFactory
66 {
67 // SECTION-START[InitialContextFactory]
68
69 private static Context instance;
70
71 public Context getInitialContext( final Hashtable env ) throws NamingException
72 {
73 synchronized ( StandaloneContextFactory.class )
74 {
75 try
76 {
77 if ( instance == null )
78 {
79 instance = new StandaloneContext();
80
81 if ( env != null )
82 {
83 System.getProperties().putAll( env );
84 instance.getEnvironment().putAll( env );
85
86 if ( this.getStandaloneEnvironment().getDataSourceContextFactoryName() != null )
87 {
88 final InitialContextFactory dataSourceContextFactory = (InitialContextFactory) Class.forName(
89 this.getStandaloneEnvironment().getDataSourceContextFactoryName() ).newInstance();
90
91 dataSourceContextFactory.getInitialContext( env );
92 }
93 if ( this.getStandaloneEnvironment().getJtaContextFactoryName() != null )
94 {
95 final InitialContextFactory jtaContextFactory = (InitialContextFactory) Class.forName(
96 this.getStandaloneEnvironment().getJtaContextFactoryName() ).newInstance();
97
98 jtaContextFactory.getInitialContext( env );
99 }
100 if ( this.getStandaloneEnvironment().getJpaContextFactoryName() != null )
101 {
102 final InitialContextFactory jpaContextFactory = (InitialContextFactory) Class.forName(
103 this.getStandaloneEnvironment().getJpaContextFactoryName() ).newInstance();
104
105 jpaContextFactory.getInitialContext( env );
106 }
107
108 this.isXAPoolInitialized();
109 }
110 }
111
112 return instance;
113 }
114 catch ( final InstantiationException e )
115 {
116 throw (NamingException) new NamingException( e.getMessage() ).initCause( e );
117 }
118 catch ( final IllegalAccessException e )
119 {
120 throw (NamingException) new NamingException( e.getMessage() ).initCause( e );
121 }
122 catch ( final ClassNotFoundException e )
123 {
124 throw (NamingException) new NamingException( e.getMessage() ).initCause( e );
125 }
126 }
127 }
128
129 // SECTION-END
130 // SECTION-START[StandaloneContextFactory]
131 private StandaloneEnvironment standaloneEnvironment;
132
133 public StandaloneEnvironment getStandaloneEnvironment()
134 {
135 if ( this.standaloneEnvironment == null )
136 {
137 this.standaloneEnvironment = new StandaloneEnvironment();
138 }
139
140 return this.standaloneEnvironment;
141 }
142
143 protected boolean isXAPoolInitialized()
144 {
145 try
146 {
147 Class.forName( "org.enhydra.jdbc.standard.StandardXADataSource" );
148 final Class helperClass = Class.forName( "org.jomc.standalone.ri.naming.support.XAPoolHelper" );
149 final Method helperMethod = helperClass.getDeclaredMethod(
150 "initializeXAPool", StandaloneEnvironment.class, Context.class );
151
152 helperMethod.invoke( null, this.getStandaloneEnvironment(), instance );
153 return true;
154 }
155 catch ( final IllegalAccessException e )
156 {
157 return false;
158 }
159 catch ( final InvocationTargetException e )
160 {
161 return false;
162 }
163 catch ( final NoSuchMethodException e )
164 {
165 return false;
166 }
167 catch ( final ClassNotFoundException e )
168 {
169 return false;
170 }
171 }
172
173 // SECTION-END
174 // SECTION-START[Constructors]
175 // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
176
177 /** Creates a new {@code StandaloneContextFactory} instance. */
178 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
179 public StandaloneContextFactory()
180 {
181 // SECTION-START[Default Constructor]
182 super();
183 // SECTION-END
184 }
185 // </editor-fold>
186 // SECTION-END
187 // SECTION-START[Dependencies]
188 // SECTION-END
189 // SECTION-START[Properties]
190 // SECTION-END
191 // SECTION-START[Messages]
192 // SECTION-END
193 }