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.io.IOException;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027:
028: import javax.ejb.EJBHome;
029: import javax.ejb.EJBObject;
030: import javax.ejb.spi.HandleDelegate;
031: import javax.naming.InitialContext;
032: import javax.naming.NamingException;
033: import javax.rmi.PortableRemoteObject;
034: import javax.rmi.CORBA.Stub;
035:
036: import org.jboss.util.NestedRuntimeException;
037: import org.omg.CORBA.BAD_OPERATION;
038: import org.omg.CORBA.ORB;
039: import org.omg.CORBA.portable.ObjectImpl;
040:
041: /**
042: * <P>Implementation of the javax.ejb.spi.HandleDelegate interface</P>
043: *
044: * <P>The HandleDelegate interface is implemented by the EJB container.
045: * It is used by portable implementations of javax.ejb.Handle and
046: * javax.ejb.HomeHandle. It is not used by EJB components or by client components.
047: * It provides methods to serialize and deserialize EJBObject and EJBHome
048: * references to streams.</P>
049: *
050: * <P>The HandleDelegate object is obtained by JNDI lookup at the reserved name
051: * "java:comp/HandleDelegate".</P>
052: *
053: * @author Dimitris.Andreadis@jboss.org
054: * @author adrian@jboss.com
055: * @version $Revision: 57209 $
056: */
057: public class HandleDelegateImpl implements HandleDelegate {
058: public static HandleDelegate getDelegate() {
059: try {
060: InitialContext ctx = new InitialContext();
061: return (HandleDelegate) ctx
062: .lookup("java:comp/HandleDelegate");
063: } catch (NamingException e) {
064: throw new NestedRuntimeException(e);
065: }
066: }
067:
068: // HandleDelegate implementation ---------------------------------
069:
070: public void writeEJBObject(EJBObject ejbObject,
071: ObjectOutputStream oostream) throws IOException {
072: oostream.writeObject(ejbObject);
073: }
074:
075: public EJBObject readEJBObject(ObjectInputStream oistream)
076: throws IOException, ClassNotFoundException {
077: Object ejbObject = oistream.readObject();
078: reconnect(ejbObject);
079: return (EJBObject) PortableRemoteObject.narrow(ejbObject,
080: EJBObject.class);
081: }
082:
083: public void writeEJBHome(EJBHome ejbHome,
084: ObjectOutputStream oostream) throws IOException {
085: oostream.writeObject(ejbHome);
086: }
087:
088: public EJBHome readEJBHome(ObjectInputStream oistream)
089: throws IOException, ClassNotFoundException {
090: Object ejbHome = oistream.readObject();
091: reconnect(ejbHome);
092: return (EJBHome) PortableRemoteObject.narrow(ejbHome,
093: EJBHome.class);
094: }
095:
096: protected void reconnect(Object object) throws IOException {
097: if (object instanceof ObjectImpl) {
098: try {
099: // Check we are still connected
100: ObjectImpl objectImpl = (ObjectImpl) object;
101: objectImpl._get_delegate();
102: } catch (BAD_OPERATION e) {
103: try {
104: // Reconnect
105: Stub stub = (Stub) object;
106: ORB orb = (ORB) new InitialContext()
107: .lookup("java:comp/ORB");
108: stub.connect(orb);
109: } catch (NamingException ne) {
110: throw new IOException(
111: "Unable to lookup java:comp/ORB");
112: }
113: }
114: } else
115: throw new IOException("Not an ObjectImpl "
116: + object.getClass().getName());
117: }
118: }
|