1. /*
  2. * @(#)ServiceContextData.java 1.15 04/06/21
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.spi.servicecontext;
  8. import org.omg.CORBA.BAD_PARAM ;
  9. import org.omg.CORBA_2_3.portable.InputStream ;
  10. import com.sun.corba.se.spi.servicecontext.ServiceContext ;
  11. import java.lang.reflect.InvocationTargetException ;
  12. import java.lang.reflect.Modifier ;
  13. import java.lang.reflect.Field ;
  14. import java.lang.reflect.Constructor ;
  15. import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
  16. import com.sun.corba.se.spi.orb.ORB ;
  17. import com.sun.corba.se.impl.orbutil.ORBUtility ;
  18. /** Internal class used to hold data about a service context class.
  19. */
  20. public class ServiceContextData {
  21. private void dprint( String msg )
  22. {
  23. ORBUtility.dprint( this, msg ) ;
  24. }
  25. private void throwBadParam( String msg, Throwable exc )
  26. {
  27. BAD_PARAM error = new BAD_PARAM( msg ) ;
  28. if (exc != null)
  29. error.initCause( exc ) ;
  30. throw error ;
  31. }
  32. public ServiceContextData( Class cls )
  33. {
  34. if (ORB.ORBInitDebug)
  35. dprint( "ServiceContextData constructor called for class " + cls ) ;
  36. scClass = cls ;
  37. try {
  38. if (ORB.ORBInitDebug)
  39. dprint( "Finding constructor for " + cls ) ;
  40. // Find the appropriate constructor in cls
  41. Class[] args = new Class[2] ;
  42. args[0] = InputStream.class ;
  43. args[1] = GIOPVersion.class;
  44. try {
  45. scConstructor = cls.getConstructor( args ) ;
  46. } catch (NoSuchMethodException nsme) {
  47. throwBadParam( "Class does not have an InputStream constructor", nsme ) ;
  48. }
  49. if (ORB.ORBInitDebug)
  50. dprint( "Finding SERVICE_CONTEXT_ID field in " + cls ) ;
  51. // get the ID from the public static final int SERVICE_CONTEXT_ID
  52. Field fld = null ;
  53. try {
  54. fld = cls.getField( "SERVICE_CONTEXT_ID" ) ;
  55. } catch (NoSuchFieldException nsfe) {
  56. throwBadParam( "Class does not have a SERVICE_CONTEXT_ID member", nsfe ) ;
  57. } catch (SecurityException se) {
  58. throwBadParam( "Could not access SERVICE_CONTEXT_ID member", se ) ;
  59. }
  60. if (ORB.ORBInitDebug)
  61. dprint( "Checking modifiers of SERVICE_CONTEXT_ID field in " + cls ) ;
  62. int mod = fld.getModifiers() ;
  63. if (!Modifier.isPublic(mod) || !Modifier.isStatic(mod) ||
  64. !Modifier.isFinal(mod) )
  65. throwBadParam( "SERVICE_CONTEXT_ID field is not public static final", null ) ;
  66. if (ORB.ORBInitDebug)
  67. dprint( "Getting value of SERVICE_CONTEXT_ID in " + cls ) ;
  68. try {
  69. scId = fld.getInt( null ) ;
  70. } catch (IllegalArgumentException iae) {
  71. throwBadParam( "SERVICE_CONTEXT_ID not convertible to int", iae ) ;
  72. } catch (IllegalAccessException iae2) {
  73. throwBadParam( "Could not access value of SERVICE_CONTEXT_ID", iae2 ) ;
  74. }
  75. } catch (BAD_PARAM nssc) {
  76. if (ORB.ORBInitDebug)
  77. dprint( "Exception in ServiceContextData constructor: " + nssc ) ;
  78. throw nssc ;
  79. } catch (Throwable thr) {
  80. if (ORB.ORBInitDebug)
  81. dprint( "Unexpected Exception in ServiceContextData constructor: " +
  82. thr ) ;
  83. }
  84. if (ORB.ORBInitDebug)
  85. dprint( "ServiceContextData constructor completed" ) ;
  86. }
  87. /** Factory method used to create a ServiceContext object by
  88. * unmarshalling it from the InputStream.
  89. */
  90. public ServiceContext makeServiceContext(InputStream is, GIOPVersion gv)
  91. {
  92. Object[] args = new Object[2];
  93. args[0] = is ;
  94. args[1] = gv;
  95. ServiceContext sc = null ;
  96. try {
  97. sc = (ServiceContext)(scConstructor.newInstance( args )) ;
  98. } catch (IllegalArgumentException iae) {
  99. throwBadParam( "InputStream constructor argument error", iae ) ;
  100. } catch (IllegalAccessException iae2) {
  101. throwBadParam( "InputStream constructor argument error", iae2 ) ;
  102. } catch (InstantiationException ie) {
  103. throwBadParam( "InputStream constructor called for abstract class", ie ) ;
  104. } catch (InvocationTargetException ite) {
  105. throwBadParam( "InputStream constructor threw exception " +
  106. ite.getTargetException(), ite ) ;
  107. }
  108. return sc ;
  109. }
  110. int getId()
  111. {
  112. return scId ;
  113. }
  114. public String toString()
  115. {
  116. return "ServiceContextData[ scClass=" + scClass + " scConstructor=" +
  117. scConstructor + " scId=" + scId + " ]" ;
  118. }
  119. private Class scClass ;
  120. private Constructor scConstructor ;
  121. private int scId ;
  122. }