OSDN Git Service

Imported Classpath 0.18.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / CORBA / Restricted_ORB.java
1 /* RestrictedORB.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 gnu.CORBA.CDR.cdrBufOutput;
42
43 import org.omg.CORBA.Any;
44 import org.omg.CORBA.BAD_PARAM;
45 import org.omg.CORBA.ContextList;
46 import org.omg.CORBA.Environment;
47 import org.omg.CORBA.ExceptionList;
48 import org.omg.CORBA.NO_IMPLEMENT;
49 import org.omg.CORBA.NVList;
50 import org.omg.CORBA.NamedValue;
51 import org.omg.CORBA.ORB;
52 import org.omg.CORBA.ORBPackage.InvalidName;
53 import org.omg.CORBA.Request;
54 import org.omg.CORBA.StructMember;
55 import org.omg.CORBA.TCKind;
56 import org.omg.CORBA.TypeCode;
57 import org.omg.CORBA.TypeCodePackage.BadKind;
58 import org.omg.CORBA.UnionMember;
59 import org.omg.CORBA.portable.OutputStream;
60 import org.omg.CORBA.portable.ValueFactory;
61 import org.omg.PortableInterceptor.ClientRequestInterceptorOperations;
62 import org.omg.PortableInterceptor.IORInterceptorOperations;
63 import org.omg.PortableInterceptor.ServerRequestInterceptorOperations;
64
65 import java.applet.Applet;
66
67 import java.util.Hashtable;
68 import java.util.Properties;
69
70 /**
71  * This class implements so-called Singleton ORB, a highly restricted version
72  * that cannot communicate over network. This ORB is provided for the
73  * potentially malicious applets with heavy security restrictions. It, however,
74  * supports some basic features that might be needed even when the network
75  * access is not granted.
76  *
77  * This ORB can only create typecodes, {@link Any}, {@link ContextList},
78  * {@link NVList} and {@link org.omg.CORBA.portable.OutputStream} that writes to
79  * an internal buffer.
80  *
81  * All other methods throw the {@link NO_IMPLEMENT} exception.
82  *
83  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
84  */
85 public class Restricted_ORB extends org.omg.CORBA_2_3.ORB
86 {
87   /**
88    * The singleton instance of this ORB.
89    */
90   public static final ORB Singleton = new Restricted_ORB();
91
92   /**
93    * The cumulated listener for all IOR interceptors. Interceptors are used by
94    * {@link gnu.CORBA.Poa.ORB_1_4}.
95    */
96   public IORInterceptorOperations iIor;
97
98   /**
99    * The cumulated listener for all server request interceptors. Interceptors
100    * are used by {@link gnu.CORBA.Poa.ORB_1_4}.
101    */
102   public ServerRequestInterceptorOperations iServer;
103
104   /**
105    * The cumulated listener for all client request interceptros. Interceptors
106    * are used by {@link gnu.CORBA.Poa.ORB_1_4}.
107    */
108   public ClientRequestInterceptorOperations iClient;
109
110   /**
111    * The required size of the interceptor slot array.
112    */
113   public int icSlotSize = 0;
114
115   /**
116    * The value factories.
117    */
118   protected Hashtable factories = new Hashtable();
119
120   /**
121    * The policy factories.
122    */
123   protected Hashtable policyFactories = new Hashtable();
124
125   /**
126        * Create a new instance of the RestrictedORB. This is used in derived classes
127    * only.
128    */
129   protected Restricted_ORB()
130   {
131   }
132
133   /** {@inheritDoc} */
134   public TypeCode create_alias_tc(String id, String name, TypeCode typecode)
135   {
136     return new aliasTypeCode(typecode, id, name);
137   }
138
139   /** {@inheritDoc} */
140   public Any create_any()
141   {
142     gnuAny any = new gnuAny();
143     any.setOrb(this);
144     return any;
145   }
146
147   /** {@inheritDoc} */
148   public TypeCode create_array_tc(int length, TypeCode element_type)
149   {
150     primitiveArrayTypeCode p =
151       new primitiveArrayTypeCode(TCKind.tk_array, element_type);
152     p.setLength(length);
153     return p;
154   }
155
156   /** {@inheritDoc} */
157   public ContextList create_context_list()
158   {
159     return new gnuContextList();
160   }
161
162   /** {@inheritDoc} */
163   public TypeCode create_enum_tc(String id, String name, String[] values)
164   {
165     recordTypeCode r = new recordTypeCode(TCKind.tk_enum);
166     for (int i = 0; i < values.length; i++)
167       {
168         r.field().name = values [ i ];
169       }
170
171     r.setId(id);
172     r.setName(name);
173
174     return r;
175   }
176
177   /** {@inheritDoc} */
178   public Environment create_environment()
179   {
180     return new gnuEnvironment();
181   }
182
183   /** {@inheritDoc} */
184   public ExceptionList create_exception_list()
185   {
186     return new gnuExceptionList();
187   }
188
189   /** {@inheritDoc} */
190   public TypeCode create_exception_tc(String id, String name,
191     StructMember[] members
192   )
193   {
194     recordTypeCode r = new recordTypeCode(TCKind.tk_except);
195     r.setId(id);
196     r.setName(name);
197
198     for (int i = 0; i < members.length; i++)
199       {
200         r.add(members [ i ]);
201       }
202
203     return r;
204   }
205
206   /**
207    * This method is not allowed for a RestrictedORB.
208    *
209    * @throws NO_IMPLEMENT, always.
210    */
211   public TypeCode create_interface_tc(String id, String name)
212   {
213     no();
214     return null;
215   }
216
217   /** {@inheritDoc} */
218   public NVList create_list(int count)
219   {
220     return new gnuNVList(count);
221   }
222
223   /** {@inheritDoc} */
224   public NamedValue create_named_value(String s, Any any, int flags)
225   {
226     return new gnuNamedValue();
227   }
228
229   /** {@inheritDoc} */
230   public OutputStream create_output_stream()
231   {
232     cdrBufOutput stream = new cdrBufOutput();
233     stream.setOrb(this);
234     return stream;
235   }
236
237   /** {@inheritDoc} */
238   public TypeCode create_sequence_tc(int bound, TypeCode element_type)
239   {
240     primitiveArrayTypeCode p =
241       new primitiveArrayTypeCode(TCKind.tk_sequence, element_type);
242     p.setLength(bound);
243     return p;
244   }
245
246   /** {@inheritDoc} */
247   public TypeCode create_string_tc(int bound)
248   {
249     stringTypeCode p = new stringTypeCode(TCKind.tk_string);
250     p.setLength(bound);
251     return p;
252   }
253
254   /** {@inheritDoc} */
255   public TypeCode create_struct_tc(String id, String name,
256     StructMember[] members
257   )
258   {
259     recordTypeCode r = new recordTypeCode(TCKind.tk_struct);
260     r.setId(id);
261     r.setName(name);
262
263     for (int i = 0; i < members.length; i++)
264       {
265         r.add(members [ i ]);
266       }
267
268     return r;
269   }
270
271   /** {@inheritDoc} */
272   public TypeCode create_union_tc(String id, String name,
273     TypeCode discriminator_type, UnionMember[] members
274   )
275   {
276     recordTypeCode r = new recordTypeCode(TCKind.tk_union);
277     r.setId(id);
278     r.setName(name);
279     r.setDiscriminator_type(discriminator_type);
280     r.setDefaultIndex(0);
281
282     for (int i = 0; i < members.length; i++)
283       {
284         r.add(members [ i ]);
285       }
286
287     return r;
288   }
289
290   /** {@inheritDoc} */
291   public TypeCode create_wstring_tc(int bound)
292   {
293     stringTypeCode p = new stringTypeCode(TCKind.tk_wstring);
294     p.setLength(bound);
295     return p;
296   }
297
298   /** {@inheritDoc} */
299   public TypeCode get_primitive_tc(TCKind tcKind)
300   {
301     try
302       {
303         return typeNamer.getPrimitveTC(tcKind);
304       }
305     catch (BadKind ex)
306       {
307         throw new BAD_PARAM("This is not a primitive type code: " +
308           tcKind.value()
309         );
310       }
311   }
312
313   /**
314    * This method is not allowed for a RestrictedORB.
315    *
316    * @throws NO_IMPLEMENT, always.
317    */
318   public String[] list_initial_services()
319   {
320     no();
321     throw new InternalError();
322   }
323
324   /**
325    * This method is not allowed for a RestrictedORB.
326    *
327    * @throws NO_IMPLEMENT, always.
328    */
329   public String object_to_string(org.omg.CORBA.Object forObject)
330   {
331     no();
332     throw new InternalError();
333   }
334
335   /**
336    * This method is not allowed for a RestrictedORB.
337    *
338    * @throws InvalidName never in this class, but it is thrown in the derived
339    * classes.
340    *
341    * @throws NO_IMPLEMENT, always.
342    */
343   public org.omg.CORBA.Object resolve_initial_references(String name)
344     throws InvalidName
345   {
346     no();
347     throw new InternalError();
348   }
349
350   /**
351    * Shutdown the ORB server.
352    *
353    * For RestrictedORB, returns witout action.
354    */
355   public void run()
356   {
357   }
358
359   /**
360    * Shutdown the ORB server.
361    *
362    * For RestrictedORB, returns witout action.
363    */
364   public void shutdown(boolean wait_for_completion)
365   {
366   }
367
368   /**
369    * This method is not allowed for a RestrictedORB.
370    *
371    * @throws NO_IMPLEMENT, always.
372    */
373   public org.omg.CORBA.Object string_to_object(String IOR)
374   {
375     no();
376     throw new InternalError();
377   }
378
379   /**
380    * This method is not allowed for a RestrictedORB.
381    *
382    * @throws NO_IMPLEMENT, always.
383    */
384   protected void set_parameters(Applet app, Properties props)
385   {
386     no();
387   }
388
389   /**
390    * This method is not allowed for a RestrictedORB.
391    *
392    * @throws NO_IMPLEMENT, always.
393    */
394   protected void set_parameters(String[] args, Properties props)
395   {
396     no();
397   }
398
399   /**
400    * Throws an exception, stating that the given method is not supported by the
401    * Restricted ORB.
402    */
403   private final void no()
404   {
405     // Apart the programming errors, this can only happen if the
406     // malicious code is trying to do that it is not allowed.
407     throw new NO_IMPLEMENT("Use init(args, props) for the functional version.");
408   }
409
410   /**
411    * This method is not allowed for a RestrictedORB.
412    *
413    * @throws NO_IMPLEMENT, always.
414    */
415   public Request get_next_response() throws org.omg.CORBA.WrongTransaction
416   {
417     no();
418     throw new InternalError();
419   }
420
421   /**
422    * This method is not allowed for a RestrictedORB.
423    *
424    * @throws NO_IMPLEMENT, always.
425    */
426   public boolean poll_next_response()
427   {
428     no();
429     throw new InternalError();
430   }
431
432   /**
433    * This method is not allowed for a RestrictedORB.
434    *
435    * @throws NO_IMPLEMENT, always.
436    */
437   public void send_multiple_requests_deferred(Request[] requests)
438   {
439     no();
440   }
441
442   /**
443    * This method is not allowed for a RestrictedORB.
444    *
445    * @throws NO_IMPLEMENT, always.
446    */
447   public void send_multiple_requests_oneway(Request[] requests)
448   {
449     no();
450   }
451
452   /**
453    * Register the value factory under the given repository id.
454    */
455   public ValueFactory register_value_factory(String repository_id,
456     ValueFactory factory
457   )
458   {
459     factories.put(repository_id, factory);
460     return factory;
461   }
462
463   /**
464    * Unregister the value factroy.
465    */
466   public void unregister_value_factory(String id)
467   {
468     factories.remove(id);
469   }
470
471   /**
472    * Look for the value factory for the value, having the given repository id.
473        * The implementation checks for the registered value factories first. If none
474        * found, it tries to load and instantiate the class, mathing the given naming
475    * convention. If this faild, null is returned.
476    *
477    * @param repository_id a repository id.
478    *
479    * @return a found value factory, null if none.
480    */
481   public ValueFactory lookup_value_factory(String repository_id)
482   {
483     ValueFactory f = (ValueFactory) factories.get(repository_id);
484     if (f != null)
485       {
486         return f;
487       }
488
489     f = (ValueFactory) ObjectCreator.createObject(repository_id,
490         "DefaultFactory"
491       );
492     if (f != null)
493       {
494         factories.put(repository_id, f);
495       }
496     return f;
497   }
498
499   /**
500    * Destroy the interceptors, if they are present.
501    */
502   public void destroy()
503   {
504     if (iIor != null)
505       {
506         iIor.destroy();
507         iIor = null;
508       }
509
510     if (iServer != null)
511       {
512         iServer.destroy();
513         iServer = null;
514       }
515
516     if (iClient != null)
517       {
518         iClient.destroy();
519         iClient = null;
520       }
521
522     super.destroy();
523   }
524 }