001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.tx.common;
038:
039: import com.sun.xml.ws.tx.at.CoordinationXid;
040: import com.sun.xml.ws.tx.coordinator.CoordinationContextInterface;
041: import java.lang.reflect.InvocationTargetException;
042: import java.lang.reflect.Method;
043: import java.util.logging.Level;
044:
045: import javax.naming.Context;
046: import javax.naming.InitialContext;
047: import javax.naming.NamingException;
048: import javax.transaction.*;
049: import javax.transaction.xa.Xid;
050: import java.util.HashMap;
051: import java.util.Map;
052:
053: /**
054: * Access hosting JTA 1.1 TransactionManager and TransactionSynchronizationRegistry.
055: * <p/>
056: * <p> Dependencies: Sun Java System Application Server publishes TM at JNDI name:
057: *
058: * @author jf39279
059: */
060: public class TransactionManagerImpl implements TransactionManager,
061: TransactionSynchronizationRegistry {
062: final static private TxLogger logger = TxLogger
063: .getATLogger(TransactionManagerImpl.class);
064: final private static TransactionManagerImpl singleton = new TransactionManagerImpl();
065: final private TransactionManager javaeeTM;
066: final private TransactionSynchronizationRegistry javaeeSynchReg;
067:
068: // no standardized JNDI name exists across as implementations for TM, this is Sun App Server specific.
069: private static final String AS_TXN_MGR_JNDI_NAME = "java:appserver/TransactionManager";
070:
071: // standardized name by JTA 1.1 spec
072: private static final String TXN_SYNC_REG_JNDI_NAME = "java:comp/TransactionSynchronizationRegistry";
073:
074: private static final String USER_TRANSACTION_JNDI_NAME = "java:comp/UserTransaction";
075:
076: static public TransactionManagerImpl getInstance() {
077: return singleton;
078: }
079:
080: TransactionManager getTransactionManager() {
081: return javaeeTM;
082: }
083:
084: static private Object jndiLookup(final String jndiName) {
085: Object result = null;
086: try {
087: final Context ctx = new InitialContext();
088: result = ctx.lookup(jndiName);
089: } catch (NamingException e) {
090: logger.fine("jndiLookup", LocalizationMessages
091: .FAILED_JNDI_LOOKUP_2001(jndiName));
092: }
093: return result;
094: }
095:
096: public UserTransaction getUserTransaction() {
097: return (UserTransaction) jndiLookup(USER_TRANSACTION_JNDI_NAME);
098: }
099:
100: public boolean isTransactionManagerAvailable() {
101: return javaeeTM != null;
102: }
103:
104: /**
105: * Creates a new instance of TransactionManagerImpl
106: */
107: private TransactionManagerImpl() {
108: javaeeTM = (TransactionManager) jndiLookup(AS_TXN_MGR_JNDI_NAME);
109: javaeeSynchReg = (TransactionSynchronizationRegistry) jndiLookup(TXN_SYNC_REG_JNDI_NAME);
110: }
111:
112: public void begin() throws NotSupportedException, SystemException {
113: javaeeTM.begin();
114: }
115:
116: public void commit() throws RollbackException,
117: HeuristicMixedException, HeuristicRollbackException,
118: SecurityException, IllegalStateException, SystemException {
119: javaeeTM.commit();
120: }
121:
122: public int getStatus() throws SystemException {
123: return javaeeTM.getStatus();
124: }
125:
126: public javax.transaction.Transaction getTransaction()
127: throws SystemException {
128: return javaeeTM.getTransaction();
129: }
130:
131: public void resume(final Transaction transaction)
132: throws InvalidTransactionException, IllegalStateException,
133: SystemException {
134: javaeeTM.resume(transaction);
135: servletPreInvokeTx();
136: }
137:
138: public void rollback() throws IllegalStateException,
139: SecurityException, SystemException {
140: javaeeTM.rollback();
141: }
142:
143: public void setRollbackOnly() throws IllegalStateException {
144: javaeeSynchReg.setRollbackOnly();
145: }
146:
147: public void setTransactionTimeout(final int seconds)
148: throws SystemException {
149: javaeeTM.setTransactionTimeout(seconds);
150: }
151:
152: public Transaction suspend() throws SystemException {
153: servletPostInvokeTx(true);
154: return javaeeTM.suspend();
155: }
156:
157: public Object getTransactionKey() {
158: return javaeeSynchReg.getTransactionKey();
159: }
160:
161: public void putResource(final Object object, final Object object0) {
162: javaeeSynchReg.putResource(object, object0);
163: }
164:
165: public Object getResource(final Object object) {
166: return javaeeSynchReg.getResource(object);
167: }
168:
169: public void registerInterposedSynchronization(
170: final Synchronization synchronization) {
171: javaeeSynchReg
172: .registerInterposedSynchronization(synchronization);
173: }
174:
175: public void registerSynchronization(final Synchronization sync) {
176: final String METHOD = "registerSynchronization";
177:
178: if (sync == null) {
179: return;
180: }
181:
182: Transaction txn = null;
183: try {
184: txn = javaeeTM.getTransaction();
185: } catch (SystemException ex) {
186: logger.info(METHOD, LocalizationMessages
187: .OPERATION_FAILED_2010("getTransaction"), ex);
188: }
189: if (txn == null) {
190: logger.warning(METHOD, LocalizationMessages
191: .REGISTER_SYNCH_NO_CURRENT_TXN_2011(sync.getClass()
192: .getName()));
193: } else {
194: try {
195: txn.registerSynchronization(sync);
196: } catch (IllegalStateException ex) {
197: logger.info(METHOD, LocalizationMessages
198: .OPERATION_FAILED_2010(METHOD), ex);
199: } catch (RollbackException ex) {
200: logger.info(METHOD, LocalizationMessages
201: .OPERATION_FAILED_2010(METHOD), ex);
202: } catch (SystemException ex) {
203: logger.info(METHOD, LocalizationMessages
204: .OPERATION_FAILED_2010(METHOD), ex);
205: }
206: }
207: }
208:
209: public int getTransactionStatus() {
210: return javaeeSynchReg.getTransactionStatus();
211: }
212:
213: public boolean getRollbackOnly() {
214: return javaeeSynchReg.getRollbackOnly();
215: }
216:
217: /**
218: * Get the coordination context associated with the current transaction.
219: * <p/>
220: * Returns null if none set.
221: */
222: public CoordinationContextInterface getCoordinationContext() {
223: return (CoordinationContextInterface) getResource("WSCOOR-SUN");
224: }
225:
226: /**
227: * Set the coordination context associated with the current transaction.
228: */
229: public void setCoordinationContext(
230: final CoordinationContextInterface coordCtx) {
231: putResource("WSCOOR-SUN", coordCtx);
232: }
233:
234: static private Method getMethod(Class theClass, String methodName,
235: Class param) {
236: Method method = null;
237: try {
238: if (param == null) {
239: method = theClass.getMethod(methodName);
240: } else {
241: method = theClass.getMethod(methodName, param);
242: }
243: logger.finest("getMethod",
244: "found Sun App Server 9.1 container specific method via reflection "
245: + theClass.getName() + "." + methodName);
246: } catch (Exception e) {
247: logger.finest("getMethod", "reflection lookup of "
248: + theClass.getName() + "." + methodName + "("
249: + (param == null ? "" : param.getName())
250: + ") failed with handled exception ", e);
251: }
252: return method;
253: }
254:
255: static private boolean initialized = false;
256: static private Method servletPreInvokeTxMethod = null;
257: static private Method servletPostInvokeTxMethod = null;
258:
259: private void initServletMethods() {
260: if (initialized == false) {
261: initialized = true;
262: servletPreInvokeTxMethod = getMethod(javaeeTM.getClass(),
263: "servletPreInvokeTx", null);
264: servletPostInvokeTxMethod = getMethod(javaeeTM.getClass(),
265: "servletPostInvokeTx", boolean.class);
266: }
267: }
268:
269: /**
270: * PreInvoke Transaction configuration for Servlet Container.
271: * BaseContainer.preInvokeTx() handles all this for CMT EJB.
272: *
273: * Compensate that J2EEInstanceListener.handleBeforeEvent(BEFORE_SERVICE_EVENT)
274: * gets called before WSIT WSTX Service pipe associates a JTA txn with incoming thread.
275: *
276: * Precondition: assumes JTA transaction already associated with current thread.
277: *
278: * Note: this method is a no-op when invoked on an EJB.
279: */
280: public void servletPreInvokeTx() {
281: final String METHOD = "servletPreInvokeTx";
282: initServletMethods();
283: if (servletPreInvokeTxMethod != null) {
284: try {
285: servletPreInvokeTxMethod.invoke(javaeeTM);
286: } catch (Throwable ex) {
287: logger.info(METHOD, LocalizationMessages
288: .OPERATION_FAILED_2010(METHOD), ex);
289: }
290: }
291: }
292:
293: /**
294: * PostInvoke Transaction configuration for Servlet Container.
295: * BaseContainer.preInvokeTx() handles all this for CMT EJB.
296: *
297: * Precondition: assumed called prior to current transcation being suspended or released.
298: *
299: * Note: this method is a no-op when invoked on an EJB. The J2EE method only has an effect
300: * on servlets.
301: *
302: * @param suspend indicate whether the delisting is due to suspension or transaction completion(commmit/rollback)
303: */
304: public void servletPostInvokeTx(Boolean suspend) {
305: final String METHOD = "servletPostInvokeTx";
306: initServletMethods();
307: if (servletPostInvokeTxMethod != null) {
308: try {
309: servletPostInvokeTxMethod.invoke(javaeeTM, suspend);
310: } catch (Throwable ex) {
311: logger.info(METHOD, LocalizationMessages
312: .OPERATION_FAILED_2010(METHOD), ex);
313: }
314: }
315: }
316:
317: /**
318: * Returns in seconds duration till current transaction times out.
319: * Returns negative value if transaction has already timedout.
320: * Returns 0 if there is no timeout.
321: * Returns 0 if any exceptions occur looking up remaining transaction timeout.
322: */
323: public int getRemainingTimeout() {
324: final String METHOD = "getRemainingTimeout";
325: try {
326: return TransactionImportManager.getInstance()
327: .getTransactionRemainingTimeout();
328: } catch (SystemException se) {
329: if (logger.isLogging(Level.FINEST)) {
330: logger.finest(METHOD,
331: "getRemainingTimeout stack trace", se);
332: } else {
333: logger.info(METHOD, LocalizationMessages
334: .TXN_MGR_OPERATION_FAILED_2008(
335: "getTransactionRemainingTimeout", se
336: .getLocalizedMessage()));
337: }
338: } catch (Throwable t) {
339: if (logger.isLogging(Level.FINEST)) {
340: logger
341: .finest(METHOD,
342: "getTransactionRemainingTimeout() failed, default to no timeout");
343: } else {
344: logger.info(METHOD, LocalizationMessages
345: .TXN_MGR_OPERATION_FAILED_2008(
346: "getTransactionRemainingTimeout", t
347: .getLocalizedMessage()));
348:
349: }
350: }
351: return 0;
352: }
353: }
|