1 /* Connected_objects.java --
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. */
41 import java
.util
.Iterator
;
44 import java
.util
.TreeMap
;
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)}.
51 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
53 public class Connected_objects
56 * The reference data about the connected object.
61 * Create an initialised instance.
63 cObject(org
.omg
.CORBA
.Object _object
, int _port
, byte[] _key
,
64 java
.lang
.Object an_identity
70 identity
= an_identity
;
76 public final org
.omg
.CORBA
.Object object
;
79 * The port on that the object is connected.
81 public final int port
;
86 public final byte[] key
;
89 * The shared serving identity (usually POA) or null if no such
92 public final java
.lang
.Object identity
;
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.
101 private static long free_object_number
;
104 * The map of the all connected objects, maps the object key to the
107 private Map objects
= new TreeMap(new ByteArrayComparator());
110 * Get the record of the stored object.
112 * @param stored_object the stored object
114 * @return the record about the stored object, null if
115 * this object is not stored here.
117 public cObject
getKey(org
.omg
.CORBA
.Object stored_object
)
119 synchronized (objects
)
122 Iterator iter
= objects
.entrySet().iterator();
125 while (iter
.hasNext())
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
)
140 * Add the new object to the repository. The object key is
141 * generated automatically.
143 * @param object the object to add.
144 * @param port on that the ORB will be listening to the remote
147 * @return the newly created object record.
149 public cObject
add(org
.omg
.CORBA
.Object object
, int port
)
151 return add(generateObjectKey(object
), object
, port
, null);
155 * Add the new object to the repository.
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.
162 public cObject
add(byte[] key
, org
.omg
.CORBA
.Object object
, int port
,
163 java
.lang
.Object identity
166 cObject rec
= new cObject(object
, port
, key
, identity
);
167 synchronized (objects
)
169 objects
.put(key
, rec
);
175 * Get the stored object.
177 * @param key the key (in the byte array form).
179 * @return the matching object, null if none is matching.
181 public cObject
get(byte[] key
)
183 synchronized (objects
)
185 return (cObject
) objects
.get(key
);
190 * Get the map entry set.
192 public Set
entrySet()
194 return objects
.entrySet();
198 * Remove the given object.
200 * @param object the object to remove.
202 public void remove(org
.omg
.CORBA
.Object object
)
204 synchronized (objects
)
206 cObject ref
= getKey(object
);
208 objects
.remove(ref
.key
);
213 * Remove the given object, indiciating it by the key.
215 * @param object the object to remove.
217 public void remove(byte[] key
)
223 * Generate the object key, unique in the currently
224 * running java virtual machine.
226 * The generated key includes the object class name
227 * and the absolute instance number.
229 * @return the generated key.
231 protected byte[] generateObjectKey(org
.omg
.CORBA
.Object object
)
233 return (object
.getClass().getName() + ":" + getFreeInstanceNumber()).getBytes();
237 * Get next free instance number.
239 private static synchronized long getFreeInstanceNumber()
241 long instance_number
= free_object_number
;
242 free_object_number
++;
243 return instance_number
;
247 * Get the number of the connected objects.
249 * @return the size of the internal map.
253 return objects
.size();