KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.iiop;
24
25 import java.rmi.Remote JavaDoc;
26 import java.rmi.RemoteException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.*;
29 import java.io.File JavaDoc;
30
31 import javax.rmi.PortableRemoteObject JavaDoc;
32 import javax.rmi.CORBA.*;
33 import javax.transaction.*;
34 import javax.ejb.EJBException JavaDoc;
35 import javax.ejb.NoSuchObjectLocalException JavaDoc;
36 import javax.ejb.TransactionRolledbackLocalException JavaDoc;
37 import javax.ejb.TransactionRequiredLocalException JavaDoc;
38 import javax.ejb.ConcurrentAccessException JavaDoc;
39 import javax.naming.*;
40
41 import org.omg.CORBA.*;
42
43 import com.sun.enterprise.*;
44 import com.sun.enterprise.deployment.*;
45 import com.sun.enterprise.util.*;
46 import com.sun.enterprise.log.Log;
47 import com.sun.enterprise.server.*;
48 import com.sun.ejb.*;
49 import com.sun.ejb.containers.InternalEJBContainerException;
50 import com.sun.ejb.containers.ParallelAccessException;
51
52 import com.sun.enterprise.security.SecurityContext;
53
54 import org.omg.CosNaming.NamingContext JavaDoc;
55
56 import com.sun.corba.ee.spi.oa.rfm.ReferenceFactoryManager ;
57 import com.sun.corba.ee.spi.presentation.rmi.PresentationManager;
58 import com.sun.corba.ee.spi.presentation.rmi.DynamicStub;
59 import com.sun.corba.ee.spi.presentation.rmi.StubAdapter;
60 import com.sun.corba.ee.spi.extension.ServantCachingPolicy;
61 import com.sun.corba.ee.spi.ior.*;
62
63 import com.sun.corba.ee.impl.naming.cosnaming.TransientNameService;
64 import com.sun.corba.ee.impl.orbutil.ORBConstants;
65
66 import java.util.logging.*;
67 import com.sun.logging.*;
68
69
70 /**
71  * This class implements the ProtocolManager interface for the
72  * RMI/IIOP ORB with POA (Portable Object Adapter).
73  * Note that the POA is now accessed only through the
74  * ReferenceFactoryManager for EJB.
75  *
76  * @author Vivek Nagar
77  */

