KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > spi > servicecontext > ServiceContextData


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
8 package com.sun.corba.se.spi.servicecontext;
9
10 import org.omg.CORBA.BAD_PARAM JavaDoc ;
11 import org.omg.CORBA_2_3.portable.InputStream JavaDoc ;
12 import com.sun.corba.se.spi.servicecontext.ServiceContext ;
13 import java.lang.reflect.InvocationTargetException JavaDoc ;
14 import java.lang.reflect.Modifier JavaDoc ;
15 import java.lang.reflect.Field JavaDoc ;
16 import java.lang.reflect.Constructor JavaDoc ;
17 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
18 import com.sun.corba.se.spi.orb.ORB ;
19 import com.sun.corba.se.impl.orbutil.ORBUtility ;
20
21 /** Internal class used to hold data about a service context class.
22 */

23 public class ServiceContextData {
24     private void dprint( String JavaDoc msg )
25     {
26     ORBUtility.dprint( this, msg ) ;
27     }
28     
29     private void throwBadParam( String JavaDoc msg, Throwable JavaDoc exc )
30     {
31     BAD_PARAM JavaDoc error = new BAD_PARAM JavaDoc( msg ) ;
32     if (exc != null)
33         error.initCause( exc ) ;
34     throw error ;
35     }
36
37     public ServiceContextData( Class JavaDoc cls )
38     {
39     if (ORB.ORBInitDebug)
40         dprint( "ServiceContextData constructor called for class " + cls ) ;
41
42     scClass = cls ;
43
44     try {
45         if (ORB.ORBInitDebug)
46         dprint( "Finding constructor for " + cls ) ;
47
48         // Find the appropriate constructor in cls
49
Class JavaDoc[] args = new Class JavaDoc[2] ;
50         args[0] = InputStream JavaDoc.class ;
51         args[1] = GIOPVersion.class;
52         try {
53         scConstructor = cls.getConstructor( args ) ;
54         } catch (NoSuchMethodException JavaDoc nsme) {
55         throwBadParam( "Class does not have an InputStream constructor", nsme ) ;
56         }
57
58         if (ORB.ORBInitDebug)
59         dprint( "Finding SERVICE_CONTEXT_ID field in " + cls ) ;
60
61         // get the ID from the public static final int SERVICE_CONTEXT_ID
62
Field JavaDoc fld = null ;
63         try {
64         fld = cls.getField( "SERVICE_CONTEXT_ID" ) ;
65         } catch (NoSuchFieldException JavaDoc nsfe) {
66         throwBadParam( "Class does not have a SERVICE_CONTEXT_ID member", nsfe ) ;
67         } catch (SecurityException JavaDoc se) {
68         throwBadParam( "Could not access SERVICE_CONTEXT_ID member", se ) ;
69         }
70
71         if (ORB.ORBInitDebug)
72         dprint( "Checking modifiers of SERVICE_CONTEXT_ID field in " + cls ) ;
73
74         int mod = fld.getModifiers() ;
75         if (!Modifier.isPublic(mod) || !Modifier.isStatic(mod) ||
76         !Modifier.isFinal(mod) )
77         throwBadParam( "SERVICE_CONTEXT_ID field is not public static final", null ) ;
78
79         if (ORB.ORBInitDebug)
80         dprint( "Getting value of SERVICE_CONTEXT_ID in " + cls ) ;
81
82         try {
83         scId = fld.getInt( null ) ;
84         } catch (IllegalArgumentException JavaDoc iae) {
85         throwBadParam( "SERVICE_CONTEXT_ID not convertible to int", iae ) ;
86         } catch (IllegalAccessException JavaDoc iae2) {
87         throwBadParam( "Could not access value of SERVICE_CONTEXT_ID", iae2 ) ;
88         }
89     } catch (BAD_PARAM JavaDoc nssc) {
90         if (ORB.ORBInitDebug)
91         dprint( "Exception in ServiceContextData constructor: " + nssc ) ;
92         throw nssc ;
93     } catch (Throwable JavaDoc thr) {
94         if (ORB.ORBInitDebug)
95         dprint( "Unexpected Exception in ServiceContextData constructor: " +
96             thr ) ;
97     }
98
99     if (ORB.ORBInitDebug)
100         dprint( "ServiceContextData constructor completed" ) ;
101     }
102
103     /** Factory method used to create a ServiceContext object by
104      * unmarshalling it from the InputStream.
105      */

106     public ServiceContext makeServiceContext(InputStream JavaDoc is, GIOPVersion gv)
107     {
108     Object JavaDoc[] args = new Object JavaDoc[2];
109     args[0] = is ;
110     args[1] = gv;
111     ServiceContext sc = null ;
112
113     try {
114         sc = (ServiceContext)(scConstructor.newInstance( args )) ;
115     } catch (IllegalArgumentException JavaDoc iae) {
116         throwBadParam( "InputStream constructor argument error", iae ) ;
117     } catch (IllegalAccessException JavaDoc iae2) {
118         throwBadParam( "InputStream constructor argument error", iae2 ) ;
119     } catch (InstantiationException JavaDoc ie) {
120         throwBadParam( "InputStream constructor called for abstract class", ie ) ;
121     } catch (InvocationTargetException JavaDoc ite) {
122         throwBadParam( "InputStream constructor threw exception " +
123         ite.getTargetException(), ite ) ;
124     }
125        
126     return sc ;
127     }
128
129     int getId()
130     {
131     return scId ;
132     }
133
134     public String JavaDoc toString()
135     {
136     return "ServiceContextData[ scClass=" + scClass + " scConstructor=" +
137         scConstructor + " scId=" + scId + " ]" ;
138     }
139
140     private Class JavaDoc scClass ;
141     private Constructor JavaDoc scConstructor ;
142     private int scId ;
143 }
144
145
Popular Tags