001 // SECTION-START[License Header] 002 // <editor-fold defaultstate="collapsed" desc=" Generated License "> 003 /* 004 * Copyright (c) 2010 The JOMC Project 005 * Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net> 006 * All rights reserved. 007 * 008 * Redistribution and use in source and binary forms, with or without 009 * modification, are permitted provided that the following conditions 010 * are met: 011 * 012 * o Redistributions of source code must retain the above copyright 013 * notice, this list of conditions and the following disclaimer. 014 * 015 * o Redistributions in binary form must reproduce the above copyright 016 * notice, this list of conditions and the following disclaimer in 017 * the documentation and/or other materials provided with the 018 * distribution. 019 * 020 * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS" 021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 022 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 023 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR 024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 027 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 028 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 029 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 030 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 031 * 032 * $Id: StandaloneContextFactory.java 2270 2010-06-29 13:25:34Z schulte2005 $ 033 * 034 */ 035 // </editor-fold> 036 // SECTION-END 037 package org.jomc.standalone.ri.naming; 038 039 import java.lang.reflect.InvocationTargetException; 040 import java.lang.reflect.Method; 041 import java.util.Hashtable; 042 import javax.naming.Context; 043 import javax.naming.NamingException; 044 import javax.naming.spi.InitialContextFactory; 045 import org.jomc.standalone.ri.StandaloneEnvironment; 046 047 // SECTION-START[Documentation] 048 // <editor-fold defaultstate="collapsed" desc=" Generated Documentation "> 049 /** 050 * Standalone {@code InitialContextFactory} implementation. 051 * <p><b>Specifications</b><ul> 052 * <li>{@code 'javax.naming.spi.InitialContextFactory'} {@code (javax.naming.spi.InitialContextFactory)} {@code Multiton}</li> 053 * </ul></p> 054 * 055 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0 056 * @version $Id: StandaloneContextFactory.java 2270 2010-06-29 13:25:34Z schulte2005 $ 057 */ 058 // </editor-fold> 059 // SECTION-END 060 // SECTION-START[Annotations] 061 // <editor-fold defaultstate="collapsed" desc=" Generated Annotations "> 062 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" ) 063 // </editor-fold> 064 // SECTION-END 065 public class StandaloneContextFactory implements InitialContextFactory 066 { 067 // SECTION-START[InitialContextFactory] 068 069 private static Context instance; 070 071 public Context getInitialContext( final Hashtable env ) throws NamingException 072 { 073 synchronized ( StandaloneContextFactory.class ) 074 { 075 try 076 { 077 if ( instance == null ) 078 { 079 instance = new StandaloneContext(); 080 081 if ( env != null ) 082 { 083 System.getProperties().putAll( env ); 084 instance.getEnvironment().putAll( env ); 085 086 if ( this.getStandaloneEnvironment().getDataSourceContextFactoryName() != null ) 087 { 088 final InitialContextFactory dataSourceContextFactory = (InitialContextFactory) Class.forName( 089 this.getStandaloneEnvironment().getDataSourceContextFactoryName() ).newInstance(); 090 091 dataSourceContextFactory.getInitialContext( env ); 092 } 093 if ( this.getStandaloneEnvironment().getJtaContextFactoryName() != null ) 094 { 095 final InitialContextFactory jtaContextFactory = (InitialContextFactory) Class.forName( 096 this.getStandaloneEnvironment().getJtaContextFactoryName() ).newInstance(); 097 098 jtaContextFactory.getInitialContext( env ); 099 } 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 }