KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ejb > SessionContextImpl


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.ejb;
8
9 import java.lang.reflect.Proxy JavaDoc;
10 import javax.ejb.EJBException JavaDoc;
11 import javax.ejb.EJBLocalObject JavaDoc;
12 import javax.ejb.EJBObject JavaDoc;
13 import javax.ejb.SessionContext JavaDoc;
14 import javax.transaction.Status JavaDoc;
15 import javax.transaction.SystemException JavaDoc;
16 import javax.transaction.UserTransaction JavaDoc;
17
18 import org.jfox.ejb.connector.EJBConnectorInvoker;
19 import org.jfox.ejb.meta.SessionDescriptor;
20 import org.jfox.ioc.connector.local.LOCALConnectorRemote;
21 import org.jfox.tm.ServerUserTransaction;
22 import org.jfox.tm.TxManager;
23
24 /**
25  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
26  */

27
28 public class SessionContextImpl extends EJBContextSupport implements SessionContext JavaDoc {
29     private UserTransaction JavaDoc ut = null;
30
31     public SessionContextImpl(EJBObjectId objectId, BucketMeta bucketMeta) {
32         super(objectId, bucketMeta);
33     }
34
35     public EJBLocalObject JavaDoc getEJBLocalObject() throws IllegalStateException JavaDoc {
36         throw new IllegalStateException JavaDoc("do not need EJBLocalObject as to jfox!");
37     }
38
39     // use LocalInvocationHanlder & LOCALContainerService
40
public EJBObject JavaDoc getEJBObject() throws IllegalStateException JavaDoc {
41         ClassLoader JavaDoc ctxLoader = Thread.currentThread().getContextClassLoader();
42         // get the EJBObject class name
43
String JavaDoc remoteInterfaceName = objectId.getRemoteInterfaceName();
44         try {
45             Class JavaDoc remoteInterface = ctxLoader.loadClass(remoteInterfaceName);
46             return (EJBObject JavaDoc) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
47                     new Class JavaDoc[]{remoteInterface},
48                     new EJBConnectorInvoker(objectId, LOCALConnectorRemote.getInstance()));
49         }
50         catch(Exception JavaDoc e) {
51             throw new EJBException JavaDoc(e);
52         }
53     }
54
55     public UserTransaction JavaDoc getUserTransaction() throws IllegalStateException JavaDoc {
56         if(ut == null) {
57             if(!((SessionDescriptor) bucketMeta.getEJBDescriptor()).isBeanManagedTransaction()) {
58                 throw new IllegalStateException JavaDoc("EJBContext.getUserTransaction() not allowed for CMT beans");
59             }
60             else {
61                 ut = ServerUserTransaction.getInstance();
62             }
63         }
64         return ut;
65     }
66
67
68     public void setRollbackOnly() throws IllegalStateException JavaDoc {
69         if(((SessionDescriptor) bucketMeta.getEJBDescriptor()).isBeanManagedTransaction()) {
70             throw new IllegalStateException JavaDoc("EJBContext.setRollbackOnly() not allowed for BMT beans.");
71         }
72
73         try {
74             TxManager.getInstance().setRollbackOnly();
75         }
76         catch(IllegalStateException JavaDoc e) {
77             logger.warn(e.getMessage(), e);
78         }
79         catch(SystemException JavaDoc e) {
80             logger.warn("failed to set rollback only", e);
81         }
82     }
83
84     public boolean getRollbackOnly() throws IllegalStateException JavaDoc {
85         if(((SessionDescriptor) bucketMeta.getEJBDescriptor()).isBeanManagedTransaction()) {
86             throw new IllegalStateException JavaDoc("EJBContext.setRollbackOnly() not allowed for BMT beans.");
87         }
88         try {
89             return TxManager.getInstance().getStatus() == Status.STATUS_MARKED_ROLLBACK;
90         }
91         catch(SystemException JavaDoc e) {
92             logger.warn("failed to set get tx manager status", e);
93             return true;
94         }
95     }
96
97     public javax.xml.rpc.handler.MessageContext JavaDoc getMessageContext() throws IllegalStateException JavaDoc {
98         throw new EJBException JavaDoc("not implement yet!");
99     }
100
101 }
102
Popular Tags