001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JHomeHandleIIOP.java 6072 2005-01-12 14:40:21Z pelletib $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.svc;
025:
026: import java.io.IOException;
027: import java.io.Serializable;
028: import java.rmi.RemoteException;
029:
030: import javax.ejb.EJBHome;
031: import javax.ejb.HomeHandle;
032: import javax.ejb.spi.HandleDelegate;
033: import java.util.logging.Level;
034: import java.util.logging.Logger;
035:
036: import javax.naming.NamingException;
037: import javax.rmi.PortableRemoteObject;
038: import javax.rmi.CORBA.Util;
039:
040: import org.omg.PortableServer.Servant;
041:
042: /**
043: * This class implements javax.ejb.HomeHandle interface. A handle is an
044: * abstraction of a network reference to a home object. A handle is intended to
045: * be used as a "robust" persistent reference to a home object.
046: * @author Philippe Durieux, Philippe Coq
047: */
048: public class JHomeHandleIIOP implements HomeHandle, Serializable {
049:
050: /**
051: * JDK logger to be portable
052: */
053: private static Logger logger = Logger
054: .getLogger("org.objectweb.jonas_ejb.svc");
055:
056: /**
057: * CORBA ref
058: * @serial
059: */
060: private String ior = null;
061:
062: /**
063: * Classloader of EJB
064: */
065: private ClassLoader cl = null;
066:
067: /**
068: * EjbHome class
069: */
070: private EJBHome ejbHome = null;
071:
072: /**
073: * constructor
074: * @param h EJBHome
075: * @param cl classloader used for EJB
076: */
077: public JHomeHandleIIOP(EJBHome h, ClassLoader cl) {
078: logger.log(Level.FINE, "h=" + h);
079: try {
080: Servant servant = (Servant) Util.getTie(h);
081: org.omg.CORBA.Object o = servant._this _object();
082: this .ior = Utility.getORB().object_to_string(o);
083: this .cl = cl;
084: logger.log(Level.FINE, "ior=" + ior);
085: } catch (Exception e) {
086: logger.log(Level.SEVERE, "cannot get Handle: ", e);
087: }
088: }
089:
090: // -----------------------------------------------------------------------
091: // HomeHandle implementation
092: // -----------------------------------------------------------------------
093:
094: /**
095: * Obtains the home object represented by this handle.
096: * @throws RemoteException The home object could not be obtained because of
097: * a system-level failure.
098: * @return The EJBHome object
099: */
100: public EJBHome getEJBHome() throws RemoteException {
101: logger.log(Level.FINE, "");
102: ClassLoader old = Thread.currentThread()
103: .getContextClassLoader();
104: logger.log(Level.FINE, "");
105: if (ejbHome == null) {
106: try {
107: Thread.currentThread().setContextClassLoader(cl);
108: ejbHome = (EJBHome) PortableRemoteObject.narrow(Utility
109: .getORB().string_to_object(ior), EJBHome.class);
110: } catch (NamingException e) {
111: throw new RemoteException("getEJBHome:" + e);
112: } finally {
113: // reset
114: Thread.currentThread().setContextClassLoader(old);
115: }
116: }
117: return ejbHome;
118: }
119:
120: /**
121: * Specific implementation of serialization.
122: * Must call HandleDelegate.writeEJBObject, as specified in 19.5.5.1 of spec EJB 2.1
123: * @param out The output stream used to write object
124: * @throws IOException error when writing object.
125: */
126: private void writeObject(java.io.ObjectOutputStream out)
127: throws IOException {
128: logger.log(Level.FINE, "");
129: HandleDelegate hdld;
130: try {
131: hdld = Utility.getHandleDelegate();
132: } catch (NamingException e) {
133: throw new IOException("Cannot get HandleDelegate");
134: }
135: hdld.writeEJBHome(getEJBHome(), out);
136: }
137:
138: /**
139: * Specific implementation of deserialization.
140: * Must call HandleDelegate.readEJBObject, as specified in 19.5.5.1 of spec EJB 2.1
141: * @param in The input Stream from where is read the object.
142: * @throws IOException error when reading object.
143: * @throws ClassNotFoundException -
144: */
145: private void readObject(java.io.ObjectInputStream in)
146: throws IOException, ClassNotFoundException {
147: logger.log(Level.FINE, "");
148: HandleDelegate hdld;
149: try {
150: hdld = Utility.getHandleDelegate();
151: } catch (NamingException e) {
152: throw new IOException("Cannot get HandleDelegate");
153: }
154: try {
155: ejbHome = hdld.readEJBHome(in);
156: } catch (ClassNotFoundException e) {
157: logger.log(Level.SEVERE, "Cant read EJBHome:" + e);
158: throw e;
159: } catch (IOException e) {
160: logger.log(Level.SEVERE, "Cant read EJBHome:" + e);
161: throw e;
162: }
163: }
164:
165: }
|