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     */
017    package org.apache.geronimo.corba;
018    
019    import java.io.IOException;
020    import java.io.ObjectOutputStream;
021    import java.io.Serializable;
022    import java.rmi.RemoteException;
023    import javax.ejb.EJBHome;
024    import javax.ejb.HomeHandle;
025    import javax.ejb.spi.HandleDelegate;
026    import javax.naming.Context;
027    import javax.naming.InitialContext;
028    import javax.rmi.PortableRemoteObject;
029    
030    import org.omg.CORBA.ORB;
031    
032    
033    /**
034     * EJB v2.1 spec, section 19.5.5.1
035     * <p/>
036     * The <code>javax.ejb.spi.HandleDelegate</code> service provider interface
037     * defines methods that enable portable implementations of <code>Handle</code>
038     * and <code>HomeHandle</code> that are instantiated in a different vendor’s
039     * container to serialize and deserialize EJBObject and EJBHome references.
040     * The <code>HandleDelegate</code> interface is not used by enterprise beans
041     * or J2EE application components directly.
042     *
043     * @version $Revision: 474745 $ $Date: 2006-11-14 03:30:38 -0800 (Tue, 14 Nov 2006) $
044     */
045    public class CORBAHomeHandle implements HomeHandle, Serializable {
046    
047        private static final long serialVersionUID = -5537884768260058215L;
048    
049        private String ior;
050    
051        public CORBAHomeHandle(String ior) {
052            this.ior = ior;
053        }
054    
055        public EJBHome getEJBHome() throws RemoteException {
056    
057            try {
058                return (EJBHome) PortableRemoteObject.narrow(getOrb().string_to_object(ior), EJBHome.class);
059            } catch (Exception e) {
060                throw new RemoteException("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) throws IOException, ClassNotFoundException {
071            HandleDelegate handleDelegate = getHandleDelegate();
072            EJBHome home = handleDelegate.readEJBHome(in);
073    
074            try {
075                ior = getOrb().object_to_string((org.omg.CORBA.Object) home);
076            } catch (Exception e) {
077                throw new RemoteException("Unable to convert object to IOR", e);
078            }
079        }
080    
081        private static ORB getOrb() {
082            try {
083                Context context = new InitialContext();
084                ORB orb = (ORB) context.lookup("java:comp/ORB");
085                return orb;
086            } catch (Throwable e) {
087                throw (org.omg.CORBA.MARSHAL)new org.omg.CORBA.MARSHAL("Could not find ORB in jndi at java:comp/ORB", 0, org.omg.CORBA.CompletionStatus.COMPLETED_YES).initCause(e);
088            }
089        }
090    
091        private static HandleDelegate getHandleDelegate() {
092            try {
093                Context context = new InitialContext();
094                HandleDelegate handleDelegate = (HandleDelegate) context.lookup("java:comp/HandleDelegate");
095                return handleDelegate;
096            } catch (Throwable e) {
097                throw (org.omg.CORBA.MARSHAL)new org.omg.CORBA.MARSHAL("Could not find handle delegate in jndi at java:comp/HandleDelegate", 0, org.omg.CORBA.CompletionStatus.COMPLETED_YES).initCause(e);
098            }
099        }
100    }