1 package org.jacorb.poa.util; 2 3 22 23 import java.util.*; 24 import org.jacorb.poa.POAConstants; 25 import org.jacorb.poa.except.POAInternalError; 26 import org.omg.PortableServer.*; 27 28 34 35 public final class POAUtil 36 { 37 static private final int bytesPerLine = 20; 38 private static final char[] lookup = 39 new char[]{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 40 41 private POAUtil () {} 42 43 52 public static String convert( byte[] data ) 53 { 54 StringBuffer result = new StringBuffer (); 55 result.append ('\n'); 56 int k = 0; 57 58 for (int j = 0; j < data.length; j++) 59 { 60 result.append( toHex(data[j])); 61 62 boolean lastLine = (j >= (data.length - 1)); 63 64 if (lastLine) 65 { 66 for (int p = 0; 67 p < (bytesPerLine - (j % bytesPerLine) - 1); 68 p++) 69 { 70 result.append (" "); 71 } 72 } 73 74 if (((j % bytesPerLine) == (bytesPerLine - 1)) || lastLine) 75 { 76 for (; k <= j; k++) 77 { 78 result.append ((data[k] < 32) ? '.' : (char) data[k]); 79 } 80 81 result.append ('\n'); 82 } 83 } 84 return result.toString (); 85 } 86 87 91 92 public static String convert(org.omg.CORBA.Policy policy, int policy_type) 93 { 94 switch (policy_type) 95 { 96 case THREAD_POLICY_ID.value: 97 if (policy == null || ((ThreadPolicy) policy).value() == ThreadPolicyValue.ORB_CTRL_MODEL) return "ORB_CTRL_MODEL"; 98 else if (((ThreadPolicy) policy).value() == ThreadPolicyValue.SINGLE_THREAD_MODEL) return "SINGLE_THREAD_MODEL"; 99 break; 100 101 case LIFESPAN_POLICY_ID.value: 102 if (policy == null || ((LifespanPolicy) policy).value() == LifespanPolicyValue.TRANSIENT) return "TRANSIENT"; 103 else if (((LifespanPolicy) policy).value() == LifespanPolicyValue.PERSISTENT) return "PERSISTENT"; 104 break; 105 106 case ID_UNIQUENESS_POLICY_ID.value: 107 if (policy == null || ((IdUniquenessPolicy) policy).value() == IdUniquenessPolicyValue.UNIQUE_ID) return "UNIQUE_ID"; 108 else if (((IdUniquenessPolicy) policy).value() == IdUniquenessPolicyValue.MULTIPLE_ID) return "MULTIPLE_ID"; 109 break; 110 111 case ID_ASSIGNMENT_POLICY_ID.value: 112 if (policy == null || ((IdAssignmentPolicy) policy).value() == IdAssignmentPolicyValue.SYSTEM_ID) return "SYSTEM_ID"; 113 else if (((IdAssignmentPolicy) policy).value() == IdAssignmentPolicyValue.USER_ID) return "USER_ID"; 114 break; 115 116 case SERVANT_RETENTION_POLICY_ID.value: 117 if (policy == null || ((ServantRetentionPolicy) policy).value() == ServantRetentionPolicyValue.RETAIN) return "RETAIN"; 118 else if (((ServantRetentionPolicy) policy).value() == ServantRetentionPolicyValue.NON_RETAIN) return "NON_RETAIN"; 119 break; 120 121 case REQUEST_PROCESSING_POLICY_ID.value: 122 if (policy == null || ((RequestProcessingPolicy) policy).value() == RequestProcessingPolicyValue.USE_ACTIVE_OBJECT_MAP_ONLY) return "USE_ACTIVE_OBJECT_MAP_ONLY"; 123 else if (((RequestProcessingPolicy) policy).value() == RequestProcessingPolicyValue.USE_SERVANT_MANAGER) return "USE_SERVANT_MANAGER"; 124 else if (((RequestProcessingPolicy) policy).value() == RequestProcessingPolicyValue.USE_DEFAULT_SERVANT) return "USE_DEFAULT_SERVANT"; 125 break; 126 127 case IMPLICIT_ACTIVATION_POLICY_ID.value: 128 if (policy == null || ((ImplicitActivationPolicy) policy).value() == ImplicitActivationPolicyValue.NO_IMPLICIT_ACTIVATION) return "NO_IMPLICIT_ACTIVATION"; 129 else if (((ImplicitActivationPolicy) policy).value() == ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION) return "IMPLICIT_ACTIVATION"; 130 break; 131 } 132 return "unknown"; 133 } 134 135 138 139 public static String convert(org.omg.PortableServer.POAManagerPackage.State state) 140 { 141 if (state.value() == org.omg.PortableServer.POAManagerPackage.State._ACTIVE) 142 return "active"; 143 if (state.value() == org.omg.PortableServer.POAManagerPackage.State._HOLDING) 144 return "holding"; 145 if (state.value() == org.omg.PortableServer.POAManagerPackage.State._DISCARDING) 146 return "discarding"; 147 if (state.value() == org.omg.PortableServer.POAManagerPackage.State._INACTIVE) 148 return "inactive"; 149 150 return "unknown"; 151 } 152 153 156 157 public static String extractImplName(byte[] object_key) 158 { 159 for (int i = 0; i < object_key.length; i++) 160 { 161 if (object_key[i] == POAConstants.OBJECT_KEY_SEP_BYTE 162 && (i==0 || object_key[i-1] != POAConstants.MASK_BYTE)) 163 { 164 byte[] result = IdUtil.extract(object_key, 0, i); 165 return unmaskStr( new String (result) ); 166 } 167 } 168 throw new POAInternalError("error extracting impl name from object_key: "+ 169 convert(object_key)); 170 } 171 172 173 176 177 public static byte[] extractOID(byte[] object_key) 178 { 179 for (int i=object_key.length-1; i>=0; i--) 180 { 181 if (object_key[i] == POAConstants.OBJECT_KEY_SEP_BYTE 182 && (i==0 || object_key[i-1] != POAConstants.MASK_BYTE)) 183 { 184 i++; 185 byte[] result = 186 IdUtil.extract(object_key, i, object_key.length - i); 187 return unmaskId(result); 188 } 189 } 190 throw new POAInternalError("error extracting oid from object_key: "+ 191 convert(object_key)); 192 } 193 194 197 198 public static byte[] extractOID(org.omg.CORBA.Object reference) 199 { 200 return ((org.jacorb.orb.Delegate) ((org.omg.CORBA.portable.ObjectImpl ) reference)._get_delegate()).getObjectId(); 201 } 202 203 206 207 public static String extractPOAName(byte[] object_key) 208 { 209 int begin = object_key.length; 210 int end = 0; 211 for (int i=0; i<object_key.length; i++) 212 { 213 if (object_key[i] == POAConstants.OBJECT_KEY_SEP_BYTE 214 && (i==0 || object_key[i-1] != POAConstants.MASK_BYTE)) 215 { 216 begin = i; 217 break; 218 } 219 } 220 for (int i=object_key.length-1; i>=0; i--) 221 { 222 if (object_key[i] == POAConstants.OBJECT_KEY_SEP_BYTE 223 && (i==0 || object_key[i-1] != POAConstants.MASK_BYTE)) 224 { 225 end = i; 226 break; 227 } 228 } 229 if (begin > end) 230 { 231 throw new POAInternalError("error extracting poa name from object_key: "+convert(object_key)); 232 } 233 if (begin == end) 234 { 235 return ""; 236 } 237 else 238 { 239 begin++; 240 return new String (IdUtil.extract(object_key, begin, end-begin)); 241 } 242 } 243 244 245 254 public static List extractScopedPOANames (String poa_name) 255 { 256 List scopes = new ArrayList(); 257 258 if (poa_name.length () > 0) 259 { 260 int previous = 0, current=0; 262 263 for ( ; current < poa_name.length (); current++) 264 { 265 if (poa_name.charAt (current) == POAConstants.OBJECT_KEY_SEPARATOR) 267 { 268 scopes.add (poa_name.substring (previous, current)); 269 current++; 270 previous = current; 271 } 272 } 273 scopes.add (poa_name.substring (previous, current)); 275 } 276 return scopes; 277 } 278 279 280 283 284 public static org.omg.CORBA.Policy getPolicy(org.omg.CORBA.Policy [] policies, int policy_type) 285 { 286 if (policies != null) 287 { 288 for (int i = 0; i < policies.length; i++) 289 { 290 if (policies[i].policy_type() == policy_type) 291 { 292 return policies[i]; 293 } 294 } 295 } 296 return null; 297 } 298 299 300 public static boolean isActive(org.omg.PortableServer.POAManagerPackage.State state) { 301 return state.value() == org.omg.PortableServer.POAManagerPackage.State._ACTIVE ? true : false; 302 } 303 304 305 public static boolean isDiscarding(org.omg.PortableServer.POAManagerPackage.State state) { 306 return state.value() == org.omg.PortableServer.POAManagerPackage.State._DISCARDING ? true : false; 307 } 308 309 310 public static boolean isHolding(org.omg.PortableServer.POAManagerPackage.State state) { 311 return state.value() == org.omg.PortableServer.POAManagerPackage.State._HOLDING ? true : false; 312 } 313 314 315 public static boolean isInactive(org.omg.PortableServer.POAManagerPackage.State state) { 316 return state.value() == org.omg.PortableServer.POAManagerPackage.State._INACTIVE ? true : false; 317 } 318 319 322 323 public static byte[] maskId(byte[] id) 324 { 325 int altered = id.length; 326 for (int i=0; i<id.length; i++) 327 { 328 if (id[i] == POAConstants.OBJECT_KEY_SEP_BYTE) 329 { 330 altered++; 331 } 332 else if (id[i] == POAConstants.MASK_BYTE) 333 { 334 altered++; 335 } 336 } 337 if (altered == id.length) return id; 338 339 byte[] result = new byte[altered]; 340 341 altered = 0; 342 for (int i=0; i<id.length; i++) { 343 if (id[i] == POAConstants.OBJECT_KEY_SEP_BYTE) { 344 result[altered] = POAConstants.MASK_BYTE; 345 result[altered+1] = POAConstants.SEPA_MASK_BYTE; 346 altered += 2; 347 348 } else if (id[i] == POAConstants.MASK_BYTE) { 349 result[altered] = POAConstants.MASK_BYTE; 350 result[altered+1] = POAConstants.MASK_MASK_BYTE; 351 altered += 2; 352 353 } else { 354 result[altered] = id[i]; 355 altered ++; 356 } 357 } 358 return result; 359 } 360 361 364 365 public static String maskStr(String str) 366 { 367 return new String (maskId(str.getBytes())); 368 } 369 370 373 374 public static String unmaskStr(String str) 375 { 376 return new String (unmaskId(str.getBytes())); 377 } 378 379 380 383 384 public static byte[] unmaskId(byte[] id) 385 { 386 int altered = id.length; 387 for (int i=0; i<id.length; i++) 388 { 389 if (id[i] == POAConstants.MASK_BYTE) 390 { 391 altered--; 392 i++; 393 } 394 } 395 if (altered == id.length) return id; 396 397 byte[] result = new byte[altered]; 398 399 altered = 0; 400 for (int i=0; i<id.length; i++) 401 { 402 if (id[i] == POAConstants.MASK_BYTE) 403 { 404 if (id[i+1] == POAConstants.MASK_MASK_BYTE) 405 { 406 result[altered] = POAConstants.MASK_BYTE; 407 } 408 else if (id[i+1] == POAConstants.SEPA_MASK_BYTE) 409 { 410 result[altered] = POAConstants.OBJECT_KEY_SEP_BYTE; 411 412 } 413 else 414 { 415 throw new POAInternalError("error: forbidden byte sequence \"" 416 +POAConstants.MASK_BYTE+id[i+1]+"\" (unmaskId)"); 417 } 418 i++; 419 420 } 421 else 422 { 423 result[altered] = id[i]; 424 } 425 altered++; 426 } 427 return result; 428 } 429 430 431 432 433 439 440 public static final String toHex(byte b) 441 { 442 StringBuffer sb = new StringBuffer (); 443 444 int upper = (b >> 4) & 0x0F; 445 sb.append( lookup[upper] ); 446 447 int lower = b & 0x0F; 448 sb.append( lookup[lower] ); 449 450 sb.append( ' ' ); 451 452 return sb.toString(); 453 } 454 455 456 457 } 458 | Popular Tags |