KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > proxy > ejb > handle > HandleDelegateImpl


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.proxy.ejb.handle;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27
28 import javax.ejb.EJBHome JavaDoc;
29 import javax.ejb.EJBObject JavaDoc;
30 import javax.ejb.spi.HandleDelegate JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.rmi.PortableRemoteObject JavaDoc;
34 import javax.rmi.CORBA.Stub JavaDoc;
35
36 import org.jboss.util.NestedRuntimeException;
37 import org.omg.CORBA.BAD_OPERATION JavaDoc;
38 import org.omg.CORBA.ORB JavaDoc;
39 import org.omg.CORBA.portable.Delegate JavaDoc;
40 import org.omg.CORBA.portable.ObjectImpl JavaDoc;
41
42 /**
43  * <P>Implementation of the javax.ejb.spi.HandleDelegate interface</P>
44  *
45  * <P>The HandleDelegate interface is implemented by the EJB container.
46  * It is used by portable implementations of javax.ejb.Handle and
47  * javax.ejb.HomeHandle. It is not used by EJB components or by client components.
48  * It provides methods to serialize and deserialize EJBObject and EJBHome
49  * references to streams.</P>
50  *
51  * <P>The HandleDelegate object is obtained by JNDI lookup at the reserved name
52  * "java:comp/HandleDelegate".</P>
53  *
54  * @author Dimitris.Andreadis@jboss.org
55  * @author adrian@jboss.com
56  * @version $Revision: 37459 $
57  */

58 public class HandleDelegateImpl
59     implements HandleDelegate JavaDoc
60 {
61    public static HandleDelegate JavaDoc getDelegate()
62    {
63       try
64       {
65          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
66          return (HandleDelegate JavaDoc) ctx.lookup("java:comp/HandleDelegate");
67       }
68       catch (NamingException JavaDoc e)
69       {
70          throw new NestedRuntimeException(e);
71       }
72    }
73
74    // HandleDelegate implementation ---------------------------------
75

76    public void writeEJBObject(EJBObject JavaDoc ejbObject, ObjectOutputStream JavaDoc oostream)
77       throws IOException JavaDoc
78    {
79       oostream.writeObject(ejbObject);
80    }
81    
82    public EJBObject JavaDoc readEJBObject(ObjectInputStream JavaDoc oistream)
83       throws IOException JavaDoc, ClassNotFoundException JavaDoc
84    {
85       Object JavaDoc ejbObject = oistream.readObject();
86       reconnect(ejbObject);
87       return (EJBObject JavaDoc) PortableRemoteObject.narrow(ejbObject, EJBObject JavaDoc.class);
88    }
89
90    public void writeEJBHome(EJBHome JavaDoc ejbHome, ObjectOutputStream JavaDoc oostream)
91       throws IOException JavaDoc
92    {
93       oostream.writeObject(ejbHome);
94    }
95
96    public EJBHome JavaDoc readEJBHome(ObjectInputStream JavaDoc oistream)
97      throws IOException JavaDoc, ClassNotFoundException JavaDoc
98    {
99       Object JavaDoc ejbHome = oistream.readObject();
100       reconnect(ejbHome);
101       return (EJBHome JavaDoc) PortableRemoteObject.narrow(ejbHome, EJBHome JavaDoc.class);
102    }
103    
104    protected void reconnect(Object JavaDoc object) throws IOException JavaDoc
105    {
106       if (object instanceof ObjectImpl JavaDoc)
107       {
108          try
109          {
110             // Check we are still connected
111
ObjectImpl JavaDoc objectImpl = (ObjectImpl JavaDoc) object;
112             objectImpl._get_delegate();
113          }
114          catch (BAD_OPERATION JavaDoc e)
115          {
116             try
117             {
118                // Reconnect
119
Stub JavaDoc stub = (Stub JavaDoc) object;
120                ORB JavaDoc orb = (ORB JavaDoc) new InitialContext JavaDoc().lookup("java:comp/ORB");
121                stub.connect(orb);
122             }
123             catch (NamingException JavaDoc ne)
124             {
125                throw new IOException JavaDoc("Unable to lookup java:comp/ORB");
126             }
127          }
128       }
129       else
130          throw new IOException JavaDoc("Not an ObjectImpl " + object.getClass().getName());
131    }
132 }
133
Popular Tags