KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > panther > wrapper > SessionBeanWrapper


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.panther.wrapper;
56
57 import java.io.Serializable JavaDoc;
58 import java.rmi.RemoteException JavaDoc;
59 import java.rmi.server.UnicastRemoteObject JavaDoc;
60 import javax.ejb.EJBHome JavaDoc;
61 import javax.ejb.EJBObject JavaDoc;
62 import javax.ejb.Handle JavaDoc;
63 import javax.ejb.RemoveException JavaDoc;
64 import javax.ejb.SessionContext JavaDoc;
65 import javax.transaction.Status JavaDoc;
66 import javax.transaction.TransactionManager JavaDoc;
67
68 import org.apache.log4j.Logger;
69
70 import org.lateralnz.common.util.Constants;
71 import org.lateralnz.common.util.JNDIUtils;
72 import org.lateralnz.common.util.StringUtils;
73 import org.lateralnz.common.util.TransUtils;
74 import org.lateralnz.common.util.StackThreadLocal;
75
76 /**
77  * a wrapper over a session bean --
78  * See the sessionbean.vm (velocity macro) which subclasses SessionBeanWrapper
79  * to provide the rest of the required functionality for a Session Bean.
80  *
81  * Note that this implements the Handle interface, but probably violates the 'contract'
82  * for a handle (e.g. A handle is an abstraction of a network reference to an EJB object.)
83  *
84  * @author J R Briggs
85  */

86 public abstract class SessionBeanWrapper extends UnicastRemoteObject JavaDoc implements Serializable JavaDoc, Handle JavaDoc, Constants {
87   private static final Logger log = Logger.getLogger(SessionBeanWrapper.class.getName());
88     
89   private static TransactionManager JavaDoc tm = null;
90   protected SessionContext JavaDoc ctx;
91   
92   protected SessionBeanWrapper() throws RemoteException JavaDoc {
93     super();
94     
95     if (tm == null) {
96       synchronized (SessionBeanWrapper.class) {
97         if (tm == null) {
98           try {
99             tm = (TransactionManager JavaDoc)JNDIUtils.get("java:/TransactionManager");
100           }
101           catch (Exception JavaDoc e) {
102             log.error(e);
103           }
104         }
105       }
106     }
107   }
108   
109   protected boolean beginTrans(String JavaDoc transactionType) {
110     try {
111       if (log.isDebugEnabled()) {
112         log.debug("transaction manager status: " + TransUtils.getStatus(tm));
113       }
114       if (tm.getStatus() != Status.STATUS_ACTIVE && !transactionType.equalsIgnoreCase(SUPPORTS)) {
115         tm.begin();
116         return true;
117       }
118     }
119     catch (Exception JavaDoc e) {
120       // TO DO: handle this better
121
e.printStackTrace();
122     }
123     return false;
124   }
125   
126   protected void checkTransEnd(boolean transStartedByCaller) {
127     try {
128       // if there is nothing in the trans attr threadlocal variable
129
// then we are at the end of the ejb method chain
130
// so we'll do the rollback or commit
131
// or if the transaction was started by the caller then it should be finished by the caller
132
if (StringUtils.isEmpty((String JavaDoc)StackThreadLocal.peek()) || transStartedByCaller) {
133         int status = tm.getStatus();
134         if (log.isDebugEnabled()) {
135           log.debug("status: " + TransUtils.getStatus(tm));
136         }
137         
138         if (status == Status.STATUS_MARKED_ROLLBACK) {
139           if (log.isDebugEnabled()) {
140             log.debug("rolling back transaction");
141           }
142           tm.rollback();
143         }
144         else {
145           if (log.isDebugEnabled()) {
146             log.debug("committing transaction");
147           }
148           tm.commit();
149         }
150       }
151       else if (log.isDebugEnabled()) {
152         log.debug("not at end of transaction chain");
153       }
154     }
155     catch (Exception JavaDoc e) {
156       // need to replace this code if we change to a 'better' transaction
157
// manager. But in the meantime this error shouldn't occur
158
e.printStackTrace();
159     }
160   }
161   
162   public EJBHome JavaDoc getEJBHome() throws RemoteException JavaDoc {
163     return ctx.getEJBHome();
164   }
165
166   public void remove() throws RemoteException JavaDoc, RemoveException JavaDoc {
167     ctx.getEJBHome().remove(this.getHandle());
168   }
169   
170   public boolean isIdentical(EJBObject JavaDoc obj) throws RemoteException JavaDoc {
171     if (obj == null) {
172       return false;
173     }
174     else if (getEJBHome().getEJBMetaData().isStatelessSession()) {
175       return this.getClass().equals(obj.getClass());
176     }
177     else {
178       return this.equals(obj);
179     }
180   }
181
182   public Handle JavaDoc getHandle() throws RemoteException JavaDoc {
183     return this;
184   }
185
186   public Object JavaDoc getPrimaryKey() throws RemoteException JavaDoc {
187     throw new RemoteException JavaDoc("primary key method not supported for session beans");
188   }
189
190 }
Popular Tags