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.openejb.client;
017:
018: import java.io.IOException;
019: import java.io.ObjectInput;
020: import java.io.ObjectOutput;
021: import java.rmi.RemoteException;
022:
023: import javax.ejb.EJBObject;
024:
025: public class EJBObjectHandle implements java.io.Externalizable,
026: javax.ejb.Handle {
027:
028: protected transient EJBObjectProxy ejbObjectProxy;
029: protected transient EJBObjectHandler handler;
030:
031: public EJBObjectHandle() {
032: }
033:
034: public EJBObjectHandle(EJBObjectProxy proxy) {
035: this .ejbObjectProxy = proxy;
036: this .handler = ejbObjectProxy.getEJBObjectHandler();
037: }
038:
039: protected void setEJBObjectProxy(EJBObjectProxy ejbObjectProxy) {
040: this .ejbObjectProxy = ejbObjectProxy;
041: this .handler = ejbObjectProxy.getEJBObjectHandler();
042: }
043:
044: public EJBObject getEJBObject() throws RemoteException {
045: return (EJBObject) ejbObjectProxy;
046: }
047:
048: public void writeExternal(ObjectOutput out) throws IOException {
049: // write out the version of the serialized data for future use
050: out.writeByte(1);
051:
052: handler.client.writeExternal(out);
053:
054: EJBMetaDataImpl ejb = handler.ejb;
055: out.writeObject(getClassName(ejb.homeClass));
056: out.writeObject(getClassName(ejb.remoteClass));
057: out.writeObject(getClassName(ejb.keyClass));
058: out.writeByte(ejb.type);
059: out.writeUTF(ejb.deploymentID);
060: out.writeShort(ejb.deploymentCode);
061: handler.server.writeExternal(out);
062: out.writeObject(handler.primaryKey);
063: }
064:
065: public void readExternal(ObjectInput in) throws IOException,
066: ClassNotFoundException {
067: byte version = in.readByte(); // future use
068:
069: ClientMetaData client = new ClientMetaData();
070: EJBMetaDataImpl ejb = new EJBMetaDataImpl();
071: ServerMetaData server = new ServerMetaData();
072:
073: client.readExternal(in);
074:
075: ClassLoader classLoader = Thread.currentThread()
076: .getContextClassLoader();
077: if (classLoader == null) {
078: classLoader = this .getClass().getClassLoader();
079: }
080:
081: ejb.homeClass = loadClass(classLoader, (String) in.readObject());
082: ejb.remoteClass = loadClass(classLoader, (String) in
083: .readObject());
084: ejb.keyClass = loadClass(classLoader, (String) in.readObject());
085: ejb.type = in.readByte();
086: ejb.deploymentID = in.readUTF();
087: ejb.deploymentCode = in.readShort();
088:
089: server.readExternal(in);
090: Object primaryKey = in.readObject();
091:
092: handler = EJBObjectHandler.createEJBObjectHandler(ejb, server,
093: client, primaryKey);
094: ejbObjectProxy = handler.createEJBObjectProxy();
095: }
096:
097: private static String getClassName(Class clazz) {
098: return (clazz == null) ? null : clazz.getName();
099: }
100:
101: private static Class loadClass(ClassLoader classLoader,
102: String homeClassName) throws ClassNotFoundException {
103: return (homeClassName == null) ? null : Class.forName(
104: homeClassName, true, classLoader);
105: }
106: }
|