Source for gnu.CORBA.Connected_objects

   1: /* Connected_objects.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.CORBA;
  40: 
  41: import java.util.Iterator;
  42: import java.util.Map;
  43: import java.util.Set;
  44: import java.util.TreeMap;
  45: 
  46: /**
  47:  * The repository of objects, that have been connected to the
  48:  * {@link FunctionalORB} by the method
  49:  * {@link ORB.connect(org.omg.CORBA.Object)}.
  50:  *
  51:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  52:  */
  53: public class Connected_objects
  54: {
  55:   /**
  56:    * The reference data about the connected object.
  57:    */
  58:   public class cObject
  59:   {
  60:     /**
  61:      * Create an initialised instance.
  62:      */
  63:     cObject(org.omg.CORBA.Object _object, int _port, byte[] _key,
  64:             java.lang.Object an_identity
  65:            )
  66:     {
  67:       object = _object;
  68:       port = _port;
  69:       key = _key;
  70:       identity = an_identity;
  71:     }
  72: 
  73:     /**
  74:      * The object.
  75:      */
  76:     public final org.omg.CORBA.Object object;
  77: 
  78:     /**
  79:      * The port on that the object is connected.
  80:      */
  81:     public final int port;
  82: 
  83:     /**
  84:      * The object key.
  85:      */
  86:     public final byte[] key;
  87: 
  88:     /**
  89:      * The shared serving identity (usually POA) or null if no such
  90:      * applicable.
  91:      */
  92:     public final java.lang.Object identity;
  93:   }
  94: 
  95:   /**
  96:    * The free number to give for the next instance.
  97:    * This field is incremented each time the
  98:    * new collection of the connected objects is created.
  99:    * Each collection has its own unique instance number.
 100:    */
 101:   private static long free_object_number;
 102: 
 103:   /**
 104:    * The map of the all connected objects, maps the object key to the
 105:    * object.
 106:    */
 107:   private Map objects = new TreeMap(new ByteArrayComparator());
 108: 
 109:   /**
 110:    * Get the record of the stored object.
 111:    *
 112:    * @param stored_object the stored object
 113:    *
 114:    * @return the record about the stored object, null if
 115:    * this object is not stored here.
 116:    */
 117:   public cObject getKey(org.omg.CORBA.Object stored_object)
 118:   {
 119:     synchronized (objects)
 120:       {
 121:         Map.Entry item;
 122:         Iterator iter = objects.entrySet().iterator();
 123:         cObject ref;
 124: 
 125:         while (iter.hasNext())
 126:           {
 127:             item = (Map.Entry) iter.next();
 128:             ref = (cObject) item.getValue();
 129:             if (stored_object.equals(ref.object) ||
 130:                 stored_object._is_equivalent(ref.object)
 131:                )
 132:               return ref;
 133:           }
 134:       }
 135: 
 136:     return null;
 137:   }
 138: 
 139:   /**
 140:    * Add the new object to the repository. The object key is
 141:    * generated automatically.
 142:    *
 143:    * @param object the object to add.
 144:    * @param port on that the ORB will be listening to the remote
 145:    * invocations.
 146:    *
 147:    * @return the newly created object record.
 148:    */
 149:   public cObject add(org.omg.CORBA.Object object, int port)
 150:   {
 151:     return add(generateObjectKey(object), object, port, null);
 152:   }
 153: 
 154:   /**
 155:    * Add the new object to the repository.
 156:    *
 157:    * @param key the object key.
 158:    * @param object the object to add.
 159:    * @param port the port, on that the ORB will be listening on the
 160:    * remote invocations.
 161:    */
 162:   public cObject add(byte[] key, org.omg.CORBA.Object object, int port,
 163:                      java.lang.Object identity
 164:                     )
 165:   {
 166:     cObject rec = new cObject(object, port, key, identity);
 167:     synchronized (objects)
 168:       {
 169:         objects.put(key, rec);
 170:       }
 171:     return rec;
 172:   }
 173: 
 174:   /**
 175:    * Get the stored object.
 176:    *
 177:    * @param key the key (in the byte array form).
 178:    *
 179:    * @return the matching object, null if none is matching.
 180:    */
 181:   public cObject get(byte[] key)
 182:   {
 183:     synchronized (objects)
 184:       {
 185:         return (cObject) objects.get(key);
 186:       }
 187:   }
 188: 
 189:   /**
 190:    * Get the map entry set.
 191:    */
 192:   public Set entrySet()
 193:   {
 194:     return objects.entrySet();
 195:   }
 196: 
 197:   /**
 198:    * Remove the given object.
 199:    *
 200:    * @param object the object to remove.
 201:    */
 202:   public void remove(org.omg.CORBA.Object object)
 203:   {
 204:     synchronized (objects)
 205:       {
 206:         cObject ref = getKey(object);
 207:         if (ref != null)
 208:           objects.remove(ref.key);
 209:       }
 210:   }
 211: 
 212:   /**
 213:    * Remove the given object, indiciating it by the key.
 214:    *
 215:    * @param object the object to remove.
 216:    */
 217:   public void remove(byte[] key)
 218:   {
 219:     objects.remove(key);
 220:   }
 221: 
 222:   /**
 223:    * Generate the object key, unique in the currently
 224:    * running java virtual machine.
 225:    *
 226:    * The generated key includes the object class name
 227:    * and the absolute instance number.
 228:    *
 229:    * @return the generated key.
 230:    */
 231:   protected byte[] generateObjectKey(org.omg.CORBA.Object object)
 232:   {
 233:     return (object.getClass().getName() + ":" + getFreeInstanceNumber()).getBytes();
 234:   }
 235: 
 236:   /**
 237:    * Get next free instance number.
 238:    */
 239:   private static synchronized long getFreeInstanceNumber()
 240:   {
 241:     long instance_number = free_object_number;
 242:     free_object_number++;
 243:     return instance_number;
 244:   }
 245: 
 246:   /**
 247:    * Get the number of the connected objects.
 248:    *
 249:    * @return the size of the internal map.
 250:    */
 251:   public int size()
 252:   {
 253:     return objects.size();
 254:   }
 255: }