78
79 public final class POAProtocolMgr extends org.omg.CORBA.LocalObject JavaDoc
80                  implements ProtocolManager
81 {
82     private static Logger _logger=null;
83     static{
84     _logger=LogDomains.getLogger(LogDomains.CORBA_LOGGER);
85     }
86     private static final int MAPEXCEPTION_CODE = 9998;
87     private static final String JavaDoc InitialObjectsDb = "initial.db";
88
89     private static final String JavaDoc ORG_OMG_STUB_PREFIX = "org.omg.stub.";
90
91     private ORB orb;
92     private Switch theSwitch;
93     private ContainerFactory containerFactory;
94     private PresentationManager presentationMgr;
95
96     public POAProtocolMgr(ORB o)
97     {
98     this.orb = (ORB)o;
99
100     theSwitch = Switch.getSwitch();
101     containerFactory = theSwitch.getContainerFactory();
102         this.presentationMgr =
103             ((com.sun.corba.ee.spi.orb.ORB) orb).getPresentationManager();
104     }
105
106
107     // Called in all VMs, must be called only after InitialNaming is available
108
public void initializePOAs()
109         throws Exception JavaDoc
110     {
111     // NOTE: The RootPOA manager used to activated here.
112
ReferenceFactoryManager rfm =
113         (ReferenceFactoryManager)orb.resolve_initial_references(
114         ORBConstants.REFERENCE_FACTORY_MANAGER ) ;
115     rfm.activate() ;
116     _logger.log(Level.FINE, "POAProtocolMgr.initializePOAs: RFM resolved and activated");
117     }
118
119
120     public ORB getORB()
121     {
122     return orb;
123     }
124
125     private ContainerFactory getContainerFactory()
126     {
127     if ( containerFactory == null )
128         containerFactory = theSwitch.getContainerFactory();
129     return containerFactory;
130     }
131
132     // Called only in J2EE Server VM
133
public void initializeNaming(File JavaDoc dbDir, int orbInitialPort) throws Exception JavaDoc
134     {
135     // NOTE: The TransientNameService reference is NOT HA.
136
TransientNameService nameService = new
137          TransientNameService((com.sun.corba.ee.spi.orb.ORB)orb);
138     _logger.log(Level.FINE, "POAProtocolMgr.initializeNaming: complete");
139     }
140
141
142     /**
143      * Return a factory that can be used to create/destroy remote
144      * references for a particular EJB type.
145      */

146     public RemoteReferenceFactory getRemoteReferenceFactory
147         (Container container, boolean remoteHomeView, String JavaDoc id)
148     {
149     RemoteReferenceFactory factory = new POARemoteReferenceFactory
150             (container, this, orb, remoteHomeView, id);
151                             
152     // XXX store factory in some kind of table ??
153
return factory;
154     }
155
156     /**
157      * Connect the RMI object to the protocol.
158      */

159     public void connectObject(Remote JavaDoc remoteObj)
160     throws RemoteException JavaDoc
161     {
162         StubAdapter.connect
163             (remoteObj, (com.sun.corba.ee.spi.orb.ORB)ORBManager.getORB());
164     }
165
166     /**
167      * Return true if the two object references refer to the same
168      * remote object.
169      */

170     public boolean isIdentical(Remote JavaDoc obj1, Remote JavaDoc obj2)
171     {
172     org.omg.CORBA.Object JavaDoc corbaObj1 = (org.omg.CORBA.Object JavaDoc)obj1;
173     org.omg.CORBA.Object JavaDoc corbaObj2 = (org.omg.CORBA.Object JavaDoc)obj2;
174
175     return corbaObj1._is_equivalent(corbaObj2);
176     }
177
178     public void validateTargetObjectInterfaces(Remote JavaDoc targetObj) {
179         
180         if( targetObj != null ) {
181             // All Remote interfaces implemented by targetObj will be
182
// validated as a side-effect of calling setTarget().
183
// A runtime exception will be propagated if validation fails.
184
Tie tie = presentationMgr.getTie();
185             tie.setTarget(targetObj);
186         } else {
187             throw new IllegalArgumentException JavaDoc
188                 ("null passed to validateTargetObjectInterfaces");
189         }
190
191     }
192
193
194     /**
195      * Map the EJB/RMI exception to a protocol-specific (e.g. CORBA) exception
196      */

197     public Throwable JavaDoc mapException(Throwable JavaDoc exception) {
198
199         boolean mapped = true;
200         Throwable JavaDoc mappedException = null;
201
202         if ( exception instanceof java.rmi.NoSuchObjectException JavaDoc
203             || exception instanceof NoSuchObjectLocalException JavaDoc )
204         {
205             mappedException = new OBJECT_NOT_EXIST(MAPEXCEPTION_CODE,
206                 CompletionStatus.COMPLETED_MAYBE);
207         } else if ( exception instanceof java.rmi.AccessException JavaDoc
208             || exception instanceof javax.ejb.AccessLocalException JavaDoc )
209         {
210             mappedException = new NO_PERMISSION(MAPEXCEPTION_CODE,
211                 CompletionStatus.COMPLETED_MAYBE);
212         } else if ( exception instanceof java.rmi.MarshalException JavaDoc ) {
213             mappedException = new MARSHAL(MAPEXCEPTION_CODE,
214                 CompletionStatus.COMPLETED_MAYBE);
215         } else if ( exception instanceof javax.transaction.TransactionRolledbackException JavaDoc
216             || exception instanceof TransactionRolledbackLocalException JavaDoc )
217         {
218             mappedException = new TRANSACTION_ROLLEDBACK(MAPEXCEPTION_CODE,
219                 CompletionStatus.COMPLETED_MAYBE);
220         } else if ( exception instanceof javax.transaction.TransactionRequiredException JavaDoc
221             || exception instanceof TransactionRequiredLocalException JavaDoc )
222         {
223             mappedException = new TRANSACTION_REQUIRED(MAPEXCEPTION_CODE,
224                 CompletionStatus.COMPLETED_MAYBE);
225         } else if ( exception instanceof javax.transaction.InvalidTransactionException JavaDoc ) {
226             mappedException = new INVALID_TRANSACTION(MAPEXCEPTION_CODE,
227                 CompletionStatus.COMPLETED_MAYBE);
228         } else if (exception instanceof ConcurrentAccessException JavaDoc) {
229             ConcurrentAccessException JavaDoc ex = (ConcurrentAccessException JavaDoc) exception;
230             exception = new ParallelAccessException(ex.getMessage(), ex);
231             mapped = false;
232         } else if ( exception instanceof EJBException JavaDoc ) {
233             EJBException JavaDoc ex = (EJBException JavaDoc) exception;
234             exception = new RemoteException JavaDoc(ex.getMessage(),
235                                 ex.getCausedByException());
236             mapped = false;
237         } else {
238             mapped = false;
239         }
240         
241         return (mapped)
242             ? mappedException.initCause(exception)
243             : exception;
244     }
245
246
247     /**
248      * Called from SecurityMechanismSelector for each objref creation
249      */

250     public EjbDescriptor getEjbDescriptor(byte[] ejbKey)
251     {
252     EjbDescriptor result = null;
253     try {
254         if(_logger.isLoggable(Level.FINE)) {
255         _logger.log(Level.FINE, "POAProtocolMgr.getEjbDescriptor->: "
256                 + ejbKey);
257         }
258
259         if ( ejbKey.length < POARemoteReferenceFactory.EJBID_OFFSET + 8 ) {
260         if(_logger.isLoggable(Level.FINE)) {
261             _logger.log(Level.FINE, "POAProtocolMgr.getEjbDescriptor: "
262                 + ejbKey
263                 + ": " + ejbKey.length + " < "
264                 + POARemoteReferenceFactory.EJBID_OFFSET + 8);
265         }
266         return null;
267         }
268
269         long ejbId =
270         Utility.bytesToLong(ejbKey,
271                     POARemoteReferenceFactory.EJBID_OFFSET);
272
273         if(_logger.isLoggable(Level.FINE)) {
274         _logger.log(Level.FINE, "POAProtocolMgr.getEjbDescriptor: "
275                 + ejbKey + ": ejbId: " + ejbId);
276         }
277
278         if ( getContainerFactory() != null ) {
279         result = containerFactory.getEjbDescriptor(ejbId);
280         } else {
281         if(_logger.isLoggable(Level.FINE)) {
282             _logger.log(Level.FINE, "POAProtocolMgr.getEjbDescriptor: "
283                 + ejbKey + ": no container factory");
284         }
285         }
286
287         return result;
288     } finally {
289         if(_logger.isLoggable(Level.FINE)) {
290         _logger.log(Level.FINE, "POAProtocolMgr.getEjbDescriptor<-: "
291                 + ejbKey + ": " + result);
292         }
293     }
294     }
295 }
296
Popular Tags