001: package org.jacorb.poa.util;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.util.*;
024: import org.jacorb.poa.POAConstants;
025: import org.jacorb.poa.except.POAInternalError;
026: import org.omg.PortableServer.*;
027:
028: /**
029: * This class collects some useful routines for the POA.
030: *
031: * @author Reimo Tiedemann, FU Berlin
032: * @version $Id: POAUtil.java,v 1.20 2006/06/14 12:12:04 alphonse.bendt Exp $
033: */
034:
035: public final class POAUtil {
036: static private final int bytesPerLine = 20;
037: private static final char[] lookup = new char[] { '0', '1', '2',
038: '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
039: 'F' };
040:
041: private POAUtil() {
042: }
043:
044: /**
045: * <code>convert</code> outputs a byte oid in a hex string dump formatted
046: * like e.g.:
047: * 49 6d 52 ImR
048: * ....
049: *
050: * @param data a <code>byte</code> value
051: * @return a <code>String</code> value
052: */
053: public static String convert(byte[] data) {
054: StringBuffer result = new StringBuffer();
055: result.append('\n');
056: int k = 0;
057:
058: for (int j = 0; j < data.length; j++) {
059: result.append(toHex(data[j]));
060:
061: boolean lastLine = (j >= (data.length - 1));
062:
063: if (lastLine) {
064: for (int p = 0; p < (bytesPerLine - (j % bytesPerLine) - 1); p++) {
065: result.append(" ");
066: }
067: }
068:
069: if (((j % bytesPerLine) == (bytesPerLine - 1)) || lastLine) {
070: for (; k <= j; k++) {
071: result
072: .append((data[k] < 32) ? '.'
073: : (char) data[k]);
074: }
075:
076: result.append('\n');
077: }
078: }
079: return result.toString();
080: }
081:
082: /**
083: * reads the policy value from the specified policy and
084: * converts it into a string
085: */
086:
087: public static String convert(org.omg.CORBA.Policy policy,
088: int policy_type) {
089: switch (policy_type) {
090: case THREAD_POLICY_ID.value:
091: if (policy == null
092: || ((ThreadPolicy) policy).value() == ThreadPolicyValue.ORB_CTRL_MODEL)
093: return "ORB_CTRL_MODEL";
094: else if (((ThreadPolicy) policy).value() == ThreadPolicyValue.SINGLE_THREAD_MODEL)
095: return "SINGLE_THREAD_MODEL";
096: break;
097:
098: case LIFESPAN_POLICY_ID.value:
099: if (policy == null
100: || ((LifespanPolicy) policy).value() == LifespanPolicyValue.TRANSIENT)
101: return "TRANSIENT";
102: else if (((LifespanPolicy) policy).value() == LifespanPolicyValue.PERSISTENT)
103: return "PERSISTENT";
104: break;
105:
106: case ID_UNIQUENESS_POLICY_ID.value:
107: if (policy == null
108: || ((IdUniquenessPolicy) policy).value() == IdUniquenessPolicyValue.UNIQUE_ID)
109: return "UNIQUE_ID";
110: else if (((IdUniquenessPolicy) policy).value() == IdUniquenessPolicyValue.MULTIPLE_ID)
111: return "MULTIPLE_ID";
112: break;
113:
114: case ID_ASSIGNMENT_POLICY_ID.value:
115: if (policy == null
116: || ((IdAssignmentPolicy) policy).value() == IdAssignmentPolicyValue.SYSTEM_ID)
117: return "SYSTEM_ID";
118: else if (((IdAssignmentPolicy) policy).value() == IdAssignmentPolicyValue.USER_ID)
119: return "USER_ID";
120: break;
121:
122: case SERVANT_RETENTION_POLICY_ID.value:
123: if (policy == null
124: || ((ServantRetentionPolicy) policy).value() == ServantRetentionPolicyValue.RETAIN)
125: return "RETAIN";
126: else if (((ServantRetentionPolicy) policy).value() == ServantRetentionPolicyValue.NON_RETAIN)
127: return "NON_RETAIN";
128: break;
129:
130: case REQUEST_PROCESSING_POLICY_ID.value:
131: if (policy == null
132: || ((RequestProcessingPolicy) policy).value() == RequestProcessingPolicyValue.USE_ACTIVE_OBJECT_MAP_ONLY)
133: return "USE_ACTIVE_OBJECT_MAP_ONLY";
134: else if (((RequestProcessingPolicy) policy).value() == RequestProcessingPolicyValue.USE_SERVANT_MANAGER)
135: return "USE_SERVANT_MANAGER";
136: else if (((RequestProcessingPolicy) policy).value() == RequestProcessingPolicyValue.USE_DEFAULT_SERVANT)
137: return "USE_DEFAULT_SERVANT";
138: break;
139:
140: case IMPLICIT_ACTIVATION_POLICY_ID.value:
141: if (policy == null
142: || ((ImplicitActivationPolicy) policy).value() == ImplicitActivationPolicyValue.NO_IMPLICIT_ACTIVATION)
143: return "NO_IMPLICIT_ACTIVATION";
144: else if (((ImplicitActivationPolicy) policy).value() == ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION)
145: return "IMPLICIT_ACTIVATION";
146: break;
147: }
148: return "unknown";
149: }
150:
151: /**
152: * converts the state into a string
153: */
154:
155: public static String convert(
156: org.omg.PortableServer.POAManagerPackage.State state) {
157: if (state.value() == org.omg.PortableServer.POAManagerPackage.State._ACTIVE)
158: return "active";
159: if (state.value() == org.omg.PortableServer.POAManagerPackage.State._HOLDING)
160: return "holding";
161: if (state.value() == org.omg.PortableServer.POAManagerPackage.State._DISCARDING)
162: return "discarding";
163: if (state.value() == org.omg.PortableServer.POAManagerPackage.State._INACTIVE)
164: return "inactive";
165:
166: return "unknown";
167: }
168:
169: /**
170: * extracts the impl name from a specified object key
171: */
172:
173: public static String extractImplName(byte[] object_key) {
174: for (int i = 0; i < object_key.length; i++) {
175: if (object_key[i] == POAConstants.OBJECT_KEY_SEP_BYTE
176: && (i == 0 || object_key[i - 1] != POAConstants.MASK_BYTE)) {
177: byte[] result = IdUtil.extract(object_key, 0, i);
178: return unmaskStr(new String(result));
179: }
180: }
181: throw new POAInternalError(
182: "error extracting impl name from object_key: "
183: + convert(object_key));
184: }
185:
186: /**
187: * extracts the oid from a specified object key
188: */
189:
190: public static byte[] extractOID(byte[] object_key) {
191: for (int i = object_key.length - 1; i >= 0; i--) {
192: if (object_key[i] == POAConstants.OBJECT_KEY_SEP_BYTE
193: && (i == 0 || object_key[i - 1] != POAConstants.MASK_BYTE)) {
194: i++;
195: byte[] result = IdUtil.extract(object_key, i,
196: object_key.length - i);
197: return unmaskId(result);
198: }
199: }
200: throw new POAInternalError(
201: "error extracting oid from object_key: "
202: + convert(object_key));
203: }
204:
205: /**
206: * extracts the oid from a specified object reference
207: */
208:
209: public static byte[] extractOID(org.omg.CORBA.Object reference) {
210: return ((org.jacorb.orb.Delegate) ((org.omg.CORBA.portable.ObjectImpl) reference)
211: ._get_delegate()).getObjectId();
212: }
213:
214: /**
215: * extracts the poa name from a specified object key
216: */
217: public static String extractPOAName(byte[] object_key) {
218: int begin = object_key.length;
219: int end = 0;
220: for (int i = 0; i < object_key.length; i++) {
221: if (object_key[i] == POAConstants.OBJECT_KEY_SEP_BYTE
222: && (i == 0 || object_key[i - 1] != POAConstants.MASK_BYTE)) {
223: begin = i;
224: break;
225: }
226: }
227: for (int i = object_key.length - 1; i >= 0; i--) {
228: if (object_key[i] == POAConstants.OBJECT_KEY_SEP_BYTE
229: && (i == 0 || object_key[i - 1] != POAConstants.MASK_BYTE)) {
230: end = i;
231: break;
232: }
233: }
234: if (begin > end) {
235: throw new POAInternalError(
236: "error extracting poa name from object_key: "
237: + convert(object_key));
238: }
239: if (begin == end) {
240: return "";
241: }
242:
243: begin++;
244: return new String(IdUtil
245: .extract(object_key, begin, end - begin));
246: }
247:
248: /**
249: * <code>extractScopedPOANames</code> returns a list containing the
250: * poa_names. This method is faster than using a StringTokenizer.
251: *
252: * @param poa_name is a <code>String</code> value which may contain
253: * poa_names separated by
254: * {@link POAConstants#OBJECT_KEY_SEPARATOR OBJECT_KEY_SEPARATOR}
255: * @return a <code>Vector</code> value
256: */
257: public static List extractScopedPOANames(String poa_name) {
258: List scopes = new ArrayList();
259:
260: if (poa_name.length() > 0) {
261: // Fill in the list with the poa_names.
262: int previous = 0, current = 0;
263:
264: for (; current < poa_name.length(); current++) {
265: // If we've found a separator skip over it and add to the vector
266: if (poa_name.charAt(current) == POAConstants.OBJECT_KEY_SEPARATOR) {
267: scopes.add(poa_name.substring(previous, current));
268: current++;
269: previous = current;
270: }
271: }
272: // Add the final POA name
273: scopes.add(poa_name.substring(previous, current));
274: }
275: return scopes;
276: }
277:
278: /**
279: * returns the policy with the specified policy_type from a policy list
280: */
281:
282: public static org.omg.CORBA.Policy getPolicy(
283: org.omg.CORBA.Policy[] policies, int policy_type) {
284: if (policies != null) {
285: for (int i = 0; i < policies.length; i++) {
286: if (policies[i].policy_type() == policy_type) {
287: return policies[i];
288: }
289: }
290: }
291: return null;
292: }
293:
294: public static boolean isActive(
295: org.omg.PortableServer.POAManagerPackage.State state) {
296: return state.value() == org.omg.PortableServer.POAManagerPackage.State._ACTIVE ? true
297: : false;
298: }
299:
300: public static boolean isDiscarding(
301: org.omg.PortableServer.POAManagerPackage.State state) {
302: return state.value() == org.omg.PortableServer.POAManagerPackage.State._DISCARDING ? true
303: : false;
304: }
305:
306: public static boolean isHolding(
307: org.omg.PortableServer.POAManagerPackage.State state) {
308: return state.value() == org.omg.PortableServer.POAManagerPackage.State._HOLDING ? true
309: : false;
310: }
311:
312: public static boolean isInactive(
313: org.omg.PortableServer.POAManagerPackage.State state) {
314: return state.value() == org.omg.PortableServer.POAManagerPackage.State._INACTIVE ? true
315: : false;
316: }
317:
318: /**
319: * masks the object key separator bytes
320: */
321:
322: public static byte[] maskId(byte[] id) {
323: int altered = id.length;
324: for (int i = 0; i < id.length; i++) {
325: if (id[i] == POAConstants.OBJECT_KEY_SEP_BYTE) {
326: altered++;
327: } else if (id[i] == POAConstants.MASK_BYTE) {
328: altered++;
329: }
330: }
331: if (altered == id.length)
332: return id;
333:
334: byte[] result = new byte[altered];
335:
336: altered = 0;
337: for (int i = 0; i < id.length; i++) {
338: if (id[i] == POAConstants.OBJECT_KEY_SEP_BYTE) {
339: result[altered] = POAConstants.MASK_BYTE;
340: result[altered + 1] = POAConstants.SEPA_MASK_BYTE;
341: altered += 2;
342:
343: } else if (id[i] == POAConstants.MASK_BYTE) {
344: result[altered] = POAConstants.MASK_BYTE;
345: result[altered + 1] = POAConstants.MASK_MASK_BYTE;
346: altered += 2;
347:
348: } else {
349: result[altered] = id[i];
350: altered++;
351: }
352: }
353: return result;
354: }
355:
356: /**
357: * masks the object key separator chars
358: */
359:
360: public static String maskStr(String str) {
361: return new String(maskId(str.getBytes()));
362: }
363:
364: /**
365: * unmasks the object key separator chars
366: */
367:
368: public static String unmaskStr(String str) {
369: return new String(unmaskId(str.getBytes()));
370: }
371:
372: /**
373: * unmasks the object key separator bytes
374: */
375:
376: public static byte[] unmaskId(byte[] id) {
377: int altered = id.length;
378: for (int i = 0; i < id.length; i++) {
379: if (id[i] == POAConstants.MASK_BYTE) {
380: altered--;
381: i++;
382: }
383: }
384: if (altered == id.length)
385: return id;
386:
387: byte[] result = new byte[altered];
388:
389: altered = 0;
390: for (int i = 0; i < id.length; i++) {
391: if (id[i] == POAConstants.MASK_BYTE) {
392: if (id[i + 1] == POAConstants.MASK_MASK_BYTE) {
393: result[altered] = POAConstants.MASK_BYTE;
394: } else if (id[i + 1] == POAConstants.SEPA_MASK_BYTE) {
395: result[altered] = POAConstants.OBJECT_KEY_SEP_BYTE;
396:
397: } else {
398: throw new POAInternalError(
399: "error: forbidden byte sequence \""
400: + POAConstants.MASK_BYTE
401: + id[i + 1] + "\" (unmaskId)");
402: }
403: i++;
404:
405: } else {
406: result[altered] = id[i];
407: }
408: altered++;
409: }
410: return result;
411: }
412:
413: /**
414: * <code>toHex</code> converts a byte into a readable string.
415: *
416: * @param b a <code>byte</code> value
417: * @return a <code>String</code> value
418: */
419:
420: public static final String toHex(byte b) {
421: StringBuffer sb = new StringBuffer();
422:
423: int upper = (b >> 4) & 0x0F;
424: sb.append(lookup[upper]);
425:
426: int lower = b & 0x0F;
427: sb.append(lookup[lower]);
428:
429: sb.append(' ');
430:
431: return sb.toString();
432: }
433:
434: }
|