Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / gnu / CORBA / Interceptor / Registrator.java
blobe0ce16a149678de80e31c6b59f14dc36b1d9088f
1 /* Registrator.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)
9 any later version.
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
19 02110-1301 USA.
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.
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.Interceptor;
41 import gnu.CORBA.Poa.ORB_1_4;
42 import gnu.CORBA.ObjectCreator;
43 import gnu.CORBA.gnuCodecFactory;
45 import org.omg.CORBA.BAD_INV_ORDER;
46 import org.omg.CORBA.CompletionStatus;
47 import org.omg.CORBA.LocalObject;
48 import org.omg.CORBA.Object;
49 import org.omg.IOP.CodecFactory;
50 import org.omg.PortableInterceptor.ClientRequestInterceptor;
51 import org.omg.PortableInterceptor.IORInterceptor;
52 import org.omg.PortableInterceptor.Interceptor;
53 import org.omg.PortableInterceptor.ORBInitInfo;
54 import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName;
55 import org.omg.PortableInterceptor.ORBInitInfoPackage.InvalidName;
56 import org.omg.PortableInterceptor.ORBInitializer;
57 import org.omg.PortableInterceptor.ORBInitializerOperations;
58 import org.omg.PortableInterceptor.PolicyFactory;
59 import org.omg.PortableInterceptor.ServerRequestInterceptor;
61 import java.io.BufferedInputStream;
62 import java.io.File;
63 import java.io.FileInputStream;
64 import java.io.IOException;
66 import java.util.ArrayList;
67 import java.util.Enumeration;
68 import java.util.Hashtable;
69 import java.util.Iterator;
70 import java.util.Map;
71 import java.util.Properties;
72 import java.util.TreeMap;
74 /**
75 * Collects interceptors, references and factories into arrays during
76 * registration. As the class is security sensitive, the most of the fields are
77 * private.
79 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
81 public class Registrator extends LocalObject implements ORBInitInfo
83 /**
84 * Use serialVersionUID for interoperability.
86 private static final long serialVersionUID = 1;
88 /**
89 * The agreed properties prefix.
91 public static final String m_prefix =
92 "org.omg.PortableInterceptor.ORBInitializerClass.";
94 /**
95 * The initialization - time server request interceptors.
97 private ArrayList m_server = new ArrayList();
99 /**
100 * The initialization - time client request interceptors.
102 private ArrayList m_client = new ArrayList();
105 * The initialization - time ior interceptors.
107 private ArrayList m_ior = new ArrayList();
110 * The policy factories.
112 public Hashtable m_policyFactories = new Hashtable();
115 * The registered references. To avoid exposing the ORB's references map, the
116 * are added by ORB from inside the ORB code. The ORB is responsible for
117 * taking them from this field between pre_init and post_init.
119 public TreeMap m_references = new TreeMap();
122 * The initializers.
124 public ArrayList m_initializers = new ArrayList();
127 * The ORB being intialised.
129 final ORB_1_4 orb;
132 * The argument string array, passed to ORB.init.
134 final String[] m_args;
137 * The codec factory.
139 final gnuCodecFactory m_codecFactory;
142 * Create the interceptor collection from the given properties, using the
143 * agreed naming convention.
145 * @param an_orb the ORB being initialised.
146 * @param props the cumulated set of properties where the orb initializer
147 * pattern is searched.
148 * @param an_args the argument string array, passed to ORB.init.
150 public Registrator(ORB_1_4 an_orb, Properties props, String[] an_args)
152 orb = an_orb;
153 m_args = an_args;
154 m_codecFactory = new gnuCodecFactory(orb);
155 checkProperties(props);
156 checkProperties(System.getProperties());
157 checkFile("user.home", null);
158 checkFile("java.home", "lib");
162 * Scan the given properties for the possible interceptors.
164 private void checkProperties(Properties props)
166 if (props == null)
168 return;
171 Enumeration names = props.propertyNames();
172 java.lang.Object key;
173 String sk;
175 while (names.hasMoreElements())
177 key = names.nextElement();
178 if (key != null)
180 sk = key.toString();
181 if (sk.startsWith(m_prefix))
185 String cn = sk.substring(m_prefix.length());
186 Class iClass = ObjectCreator.forName(cn);
188 ORBInitializer initializer =
189 (ORBInitializer) iClass.newInstance();
190 m_initializers.add(initializer);
192 catch (Exception exc)
194 // OMG states we should not throw an exception, but
195 // this will help the user to detect his error
196 // in initialiser properties. Should never print during
197 // normal run.
198 System.err.println(sk + " failed");
206 * Check if the property is defined in the existsting file orb.properties.
208 private void checkFile(String dir, String subdir)
212 File f = new File(dir);
213 if (!f.exists())
215 return;
218 if (subdir != null)
220 f = new File(f, subdir);
222 f = new File(f, "orb.properties");
224 if (!f.exists())
226 return;
229 Properties p = new Properties();
230 p.load(new BufferedInputStream(new FileInputStream(f)));
232 checkProperties(p);
234 catch (IOException ex)
240 * Called by ORB as a pre_init for all initializers.
242 public void pre_init()
244 Iterator iter = m_initializers.iterator();
245 while (iter.hasNext())
247 ORBInitializerOperations initializer =
248 (ORBInitializerOperations) iter.next();
249 initializer.pre_init(this);
254 * Get the map of the registered references. The ORB calls this method to
255 * import the references into its references map.
257 public Map getRegisteredReferences()
259 return m_references;
263 * Called by ORB as a post-init for all initializers. After this call, the
264 * interceptor sets are fixed and redundant information is discarded.
266 public void post_init()
268 Iterator iter = m_initializers.iterator();
269 while (iter.hasNext())
271 ORBInitializerOperations initializer =
272 (ORBInitializerOperations) iter.next();
273 initializer.post_init(this);
277 public ServerRequestInterceptor[] getServerRequestInterceptors()
279 ServerRequestInterceptor[] iServer =
280 new ServerRequestInterceptor[ m_server.size() ];
281 for (int i = 0; i < iServer.length; i++)
283 iServer [ i ] = (ServerRequestInterceptor) m_server.get(i);
285 return iServer;
288 public ClientRequestInterceptor[] getClientRequestInterceptors()
290 ClientRequestInterceptor[] iClient =
291 new ClientRequestInterceptor[ m_client.size() ];
292 for (int i = 0; i < iClient.length; i++)
294 iClient [ i ] = (ClientRequestInterceptor) m_client.get(i);
296 return iClient;
299 public IORInterceptor[] getIORInterceptors()
301 IORInterceptor[] iIor = new IORInterceptor[ m_ior.size() ];
302 for (int i = 0; i < iIor.length; i++)
304 iIor [ i ] = (IORInterceptor) m_ior.get(i);
306 return iIor;
309 public void add_client_request_interceptor(
310 ClientRequestInterceptor interceptor
311 ) throws DuplicateName
313 add(m_client, interceptor);
316 public void add_ior_interceptor(IORInterceptor interceptor)
317 throws DuplicateName
319 add(m_ior, interceptor);
322 public void add_server_request_interceptor(
323 ServerRequestInterceptor interceptor
324 ) throws DuplicateName
326 add(m_server, interceptor);
330 * Allocate a new slot for request - specific records.
332 public int allocate_slot_id()
334 return orb.icSlotSize++;
338 * Add the interceptor to the given collection.
340 * @param list the collection to add.
341 * @param interceptor the interceptor to add.
343 private void add(ArrayList list, Interceptor interceptor)
344 throws DuplicateName
346 if (interceptor.name().length() > 0)
348 Iterator iter = list.iterator();
349 Interceptor ic;
351 while (iter.hasNext())
353 ic = (Interceptor) iter.next();
354 if (ic.name().equals(interceptor.name()))
356 throw new DuplicateName(interceptor.name());
360 list.add(interceptor);
364 * Get string array, passed to ORB.init.
366 public String[] arguments()
368 return m_args;
372 * Get the codec factory.
374 public CodecFactory codec_factory()
376 return m_codecFactory;
380 * Get the ORB's id, currently using .toString.
382 public String orb_id()
384 return "orb_" + orb;
388 * Register reference.
390 public void register_initial_reference(String object_name, Object object)
391 throws InvalidName
393 if (object_name == null)
395 throw new InvalidName("null");
397 else if (object_name.length() == 0)
399 throw new InvalidName("Empty string");
401 else if (m_references.containsKey(object_name))
403 throw new InvalidName(object_name);
405 else
407 m_references.put(object_name, object);
412 * Accumulates the policy factory map.
414 public void register_policy_factory(int policy_type,
415 PolicyFactory policy_factory
418 Integer it = new Integer(policy_type);
419 if (m_policyFactories.containsKey(it))
421 throw new BAD_INV_ORDER(
422 "Repetetive registration of the policy factory for type " +
423 policy_type,
425 CompletionStatus.COMPLETED_NO
428 m_policyFactories.put(it, policy_factory);
432 * Delegates to ORB.
434 public org.omg.CORBA.Object resolve_initial_references(String object_name)
435 throws InvalidName
439 return orb.resolve_initial_references(object_name);
441 catch (org.omg.CORBA.ORBPackage.InvalidName e)
443 InvalidName in = new InvalidName(e.getMessage());
444 in.initCause(e);
445 throw in;
450 * Check if any interceptors of this type were registered.
452 public boolean hasClientRequestInterceptors()
454 return m_client.size() > 0;
458 * Check if any interceptors of this type were registered.
460 public boolean hasServerRequestInterceptors()
462 return m_server.size() > 0;
466 * Check if any interceptors of this type were registered.
468 public boolean hasIorInterceptors()
470 return m_ior.size() > 0;