1 7 8 package com.sun.corba.se.spi.servicecontext; 9 10 import java.lang.reflect.InvocationTargetException ; 11 import java.lang.reflect.Modifier ; 12 import java.lang.reflect.Field ; 13 import java.lang.reflect.Constructor ; 14 import java.util.*; 15 16 import org.omg.CORBA.OctetSeqHelper ; 17 import org.omg.CORBA.SystemException ; 18 import org.omg.CORBA.INTERNAL ; 19 import org.omg.CORBA.CompletionStatus ; 20 import org.omg.CORBA_2_3.portable.OutputStream ; 21 import org.omg.CORBA_2_3.portable.InputStream ; 22 23 import com.sun.org.omg.SendingContext.CodeBase; 24 25 import com.sun.corba.se.spi.ior.iiop.GIOPVersion; 26 27 import com.sun.corba.se.spi.orb.ORB ; 28 29 import com.sun.corba.se.spi.logging.CORBALogDomains; 30 31 32 import com.sun.corba.se.spi.servicecontext.ServiceContext ; 33 import com.sun.corba.se.spi.servicecontext.ServiceContextRegistry ; 34 import com.sun.corba.se.spi.servicecontext.ServiceContextData ; 35 import com.sun.corba.se.spi.servicecontext.UnknownServiceContext ; 36 37 import com.sun.corba.se.impl.encoding.CDRInputStream; 38 import com.sun.corba.se.impl.encoding.EncapsInputStream ; 39 import com.sun.corba.se.impl.orbutil.ORBUtility ; 40 import com.sun.corba.se.impl.util.Utility ; 41 import com.sun.corba.se.impl.logging.ORBUtilSystemException ; 42 43 public class ServiceContexts { 44 private static boolean isDebugging( OutputStream os ) 45 { 46 ORB orb = (ORB)(os.orb()) ; 47 if (orb==null) 48 return false ; 49 return orb.serviceContextDebugFlag ; 50 } 51 52 private static boolean isDebugging( InputStream is ) 53 { 54 ORB orb = (ORB)(is.orb()) ; 55 if (orb==null) 56 return false ; 57 return orb.serviceContextDebugFlag ; 58 } 59 60 private void dprint( String msg ) 61 { 62 ORBUtility.dprint( this, msg ) ; 63 } 64 65 public static void writeNullServiceContext( OutputStream os ) 66 { 67 if (isDebugging(os)) 68 ORBUtility.dprint( "ServiceContexts", "Writing null service context" ) ; 69 os.write_long( 0 ) ; 70 } 71 72 82 private void createMapFromInputStream(InputStream is) 83 { 84 orb = (ORB)(is.orb()) ; 85 if (orb.serviceContextDebugFlag) 86 dprint( "Constructing ServiceContexts from input stream" ) ; 87 88 int numValid = is.read_long() ; 89 90 if (orb.serviceContextDebugFlag) 91 dprint("Number of service contexts = " + numValid); 92 93 for (int ctr = 0; ctr < numValid; ctr++) { 94 int scId = is.read_long(); 95 96 if (orb.serviceContextDebugFlag) 97 dprint("Reading service context id " + scId); 98 99 byte[] data = OctetSeqHelper.read(is); 100 101 if (orb.serviceContextDebugFlag) 102 dprint("Service context" + scId + " length: " + data.length); 103 104 scMap.put(new Integer (scId), data); 105 } 106 } 107 108 public ServiceContexts( ORB orb ) 109 { 110 this.orb = orb ; 111 wrapper = ORBUtilSystemException.get( orb, 112 CORBALogDomains.RPC_PROTOCOL ) ; 113 114 addAlignmentOnWrite = false ; 115 116 scMap = new HashMap(); 117 118 giopVersion = orb.getORBData().getGIOPVersion(); 122 codeBase = null ; 123 } 124 125 128 public ServiceContexts(InputStream s) 129 { 130 this( (ORB)(s.orb()) ) ; 131 132 codeBase = ((CDRInputStream)s).getCodeBase(); 137 138 createMapFromInputStream(s); 139 140 giopVersion = ((CDRInputStream)s).getGIOPVersion(); 142 } 143 144 148 private ServiceContext unmarshal(Integer scId, byte[] data) { 149 150 ServiceContextRegistry scr = orb.getServiceContextRegistry(); 151 152 ServiceContextData scd = scr.findServiceContextData(scId.intValue()); 153 ServiceContext sc = null; 154 155 if (scd == null) { 156 if (orb.serviceContextDebugFlag) { 157 dprint("Could not find ServiceContextData for " 158 + scId 159 + " using UnknownServiceContext"); 160 } 161 162 sc = new UnknownServiceContext(scId.intValue(), data); 163 164 } else { 165 166 if (orb.serviceContextDebugFlag) { 167 dprint("Found " + scd); 168 } 169 170 EncapsInputStream eis 183 = new EncapsInputStream(orb, 184 data, 185 data.length, 186 giopVersion, 187 codeBase); 188 eis.consumeEndian(); 189 190 sc = scd.makeServiceContext(eis, giopVersion); 196 if (sc == null) 197 throw wrapper.svcctxUnmarshalError( 198 CompletionStatus.COMPLETED_MAYBE); 199 } 200 201 return sc; 202 } 203 204 public void addAlignmentPadding() 205 { 206 addAlignmentOnWrite = true ; 212 } 213 214 218 private static final int JAVAIDL_ALIGN_SERVICE_ID = 0xbe1345cd ; 219 220 226 public void write(OutputStream os, GIOPVersion gv) 227 { 228 if (isDebugging(os)) { 229 dprint( "Writing service contexts to output stream" ) ; 230 Utility.printStackTrace() ; 231 } 232 233 int numsc = scMap.size(); 234 235 if (addAlignmentOnWrite) { 236 if (isDebugging(os)) 237 dprint( "Adding alignment padding" ) ; 238 239 numsc++ ; 240 } 241 242 if (isDebugging(os)) 243 dprint( "Service context has " + numsc + " components" ) ; 244 245 os.write_long( numsc ) ; 246 247 writeServiceContextsInOrder(os, gv); 248 249 if (addAlignmentOnWrite) { 250 if (isDebugging(os)) 251 dprint( "Writing alignment padding" ) ; 252 253 os.write_long( JAVAIDL_ALIGN_SERVICE_ID ) ; 254 os.write_long( 4 ) ; 255 os.write_octet( (byte)0 ) ; 256 os.write_octet( (byte)0 ) ; 257 os.write_octet( (byte)0 ) ; 258 os.write_octet( (byte)0 ) ; 259 } 260 261 if (isDebugging(os)) 262 dprint( "Service context writing complete" ) ; 263 } 264 265 270 private void writeServiceContextsInOrder(OutputStream os, GIOPVersion gv) { 271 272 Integer ueInfoId 274 = new Integer (UEInfoServiceContext.SERVICE_CONTEXT_ID); 275 276 Object unknownExceptionInfo = scMap.remove(ueInfoId); 277 278 Iterator iter = scMap.keySet().iterator(); 279 280 while (iter.hasNext()) { 281 Integer id = (Integer )iter.next(); 282 283 writeMapEntry(os, id, scMap.get(id), gv); 284 } 285 286 if (unknownExceptionInfo != null) { 290 writeMapEntry(os, ueInfoId, unknownExceptionInfo, gv); 291 292 scMap.put(ueInfoId, unknownExceptionInfo); 293 } 294 } 295 296 301 private void writeMapEntry(OutputStream os, Integer id, Object scObj, GIOPVersion gv) { 302 303 307 if (scObj instanceof byte[]) { 308 if (isDebugging(os)) 309 dprint( "Writing service context bytes for id " + id); 310 311 OctetSeqHelper.write(os, (byte[])scObj); 312 313 } else { 314 315 ServiceContext sc = (ServiceContext)scObj; 318 319 if (isDebugging(os)) 320 dprint( "Writing service context " + sc ) ; 321 322 sc.write(os, gv); 323 } 324 } 325 326 329 public void put( ServiceContext sc ) 330 { 331 Integer id = new Integer (sc.getId()); 332 scMap.put(id, sc); 333 } 334 335 public void delete( int scId ) { 336 this.delete(new Integer (scId)); 337 } 338 339 public void delete(Integer id) 340 { 341 scMap.remove(id) ; 342 } 343 344 public ServiceContext get(int scId) { 345 return this.get(new Integer (scId)); 346 } 347 348 public ServiceContext get(Integer id) 349 { 350 Object result = scMap.get(id); 351 if (result == null) 352 return null ; 353 354 if (result instanceof byte[]) { 356 357 ServiceContext sc = unmarshal(id, (byte[])result); 358 359 scMap.put(id, sc); 360 361 return sc; 362 } else { 363 return (ServiceContext)result; 364 } 365 } 366 367 private ORB orb ; 368 369 383 private Map scMap; 384 385 389 private boolean addAlignmentOnWrite ; 390 391 private CodeBase codeBase; 392 private GIOPVersion giopVersion; 393 private ORBUtilSystemException wrapper ; 394 } 395 | Popular Tags |