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.Serializable;
020    import java.io.ObjectStreamException;
021    import java.io.InvalidObjectException;
022    import javax.ejb.EJBObject;
023    import javax.ejb.EJBHome;
024    import javax.rmi.PortableRemoteObject;
025    import javax.naming.Context;
026    import javax.naming.InitialContext;
027    
028    import org.omg.CORBA.ORB;
029    
030    /**
031     * @version $Revision: 474745 $ $Date: 2006-11-14 03:30:38 -0800 (Tue, 14 Nov 2006) $
032     */
033    public class CORBAEJBMemento implements Serializable {
034        private final String ior;
035        private final boolean home;
036    
037        public CORBAEJBMemento(String ior, boolean home) {
038            this.ior = ior;
039            this.home = home;
040        }
041    
042        private Object readResolve() throws ObjectStreamException {
043            Class type;
044            if (home) {
045                type = EJBHome.class;
046            } else {
047                type = EJBObject.class;
048            }
049    
050            try {
051                return PortableRemoteObject.narrow(getOrb().string_to_object(ior), type);
052            } catch (Exception e) {
053                throw (InvalidObjectException) new InvalidObjectException("Unable to convert IOR into object").initCause(e);
054            }
055        }
056    
057        private static ORB getOrb() {
058            try {
059                Context context = new InitialContext();
060                ORB orb = (ORB) context.lookup("java:comp/ORB");
061                return orb;
062            } catch (Throwable e) {
063                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);
064            }
065        }
066    }