KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > iiop > IIOPHandleDelegate


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.iiop;
25
26 import java.io.*;
27
28 import javax.ejb.*;
29 import javax.ejb.spi.HandleDelegate JavaDoc;
30 import javax.naming.*;
31
32 import javax.rmi.PortableRemoteObject JavaDoc;
33 import javax.rmi.CORBA.Stub JavaDoc;
34
35 import com.sun.enterprise.util.ORBManager;
36 import org.omg.CORBA.portable.Delegate JavaDoc;
37 import com.sun.corba.ee.spi.presentation.rmi.DynamicStub;
38 import com.sun.corba.ee.spi.presentation.rmi.StubAdapter;
39
40 /**
41  * An implementation of HandleDelegate for the IIOP Protocol.
42  *
43  */

44
45 public final class IIOPHandleDelegate
46     implements HandleDelegate JavaDoc
47 {
48
49     public static HandleDelegate JavaDoc getHandleDelegate() {
50         HandleDelegate JavaDoc handleDelegate =
51             (HandleDelegate JavaDoc) java.security.AccessController.doPrivileged(
52                 new java.security.PrivilegedAction JavaDoc() {
53                     public Object JavaDoc run() {
54                         try {
55                             ClassLoader JavaDoc cl = new HandleDelegateClassLoader();
56                             Class JavaDoc c = cl.loadClass(
57                                 "com.sun.enterprise.iiop.IIOPHandleDelegate");
58                             return c.newInstance();
59                         } catch ( Exception JavaDoc ex ) {
60                             ex.printStackTrace();
61                             return null;
62                         }
63                     }
64                 }
65             );
66         return handleDelegate;
67     }
68     
69     
70     public void writeEJBObject(javax.ejb.EJBObject JavaDoc ejbObject,
71             java.io.ObjectOutputStream JavaDoc ostream)
72         throws java.io.IOException JavaDoc
73     {
74         ostream.writeObject(ejbObject); // IIOP stubs are Serializable
75
}
76     
77     public javax.ejb.EJBObject JavaDoc readEJBObject(java.io.ObjectInputStream JavaDoc istream)
78         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
79     {
80         return (EJBObject)getStub(istream, EJBObject.class);
81     }
82     
83     public void writeEJBHome(javax.ejb.EJBHome JavaDoc ejbHome,
84             java.io.ObjectOutputStream JavaDoc ostream)
85         throws java.io.IOException JavaDoc
86     {
87         ostream.writeObject(ejbHome); // IIOP stubs are Serializable
88
}
89     
90     public javax.ejb.EJBHome JavaDoc readEJBHome(java.io.ObjectInputStream JavaDoc istream)
91         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
92     {
93         return (EJBHome)getStub(istream, EJBHome.class);
94     }
95     
96     private Object JavaDoc getStub(java.io.ObjectInputStream JavaDoc istream, Class JavaDoc stubClass)
97         throws IOException, ClassNotFoundException JavaDoc
98     {
99         // deserialize obj
100
Object JavaDoc obj = istream.readObject();
101
102         if( StubAdapter.isStub(obj) ) {
103
104             try {
105
106                 // Check if it is already connected to the ORB by getting
107
// the delegate. If BAD_OPERATION is not thrown, then the
108
// stub is connected. This will happen if istream is an
109
// IIOP input stream.
110
Delegate JavaDoc delegate = StubAdapter.getDelegate(obj);
111
112             } catch(org.omg.CORBA.BAD_OPERATION JavaDoc bo) {
113                 
114
115                 // Stub is not connected. This can happen if istream is
116
// not an IIOP input stream (e.g. it's a File stream).
117
StubAdapter.connect
118                     (obj, (com.sun.corba.ee.spi.orb.ORB) ORBManager.getORB());
119             }
120
121         } else {
122             throw new IOException("Unable to create stub for class " +
123                 stubClass.getName() +
124                 ", object deserialized is not a CORBA object, it's type is " +
125                 obj.getClass().getName());
126         }
127
128         // narrow it
129
Object JavaDoc stub = PortableRemoteObject.narrow(obj, stubClass);
130      
131         return stub;
132     }
133     
134 }
135
Popular Tags