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