Source for gnu.CORBA.OrbRestricted

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