001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.iiop.marshal;
031:
032: import java.io.*;
033: import java.lang.reflect.Proxy;
034: import java.util.logging.*;
035:
036: import com.caucho.ejb.*;
037: import com.caucho.iiop.*;
038: import com.caucho.iiop.orb.*;
039:
040: /**
041: * Remote object marshaller
042: */
043: public class RemoteMarshal extends Marshal {
044: private static final Logger log = Logger
045: .getLogger(RemoteMarshal.class.getName());
046: private Class _cl;
047:
048: public RemoteMarshal(Class cl) {
049: _cl = cl;
050: }
051:
052: @Override
053: public void marshal(org.omg.CORBA_2_3.portable.OutputStream os,
054: Object value) {
055: if (value instanceof IiopProxy) {
056: IiopProxyHandler handler = (IiopProxyHandler) Proxy
057: .getInvocationHandler(value);
058:
059: StubImpl stub = handler.getStub();
060:
061: // IOR ior = stub.getIor();
062:
063: os.write_Object(new DummyObjectImpl(stub.getIOR()));
064: } else if (value instanceof AbstractEJBObject) {
065: AbstractEJBObject absObj = (AbstractEJBObject) value;
066:
067: AbstractServer server = absObj.__caucho_getServer();
068: String local = absObj.__caucho_getId();
069:
070: // XXX TCK: ejb30/bb/session/stateless/sessioncontext/annotated/passBusinessObjectRemote1
071: String url = server.getProtocolId(_cl) + "?" + local;
072:
073: String typeName = "RMI:" + _cl.getName() + ":0";
074:
075: ORBImpl orb = (ORBImpl) os.orb();
076:
077: IOR ior = new IOR(typeName, orb.getHost(), orb.getPort(),
078: url);
079: //writer.write_boolean(true);
080: os.write_Object(new DummyObjectImpl(ior));
081: } else {
082: //writer.write_boolean(false);
083: os.write_value((Serializable) value);
084: }
085: }
086:
087: @Override
088: public Object unmarshal(org.omg.CORBA_2_3.portable.InputStream is) {
089: Object value = is.read_Object(_cl);
090:
091: if (value instanceof StubImpl) {
092: Class type = _cl;
093: StubImpl stub = (StubImpl) value;
094:
095: IOR ior = stub.getIOR();
096: String typeId = ior.getTypeId();
097:
098: if (typeId.startsWith("RMI:")) {
099: int p = typeId.lastIndexOf(':');
100: String typeName = typeId.substring(4, p);
101:
102: try {
103: ClassLoader loader = Thread.currentThread()
104: .getContextClassLoader();
105: Class typeClass = Class.forName(typeName, false,
106: loader);
107:
108: if (typeClass.isInterface())
109: type = typeClass;
110: } catch (Exception e) {
111: log.finer(e.toString());
112: }
113: }
114:
115: // XXX: check is_a
116:
117: StubMarshal stubMarshal = new StubMarshal(type);
118:
119: IiopProxyHandler handler = new IiopProxyHandler(stub
120: .getORBImpl(), stub, stubMarshal);
121:
122: return Proxy.newProxyInstance(type.getClassLoader(),
123: new Class[] { type, IiopProxy.class }, handler);
124: }
125:
126: return value;
127: }
128: }
|