KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > rmi > PortableRemoteObjectDelegateImpl


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) 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  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.orb.rmi;
22
23 /**
24  * @author Gerald Brose
25  * @version $Id: PortableRemoteObjectDelegateImpl.java,v 1.1 2004/06/21 21:25:48 david.robison Exp $
26  */

27
28 import javax.rmi.CORBA.Tie JavaDoc;
29 import javax.rmi.CORBA.Stub JavaDoc;
30 import javax.rmi.CORBA.Util JavaDoc;
31
32 import org.omg.CORBA.ORB JavaDoc;
33
34 public class PortableRemoteObjectDelegateImpl implements javax.rmi.CORBA.PortableRemoteObjectDelegate JavaDoc
35 {
36     private static ORB JavaDoc _orb = null;
37     
38     /**
39      * Return the ORB to be used for RMI communications.
40      * @return The ORB
41      */

42     public static synchronized ORB JavaDoc getORB()
43     {
44         if ( _orb == null )
45         {
46             System.out.println("Unknwon ORB");
47             _orb = ORB.init( new String JavaDoc[0], null );
48         }
49         return _orb;
50     }
51     
52     /**
53      * Set the ORB to be used for RMI communications.
54      * @param orb The ORB to use
55      */

56     public static synchronized void setORB( ORB JavaDoc orb )
57     {
58         if ( _orb != null )
59         {
60             throw new IllegalStateException JavaDoc( "RMI orb has already been initialized" );
61         }
62         _orb = orb;
63     }
64
65     /**
66      * Export an RMI object as a CORBA object
67      * @see javax.rmi.CORBA.PortableRemoteObjectDelegate#exportObject(java.rmi.Remote)
68      */

69     public void exportObject( java.rmi.Remote JavaDoc obj ) throws java.rmi.RemoteException JavaDoc
70     {
71         if (obj == null) throw new NullPointerException JavaDoc();
72         if ( obj instanceof Stub JavaDoc )
73         {
74             throw new java.rmi.server.ExportException JavaDoc( "Attempted to export a stub class" );
75         }
76         Tie JavaDoc tie = Util.getTie( obj );
77         if ( tie != null )
78         {
79             throw new java.rmi.server.ExportException JavaDoc( "Object already exported" );
80         }
81         tie = toTie( obj );
82         tie.orb( getORB() );
83         Util.registerTarget( tie, obj );
84     }
85
86     /**
87      * Return the Stub for a RMI object.
88      * @param obj The RMI object
89      * @return The Stub object
90      * @see javax.rmi.CORBA.PortableRemoteObjectDelegate#toStub(java.rmi.Remote)
91      */

92     public java.rmi.Remote JavaDoc toStub( java.rmi.Remote JavaDoc obj ) throws java.rmi.NoSuchObjectException JavaDoc
93     {
94         if ( obj instanceof Stub JavaDoc )
95         {
96             return obj;
97         }
98         
99         Tie JavaDoc tie = null;
100         if ( obj instanceof Tie JavaDoc )
101         {
102             tie = ( Tie JavaDoc ) obj;
103             obj = tie.getTarget();
104         }
105         else
106         {
107             tie = Util.getTie( obj );
108         }
109         if ( tie == null )
110         {
111             throw new java.rmi.NoSuchObjectException JavaDoc( "Object not exported" );
112         }
113         
114         org.omg.CORBA.Object JavaDoc thisObject = tie.thisObject();
115         if ( thisObject instanceof java.rmi.Remote JavaDoc )
116         {
117             return ( java.rmi.Remote JavaDoc ) thisObject;
118         }
119         throw new java.rmi.NoSuchObjectException JavaDoc( "Object not exported" );
120     }
121     
122     /**
123      * Deactivate the exported RMI object.
124      * @param obj The RMI object
125      * @see javax.rmi.CORBA.PortableRemoteObjectDelegate#unexportObject(java.rmi.Remote)
126      */

127     public void unexportObject( java.rmi.Remote JavaDoc obj ) throws java.rmi.NoSuchObjectException JavaDoc
128     {
129         Tie JavaDoc tie = Util.getTie( obj );
130         if ( tie == null )
131         {
132             throw new java.rmi.NoSuchObjectException JavaDoc( "Object not exported" );
133         }
134         Util.unexportObject( obj );
135     }
136     
137     /**
138      * Narrow the remote object.
139      * @param obj The remote object
140      * @param newClass The class to narrow to
141      * @return the narrowed object
142      * @see javax.rmi.CORBA.PortableRemoteObjectDelegate#narrow(java.lang.Object, java.lang.Class)
143      */

144     public Object JavaDoc narrow( Object JavaDoc obj, Class JavaDoc newClass ) throws ClassCastException JavaDoc
145     {
146         if (newClass == null)
147             throw new ClassCastException JavaDoc("Can't narrow to null class");
148         if (obj == null)
149             return null;
150         
151         Class JavaDoc fromClass = obj.getClass();
152         Object JavaDoc result = null;
153         
154         try
155         {
156             if (newClass.isAssignableFrom(fromClass))
157                 result = obj;
158             else
159             {
160                 Class JavaDoc[] cs = fromClass.getInterfaces();
161                 Exception JavaDoc e1 = new Exception JavaDoc();
162                 try
163                 {
164                     throw e1;
165                 }
166                 catch(Exception JavaDoc ee)
167                 {
168                     ee.printStackTrace();
169                 }
170                 System.exit(2);
171             }
172         }
173         catch(Exception JavaDoc e)
174         {
175             result = null;
176         }
177         
178         if (result == null)
179             throw new ClassCastException JavaDoc("Can't narrow from " + fromClass + " to " + newClass);
180         
181         return result;
182     }
183     
184     /**
185      * @see javax.rmi.CORBA.PortableRemoteObjectDelegate#connect(java.rmi.Remote, java.rmi.Remote)
186      */

187     public void connect( java.rmi.Remote JavaDoc target, java.rmi.Remote JavaDoc source ) throws java.rmi.RemoteException JavaDoc
188     {
189         throw new Error JavaDoc("Not implemented for PortableRemoteObjectDelegateImpl");
190     }
191     
192     /**
193      * Return the Tie object for an RMI object.
194      * @param obj The RMI object.
195      * @return The Tie object
196      * @throws java.rmi.server.ExportException
197      */

198     static Tie JavaDoc toTie( java.rmi.Remote JavaDoc obj ) throws java.rmi.server.ExportException JavaDoc
199     {
200         for (Class JavaDoc clz = obj.getClass(); clz != null; clz = clz.getSuperclass()) {
201             try
202             {
203                 String JavaDoc clzName = clz.getName();
204                 String JavaDoc[] clzParts = clzName.split("\\.");
205                 clzParts[clzParts.length - 1] = "_" + clzParts[clzParts.length - 1] + "_Tie";
206                 StringBuffer JavaDoc tieClzName = new StringBuffer JavaDoc("org.omg.stub");
207                 for (int i = 0; i < clzParts.length; i++) tieClzName.append("." + clzParts[i]);
208                 Class JavaDoc tieClass = Util.loadClass(tieClzName.toString(), Util.getCodebase( clz ), clz.getClassLoader() );
209                 return ( javax.rmi.CORBA.Tie JavaDoc ) tieClass.newInstance();
210             }
211             catch ( ClassNotFoundException JavaDoc ex )
212             {
213                 //throw new java.rmi.server.ExportException("ClassNotFoundException: " + e, e );
214
}
215             catch (InstantiationException JavaDoc e)
216             {
217                 throw new java.rmi.server.ExportException JavaDoc("InstantiationException: " + e, e );
218             }
219             catch (IllegalAccessException JavaDoc e)
220             {
221                 throw new java.rmi.server.ExportException JavaDoc("IllegalAccessException: " + e, e );
222             }
223         }
224         throw new java.rmi.server.ExportException JavaDoc("Tie class not found ");
225     }
226 }
227
Popular Tags