View Javadoc

1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // JtaTransactionStrategy.java Copyright (c) 2009 Giovanni Azua Garcia
5   // bravegag@hotmail.com
6   //  
7   // This program is free software; you can redistribute it and/or
8   // modify it under the terms of the GNU General Public License
9   // as published by the Free Software Foundation; either version 3
10  // of the License, or (at your option) any later version.
11  //
12  // This program is distributed in the hope that it will be useful,
13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  // GNU General Public License for more details.
16  //
17  // You should have received a copy of the GNU General Public License
18  // along with this program; if not, see <http://www.gnu.org/licenses/>.
19  //
20  //----------------------------------------------------------------------
21  package org.perfectjpattern.jee.integration.dao;
22  
23  import javax.transaction.*;
24  
25  import org.apache.commons.lang.*;
26  import org.perfectjpattern.core.api.structural.adapter.*;
27  import org.perfectjpattern.core.structural.adapter.*;
28  import org.perfectjpattern.jee.api.business.servicelocator.*;
29  import org.perfectjpattern.jee.api.integration.dao.*;
30  import org.perfectjpattern.jee.business.servicelocator.*;
31  
32  /**
33   * Concrete implementation of {@link ITransactionStrategy} targeting the
34   * use of {@link UserTransaction}
35   * 
36   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
37   * @version $Revision: 1.0 $Date: Feb 19, 2009 1:51:31 PM $
38   */
39  public 
40  class JtaTransactionStrategy
41  implements ITransactionStrategy
42  {
43      //------------------------------------------------------------------------
44      // public
45      //------------------------------------------------------------------------
46      /**
47       * Constructs a {@link JtaTransactionStrategy} using the default 
48       * UserTransaction service name "java:comp/UserTransaction"
49       */
50      public 
51      JtaTransactionStrategy()
52      {
53          this("java:comp/UserTransaction"); 
54      }
55      
56      //------------------------------------------------------------------------
57      /**
58       * Constructs a {@link JtaTransactionStrategy} from the UserTransaction
59       * service name provided. It can be server-specific.
60       * 
61       * @param aServiceName The Service name
62       * @throws IllegalArgumentException 'aServiceName' must not be null
63       */
64      public 
65      JtaTransactionStrategy(String aServiceName)
66      throws IllegalArgumentException
67      {
68          Validate.notNull(aServiceName, "'aServiceName' must not be null");
69          
70          theServiceName = aServiceName; 
71      }
72  
73      //------------------------------------------------------------------------
74      /** 
75       * {@inheritDoc}
76       */
77      public ITransaction 
78      getTransaction()
79      {
80          IServiceLocator myServiceLocator = ServiceLocator.getInstance();
81          
82          UserTransaction myUserTransaction = myServiceLocator.locate(
83              theServiceName);
84  
85          if (theTransactionAdapter == null || theTransactionAdapter.
86              getAdaptee() != myUserTransaction)
87          {
88              theTransactionAdapter = new Adapter<ITransaction, UserTransaction>(
89                  ITransaction.class, myUserTransaction);
90          }
91          
92          return theTransactionAdapter.getTarget();        
93      }
94      
95      //------------------------------------------------------------------------
96      // members
97      //------------------------------------------------------------------------
98      private final String theServiceName;
99      private IAdapter<ITransaction, UserTransaction> theTransactionAdapter;
100 }