1. /*
  2. * @(#)CodecFactoryImpl.java 1.13 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.Interceptors;
  8. import org.omg.IOP.Codec;
  9. import org.omg.IOP.CodecFactory;
  10. import org.omg.IOP.CodecFactoryPackage.UnknownEncoding;
  11. import org.omg.IOP.Encoding;
  12. import org.omg.IOP.ENCODING_CDR_ENCAPS;
  13. import com.sun.corba.se.internal.orbutil.MinorCodes;
  14. import org.omg.CORBA.BAD_PARAM;
  15. import org.omg.CORBA.CompletionStatus;
  16. import org.omg.CORBA.ORB;
  17. import org.omg.CORBA.LocalObject;
  18. /**
  19. * CodecFactoryImpl is the implementation of the Codec Factory, as described
  20. * in orbos/99-12-02.
  21. */
  22. public final class CodecFactoryImpl
  23. extends org.omg.CORBA.LocalObject
  24. implements CodecFactory
  25. {
  26. // The ORB that created this Codec Factory
  27. private ORB orb;
  28. // The maximum minor version of GIOP supported by this codec factory.
  29. // Currently, this is 1.2.
  30. private static final int MAX_MINOR_VERSION_SUPPORTED = 2;
  31. // The pre-created minor versions of Codec version 1.0, 1.1, ...,
  32. // 1.(MAX_MINOR_VERSION_SUPPORTED)
  33. private Codec codecs[] = new Codec[MAX_MINOR_VERSION_SUPPORTED + 1];
  34. /**
  35. * Creates a new CodecFactory implementation. Stores the ORB that
  36. * created this factory, for later use by the Codec.
  37. */
  38. public CodecFactoryImpl( ORB orb ) {
  39. this.orb = orb;
  40. // Precreate a codec for version 1.0 through
  41. // 1.(MAX_MINOR_VERSION_SUPPORTED). This can be
  42. // done since Codecs are immutable in their current implementation.
  43. // This is an optimization that eliminates the overhead of creating
  44. // a new Codec each time create_codec is called.
  45. for( int minor = 0; minor <= MAX_MINOR_VERSION_SUPPORTED; minor++ ) {
  46. codecs[minor] = new CDREncapsCodec( orb, 1, minor );
  47. }
  48. }
  49. /**
  50. * Creates a codec of the given encoding. The only format recognized
  51. * by this factory is ENCODING_CDR_ENCAPS, versions 1.0 through
  52. * 1.(MAX_MINOR_VERSION_SUPPORTED).
  53. *
  54. * @exception UnknownEncoding Thrown if this factory cannot create a
  55. * Codec of the given encoding.
  56. */
  57. public Codec create_codec ( Encoding enc )
  58. throws UnknownEncoding
  59. {
  60. if( enc == null ) nullParam();
  61. Codec result = null;
  62. // This is the only format we can currently create codecs for:
  63. if( (enc.format == ENCODING_CDR_ENCAPS.value) &&
  64. (enc.major_version == 1) )
  65. {
  66. if( (enc.minor_version >= 0) &&
  67. (enc.minor_version <= MAX_MINOR_VERSION_SUPPORTED) )
  68. {
  69. result = codecs[enc.minor_version];
  70. }
  71. }
  72. if( result == null ) {
  73. throw new UnknownEncoding();
  74. }
  75. return result;
  76. }
  77. /**
  78. * Called when an invalid null parameter was passed. Throws a
  79. * BAD_PARAM with a minor code of 1
  80. */
  81. private void nullParam()
  82. throws BAD_PARAM
  83. {
  84. throw new BAD_PARAM( MinorCodes.NULL_PARAM,
  85. CompletionStatus.COMPLETED_NO );
  86. }
  87. }