1 /* 2 * Copyright (c) 1999, 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.spi.servicecontext; 27 28 import org.omg.CORBA.BAD_PARAM ; 29 import org.omg.CORBA_2_3.portable.InputStream ; 30 import com.sun.corba.se.spi.servicecontext.ServiceContext ; 31 import java.lang.reflect.InvocationTargetException ; 32 import java.lang.reflect.Modifier ; 33 import java.lang.reflect.Field ; 34 import java.lang.reflect.Constructor ; 35 import com.sun.corba.se.spi.ior.iiop.GIOPVersion; 36 import com.sun.corba.se.spi.orb.ORB ; 37 import com.sun.corba.se.impl.orbutil.ORBUtility ; 38 39 /** Internal class used to hold data about a service context class. 40 */ 41 public class ServiceContextData { 42 private void dprint( String msg ) 43 { 44 ORBUtility.dprint( this, msg ) ; 45 } 46 47 private void throwBadParam( String msg, Throwable exc ) 48 { 49 BAD_PARAM error = new BAD_PARAM( msg ) ; 50 if (exc != null) 51 error.initCause( exc ) ; 52 throw error ; 53 } 54 55 public ServiceContextData( Class cls ) 56 { 57 if (ORB.ORBInitDebug) 58 dprint( "ServiceContextData constructor called for class " + cls ) ; 59 60 scClass = cls ; 61 62 try { 63 if (ORB.ORBInitDebug) 64 dprint( "Finding constructor for " + cls ) ; 65 66 // Find the appropriate constructor in cls 67 Class[] args = new Class[2] ; 68 args[0] = InputStream.class ; 69 args[1] = GIOPVersion.class; 70 try { 71 scConstructor = cls.getConstructor( args ) ; 72 } catch (NoSuchMethodException nsme) { 73 throwBadParam( "Class does not have an InputStream constructor", nsme ) ; 74 } 75 76 if (ORB.ORBInitDebug) 77 dprint( "Finding SERVICE_CONTEXT_ID field in " + cls ) ; 78 79 // get the ID from the public static final int SERVICE_CONTEXT_ID 80 Field fld = null ; 81 try { 82 fld = cls.getField( "SERVICE_CONTEXT_ID" ) ; 83 } catch (NoSuchFieldException nsfe) { 84 throwBadParam( "Class does not have a SERVICE_CONTEXT_ID member", nsfe ) ; 85 } catch (SecurityException se) { 86 throwBadParam( "Could not access SERVICE_CONTEXT_ID member", se ) ; 87 } 88 89 if (ORB.ORBInitDebug) 90 dprint( "Checking modifiers of SERVICE_CONTEXT_ID field in " + cls ) ; 91 92 int mod = fld.getModifiers() ; 93 if (!Modifier.isPublic(mod) || !Modifier.isStatic(mod) || 94 !Modifier.isFinal(mod) ) 95 throwBadParam( "SERVICE_CONTEXT_ID field is not public static final", null ) ; 96 97 if (ORB.ORBInitDebug) 98 dprint( "Getting value of SERVICE_CONTEXT_ID in " + cls ) ; 99 100 try { 101 scId = fld.getInt( null ) ; 102 } catch (IllegalArgumentException iae) { 103 throwBadParam( "SERVICE_CONTEXT_ID not convertible to int", iae ) ; 104 } catch (IllegalAccessException iae2) { 105 throwBadParam( "Could not access value of SERVICE_CONTEXT_ID", iae2 ) ; 106 } 107 } catch (BAD_PARAM nssc) { 108 if (ORB.ORBInitDebug) 109 dprint( "Exception in ServiceContextData constructor: " + nssc ) ; 110 throw nssc ; 111 } catch (Throwable thr) { 112 if (ORB.ORBInitDebug) 113 dprint( "Unexpected Exception in ServiceContextData constructor: " + 114 thr ) ; 115 } 116 117 if (ORB.ORBInitDebug) 118 dprint( "ServiceContextData constructor completed" ) ; 119 } 120 121 /** Factory method used to create a ServiceContext object by 122 * unmarshalling it from the InputStream. 123 */ 124 public ServiceContext makeServiceContext(InputStream is, GIOPVersion gv) 125 { 126 Object[] args = new Object[2]; 127 args[0] = is ; 128 args[1] = gv; 129 ServiceContext sc = null ; 130 131 try { 132 sc = (ServiceContext)(scConstructor.newInstance( args )) ; 133 } catch (IllegalArgumentException iae) { 134 throwBadParam( "InputStream constructor argument error", iae ) ; 135 } catch (IllegalAccessException iae2) { 136 throwBadParam( "InputStream constructor argument error", iae2 ) ; 137 } catch (InstantiationException ie) { 138 throwBadParam( "InputStream constructor called for abstract class", ie ) ; 139 } catch (InvocationTargetException ite) { 140 throwBadParam( "InputStream constructor threw exception " + 141 ite.getTargetException(), ite ) ; 142 } 143 144 return sc ; 145 } 146 147 int getId() 148 { 149 return scId ; 150 } 151 152 public String toString() 153 { 154 return "ServiceContextData[ scClass=" + scClass + " scConstructor=" + 155 scConstructor + " scId=" + scId + " ]" ; 156 } 157 158 private Class scClass ; 159 private Constructor scConstructor ; 160 private int scId ; 161 }