2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
39 package gnu.CORBA.Poa;
41 import gnu.CORBA.ByteArrayComparator;
43 import org.omg.CORBA.portable.Delegate;
44 import org.omg.CORBA.portable.ObjectImpl;
45 import org.omg.PortableServer.Servant;
47 import java.util.Iterator;
50 import java.util.TreeMap;
53 * Implements the conception of the Active Object Map.
54 * If the POA supports the RETAIN policy, it maintains an Active
55 * Object Map, that associates Object Ids with active servants.
56 * Each association constitutes an active object. We use a single map
57 * for all POAs on the given orb.
59 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
64 * The reference data about the object, placed on the AOM.
69 * Create an initialised instance.
71 Obj(gnuServantObject _object, byte[] _key, Servant _servant, gnuPOA _poa)
82 public final gnuServantObject object;
85 * The servant, serving the given object.
87 public Servant servant;
90 * The local servant that once served this object.
91 * This field is used by {@link ForwardedServant} when it discovers that
92 * the forwarding chaing returns back to the original location.
93 * It should not be used anywhere else.
95 Servant primary_servant;
98 * The POA, where the object is connected.
100 public final gnuPOA poa;
105 public final byte[] key;
108 * If true, this entry is deactivated.
110 public boolean deactivated;
113 * Set the servant value, preserving any non null
114 * value as the primary servant.
116 public void setServant(Servant s)
118 if (primary_servant == null)
126 public Servant getServant()
132 * Get the deactivation state.
134 public boolean isDeactiveted()
140 * Set the deactivation state.
142 public void setDeactivated(boolean state)
149 * The free number to give for the next instance.
150 * This field is incremented each time the
151 * new collection of the connected objects is created.
152 * Each collection has its own unique instance number.
154 private static long free_id;
157 * The map of the all connected objects, maps the object key to the
160 Map objects = new TreeMap(new ByteArrayComparator());
163 * Get the record of the stored object. If the object is mapped several times
164 * under the different keys, one of the mappings is used.
166 * @param object the stored object
168 * @return the record about the stored object, null if this object is not
171 public Obj findObject(org.omg.CORBA.Object stored_object)
173 if (stored_object == null)
180 if (stored_object instanceof ObjectImpl)
182 // If the delegate is available, search by delegate.
183 Delegate d = ((ObjectImpl) stored_object)._get_delegate();
188 iter = objects.entrySet().iterator();
189 while (iter.hasNext())
191 item = (Map.Entry) iter.next();
192 ref = (Obj) item.getValue();
193 d2 = ref.object._get_delegate();
195 if (d == d2 || (d2 != null && d2.equals(d)))
201 // For other objects (or if not possible to get the delegate),
203 iter = objects.entrySet().iterator();
204 while (iter.hasNext())
206 item = (Map.Entry) iter.next();
207 ref = (Obj) item.getValue();
208 if (stored_object.equals(ref.object))
215 * Find the reference info for the given servant. If the servant is mapped to
216 * several objects, this returns the first found occurence.
218 * @param servant a servant to find.
220 * @return the servant/object/POA binding or null if no such found.
222 public Obj findServant(Servant servant)
228 Iterator iter = objects.entrySet().iterator();
231 while (iter.hasNext())
233 item = (Map.Entry) iter.next();
234 ref = (Obj) item.getValue();
235 if (servant.equals(ref.servant))
242 * Find the reference info for the given servant.
243 * If the servant is mapped to several objects, this
244 * returns the first found occurence.
246 * @param servant a servant to find.
247 * @param speficies if to search for the inactive (true) or active
248 * (false) servant. A servant with unmatching activity is ignored
251 * @return the servant/object/POA binding or null if no such found.
253 public Obj findServant(Servant servant, boolean inactive)
259 Iterator iter = objects.entrySet().iterator();
262 while (iter.hasNext())
264 item = (Map.Entry) iter.next();
265 ref = (Obj) item.getValue();
266 if (ref.deactivated == inactive)
267 if (ref.servant != null)
268 if (servant.equals(ref.servant))
275 * Add the new object to the repository. The object key is
276 * generated automatically.
278 * @param object the object to add.
279 * @param servant a servant, serving the given object.
280 * @param poa the poa, where the object is connected.
282 * @return the newly created object record.
284 public Obj add(gnuServantObject object, Servant servant, gnuPOA poa)
286 return add(generateObjectKey(object), object, servant, poa);
290 * Add the new object to the repository.
292 * @param key the object key.
293 * @param object the object to add.
294 * @param servant a servant, serving the given object.
295 * @param poa the POA, where the object is connected.
297 public Obj add(byte[] key, gnuServantObject object, Servant servant,
301 Obj rec = new Obj(object, key, servant, poa);
302 objects.put(key, rec);
307 * Add the new object to the repository.
309 * @param delegate the delegate, providing data about the servant, key, POA
311 * @param port the port that this object would take.
313 public Obj add(ServantDelegateImpl delegate)
316 new Obj(delegate.object, delegate.servant_id, delegate.servant,
319 objects.put(delegate.servant_id, rec);
324 * Put back the definition structure that has probably been removed earlier.
326 public void put(Obj obj)
328 objects.put(obj.key, obj);
332 * Get the stored object.
334 * @param key the key (in the byte array form).
336 * @return the matching object, null if none is matching.
338 public Obj get(byte[] key)
340 return (Obj) objects.get(key);
344 * Get the map key set.
348 return objects.keySet();
352 * Remove the given object, indiciating it by the key.
354 * @param object the object to remove.
356 public void remove(byte[] key)
362 * Generate the object key, unique in the currently
363 * running java virtual machine. The passed object
364 * parameter is currently not in use.
366 * @return the generated key.
368 protected byte[] generateObjectKey(org.omg.CORBA.Object object)
372 // The repetetive keys cannot be generated, but theoretically
373 // the same keys can be passed when calling add(byte[]...).
374 // Hence we check if the key is not already in the map and,
375 // if it is, use the subsequent value.
380 while (objects.containsKey(key));
385 * Get the next free 8 byte id, surely unique between calls of this
386 * method for the currently running virtual machine.
388 public static synchronized byte[] getFreeId()
390 byte[] r = new byte[ 8 ];
392 // Start from the faster-changing.
393 r [ 0 ] = ((byte) (0xff & free_id));
394 r [ 1 ] = ((byte) (0xff & (free_id >> 8)));
395 r [ 2 ] = ((byte) (0xff & (free_id >> 16)));
396 r [ 3 ] = ((byte) (0xff & (free_id >> 24)));
397 r [ 4 ] = ((byte) (0xff & (free_id >> 32)));
398 r [ 5 ] = ((byte) (0xff & (free_id >> 40)));
399 r [ 6 ] = ((byte) (0xff & (free_id >> 48)));
400 r [ 7 ] = ((byte) (0xff & (free_id >> 56)));