1 /*
   2  * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.corba.se.spi.servicecontext;
  27 
  28 import org.omg.CORBA.BAD_PARAM;
  29 import java.util.Vector ;
  30 import java.util.Enumeration ;
  31 import com.sun.corba.se.spi.servicecontext.ServiceContext ;
  32 import com.sun.corba.se.spi.servicecontext.ServiceContextData ;
  33 import com.sun.corba.se.spi.orb.ORB ;
  34 import com.sun.corba.se.impl.orbutil.ORBUtility ;
  35 
  36 public class ServiceContextRegistry {
  37     private ORB orb ;
  38     private Vector scCollection ;
  39 
  40     private void dprint( String msg )
  41     {
  42         ORBUtility.dprint( this, msg ) ;
  43     }
  44 
  45     public ServiceContextRegistry( ORB orb )
  46     {
  47         scCollection = new Vector() ;
  48         this.orb = orb ;
  49     }
  50 
  51     /** Register the ServiceContext class so that it will be recognized
  52      * by the read method.
  53      * Class cls must have the following properties:
  54      * <ul>
  55      * <li>It must derive from com.sun.corba.se.spi.servicecontext.ServiceContext.</li>
  56      * <li>It must have a public static final int SERVICE_CONTEXT_ID
  57      * member.</li>
  58      * <li>It must implement a constructor that takes a
  59      * org.omg.CORBA_2_3.portable.InputStream argument.</li>
  60      * </ul>
  61      */
  62     public void register( Class cls )
  63     {
  64         if (ORB.ORBInitDebug)
  65             dprint( "Registering service context class " + cls ) ;
  66 
  67         ServiceContextData scd = new ServiceContextData( cls ) ;
  68 
  69         if (findServiceContextData(scd.getId()) == null)
  70             scCollection.addElement( scd ) ;
  71         else
  72             throw new BAD_PARAM( "Tried to register duplicate service context" ) ;
  73     }
  74 
  75     public ServiceContextData findServiceContextData( int scId )
  76     {
  77         if (ORB.ORBInitDebug)
  78             dprint( "Searching registry for service context id " + scId ) ;
  79 
  80         Enumeration enumeration = scCollection.elements() ;
  81         while (enumeration.hasMoreElements()) {
  82             ServiceContextData scd =
  83                 (ServiceContextData)(enumeration.nextElement()) ;
  84             if (scd.getId() == scId) {
  85                 if (ORB.ORBInitDebug)
  86                     dprint( "Service context data found: " + scd ) ;
  87 
  88                 return scd ;
  89             }
  90         }
  91 
  92         if (ORB.ORBInitDebug)
  93             dprint( "Service context data not found" ) ;
  94 
  95         return null ;
  96     }
  97 }