Source for gnu.CORBA.Interceptor.Registrator

   1: /* Registrator.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.Interceptor;
  40: 
  41: import gnu.CORBA.Poa.ORB_1_4;
  42: import gnu.CORBA.ObjectCreator;
  43: import gnu.CORBA.gnuCodecFactory;
  44: 
  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;
  60: 
  61: import java.io.BufferedInputStream;
  62: import java.io.File;
  63: import java.io.FileInputStream;
  64: import java.io.IOException;
  65: 
  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;
  73: 
  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.
  78:  *
  79:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  80:  */
  81: public class Registrator extends LocalObject implements ORBInitInfo
  82: {
  83:   /**
  84:    * Use serialVersionUID for interoperability.
  85:    */
  86:   private static final long serialVersionUID = 1;
  87: 
  88:   /**
  89:    * The agreed properties prefix.
  90:    */
  91:   public static final String m_prefix =
  92:     "org.omg.PortableInterceptor.ORBInitializerClass.";
  93: 
  94:   /**
  95:    * The initialization - time server request interceptors.
  96:    */
  97:   private ArrayList m_server = new ArrayList();
  98: 
  99:   /**
 100:    * The initialization - time client request interceptors.
 101:    */
 102:   private ArrayList m_client = new ArrayList();
 103: 
 104:   /**
 105:    * The initialization - time ior interceptors.
 106:    */
 107:   private ArrayList m_ior = new ArrayList();
 108: 
 109:   /**
 110:    * The policy factories.
 111:    */
 112:   public Hashtable m_policyFactories = new Hashtable();
 113: 
 114:   /**
 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.
 118:    */
 119:   public TreeMap m_references = new TreeMap();
 120: 
 121:   /**
 122:    * The initializers.
 123:    */
 124:   public ArrayList m_initializers = new ArrayList();
 125: 
 126:   /**
 127:    * The ORB being intialised.
 128:    */
 129:   final ORB_1_4 orb;
 130: 
 131:   /**
 132:    * The argument string array, passed to ORB.init.
 133:    */
 134:   final String[] m_args;
 135: 
 136:   /**
 137:    * The codec factory.
 138:    */
 139:   final gnuCodecFactory m_codecFactory;
 140: 
 141:   /**
 142:    * Create the interceptor collection from the given properties, using the
 143:    * agreed naming convention.
 144:    *
 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.
 149:    */
 150:   public Registrator(ORB_1_4 an_orb, Properties props, String[] an_args)
 151:   {
 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");
 159:   }
 160: 
 161:   /**
 162:    * Scan the given properties for the possible interceptors.
 163:    */
 164:   private void checkProperties(Properties props)
 165:   {
 166:     if (props == null)
 167:       {
 168:         return;
 169:       }
 170: 
 171:     Enumeration names = props.propertyNames();
 172:     java.lang.Object key;
 173:     String sk;
 174: 
 175:     while (names.hasMoreElements())
 176:       {
 177:         key = names.nextElement();
 178:         if (key != null)
 179:           {
 180:             sk = key.toString();
 181:             if (sk.startsWith(m_prefix))
 182:               {
 183:                 try
 184:                   {
 185:                     String cn = sk.substring(m_prefix.length());
 186:                     Class iClass = ObjectCreator.forName(cn);
 187: 
 188:                     ORBInitializer initializer =
 189:                       (ORBInitializer) iClass.newInstance();
 190:                     m_initializers.add(initializer);
 191:                   }
 192:                 catch (Exception exc)
 193:                   {
 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");
 199:                   }
 200:               }
 201:           }
 202:       }
 203:   }
 204: 
 205:   /**
 206:    * Check if the property is defined in the existsting file orb.properties.
 207:    */
 208:   private void checkFile(String dir, String subdir)
 209:   {
 210:     try
 211:       {
 212:         File f = new File(dir);
 213:         if (!f.exists())
 214:           {
 215:             return;
 216:           }
 217: 
 218:         if (subdir != null)
 219:           {
 220:             f = new File(f, subdir);
 221:           }
 222:         f = new File(f, "orb.properties");
 223: 
 224:         if (!f.exists())
 225:           {
 226:             return;
 227:           }
 228: 
 229:         Properties p = new Properties();
 230:         p.load(new BufferedInputStream(new FileInputStream(f)));
 231: 
 232:         checkProperties(p);
 233:       }
 234:     catch (IOException ex)
 235:       {
 236:       }
 237:   }
 238: 
 239:   /**
 240:    * Called by ORB as a pre_init for all initializers.
 241:    */
 242:   public void pre_init()
 243:   {
 244:     Iterator iter = m_initializers.iterator();
 245:     while (iter.hasNext())
 246:       {
 247:         ORBInitializerOperations initializer =
 248:           (ORBInitializerOperations) iter.next();
 249:         initializer.pre_init(this);
 250:       }
 251:   }
 252: 
 253:   /**
 254:    * Get the map of the registered references. The ORB calls this method to
 255:    * import the references into its references map.
 256:    */
 257:   public Map getRegisteredReferences()
 258:   {
 259:     return m_references;
 260:   }
 261: 
 262:   /**
 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.
 265:    */
 266:   public void post_init()
 267:   {
 268:     Iterator iter = m_initializers.iterator();
 269:     while (iter.hasNext())
 270:       {
 271:         ORBInitializerOperations initializer =
 272:           (ORBInitializerOperations) iter.next();
 273:         initializer.post_init(this);
 274:       }
 275:   }
 276: 
 277:   public ServerRequestInterceptor[] getServerRequestInterceptors()
 278:   {
 279:     ServerRequestInterceptor[] iServer =
 280:       new ServerRequestInterceptor[ m_server.size() ];
 281:     for (int i = 0; i < iServer.length; i++)
 282:       {
 283:         iServer [ i ] = (ServerRequestInterceptor) m_server.get(i);
 284:       }
 285:     return iServer;
 286:   }
 287: 
 288:   public ClientRequestInterceptor[] getClientRequestInterceptors()
 289:   {
 290:     ClientRequestInterceptor[] iClient =
 291:       new ClientRequestInterceptor[ m_client.size() ];
 292:     for (int i = 0; i < iClient.length; i++)
 293:       {
 294:         iClient [ i ] = (ClientRequestInterceptor) m_client.get(i);
 295:       }
 296:     return iClient;
 297:   }
 298: 
 299:   public IORInterceptor[] getIORInterceptors()
 300:   {
 301:     IORInterceptor[] iIor = new IORInterceptor[ m_ior.size() ];
 302:     for (int i = 0; i < iIor.length; i++)
 303:       {
 304:         iIor [ i ] = (IORInterceptor) m_ior.get(i);
 305:       }
 306:     return iIor;
 307:   }
 308: 
 309:   public void add_client_request_interceptor(
 310:     ClientRequestInterceptor interceptor
 311:   ) throws DuplicateName
 312:   {
 313:     add(m_client, interceptor);
 314:   }
 315: 
 316:   public void add_ior_interceptor(IORInterceptor interceptor)
 317:     throws DuplicateName
 318:   {
 319:     add(m_ior, interceptor);
 320:   }
 321: 
 322:   public void add_server_request_interceptor(
 323:     ServerRequestInterceptor interceptor
 324:   ) throws DuplicateName
 325:   {
 326:     add(m_server, interceptor);
 327:   }
 328: 
 329:   /**
 330:    * Allocate a new slot for request - specific records.
 331:    */
 332:   public int allocate_slot_id()
 333:   {
 334:     return orb.icSlotSize++;
 335:   }
 336: 
 337:   /**
 338:    * Add the interceptor to the given collection.
 339:    *
 340:    * @param list the collection to add.
 341:    * @param interceptor the interceptor to add.
 342:    */
 343:   private void add(ArrayList list, Interceptor interceptor)
 344:     throws DuplicateName
 345:   {
 346:     if (interceptor.name().length() > 0)
 347:       {
 348:         Iterator iter = list.iterator();
 349:         Interceptor ic;
 350: 
 351:         while (iter.hasNext())
 352:           {
 353:             ic = (Interceptor) iter.next();
 354:             if (ic.name().equals(interceptor.name()))
 355:               {
 356:                 throw new DuplicateName(interceptor.name());
 357:               }
 358:           }
 359:       }
 360:     list.add(interceptor);
 361:   }
 362: 
 363:   /**
 364:    * Get string array, passed to ORB.init.
 365:    */
 366:   public String[] arguments()
 367:   {
 368:     return m_args;
 369:   }
 370: 
 371:   /**
 372:    * Get the codec factory.
 373:    */
 374:   public CodecFactory codec_factory()
 375:   {
 376:     return m_codecFactory;
 377:   }
 378: 
 379:   /**
 380:    * Get the ORB's id, currently using .toString.
 381:    */
 382:   public String orb_id()
 383:   {
 384:     return "orb_" + orb;
 385:   }
 386: 
 387:   /**
 388:    * Register reference.
 389:    */
 390:   public void register_initial_reference(String object_name, Object object)
 391:     throws InvalidName
 392:   {
 393:     if (object_name == null)
 394:       {
 395:         throw new InvalidName("null");
 396:       }
 397:     else if (object_name.length() == 0)
 398:       {
 399:         throw new InvalidName("Empty string");
 400:       }
 401:     else if (m_references.containsKey(object_name))
 402:       {
 403:         throw new InvalidName(object_name);
 404:       }
 405:     else
 406:       {
 407:         m_references.put(object_name, object);
 408:       }
 409:   }
 410: 
 411:   /**
 412:    * Accumulates the policy factory map.
 413:    */
 414:   public void register_policy_factory(int policy_type,
 415:     PolicyFactory policy_factory
 416:   )
 417:   {
 418:     Integer it = new Integer(policy_type);
 419:     if (m_policyFactories.containsKey(it))
 420:       {
 421:         throw new BAD_INV_ORDER(
 422:           "Repetetive registration of the policy factory for type " +
 423:           policy_type,
 424:           16,
 425:           CompletionStatus.COMPLETED_NO
 426:         );
 427:       }
 428:     m_policyFactories.put(it, policy_factory);
 429:   }
 430: 
 431:   /**
 432:    * Delegates to ORB.
 433:    */
 434:   public org.omg.CORBA.Object resolve_initial_references(String object_name)
 435:     throws InvalidName
 436:   {
 437:     try
 438:       {
 439:         return orb.resolve_initial_references(object_name);
 440:       }
 441:     catch (org.omg.CORBA.ORBPackage.InvalidName e)
 442:       {
 443:         InvalidName in = new InvalidName(e.getMessage());
 444:         in.initCause(e);
 445:         throw in;
 446:       }
 447:   }
 448: 
 449:   /**
 450:    * Check if any interceptors of this type were registered.
 451:    */
 452:   public boolean hasClientRequestInterceptors()
 453:   {
 454:     return m_client.size() > 0;
 455:   }
 456: 
 457:   /**
 458:    * Check if any interceptors of this type were registered.
 459:    */
 460:   public boolean hasServerRequestInterceptors()
 461:   {
 462:     return m_server.size() > 0;
 463:   }
 464: 
 465:   /**
 466:    * Check if any interceptors of this type were registered.
 467:    */
 468:   public boolean hasIorInterceptors()
 469:   {
 470:     return m_ior.size() > 0;
 471:   }
 472: }