001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.corba;
017:
018: import java.io.IOException;
019: import java.io.ObjectOutputStream;
020: import java.io.Serializable;
021: import java.rmi.RemoteException;
022: import javax.ejb.EJBHome;
023: import javax.ejb.HomeHandle;
024: import javax.ejb.spi.HandleDelegate;
025: import javax.naming.Context;
026: import javax.naming.InitialContext;
027: import javax.rmi.PortableRemoteObject;
028:
029: import org.omg.CORBA.ORB;
030:
031: /**
032: * EJB v2.1 spec, section 19.5.5.1
033: * <p/>
034: * The <code>javax.ejb.spi.HandleDelegate</code> service provider interface
035: * defines methods that enable portable implementations of <code>Handle</code>
036: * and <code>HomeHandle</code> that are instantiated in a different vendor’s
037: * container to serialize and deserialize EJBObject and EJBHome references.
038: * The <code>HandleDelegate</code> interface is not used by enterprise beans
039: * or J2EE application components directly.
040: *
041: * @version $Revision: 474745 $ $Date: 2006-11-14 03:30:38 -0800 (Tue, 14 Nov 2006) $
042: */
043: public class CORBAHomeHandle implements HomeHandle, Serializable {
044:
045: private static final long serialVersionUID = -5537884768260058215L;
046:
047: private String ior;
048:
049: public CORBAHomeHandle(String ior) {
050: this .ior = ior;
051: }
052:
053: public EJBHome getEJBHome() throws RemoteException {
054:
055: try {
056: return (EJBHome) PortableRemoteObject.narrow(getOrb()
057: .string_to_object(ior), EJBHome.class);
058: } catch (Exception e) {
059: throw new RemoteException(
060: "Unable to convert IOR into home", e);
061: }
062:
063: }
064:
065: private void writeObject(ObjectOutputStream out) throws IOException {
066: HandleDelegate handleDelegate = getHandleDelegate();
067: handleDelegate.writeEJBHome(getEJBHome(), out);
068: }
069:
070: private void readObject(java.io.ObjectInputStream in)
071: throws IOException, ClassNotFoundException {
072: HandleDelegate handleDelegate = getHandleDelegate();
073: EJBHome home = handleDelegate.readEJBHome(in);
074:
075: try {
076: ior = getOrb()
077: .object_to_string((org.omg.CORBA.Object) home);
078: } catch (Exception e) {
079: throw new RemoteException(
080: "Unable to convert object to IOR", e);
081: }
082: }
083:
084: private static ORB getOrb() {
085: try {
086: Context context = new InitialContext();
087: ORB orb = (ORB) context.lookup("java:comp/ORB");
088: return orb;
089: } catch (Throwable e) {
090: throw (org.omg.CORBA.MARSHAL) new org.omg.CORBA.MARSHAL(
091: "Could not find ORB in jndi at java:comp/ORB", 0,
092: org.omg.CORBA.CompletionStatus.COMPLETED_YES)
093: .initCause(e);
094: }
095: }
096:
097: private static HandleDelegate getHandleDelegate() {
098: try {
099: Context context = new InitialContext();
100: HandleDelegate handleDelegate = (HandleDelegate) context
101: .lookup("java:comp/HandleDelegate");
102: return handleDelegate;
103: } catch (Throwable e) {
104: throw (org.omg.CORBA.MARSHAL) new org.omg.CORBA.MARSHAL(
105: "Could not find handle delegate in jndi at java:comp/HandleDelegate",
106: 0, org.omg.CORBA.CompletionStatus.COMPLETED_YES)
107: .initCause(e);
108: }
109: }
110: }
|