KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > naming > JacORBPRODelegate


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JacORBPRODelegate.java,v 1.4 2005/05/10 14:10:14 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas_lib.naming;
26
27 import java.rmi.Remote JavaDoc;
28 import java.rmi.RemoteException JavaDoc;
29
30 import javax.rmi.CORBA.Tie JavaDoc;
31 import javax.rmi.CORBA.Util JavaDoc;
32
33 import org.jacorb.poa.RequestProcessor;
34 import org.omg.PortableServer.Servant JavaDoc;
35
36 import org.objectweb.carol.jndi.ns.JacORBCosNaming;
37 import org.objectweb.carol.jndi.spi.JacORBIIOPContext;
38
39 import org.objectweb.jonas_ejb.container.JHome;
40 import org.objectweb.jonas_ejb.container.JRemote;
41
42 /**
43  * Use EJB context classloader when possible
44  * @author Florent Benoit
45  */

46 public class JacORBPRODelegate extends org.objectweb.carol.rmi.multi.JacORBPRODelegate {
47
48
49     /**
50      * Makes a server object ready to receive remote calls. Note that subclasses
51      * of PortableRemoteObject do not need to call this method, as it is called
52      * by the constructor.
53      * @param obj the server object to export.
54      * @exception RemoteException if export fails.
55      */

56     public void exportObject(Remote JavaDoc obj) throws RemoteException JavaDoc {
57
58         // For JacORB, we need first to unexport object as it is not associated
59
// to an ORB
60
try {
61             unexportObject(obj);
62         } catch (Exception JavaDoc eee) {
63             // Nothing
64
}
65
66         /* Now export it */
67         try {
68             super.exportObject(obj);
69         } catch (Exception JavaDoc ee) {
70             // Nothing
71
}
72
73
74         // Connect all EJB Home and Remote interfaces, and objectweb components (JOTM, JORAM, ...)
75
if (obj instanceof JHome || obj instanceof JRemote || obj.getClass().getName().indexOf("org.objectweb") != -1 || obj.getClass().getName().equals("javax.management.remote.rmi.RMIConnectionImpl")) {
76
77             Tie JavaDoc theTie = Util.getTie(obj);
78
79             // Then connect it to the ORB
80
if (theTie != null) {
81                 theTie.orb(JacORBCosNaming.getOrb());
82             }
83         }
84
85
86
87     }
88
89     /**
90      * Checks to ensure that an object of a remote or abstract interface type
91      * can be cast to a desired type.
92      * @param narrowFrom the object to check.
93      * @param narrowTo the desired type.
94      * @return an object which can be cast to the desired type.
95      * @throws ClassCastException if narrowFrom cannot be cast to narrowTo.
96      */

97     public Object JavaDoc narrow(Object JavaDoc narrowFrom, Class JavaDoc narrowTo) throws ClassCastException JavaDoc {
98         // Save old classloader
99
ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
100
101         try {
102             // Try to get EJB classloader
103
Thread JavaDoc currentThread = Thread.currentThread();
104             if (currentThread != null && (currentThread instanceof RequestProcessor)) {
105                 RequestProcessor rp = (RequestProcessor) currentThread;
106                 if (rp != null) {
107                     Servant JavaDoc servant = rp.getServant();
108                     if (servant != null) {
109                         Tie JavaDoc tie = null;
110                         if (servant instanceof Tie JavaDoc) {
111                             tie = (Tie JavaDoc) servant;
112                             Remote JavaDoc target = tie.getTarget();
113                             if (target instanceof JHome) {
114                                 JHome jHome = (JHome) target;
115                                 ClassLoader JavaDoc cll = jHome.getBf().myClassLoader();
116                                 Thread.currentThread().setContextClassLoader(cll);
117                             } else if (target instanceof JRemote) {
118                                 JRemote jRemote = (JRemote) target;
119                                 ClassLoader JavaDoc cll = jRemote.getBf().myClassLoader();
120                                 Thread.currentThread().setContextClassLoader(cll);
121                             }
122                         }
123                     }
124                 }
125             }
126         } catch (Exception JavaDoc e) {
127             // nothing
128
} catch (Error JavaDoc e) {
129             // nothing
130
}
131
132         // Call super method with right classloader
133
Object JavaDoc narrowResult = null;
134         try {
135             narrowResult = super.narrow(narrowFrom, narrowTo);
136         } finally {
137             // reset to old classloader
138
Thread.currentThread().setContextClassLoader(old);
139         }
140
141         return narrowResult;
142     }
143
144 }
Popular Tags