1 6 7 package org.jfox.ejb; 8 9 import java.lang.reflect.Proxy ; 10 import java.rmi.RemoteException ; 11 import javax.ejb.EJBContext ; 12 import javax.ejb.EJBException ; 13 import javax.ejb.EnterpriseBean ; 14 import javax.ejb.Handle ; 15 import javax.ejb.MessageDrivenBean ; 16 import javax.ejb.MessageDrivenContext ; 17 import javax.ejb.RemoveException ; 18 import javax.jms.Destination ; 19 import javax.jms.JMSException ; 20 import javax.jms.Session ; 21 22 import org.jfox.ejb.connector.EJBInvocation; 23 import org.jfox.ejb.meta.MDBDescriptor; 24 import org.jfox.ejb.timer.EJBTimerService; 25 import org.jfox.ioc.ComponentName; 26 import org.jfox.ioc.connector.ConnectorHelper; 27 import org.jfox.ioc.connector.ConnectorRemote; 28 import org.jfox.ioc.connector.ObjectId; 29 import org.jfox.ioc.depend.DependencyPack; 30 import org.jfox.ioc.depend.ObjectDependency; 31 import org.jfox.ioc.util.ObjectUUID; 32 import org.jfox.jms.JMSConnectionRemote; 33 import org.jfox.jms.JMSConsumer; 34 import org.jfox.jms.connector.JMSConnectorInvoker; 35 import org.jfox.jms.connector.JMSContainer; 36 import org.jfox.jms.message.JMSMessage; 37 import org.jfox.jndi.InitialContextHelper; 38 import org.jfox.jndi.JNDIProperties; 39 40 45 46 public class MDBBucket extends BucketSupport { 47 50 private static MDBConnection conn = null; 51 52 private MDBSession session = null; 53 54 private MDBObjectPool instancePool = null; 55 56 private MessageDrivenContext ejbConext = null; 57 58 private String user = null; 59 private String password = null; 60 61 public MDBBucket() { 62 super(); 63 } 64 65 public Object getPrimaryKey(EnterpriseBean obj) throws RemoteException { 67 throw new EJBException ("NOT SUPPORT!"); 68 } 69 70 public Handle getHandle(EnterpriseBean obj) throws RemoteException { 71 throw new EJBException ("NOT SUPPORT!"); 72 } 73 74 public boolean isIdentical(EJBObjectId thisObjectId, EJBObjectId thatObjectId) throws RemoteException { 75 throw new EJBException ("NOT SUPPORT!"); 76 } 77 78 public void remove(EnterpriseBean bean) throws RemoteException , RemoveException { 79 ejbRemove(bean); 80 } 81 82 public void remove(Handle handle) throws RemoteException , RemoveException { 83 throw new EJBException ("NOT SUPPORT!"); 84 } 85 86 public void remove(Object primaryKey) throws RemoteException , RemoveException { 87 throw new EJBException ("NOT SUPPORT!"); 88 } 89 90 96 public Object invokeHome(EJBInvocation invocation) throws Exception { 97 throw new EJBException ("MessageDrivenBean has no EJBHome."); 98 } 99 100 public Object invokeBean(EJBInvocation invocation) throws Exception { 101 return super.invokeBean(invocation); 102 } 103 104 public void ejbActivate(EnterpriseBean bean) throws EJBException , RemoteException { 105 throw new EJBException ("NOT SUPPORT!"); 106 } 107 108 public void ejbPassivate(EnterpriseBean bean) throws EJBException , RemoteException { 109 throw new EJBException ("NOT SUPPORT!"); 110 } 111 112 public void ejbRemove(EnterpriseBean bean) throws EJBException , RemoteException { 113 logger.debug(bean + " remove"); 114 ((MessageDrivenBean )bean).ejbRemove(); 115 } 116 117 public void setContext(EnterpriseBean bean, EJBContext ctx) throws EJBException , RemoteException { 118 logger.debug(bean + " setMessageDrivenContext"); 119 ((MessageDrivenBean )bean).setMessageDrivenContext((MessageDrivenContext )ctx); 120 } 121 122 public EJBPoolableObject retrieveBean(Object key) throws Exception { 123 MDBPoolableObject pobj = (MDBPoolableObject) instancePool.retrieveObject(); 124 if(!pobj.isEjbCreated()) { MessageDrivenBean bean = (MessageDrivenBean ) pobj.getBeanInstance(); 126 setContext(bean, ejbConext); 127 ejbCreate(bean, "create", null); 128 pobj.ejbCreated(); 129 } 130 return pobj; 131 } 132 133 public void restoreBean(Object key, EJBPoolableObject pobj) { 134 instancePool.restoreObject(pobj); 135 } 136 137 public EJBObjectId createBean(EJBInvocation invocation) throws Exception { 138 throw new EJBException ("NOT Support in MDB!"); 139 } 140 141 public void syncBean(EJBInvocation invocation) { 142 throw new EJBException ("NOT Support in MDB!"); 143 } 144 145 150 protected void doInit() throws Exception { 151 idgen = EJBObjectIDGenerator.newInstance(bucketMeta.getEJBDescriptor().getEjbName(), 152 bucketMeta.getBeanClass().getName(), 153 bucketMeta.getBeanClass().getName()); 154 155 beanMethods = introspectorBeanMethods(MDBConnection.class,JMSConnectionRemote.class); 156 157 timerService = new EJBTimerService(bucketMeta.getEJBDescriptor().getEjbName()); 158 159 bindReference(); 161 162 if(conn == null) { 164 conn = createConnection(user,password,false); 165 conn.start(); 166 } 167 168 instancePool = (MDBObjectPool) context.getRegistry().getComponentInstance( 170 ComponentName.newInstance(MDBObjectPool.class), 171 new DependencyPack[]{ 172 new DependencyPack("EJBObjectFactory", 173 new ObjectDependency(new MDBObjectFactory(MDBPoolableObject.class, this)))}); 174 session = (MDBSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE); 176 String destinationJndiName = ((MDBDescriptor) bucketMeta.getEJBDescriptor()).getDestination(); 177 Destination dest = (Destination )InitialContextHelper.getInitialContext().lookup(destinationJndiName); 178 JMSConsumer consumer = (JMSConsumer)session.createConsumer(dest); 180 session.setConsumerAsync(consumer,true); 182 183 } 184 185 protected void doDestroy() throws Exception { 186 instancePool.destroy(); 189 } 190 191 public void onMessage(String sessionId, MessageDrivenBean bean, JMSMessage msg) throws RemoteException , JMSException { 192 conn.onMessage(sessionId,bean,msg); 193 } 194 195 protected synchronized MDBConnection createConnection(String userName, String password, boolean isXA) throws JMSException { 196 197 ObjectId clientId = new ObjectId(ObjectUUID.randomUUID()); 198 ConnectorRemote remote = ConnectorHelper.lookupConnector(JNDIProperties.getProviderURL()); 200 201 JMSContainer container = null; 202 203 if(container == null) { 204 container = (JMSContainer) Proxy.newProxyInstance(this.getClass().getClassLoader(), 205 new Class []{JMSContainer.class}, 206 new JMSConnectorInvoker(clientId, remote)); 207 } 208 209 container.auth(userName, password); 210 211 MDBConnection conn = new MDBConnection(clientId.toString(), container, isXA); 212 try { 213 Object proxy = Proxy.newProxyInstance(this.getClass().getClassLoader(), 216 new Class []{JMSConnectionRemote.class}, 217 new MDBConnectorInvoker(nextObjectId(),remote)); 218 container.registerConnection(clientId.toString(), proxy); 219 } 220 catch(Exception e) { 221 e.printStackTrace(); 222 } 223 return conn; 224 } 225 226 public static void main(String [] args) { 227 228 } 229 230 } 231 | Popular Tags |