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.ObjectInputStream;
021    import java.io.ObjectOutputStream;
022    import java.io.Serializable;
023    import java.rmi.RemoteException;
024    import javax.ejb.EJBObject;
025    import javax.ejb.Handle;
026    import javax.ejb.spi.HandleDelegate;
027    import javax.naming.Context;
028    import javax.naming.InitialContext;
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: 494431 $ $Date: 2007-01-09 07:18:14 -0800 (Tue, 09 Jan 2007) $
044     */
045    public class CORBAHandle implements Handle, Serializable {
046    
047        private static final long serialVersionUID = -3390719015323727224L;
048    
049        // the actual EJBObject instance
050        private EJBObject ejbObject;
051        private Object primaryKey;
052    
053        public CORBAHandle(EJBObject ejb, Object primaryKey) {
054            this.ejbObject = ejb;
055            this.primaryKey = primaryKey;
056        }
057    
058        public EJBObject getEJBObject() throws RemoteException {
059            return ejbObject;
060        }
061    
062        public Object getPrimaryKey() {
063            return primaryKey;
064        }
065    
066        private void writeObject(ObjectOutputStream out) throws IOException {
067            HandleDelegate handleDelegate = getHandleDelegate();
068            handleDelegate.writeEJBObject(getEJBObject(), out);
069            out.writeObject(primaryKey);
070        }
071    
072        private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
073            HandleDelegate handleDelegate = getHandleDelegate();
074            ejbObject = handleDelegate.readEJBObject(in);
075            primaryKey = in.readObject();
076        }
077    
078        private static ORB getOrb() {
079            try {
080                Context context = new InitialContext();
081                ORB orb = (ORB) context.lookup("java:comp/ORB");
082                return orb;
083            } catch (Throwable e) {
084                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);
085            }
086        }
087    
088        private static HandleDelegate getHandleDelegate() {
089            try {
090                Context context = new InitialContext();
091                HandleDelegate handleDelegate = (HandleDelegate) context.lookup("java:comp/HandleDelegate");
092                return handleDelegate;
093            } catch (Throwable e) {
094                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);
095            }
096        }
097    }