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