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: JBossJTAContextFactory.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 com.arjuna.ats.arjuna.coordinator.TransactionReaper;
40  import com.arjuna.ats.arjuna.recovery.RecoveryManager;
41  import com.arjuna.ats.jdbc.TransactionalDriver;
42  import com.arjuna.ats.jdbc.common.jdbcPropertyManager;
43  import com.arjuna.ats.jta.common.jtaPropertyManager;
44  import com.arjuna.ats.jta.utils.JNDIManager;
45  import java.io.PrintWriter;
46  import java.sql.Connection;
47  import java.sql.SQLException;
48  import java.util.Hashtable;
49  import java.util.Properties;
50  import javax.naming.Context;
51  import javax.naming.NamingException;
52  import javax.sql.DataSource;
53  
54  // SECTION-START[Documentation]
55  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
56  /**
57   * Standalone JBoss JTA context factory.
58   * <p><b>Specifications</b><ul>
59   * <li>{@code 'javax.naming.spi.InitialContextFactory'} {@code (javax.naming.spi.InitialContextFactory)} {@code Multiton}</li>
60   * </ul></p>
61   * <p><b>Properties</b><ul>
62   * <li>"{@link #isTransactionalDriverEnabled transactionalDriverEnabled}"
63   * <blockquote>Property of type {@code boolean}.
64   * <p>{@code true} to enable ArjunaJTA's transactional JDBC driver (no JDBC3 support); {@code false} to disable ArjunaJTA's transactional JDBC driver.</p>
65   * </blockquote></li>
66   * </ul></p>
67   *
68   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
69   * @version $Id: JBossJTAContextFactory.java 2242 2010-06-29 07:54:56Z schulte2005 $
70   */
71  // </editor-fold>
72  // SECTION-END
73  // SECTION-START[Annotations]
74  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
75  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
76  // </editor-fold>
77  // SECTION-END
78  public final class JBossJTAContextFactory extends AbstractContextFactory
79  {
80      // SECTION-START[InitialContextFactory]
81  
82      public Context getInitialContext( final Hashtable<?, ?> environment ) throws NamingException
83      {
84          jdbcPropertyManager.getPropertyManager().setProperty(
85              "Context.INITIAL_CONTEXT_FACTORY", (String) environment.get( Context.INITIAL_CONTEXT_FACTORY ) );
86  
87          jdbcPropertyManager.getPropertyManager().setProperty(
88              "Context.URL_PKG_PREFIXES", (String) environment.get( Context.URL_PKG_PREFIXES ) );
89  
90          jtaPropertyManager.getPropertyManager().setProperty(
91              com.arjuna.ats.jta.common.Environment.TM_JNDI_CONTEXT,
92              this.getStandaloneEnvironment().getTransactionManagerJndiName() );
93  
94          jtaPropertyManager.getPropertyManager().setProperty(
95              com.arjuna.ats.jta.common.Environment.UT_JNDI_CONTEXT,
96              this.getStandaloneEnvironment().getUserTransactionJndiName() );
97  
98          jtaPropertyManager.getPropertyManager().setProperty(
99              com.arjuna.ats.jta.common.Environment.TSR_JNDI_CONTEXT,
100             this.getStandaloneEnvironment().getTransactionSynchronizationRegistryJndiName() );
101 
102         JNDIManager.bindJTAImplementation();
103 
104         TransactionReaper.create();
105 
106         final RecoveryManager recoveryManager = RecoveryManager.manager();
107         recoveryManager.startRecoveryManagerThread();
108 
109         if ( this.isTransactionalDriverEnabled() )
110         {
111             this.getStandaloneContext().rebind( this.getStandaloneEnvironment().getJtaDataSourceJndiName(),
112                                                 this.getTransactionalDataSource() );
113 
114         }
115 
116         return null;
117     }
118 
119     private DataSource getTransactionalDataSource()
120     {
121         return new DataSource()
122         {
123 
124             private final TransactionalDriver driver = new TransactionalDriver();
125 
126             private PrintWriter logWriter;
127 
128             private boolean logWriterSet;
129 
130             private int loginTimeout;
131 
132             public Connection getConnection() throws SQLException
133             {
134                 return this.getConnection( getStandaloneEnvironment().getDataSourceUser(),
135                                            getStandaloneEnvironment().getDataSourcePassword() );
136 
137             }
138 
139             public Connection getConnection( final String username, final String password ) throws SQLException
140             {
141                 final String url =
142                     TransactionalDriver.arjunaDriver + getStandaloneEnvironment().getDataSourceJndiName();
143 
144                 final Properties properties = new Properties();
145                 properties.setProperty( TransactionalDriver.userName, username );
146                 properties.setProperty( TransactionalDriver.password, password );
147                 return this.driver.connect( url, properties );
148             }
149 
150             public PrintWriter getLogWriter() throws SQLException
151             {
152                 if ( this.logWriter == null && !this.logWriterSet )
153                 {
154                     this.logWriter = new PrintWriter( System.out );
155                 }
156 
157                 return this.logWriter;
158             }
159 
160             public void setLogWriter( final PrintWriter out ) throws SQLException
161             {
162                 this.logWriter = out;
163                 this.logWriterSet = true;
164             }
165 
166             public void setLoginTimeout( final int seconds ) throws SQLException
167             {
168                 this.loginTimeout = seconds;
169             }
170 
171             public int getLoginTimeout() throws SQLException
172             {
173                 return this.loginTimeout;
174             }
175 
176             public <T> T unwrap( final Class<T> iface ) throws SQLException
177             {
178                 return (T) ( this.isWrapperFor( iface ) ? this : null );
179             }
180 
181             public boolean isWrapperFor( final Class<?> iface ) throws SQLException
182             {
183                 return iface.isAssignableFrom( DataSource.class );
184             }
185 
186         };
187     }
188 
189     // SECTION-END
190     // SECTION-START[JBossJTAContextFactory]
191     // SECTION-END
192     // SECTION-START[Constructors]
193     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
194 
195     /** Creates a new {@code JBossJTAContextFactory} instance. */
196     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
197     public JBossJTAContextFactory()
198     {
199         // SECTION-START[Default Constructor]
200         super();
201         // SECTION-END
202     }
203     // </editor-fold>
204     // SECTION-END
205     // SECTION-START[Dependencies]
206     // SECTION-END
207     // SECTION-START[Properties]
208     // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
209 
210     /**
211      * Gets the value of the {@code transactionalDriverEnabled} property.
212      * @return {@code true} to enable ArjunaJTA's transactional JDBC driver (no JDBC3 support); {@code false} to disable ArjunaJTA's transactional JDBC driver.
213      * @throws org.jomc.ObjectManagementException if getting the property instance fails.
214      */
215     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
216     private boolean isTransactionalDriverEnabled()
217     {
218         final java.lang.Boolean _p = (java.lang.Boolean) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "transactionalDriverEnabled" );
219         assert _p != null : "'transactionalDriverEnabled' property not found.";
220         return _p.booleanValue();
221     }
222     // </editor-fold>
223     // SECTION-END
224     // SECTION-START[Messages]
225     // SECTION-END
226 }