Source for gnu.javax.rmi.CORBA.ValueHandlerDelegateImpl

   1: /* ValueHandlerDelegateImpl.java --
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.javax.rmi.CORBA;
  40: 
  41: import gnu.CORBA.CDR.gnuRuntime;
  42: 
  43: import org.omg.CORBA.BAD_PARAM;
  44: import org.omg.CORBA.CustomMarshal;
  45: import org.omg.CORBA.portable.OutputStream;
  46: import org.omg.CORBA.portable.Streamable;
  47: import org.omg.SendingContext.RunTime;
  48: 
  49: import java.io.Externalizable;
  50: import java.io.ObjectStreamClass;
  51: import java.io.Serializable;
  52: import java.rmi.Remote;
  53: 
  54: import javax.rmi.CORBA.ValueHandler;
  55: import javax.rmi.CORBA.ValueHandlerMultiFormat;
  56: 
  57: /**
  58:  * Implementation of the ValueHandler.
  59:  *
  60:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) (implementation)
  61:  */
  62: public class ValueHandlerDelegateImpl
  63:   extends RmiUtilities
  64:   implements ValueHandler, ValueHandlerMultiFormat
  65: {
  66:   /**
  67:    * Return the maximal supported stream format version. We currently
  68:    * support the version 1.
  69:    *
  70:    * TODO Support the version 2.
  71:    */
  72:   public byte getMaximumStreamFormatVersion()
  73:   {
  74:     return 1;
  75:   }
  76: 
  77:   /**
  78:    * Write value using the given stream format version.
  79:    */
  80:   public void writeValue(OutputStream output, Serializable value, byte version)
  81:   {
  82:     if (version!=1)
  83:       throw new BAD_PARAM("Unsupported stream format version "+version);
  84:     else
  85:       writeValue(output, value);
  86:   }
  87: 
  88:   /**
  89:    * This implementation associates RunTime with stream rather than with the
  90:    * value handler and this method is not used in the implementation. It is
  91:    * implemented just for the sake of compatibility.
  92:    */
  93:   public RunTime getRunTimeCodeBase()
  94:   {
  95:     return new gnuRuntime(null, null);
  96:   }
  97: 
  98:   /**
  99:    * Checks if an instance of this class can write its fields itself.
 100:    */
 101:   public boolean isCustomMarshaled(Class clz)
 102:   {
 103:     return CustomMarshal.class.isAssignableFrom(clz)
 104:       || Streamable.class.isAssignableFrom(clz);
 105:   }
 106: 
 107:   /**
 108:    * No replacement, returns the passed parameter.
 109:    */
 110:   public Serializable writeReplace(Serializable value)
 111:   {
 112:     return value;
 113:   }
 114: 
 115:   /**
 116:    * Compute the repository id in the RMI hashed format.
 117:    */
 118:   public String getRMIRepositoryID(final Class cx)
 119:   {
 120:     long hash = 0;
 121:     Class of = cx.isArray() ? cx.getComponentType() : null;
 122: 
 123:     if (cx.equals(String[].class))
 124:       return RMI_STRING_ARRAY_ID;
 125:     else if (cx.equals(String.class))
 126:       return RMI_STRING_ID;
 127:     else if (cx.equals(Class.class))
 128:       return RMI_CLASS_ID;
 129:     else if (Remote.class.isAssignableFrom(cx)
 130:       || !Serializable.class.isAssignableFrom(cx)
 131:       || cx.isInterface()
 132:       || (cx.isArray() && (!Serializable.class.isAssignableFrom(of)
 133:         || of.isPrimitive() || Remote.class.isAssignableFrom(of)))
 134: 
 135:     )
 136:       // Some classes that have zero hash code and serial no version id
 137:       // included.
 138:       return "RMI:" + cx.getName() + ":" + toHex(hash);
 139:     else if (cx.isArray())
 140:       // Arrays have the same hashcode and uid as they components.
 141:       return "RMI:" + cx.getName() + ":" + toHex(getHashCode(of)) + ":"
 142:         + toHex(getSid(of));
 143:     else
 144:       {
 145:         if (Externalizable.class.isAssignableFrom(cx))
 146:           hash = 1;
 147:         else
 148:           hash = getHashCode(cx);
 149: 
 150:         return "RMI:" + cx.getName() + ":" + toHex(hash) + ":"
 151:           + toHex(getSid(cx));
 152:       }
 153:   }
 154: 
 155:   /**
 156:    * Get the class serial version UID.
 157:    */
 158:   long getSid(Class cx)
 159:   {
 160:     ObjectStreamClass osc = ObjectStreamClass.lookup(cx);
 161:     return osc.getSerialVersionUID();
 162:   }
 163: }