001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.proxy.ejb.handle;
023:
024: import java.rmi.ServerException;
025: import java.lang.reflect.Method;
026: import java.io.ObjectStreamField;
027: import java.io.ObjectInputStream;
028: import java.io.IOException;
029: import java.io.ObjectOutputStream;
030: import java.util.Hashtable;
031:
032: import javax.naming.InitialContext;
033: import javax.ejb.Handle;
034: import javax.ejb.EJBObject;
035: import javax.ejb.EJBHome;
036:
037: import org.jboss.naming.NamingContextFactory;
038:
039: /**
040: * An EJB stateless session bean handle.
041: *
042: * @author <a href="mailto:marc.fleury@jboss.org">Marc Fleury</a>
043: * @author Scott.Stark@jboss.org
044: * @version $Revision: 57209 $
045: */
046: public class StatelessHandleImpl implements Handle {
047: /** Serial Version Identifier. */
048: static final long serialVersionUID = 3811452873535097661L;
049: private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[] {
050: new ObjectStreamField("jndiName", String.class),
051: new ObjectStreamField("jndiEnv", Hashtable.class) };
052:
053: /** The JNDI name of the home inteface binding */
054: private String jndiName;
055: /** The JNDI env in effect when the home handle was created */
056: private Hashtable jndiEnv;
057:
058: // Constructors --------------------------------------------------
059:
060: /**
061: * Construct a <tt>StatelessHandleImpl</tt>.
062: *
063: * @param jndiName JNDI name.
064: */
065: public StatelessHandleImpl(String jndiName) {
066: this .jndiName = jndiName;
067: this .jndiEnv = (Hashtable) NamingContextFactory.lastInitialContextEnv
068: .get();
069: }
070:
071: // Public --------------------------------------------------------
072:
073: public EJBObject getEJBObject() throws ServerException {
074: try {
075: InitialContext ic = null;
076: if (jndiEnv != null)
077: ic = new InitialContext(jndiEnv);
078: else
079: ic = new InitialContext();
080: EJBHome home = (EJBHome) ic.lookup(jndiName);
081: Class type = home.getClass();
082: Method method = type.getMethod("create", new Class[0]);
083: return (EJBObject) method.invoke(home, new Object[0]);
084: } catch (Exception e) {
085: throw new ServerException("Could not get EJBObject", e);
086: }
087: }
088:
089: /**
090: * @return the jndi name
091: */
092: public String getJNDIName() {
093: return jndiName;
094: }
095:
096: private void readObject(ObjectInputStream ois) throws IOException,
097: ClassNotFoundException {
098: ObjectInputStream.GetField getField = ois.readFields();
099: jndiName = (String) getField.get("jndiName", null);
100: jndiEnv = (Hashtable) getField.get("jndiEnv", null);
101: }
102:
103: private void writeObject(ObjectOutputStream oos) throws IOException {
104: ObjectOutputStream.PutField putField = oos.putFields();
105: putField.put("jndiName", jndiName);
106: putField.put("jndiEnv", jndiEnv);
107: oos.writeFields();
108: }
109: }
|