KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > poa > util > POAUtil


1 package org.jacorb.poa.util;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

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 /**
29  * This class collects some useful routines for the POA.
30  *
31  * @author Reimo Tiedemann, FU Berlin
32  * @version $Id: POAUtil.java,v 1.19 2005/05/12 19:21:38 andre.spiegel Exp $
33  */

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     /**
44      * <code>convert</code> outputs a byte oid in a hex string dump formatted
45      * like e.g.:
46      * 49 6d 52 ImR
47      * ....
48      *
49      * @param data[] a <code>byte</code> value
50      * @return a <code>String</code> value
51      */

52     public static String JavaDoc convert( byte[] data )
53     {
54         StringBuffer JavaDoc result = new StringBuffer JavaDoc ();
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     /**
88      * reads the policy value from the specified policy and
89      * converts it into a string
90      */

91
92     public static String JavaDoc convert(org.omg.CORBA.Policy JavaDoc 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     /**
136      * converts the state into a string
137      */

138
139     public static String JavaDoc convert(org.omg.PortableServer.POAManagerPackage.State JavaDoc 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     /**
154      * extracts the impl name from a specified object key
155      */

156
157     public static String JavaDoc 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 JavaDoc(result) );
166             }
167         }
168         throw new POAInternalError("error extracting impl name from object_key: "+
169                                    convert(object_key));
170     }
171
172
173     /**
174      * extracts the oid from a specified object key
175      */

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     /**
195      * extracts the oid from a specified object reference
196      */

197
198     public static byte[] extractOID(org.omg.CORBA.Object JavaDoc reference)
199     {
200         return ((org.jacorb.orb.Delegate) ((org.omg.CORBA.portable.ObjectImpl JavaDoc) reference)._get_delegate()).getObjectId();
201     }
202
203     /**
204      * extracts the poa name from a specified object key
205      */

206
207     public static String JavaDoc 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 JavaDoc(IdUtil.extract(object_key, begin, end-begin));
241         }
242     }
243
244
245     /**
246      * <code>extractScopedPOANames</code> returns a list containing the
247      * poa_names. This method is faster than using a StringTokenizer.
248      *
249      * @param poa_name is a <code>String</code> value which may contain
250      * poa_names separated by
251      * {@link POAConstants#OBJECT_KEY_SEPARATOR OBJECT_KEY_SEPARATOR}
252      * @return a <code>Vector</code> value
253      */

254     public static List extractScopedPOANames (String JavaDoc poa_name)
255     {
256         List scopes = new ArrayList();
257
258         if (poa_name.length () > 0)
259         {
260             // Fill in the list with the poa_names.
261
int previous = 0, current=0;
262
263             for ( ; current < poa_name.length (); current++)
264             {
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                 {
268                     scopes.add (poa_name.substring (previous, current));
269                     current++;
270                     previous = current;
271                 }
272             }
273             // Add the final POA name
274
scopes.add (poa_name.substring (previous, current));
275         }
276         return scopes;
277     }
278
279
280     /**
281      * returns the policy with the specified policy_type from a policy list
282      */

283
284     public static org.omg.CORBA.Policy JavaDoc getPolicy(org.omg.CORBA.Policy JavaDoc[] 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc state) {
316         return state.value() == org.omg.PortableServer.POAManagerPackage.State._INACTIVE ? true : false;
317     }
318
319     /**
320      * masks the object key separator bytes
321      */

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     /**
362      * masks the object key separator chars
363      */

364
365     public static String JavaDoc maskStr(String JavaDoc str)
366     {
367         return new String JavaDoc(maskId(str.getBytes()));
368     }
369
370     /**
371      * unmasks the object key separator chars
372      */

373
374     public static String JavaDoc unmaskStr(String JavaDoc str)
375     {
376         return new String JavaDoc(unmaskId(str.getBytes()));
377     }
378
379
380     /**
381      * unmasks the object key separator bytes
382      */

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     /**
434      * <code>toHex</code> converts a byte into a readable string.
435      *
436      * @param b a <code>byte</code> value
437      * @return a <code>String</code> value
438      */

439
440     public static final String JavaDoc toHex(byte b)
441     {
442         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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