1 /*
   2  * Copyright (c) 2000, 2003, 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.impl.interceptors;
  27 
  28 import org.omg.IOP.Codec;
  29 import org.omg.IOP.CodecFactory;
  30 import org.omg.IOP.CodecFactoryPackage.UnknownEncoding;
  31 import org.omg.IOP.Encoding;
  32 import org.omg.IOP.ENCODING_CDR_ENCAPS;
  33 
  34 import com.sun.corba.se.spi.logging.CORBALogDomains;
  35 
  36 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
  37 
  38 import org.omg.CORBA.ORB;
  39 import org.omg.CORBA.LocalObject;
  40 
  41 /**
  42  * CodecFactoryImpl is the implementation of the Codec Factory, as described
  43  * in orbos/99-12-02.
  44  */
  45 public final class CodecFactoryImpl
  46     extends org.omg.CORBA.LocalObject
  47     implements CodecFactory
  48 {
  49     // The ORB that created this Codec Factory
  50     private ORB orb;
  51     private ORBUtilSystemException wrapper ;
  52 
  53     // The maximum minor version of GIOP supported by this codec factory.
  54     // Currently, this is 1.2.
  55     private static final int MAX_MINOR_VERSION_SUPPORTED = 2;
  56 
  57     // The pre-created minor versions of Codec version 1.0, 1.1, ...,
  58     // 1.(MAX_MINOR_VERSION_SUPPORTED)
  59     private Codec codecs[] = new Codec[MAX_MINOR_VERSION_SUPPORTED + 1];
  60 
  61     /**
  62      * Creates a new CodecFactory implementation.  Stores the ORB that
  63      * created this factory, for later use by the Codec.
  64      */
  65     public CodecFactoryImpl( ORB orb ) {
  66         this.orb = orb;
  67         wrapper = ORBUtilSystemException.get(
  68             (com.sun.corba.se.spi.orb.ORB)orb,
  69             CORBALogDomains.RPC_PROTOCOL ) ;
  70 
  71         // Precreate a codec for version 1.0 through
  72         // 1.(MAX_MINOR_VERSION_SUPPORTED).  This can be
  73         // done since Codecs are immutable in their current implementation.
  74         // This is an optimization that eliminates the overhead of creating
  75         // a new Codec each time create_codec is called.
  76         for( int minor = 0; minor <= MAX_MINOR_VERSION_SUPPORTED; minor++ ) {
  77             codecs[minor] = new CDREncapsCodec( orb, 1, minor );
  78         }
  79     }
  80 
  81     /**
  82      * Creates a codec of the given encoding.  The only format recognized
  83      * by this factory is ENCODING_CDR_ENCAPS, versions 1.0 through
  84      * 1.(MAX_MINOR_VERSION_SUPPORTED).
  85      *
  86      * @exception UnknownEncoding Thrown if this factory cannot create a
  87      *   Codec of the given encoding.
  88      */
  89     public Codec create_codec ( Encoding enc )
  90         throws UnknownEncoding
  91     {
  92         if( enc == null ) nullParam();
  93 
  94         Codec result = null;
  95 
  96         // This is the only format we can currently create codecs for:
  97         if( (enc.format == ENCODING_CDR_ENCAPS.value) &&
  98             (enc.major_version == 1) )
  99         {
 100             if( (enc.minor_version >= 0) &&
 101                 (enc.minor_version <= MAX_MINOR_VERSION_SUPPORTED) )
 102             {
 103                 result = codecs[enc.minor_version];
 104             }
 105         }
 106 
 107         if( result == null ) {
 108             throw new UnknownEncoding();
 109         }
 110 
 111         return result;
 112     }
 113 
 114     /**
 115      * Called when an invalid null parameter was passed.  Throws a
 116      * BAD_PARAM with a minor code of 1
 117      */
 118     private void nullParam()
 119     {
 120         throw wrapper.nullParam() ;
 121     }
 122 }