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.orb;
031:
032: import com.caucho.iiop.marshal.ArrayMarshal;
033: import com.caucho.iiop.marshal.HelperMarshal;
034: import com.caucho.iiop.marshal.BooleanMarshal;
035: import com.caucho.iiop.marshal.CharMarshal;
036: import com.caucho.iiop.marshal.ByteMarshal;
037: import com.caucho.iiop.marshal.ShortMarshal;
038: import com.caucho.iiop.marshal.IntMarshal;
039: import com.caucho.iiop.marshal.LongMarshal;
040: import com.caucho.iiop.marshal.FloatMarshal;
041: import com.caucho.iiop.marshal.DoubleMarshal;
042: import com.caucho.iiop.marshal.PrincipalMarshal;
043: import com.caucho.iiop.marshal.RemoteMarshal;
044: import com.caucho.iiop.marshal.AnyMarshal;
045: import com.caucho.iiop.marshal.VoidMarshal;
046: import com.caucho.iiop.marshal.Marshal;
047:
048: import java.util.HashMap;
049: import org.omg.CORBA.portable.IDLEntity;
050:
051: /**
052: * Proxy implementation for ORB clients.
053: */
054: public class MarshalFactory {
055: private static MarshalFactory _factory = new MarshalFactory();
056:
057: private static HashMap<Class, Marshal> _classMap = new HashMap<Class, Marshal>();
058:
059: public static MarshalFactory create() {
060: return _factory;
061: }
062:
063: /**
064: * Creates the marshal object for an object.
065: *
066: * @param cl the class to marshal
067: * @param isIdl if true, handle arrays as IDL sequences
068: */
069: public Marshal create(Class cl, boolean isIdl) {
070: Marshal marshal = _classMap.get(cl);
071:
072: if (marshal != null)
073: return marshal;
074:
075: if (org.omg.CORBA.portable.Streamable.class
076: .isAssignableFrom(cl))
077: return new StreamableMarshal(cl);
078:
079: if (IDLEntity.class.isAssignableFrom(cl))
080: return new HelperMarshal(cl);
081:
082: if (cl.isArray() && isIDLEntity(cl.getComponentType())) {
083: Class compType = cl.getComponentType();
084:
085: Marshal subMarshal = create(compType, isIdl);
086:
087: return new ArrayMarshal(compType, subMarshal);
088: }
089:
090: if (java.rmi.Remote.class.isAssignableFrom(cl))
091: return new RemoteMarshal(cl);
092:
093: if (java.io.Serializable.class.isAssignableFrom(cl))
094: return SerializableMarshal.MARSHAL;
095:
096: return AnyMarshal.MARSHAL;
097: }
098:
099: private boolean isIDLEntity(Class cl) {
100: if (IDLEntity.class.isAssignableFrom(cl))
101: return true;
102: else if (cl.isArray())
103: return isIDLEntity(cl.getComponentType());
104: else
105: return false;
106: }
107:
108: static {
109: _classMap.put(void.class, VoidMarshal.MARSHAL);
110:
111: _classMap.put(boolean.class, BooleanMarshal.MARSHAL);
112: _classMap.put(char.class, CharMarshal.MARSHAL);
113: _classMap.put(byte.class, ByteMarshal.MARSHAL);
114: _classMap.put(short.class, ShortMarshal.MARSHAL);
115: _classMap.put(int.class, IntMarshal.MARSHAL);
116: _classMap.put(long.class, LongMarshal.MARSHAL);
117: _classMap.put(float.class, FloatMarshal.MARSHAL);
118: _classMap.put(double.class, DoubleMarshal.MARSHAL);
119:
120: _classMap.put(String.class, StringMarshal.MARSHAL);
121:
122: _classMap.put(org.omg.CORBA.Object.class,
123: CorbaObjectMarshal.MARSHAL);
124: }
125: }
|