1 /*****************************************************************************/
2 /* Software Testing Automation Framework (STAF)                              */
3 /* (C) Copyright IBM Corp. 2001, 2005                                        */
4 /*                                                                           */
5 /* This software is licensed under the Eclipse Public License (EPL) V1.0.    */
6 /*****************************************************************************/
7 
8 package com.ibm.staf;
9 import com.ibm.staf.service.*;
10 
11 /**
12  * This class provides STAF utility functions.
13  */
14 public class STAFUtil
15 {
16     /**
17      * Opening privacy delimiter
18      */
19     public static String privacyDelimiter1 = "!!@";
20 
21     /**
22      * Closing privacy delimiter
23      */
24     public static String privacyDelimiter2 = "@!!";
25 
26     /**
27      * Escaped opening privacy delimiter
28      */
29     public static String escapedPrivacyDelimiter1 = "^!!@";
30 
31     /**
32      * Escaped closing privacy delimiter
33      */
34     public static String escapedPrivacyDelimiter2 = "^@!!";
35 
36     /**
37      * Escape character for privacy delimiters
38      */
39     public static String privacyDelimiterEscape = "^";
40 
41     /**
42      * Number of milliseconds in a millisecond.  Used as a multiplier to get
43      * the duration in milliseconds.
44      */
45     public static int sMILLISECOND = 1;
46 
47     /**
48      * Number of milliseconds in a second.  Used as a multiplier to get the
49      * duration in milliseconds.
50      */
51     public static int sSECOND_IN_MS = 1000;
52 
53     /**
54      * Number of milliseconds in a minute.  Used as a multiplier to get the
55      * duration in milliseconds.
56      */
57     public static int sMINUTE_IN_MS = 60000;
58 
59     /**
60      * Number of milliseconds in an hour.  Used as a multiplier to get the
61      * duration in milliseconds.
62      */
63     public static int sHOUR_IN_MS = 3600000;
64 
65     /**
66      * Number of milliseconds in a day.  Used as a multiplier to get the
67      * duration in milliseconds.
68      */
69     public static int sDAY_IN_MS = 24 * 3600000;
70 
71     /**
72      * Number of milliseconds in a week.  Used as a multiplier to get the
73      * duration in milliseconds.
74      */
75     public static int sWEEK_IN_MS = 7 * 24 * 3600000;
76 
77     /**
78      * Maximum duration when specified in milliseconds.  Made the maximum
79      * 4294967294 because we use 4294967295 to indicate an indefinite wait
80      * on 32-bit machine (in C++).  For example:
81      *   STAF_EVENT_SEM_INDEFINITE_WAIT = (unsigned int)-1
82      * and because the duration value is often passed on to a C++ function such
83      * as STAFThreadManager::sleepCurrentThread(unsigned int milliseconds).
84      */
85     public static long sMAX_MILLISECONDS = 4294967294L;
86 
87     /**
88      * Maximum duration when specified in seconds.
89      */
90     public static int sMAX_SECONDS = 4294967;
91 
92     /**
93      * Maximum duration when specified in minutes.
94      */
95     public static int sMAX_MINUTES = 71582;
96 
97     /**
98      * Maximum duration when specified in hours
99      */
100     public static int sMAX_HOURS = 1193;
101 
102     /**
103      * Maximum duration when specified in days
104      */
105     public static int sMAX_DAYS = 49;
106 
107     /**
108      * Maximum duration when specified in weeks
109      */
110     public static int sMAX_WEEKS = 7;
111 
112     /**
113      * Number of bytes in a byte.  Used as a multiplier to get the size in
114      * bytes.
115      */
116     public static int sBYTES = 1;
117 
118     /**
119      * Number of bytes in a kilobyte.  Used as a multiplier to get the size in
120      * bytes.
121      */
122     public static int sBYTES_IN_KILOBYTES = 1024;
123 
124     /**
125      * Number of bytes in a megabyte.  Used as a multiplier to get the size in
126      * bytes.
127      */
128     public static int sBYTES_IN_MEGABYTES = 1048576;
129 
130     /**
131      * Maximum size when specified in bytes.  Made the maximum 4294967294
132      * because that's the maximum size of UINT_MAX on 32-bit machines in C++
133      * and because the size value is often passed on to a C++ function.
134      */
135     public static long sMAX_BYTES = 4294967294L;
136 
137     /**
138      * Maximum size when specified in kilobytes.
139      */
140     public static int sMAX_KILOBYTES = 4194303;
141 
142     /**
143      * Maximum size when specified in megabytes
144      */
145     public static int sMAX_MEGABYTES = 4095;
146 
147     /**
148      * This method returns a length delimited representation of a string,
149      * the <code>:length:</code> format for the string (known as the colon
150      * length colon format).
151      * <p>
152      * For example, if passed in "Hello world", this method would return
153      * ":11:Hello world".
154      *
155      * @param  data        the input string to be "wrapped"
156      * @return             the string in a colon length colon format
157      * @see <a href="http://staf.sourceforge.net/current/STAFUG.htm#HDROVFORM">
158      *      Section "7.2 Option Value Formats" in the STAF User's Guide</a>
159      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_wrapData">
160      *      Section "3.4.1 Static Method STAFUtil.wrapData" in the STAF Java
161      *      User's Guide</a>
162      */
wrapData(String data)163     public static String wrapData(String data)
164     {
165         return ":" + data.length() + ":" + data;
166     }
167 
168     /**
169      * This method returns the input string without the <code>:length:</code>
170      * prefix, if present.
171      * <p>
172      * For example, if passed in ":11:Hello world", this method would return
173      * "Hello world".
174      *
175      * @param  data       the input string in a colon length colon format
176      * @return            the string without the colon length colon format
177      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_unwrapData">
178      *      Section "3.4.2 Static Method STAFUtil.unwrapData" in the STAF Java
179      *      User's Guide</a>
180      */
unwrapData(String data)181     public static String unwrapData(String data)
182     {
183         if (data != null)
184         {
185             int colon1Pos = data.indexOf(":");
186 
187             if (colon1Pos == 0)
188             {
189                 int colon2Pos = data.indexOf(":", 1);
190 
191                 if (colon2Pos > -1)
192                 {
193                     try
194                     {
195                         // Verify that an integer was specified between the two
196                         // colons to make sure the value has a colonLengthColon
197                         // format, and just doesn't happen to contain two colons
198 
199                         int length = (new Integer(
200                             data.substring(1, colon2Pos))).intValue();
201 
202                         String newValue = data.substring(colon2Pos + 1);
203 
204                         if (length == newValue.length())
205                             return newValue;
206                     }
207                     catch (NumberFormatException e)
208                     {
209                         // Not a CLC format
210                     }
211                 }
212             }
213         }
214 
215         return data;
216     }
217 
218     /**
219      * This method returns the endpoint without the port (strips @nnnn if
220      * present from the end of the endpoint string).
221      * <p>
222      * For example, if passed in "tcp://client1.company.com@6500", would
223      * return "tcp://client1.company.com".
224      *
225      * @param  endpoint  an endpoint in the format of:
226      *                   [&lt;Interface>://]&lt;System identifier>[@&lt;Port>]
227      * @return           the endpoint without the port
228      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_stripPort">
229      *      Section "3.4.3 Static Method STAFUtil.stripPortFromEndpoint" in
230      *      the STAF Java User's Guide</a>
231      */
stripPortFromEndpoint(String endpoint)232     public static String stripPortFromEndpoint(String endpoint)
233     {
234         // Strip the port from the endpoint, if present.
235 
236         String endpointNoPort = endpoint;
237         int portIndex = endpoint.lastIndexOf("@");
238 
239         if (portIndex != -1)
240         {
241             // If the characters following the "@" are numeric, then assume
242             // it's a valid port and strip the @ and the port number from
243             // the endpoint.
244 
245             try
246             {
247                 int port = new Integer(endpoint.substring(portIndex + 1)).
248                     intValue();
249                 endpointNoPort = endpoint.substring(0, portIndex);
250             }
251             catch (NumberFormatException e)
252             {
253                 // Do nothing - Not valid port so don't remove from endpoint
254             }
255         }
256 
257         return endpointNoPort;
258     }
259 
260     /**
261      * This method validates that the requesting machine has the required
262      * trust to submit a service request.  This method is intended for use
263      * by writers of STAF Java services.
264      * <p>
265      * Note:  Each time a new service interface level is added, must add
266      * another <code>validateTrust</code> method overloaded to support the
267      * new service interface level.
268      *
269      * @param  requiredTrustLevel the required trust level for this service
270      *                            request
271      * @param  service            the registered name of the service
272      * @param  request            specifies the first word (or two) that
273      *                            uniquely identifies the request if an error
274      *                            occurs
275      * @param  localMachine       specifies the logical identifier for the
276      *                            service machine which will be used in the
277      *                            error message if the requesting machine has
278      *                            insufficient trust.
279      * @param  info               the STAF service interface request
280      *                            information
281      * @return                    a <code>STAFResult</code> object whose
282      *                            <code>rc</code> field contains the return
283      *                            code (0 if successful) and whose
284      *                            <code>result</code> field contains if
285      *                            successful or a detailed error message if
286      *                            not successful
287      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_validateTrust">
288      *      Section "3.4.4 Static Method STAFUtil.validateTrust" in the STAF
289      *      Java User's Guide</a>
290      * @see <a href="http://staf.sourceforge.net/current/stafsdg.html">
291      *      STAF Service Developer's Guide</a>
292      */
validateTrust( int requiredTrustLevel, String service, String request, String localMachine, STAFServiceInterfaceLevel30.RequestInfo info)293     public static STAFResult validateTrust(
294         int requiredTrustLevel, String service, String request,
295         String localMachine, STAFServiceInterfaceLevel30.RequestInfo info)
296     {
297         if (info.trustLevel < requiredTrustLevel)
298         {
299             // Strip the port from the machine's endpoint, if present.
300 
301             String endpoint = stripPortFromEndpoint(info.endpoint);
302 
303             return new STAFResult(
304                 STAFResult.AccessDenied,
305                 "Trust level " + requiredTrustLevel + " required for the " +
306                 service + " service's " + request + " request\n" +
307                 "Requester has trust level " + info.trustLevel +
308                 " on machine " + localMachine + "\nRequesting machine: " +
309                  endpoint + " (" + info.physicalInterfaceID + ")" +
310                 "\nRequesting user   : " + info.user);
311         }
312 
313         return new STAFResult(STAFResult.Ok);
314     }
315 
316     /**
317      * This method verifies that the timeout duration is valid and returns
318      * the timeout duration converted to milliseconds.
319      * <p>
320      * Examples of input timeout duration strings are: "100", "1s", "5m",
321      * "1h", "1d", "1w"
322      *
323      * @param  durationString  a string containing a timeout duration in the
324      *                         format: &lt;Number>[s|m|h|d|w]
325      * @return                 a <code>STAFResult</code> object whose
326      *                         <code>rc</code> field contains the return code
327      *                         (0 if successful) and whose <code>result</code>
328      *                         field contains the converted duration in
329      *                         milliseconds if successful or an error message
330      *                         if not successful
331      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_convertDuration">
332      *      Section "3.4.5 Static Method STAFUtil.convertDurationString" in
333      *      the STAF Java User's Guide</a>
334      * @since  STAF 3.3.2
335      */
convertDurationString(String durationString)336     public static STAFResult convertDurationString(String durationString)
337     {
338         int rc = STAFResult.Ok;
339         String durationStr = durationString;
340         long duration = 0;
341 
342         // Asume duration is specified in milliseconds if numeric
343         int multiplier = 1;
344 
345         if ((durationStr == null) || (durationStr.length() == 0))
346         {
347             rc = STAFResult.InvalidValue;
348         }
349         else
350         {
351             // Check if the duration string is not all digits
352 
353             try
354             {
355                 duration = (new Long(durationStr)).longValue();
356 
357                 if (duration < 0)
358                 {
359                     rc = STAFResult.InvalidValue;
360                 }
361             }
362             catch (NumberFormatException e)
363             {
364                 // Get duration type (last character of duration string)
365 
366                 String durationType = durationStr.substring(
367                     durationStr.length() - 1).toLowerCase();
368 
369                 if (durationType.equals("s"))
370                     multiplier = sSECOND_IN_MS;
371                 else if (durationType.equals("m"))
372                     multiplier = sMINUTE_IN_MS;
373                 else if (durationType.equals("h"))
374                     multiplier = sHOUR_IN_MS;
375                 else if (durationType.equals("d"))
376                     multiplier = sDAY_IN_MS;
377                 else if (durationType.equals("w"))
378                     multiplier = sWEEK_IN_MS;
379                 else
380                     rc = STAFResult.InvalidValue;
381 
382                 if (rc == STAFResult.Ok)
383                 {
384                     // Assign numeric duration value (all characters
385                     // except the last one)
386                     durationStr = durationString.substring(
387                         0, durationString.length() - 1);
388 
389                     try
390                     {
391                         duration = (new Long(durationStr)).longValue();
392 
393                         if (duration < 0)
394                         {
395                             rc = STAFResult.InvalidValue;
396                         }
397                     }
398                     catch (NumberFormatException e2)
399                     {
400                         rc = STAFResult.InvalidValue;
401                     }
402                 }
403             }
404         }
405 
406         if (rc != STAFResult.Ok)
407         {
408             return new STAFResult(
409                 rc,
410                 "This value may be expressed in milliseconds, seconds, " +
411                 "minutes, hours, days, or weeks.  Its format is " +
412                 "<Number>[s|m|h|d|w] where <Number> is an integer >= 0 and " +
413                 "indicates milliseconds unless one of the following " +
414                 "case-insensitive suffixes is specified:  s (for seconds), " +
415                 "m (for minutes), h (for hours), d (for days), or " +
416                 "w (for weeks).  The calculated value cannot exceed " +
417                 "4294967294 milliseconds.\n\nExamples: \n" +
418                 "  100 specifies 100 milliseconds, \n" +
419                 "  10s specifies 10 seconds, \n" +
420                 "  5m specifies 5 minutes, \n" +
421                 "  2h specifies 2 hours, \n" +
422                 "  1d specifies 1 day, \n" +
423                 "  1w specifies 1 week.");
424         }
425 
426         // Because the maximum duration in milliseconds cannot exceed
427         // 4294967294:
428         // - Duration specified in seconds cannot exceed 4294967 seconds
429         // - Duration specified in minutes cannot exceed 71582 minutes
430         // - Duration specified in hours cannot exceed 1193 hours
431         // - Duration specified in days cannot exceed 49 days
432         // - Duration specified in weeks cannot exceed 7 weeks
433 
434         // Note: The maximum is 4294967294 because we use 4294967295 to
435         // indicate an indefinite wait on 32-bit machine (in C++).
436         // For example:  STAF_EVENT_SEM_INDEFINITE_WAIT = (unsigned int)-1
437         // Alsso, because the duration value is often passed on to a C++
438         // function such as:
439         //   STAFThreadManager::sleepCurrentThread(unsigned int milliseconds)
440 
441         if (((multiplier == sMILLISECOND) && (duration > sMAX_MILLISECONDS)) ||
442             ((multiplier == sSECOND_IN_MS) && (duration > sMAX_SECONDS)) ||
443             ((multiplier == sMINUTE_IN_MS) && (duration > sMAX_MINUTES)) ||
444             ((multiplier == sHOUR_IN_MS) && (duration > sMAX_HOURS)) ||
445             ((multiplier == sDAY_IN_MS) && (duration > sMAX_DAYS)) ||
446             ((multiplier == sWEEK_IN_MS) && (duration > sMAX_WEEKS)))
447         {
448             rc = STAFResult.InvalidValue;
449         }
450 
451         if (rc == STAFResult.Ok)
452         {
453             duration *= multiplier;
454 
455             return new STAFResult(
456                 STAFResult.Ok, (new Long(duration)).toString());
457         }
458 
459         String errorMsg = "";
460 
461         if (multiplier == sMILLISECOND)
462             errorMsg = "Cannot exceed " + sMAX_MILLISECONDS + " milliseconds.";
463         else if (multiplier == sSECOND_IN_MS)
464             errorMsg = "Cannot exceed " + sMAX_SECONDS + " seconds.";
465         else if (multiplier == sMINUTE_IN_MS)
466             errorMsg = "Cannot exceed " + sMAX_MINUTES + " minutes.";
467         else if (multiplier == sHOUR_IN_MS)
468             errorMsg = "Cannot exceed " + sMAX_HOURS + " hours.";
469         else if (multiplier == sDAY_IN_MS)
470             errorMsg = "Cannot exceed " + sMAX_DAYS + " days.";
471         else if (multiplier == sWEEK_IN_MS)
472             errorMsg = "Cannot exceed " + sMAX_WEEKS + " weeks.";
473 
474         return new STAFResult(rc, errorMsg);
475     }
476 
477     /**
478      * This method verifies that the input file size string is valid and is in
479      * format &lt;Number>[k|m] and returns the size converted to bytes.
480      * <p>
481      * Examples of valid input size strings are:  "100000", "500k", "5m"
482      *
483      * @param  sizeString  a string containing a size in the format:
484      *                     &lt;Number>[k|m]
485      * @return             a <code>STAFResult</code> object whose
486      *                     <code>rc</code> field contains the return code
487      *                     (0 if successful) and whose <code>result</code>
488      *                     field contains the converted size in bytes if
489      *                     successful or an error message if not successful
490      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_convertSize">
491      *      Section "3.4.6 Static Method STAFUtil.convertSizeString" in
492      *      the STAF Java User's Guide</a>
493      * @since  STAF 3.3.4
494      */
convertSizeString(String sizeString)495     public static STAFResult convertSizeString(String sizeString)
496     {
497         int rc = STAFResult.Ok;
498         String sizeStr = sizeString;
499         long size = 0;
500 
501         // Assume size is specified in bytes if numeric
502         int multiplier = sBYTES;
503 
504         if ((sizeStr == null) || (sizeStr.length() == 0))
505         {
506             rc = STAFResult.InvalidValue;
507         }
508         else
509         {
510             // Check if the size string is not all digits
511 
512             try
513             {
514                 size = (new Long(sizeStr)).longValue();
515 
516                 if (size < 0)
517                 {
518                     rc = STAFResult.InvalidValue;
519                 }
520             }
521             catch (NumberFormatException e)
522             {
523                 // Get size type (last character of size string)
524 
525                 String sizeType = sizeStr.substring(
526                     sizeStr.length() - 1).toLowerCase();
527 
528                 if (sizeType.equals("k"))
529                     multiplier = sBYTES_IN_KILOBYTES;
530                 else if (sizeType.equals("m"))
531                     multiplier = sBYTES_IN_MEGABYTES;
532                 else
533                     rc = STAFResult.InvalidValue;
534 
535                 if (rc == STAFResult.Ok)
536                 {
537                     // Assign numeric size value (all characters
538                     // except the last one)
539                     sizeStr = sizeString.substring(
540                         0, sizeString.length() - 1);
541 
542                     try
543                     {
544                         size = (new Long(sizeStr)).longValue();
545 
546                         if (size < 0)
547                         {
548                             rc = STAFResult.InvalidValue;
549                         }
550                     }
551                     catch (NumberFormatException e2)
552                     {
553                         rc = STAFResult.InvalidValue;
554                     }
555                 }
556             }
557         }
558 
559         if (rc != STAFResult.Ok)
560         {
561             return new STAFResult(
562                 rc,
563                 "This value may be expressed in bytes, kilobytes, or " +
564                 "megabytes.  Its format is " +
565                 "<Number>[k|m] where <Number> is an integer >= 0 and " +
566                 "indicates bytes unless one of the following " +
567                 "case-insensitive suffixes is specified:  " +
568                 "k (for kilobytes) or m (for megabytes).  " +
569                 "The calculated value cannot exceed 4294967294 bytes.\n\n" +
570                 "Examples: \n" +
571                 "  100000 specifies 100,000 bytes, \n" +
572                 "  500k specifies 500 kilobytes (or 512,000 bytes), \n" +
573                 "  5m specifies 5 megabytes (or 5,242,880 bytes), \n" +
574                 "  0 specifies no maximum size limit");
575         }
576 
577         // Because the maximum size in bytes cannot exceed 4294967294:
578         // - Size specified in kilobytes cannot exceed 4294967 seconds
579         // - Size specified in megabytes cannot exceed 71582 minutes
580 
581         if (((multiplier == sBYTES) && (size > sMAX_BYTES)) ||
582             ((multiplier == sBYTES_IN_KILOBYTES) && (size > sMAX_KILOBYTES)) ||
583             ((multiplier == sBYTES_IN_MEGABYTES) && (size > sMAX_MEGABYTES)))
584         {
585             rc = STAFResult.InvalidValue;
586         }
587 
588         if (rc == STAFResult.Ok)
589         {
590             size *= multiplier;
591 
592             return new STAFResult(
593                 STAFResult.Ok, (new Long(size)).toString());
594         }
595 
596         String errorMsg = "";
597 
598         if (multiplier == sBYTES)
599             errorMsg = "Cannot exceed " + sMAX_BYTES + " bytes.";
600         else if (multiplier == sBYTES_IN_KILOBYTES)
601             errorMsg = "Cannot exceed " + sMAX_KILOBYTES + " kilobytes.";
602         else if (multiplier == sBYTES_IN_MEGABYTES)
603             errorMsg = "Cannot exceed " + sMAX_MEGABYTES + " megabytes.";
604 
605         return new STAFResult(rc, errorMsg);
606     }
607 
608     /**
609      * This method resolves any STAF variables that are contained within the
610      * string passed in by submitting a "RESOLVE REQUEST request# STRING
611      * value" request to the VAR service on the local machine. The variables
612      * will be resolved using the originating handle's pool associated with
613      * the specified request number, the local machine's shared variable pool,
614      * and the local machine's system variable pool.
615      * <p>
616      * This method should be used by writers of STAF Java services as most
617      * option values in a service request should be resolved.
618      *
619      * @param  value         the value of an option in a service request string
620      *                       that may contain STAF variables, such as
621      *                       {STAF/Config/Machine}
622      * @param  handle        a <code>STAFHandle</code> used to submit the
623      *                       VAR RESOLVE request
624      * @param  requestNumber the request number
625      * @return               a <code>STAFResult</code> object whose
626      *                       <code>rc</code> field contains the return code
627      *                       (0 if successful) and whose <code>result</code>
628      *                       field contains the resolved option's value if
629      *                       successful or an error message if not successful
630      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_resRequestVar">
631      *      Section "3.4.7 Static Method STAFUtil.resolveRequestVar" in
632      *      the STAF Java User's Guide</a>
633      * @see <a href="http://staf.sourceforge.net/current/stafsdg.html">
634      *      STAF Service Developer's Guide</a>
635      */
resolveRequestVar( String value, STAFHandle handle, int requestNumber)636     public static STAFResult resolveRequestVar(
637         String value, STAFHandle handle, int requestNumber)
638     {
639         if (value.indexOf("{") != -1)
640         {
641             // The string may contains STAF variables
642 
643             STAFResult resolvedResult = handle.submit2(
644                 "local", "VAR", "RESOLVE REQUEST " + requestNumber +
645                 " STRING " + STAFUtil.wrapData(value));
646 
647             return resolvedResult;
648         }
649 
650         return new STAFResult(STAFResult.Ok, value);
651     }
652 
653     /**
654      * This method resolves any STAF variables that are contained within
655      * the option value passed in and then checks if the resolved value is an
656      * integer.  It resolves STAF variables by submitting a
657      * "RESOLVE REQUEST request# STRING value" request to the VAR service
658      * on the local system.  STAF variables will be resolved using the
659      * originating handle's pool associated with the specified request number,
660      * the local system's shared pool, and the local system's system pool.
661      * <p>
662      * This method should be used by writers of STAF Java services as most
663      * option values in a service request should be resolved.
664      *
665      * @param  option        the name of an option in a service request string
666      *                       whose value is being resolved
667      * @param  value         the value of an option in a service request string
668      *                       that may contain STAF variables to be resolved,
669      *                       such as {STAF/Config/Machine}
670      * @param  handle        a <code>STAFHandle</code> used to submit the
671      *                       VAR RESOLVE request
672      * @param  requestNumber the request number
673      * @return               a <code>STAFResult</code> object whose
674      *                       <code>rc</code> field contains the return code
675      *                       (0 if successful) and whose <code>result</code>
676      *                       field contains the resolved option's value if
677      *                       successful or an error message if not successful
678      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_resRequestVarAndCheckInt">
679      *      Section "3.4.8 Static Method STAFUtil.resolveRequestVarAndCheckInt"
680      *      in the STAF Java User's Guide</a>
681      * @see <a href="http://staf.sourceforge.net/current/stafsdg.html">
682      *      STAF Service Developer's Guide</a>
683      */
resolveRequestVarAndCheckInt( String option, String value, STAFHandle handle, int requestNumber)684     public static STAFResult resolveRequestVarAndCheckInt(
685         String option, String value, STAFHandle handle, int requestNumber)
686     {
687         STAFResult resolvedValue = resolveRequestVar(
688             value, handle, requestNumber);
689 
690         if (resolvedValue.rc != STAFResult.Ok) return resolvedValue;
691 
692         try
693         {
694             Integer.parseInt(resolvedValue.result);
695         }
696         catch (NumberFormatException e)
697         {
698             return new STAFResult(
699                 STAFResult.InvalidValue, option +
700                 " value must be an Integer.  " +
701                 option + "=" + resolvedValue.result);
702         }
703 
704         return resolvedValue;
705     }
706 
707     /**
708      * This method resolves any STAF variables that are contained within
709      * the option value passed in and then checks if the resolved value is a
710      * valid timeout duration and if so, converts it into milliseconds.
711      * It resolves STAF variables by submitting a
712      * "RESOLVE REQUEST request# STRING value" request to the VAR service
713      * on the local system.  STAF variables will be resolved using the
714      * originating handle's pool associated with the specified request number,
715      * the local system's shared pool, and the local system's system pool.
716      * <p>
717      * This method may be used by writers of STAF Java services to resolve a
718      * duration timeout option that needs to be converted to milliseconds.
719      *
720      * @param  option        the name of an option in a service request string
721      *                       whose value is being resolved
722      * @param  value         the value of an option in a service request string
723      *                       that may contain STAF variables to be resolved.
724      *                       This value specifies the timeout duration in the
725      *                       format <code>&lt;Number>[s|m|h|d|w]</code>. For
726      *                       example: "100", "1s", "5m", "1h".
727      * @param  handle        a <code>STAFHandle</code> used to submit the
728      *                       VAR RESOLVE request
729      * @param  requestNumber the request number
730      * @return               a <code>STAFResult</code> object whose
731      *                       <code>rc</code> field contains the return code
732      *                       (0 if successful) and whose <code>result</code>
733      *                       field contains the resolved option's timeout
734      *                       duration in milliseconds if successful or an
735      *                       error message if not successful
736      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_resRequestVarAndConvertDuration">
737      *      Section "3.4.9 Static Method STAFUtil.resolveRequestVarAndConvertDuration"
738      *      in the STAF Java User's Guide</a>
739      * @see <a href="http://staf.sourceforge.net/current/stafsdg.html">
740      *      STAF Service Developer's Guide</a>
741      * @since  STAF 3.3.2
742      */
resolveRequestVarAndConvertDuration( String option, String value, STAFHandle handle, int requestNumber)743     public static STAFResult resolveRequestVarAndConvertDuration(
744         String option, String value, STAFHandle handle, int requestNumber)
745     {
746         STAFResult resolvedValue = resolveRequestVar(
747             value, handle, requestNumber);
748 
749         if (resolvedValue.rc != STAFResult.Ok) return resolvedValue;
750 
751         STAFResult result = convertDurationString(resolvedValue.result);
752 
753         if (result.rc != STAFResult.Ok)
754         {
755             result.result = "Invalid value for the " + option + " option: " +
756                 resolvedValue.result + " \n\n" + result.result;
757         }
758 
759         return result;
760     }
761 
762     /**
763      * This method resolves any STAF variables that are contained within
764      * the option value passed in and then checks if the resolved value is a
765      * valid size and if so, converts the size into bytes.
766      * It resolves STAF variables by submitting a
767      * "RESOLVE REQUEST request# STRING value" request to the VAR service
768      * on the local system.  STAF variables will be resolved using the
769      * originating handle's pool associated with the specified request number,
770      * the local system's shared pool, and the local system's system pool.
771      * <p>
772      * This method may be used by writers of STAF Java services to resolve a
773      * size option that needs to be converted to bytes.
774      *
775      * @param  option        the name of an option in a service request string
776      *                       whose value is being resolved
777      * @param  value         the value of an option in a service request string
778      *                       that may contain STAF variables to be resolved,
779      *                       This value specifies the size in the format
780      *                       <code>&lt;Number>[k|m]</code>. For example:
781      *                       "1000000", "500k", "5m".
782      * @param  handle        a <code>STAFHandle</code> used to submit the
783      *                       VAR RESOLVE request
784      * @param  requestNumber the request number
785      * @return               a <code>STAFResult</code> object whose
786      *                       <code>rc</code> field contains the return code
787      *                       (0 if successful) and whose <code>result</code>
788      *                       field contains the resolved option's size in
789      *                       bytes if successful or an error message if not
790      *                       successful
791      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_resRequestVarAndConvertSize">
792      *      Section "3.4.10 Static Method STAFUtil.resolveRequestVarAndConvertSize"
793      *      in the STAF Java User's Guide</a>
794      * @see <a href="http://staf.sourceforge.net/current/stafsdg.html">
795      *      STAF Service Developer's Guide</a>
796      * @since  STAF 3.3.4
797      */
resolveRequestVarAndConvertSize( String option, String value, STAFHandle handle, int requestNumber)798     public static STAFResult resolveRequestVarAndConvertSize(
799         String option, String value, STAFHandle handle, int requestNumber)
800     {
801         STAFResult resolvedValue = resolveRequestVar(
802             value, handle, requestNumber);
803 
804         if (resolvedValue.rc != STAFResult.Ok) return resolvedValue;
805 
806         STAFResult result = convertSizeString(resolvedValue.result);
807 
808         if (result.rc != STAFResult.Ok)
809         {
810             result.result = "Invalid value for the " + option + " option: " +
811                 resolvedValue.result + " \n\n" + result.result;
812         }
813 
814         return result;
815     }
816 
817     /**
818      * This method resolves any STAF variables that are contained within
819      * the string passed in the <code>value</code> parameter.  It does this
820      * by submitting a "RESOLVE STRING value" request to the VAR service on
821      * the local system.
822      * <p>
823      * This method should be used by writers of STAF Java services if they
824      * need to resolve a variable in the init() method for a Java service.
825      *
826      * @param  value         a string that may contain STAF variables to be
827      *                       resolved, such as {STAF/Config/Machine}
828      * @param  handle        a <code>STAFHandle</code> used to submit the
829      *                       VAR RESOLVE request
830      * @return               a <code>STAFResult</code> object whose
831      *                       <code>rc</code> field contains the return code
832      *                       (0 if successful) and whose <code>result</code>
833      *                       field contains a string with all the variables
834      *                       resolved if successful, or an error message if
835      *                       not successful
836      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_resInitVar">
837      *      Section "3.4.11 Static Method STAFUtil.resolveInitVar"
838      *      in the STAF Java User's Guide</a>
839      * @see <a href="http://staf.sourceforge.net/current/stafsdg.html">
840      *      STAF Service Developer's Guide</a>
841      */
resolveInitVar(String value, STAFHandle handle)842     public static STAFResult resolveInitVar(String value, STAFHandle handle)
843     {
844         if (value.indexOf("{") != -1)
845         {
846             // The string may contains STAF variables
847 
848             STAFResult resolvedResult = handle.submit2(
849                 "local", "VAR", "RESOLVE STRING " + STAFUtil.wrapData(value));
850 
851             return resolvedResult;
852         }
853 
854         return new STAFResult(STAFResult.Ok, value);
855     }
856 
857     /**
858      * This method resolves any STAF variables that are contained within
859      * the option value passed in and then checks if the resolved value is an
860      * integer and returns an error if it is not an integer.  It resolves
861      * STAF variables by submitting a "RESOLVE STRING value" request to the
862      * VAR service on the local system.
863      * <p>
864      * This method should be used by writers of STAF Java services if they
865      * need to resolve a variable in the init() method for a Java service.
866      *
867      * @param  option        the name of an option in a service request string
868      *                       whose value is being resolved
869      * @param  value         an option value in a service request string that
870      *                       may contain STAF variables, such as
871      *                       {STAF/Config/Machine}
872      * @param  handle        a <code>STAFHandle</code> used to submit the
873      *                       VAR RESOLVE request
874      * @return               a <code>STAFResult</code> object whose
875      *                       <code>rc</code> field contains the return code
876      *                       (0 if successful) and whose <code>result</code>
877      *                       field contains the resolved option value if
878      *                       successful or an error message if not successful
879      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_resInitVarAndCheckInt">
880      *      Section "3.4.12 Static Method STAFUtil.resolveInitVarAndCheckInt"
881      *      in the STAF Java User's Guide</a>
882      * @see <a href="http://staf.sourceforge.net/current/stafsdg.html">
883      *      STAF Service Developer's Guide</a>
884      */
resolveInitVarAndCheckInt( String option, String value, STAFHandle handle)885     public static STAFResult resolveInitVarAndCheckInt(
886         String option, String value, STAFHandle handle)
887     {
888         STAFResult resolvedValue = resolveInitVar(value, handle);
889 
890         if (resolvedValue.rc != STAFResult.Ok) return resolvedValue;
891 
892         try
893         {
894             Integer.parseInt(resolvedValue.result);
895         }
896         catch (NumberFormatException e)
897         {
898             return new STAFResult(
899                 STAFResult.InvalidValue, option + " value must be numeric.  " +
900                 option + "=" + resolvedValue.result);
901         }
902 
903         return resolvedValue;
904     }
905 
906     /**
907      * This method gets the version of STAF running on a specified machine
908      * and then checks if the version meets the minimum required version
909      * specified in the <code>minRequiredVersion</code> parameter.  It gets
910      * the STAF version by submitting a VERSION request to the MISC service
911      * on the specified machine.  If the version is at or above the required
912      * version, a <code>STAFResult</code> object is returned with its
913      * <code>rc</code> field set to 0 and its <code>result</code> field set to
914      * the version running on the specified machine.  If the version is lower
915      * than the mininum required version, a <code>STAFResult</code> object is
916      * returned with its <code>rc</code> field set to
917      * <code>STAFResult.InvalidSTAFVersion</code> and an error message in its
918      * <code>result</code> field.  If another error occurs (e.g. RC 16 if
919      * the machine is not currently running STAF, etc.), that return code
920      * and possibly an error message will be returned in the
921      * <code>STAFResult</code> object.
922      * <p>
923      * This method should be used by writers of STAF Java services if the
924      * service they are writing needs a particular version of STAF.
925      * <p>
926      * The versions being compared must have the following format unless it is
927      * blank or "<N/A>", which equates to "no version" and is internally
928      * represented as 0.0.0.0:
929      * <p>
930      *   <code>a[.b[.c[.d]]] [text]</code>
931      * <p>
932      * where:
933      * <ul>
934      *   <li>a, b, c, and d (if specified) are numeric
935      *   <li>text is separated by one or more spaces from the version numbers
936      * </ul>
937      * <p>
938      * Versions are compared as follows:
939      * <ol type="a">
940      * <li> The numeric versions (a[.b[.c[.d]]]) are numerically compared.
941      * <li> If the numeric versions are "equal", then the [text] values are
942      *      compared using a case-insensitive string compare.  Except, note
943      *      that no text is considered GREATER than any text.  For example,
944      *      "3.1.0" > "3.1.0 Beta 1").
945      * </ol>
946      * <p>
947      * Examples:
948      * <ul>
949      *   <li>"3" = "3.0" = "3.0.0" = "3.0.0.0"
950      *   <li>"3.0.0" < "3.1.0"
951      *   <li>"3.0.2" < "3.0.3"
952      *   <li>"3.0.0" < "3.1"
953      *   <li>"3.0.9" < "3.0.10"
954      *   <li>"3.1.0 Beta 1" < "3.1.0"
955      *   <li>"3.1.0 Alpha 1" < "3.1.0 Beta 1"
956      * </ul>
957      *
958      * @param  machine  endpoint of the machine whose STAF version is to be
959      *                  compared
960      * @param  handle   STAF handle to use to submit the request
961      * @param  minRequiredVersion  The minimum required version.
962      *         The version must have the following format unless it is blank
963      *         or "<N/A>", which equates to "no version" and is internally
964      *         represented as 0.0.0.0:  <code>a[.b[.c[.d]]] [text]</code>
965      * @return          a <code>STAFResult</code> object.  If the version
966      *                  is at or above the required version, its
967      *                  <code>rc</code> field set to 0 and its
968      *                  <code>result</code> field set to the version
969      *                  running on the specified machine.  If the version is
970      *                  lower than the mininum required version, its
971      *                  <code>rc</code> field is set to
972      *                  <code>STAFResult.InvalidSTAFVersion</code> and its
973      *                  <code>result</code> field contains an error message.
974      *                  Other errors may occur as well such as RC 16 if the
975      *                  machine is not currently running STAF, etc.), and
976      *                  then its <code>rc</code> field will be set to it
977      *                  and the <code>result</code> field will possibly
978      *                  contain an error message.
979      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_compareSTAFVersion">
980      *      Section "3.4.13 Static Method STAFUtil.compareSTAFVersion"
981      *      in the STAF Java User's Guide</a>
982      * @since  STAF 3.1.0
983      */
compareSTAFVersion( String machine, STAFHandle handle, String minRequiredVersion)984     public static STAFResult compareSTAFVersion(
985         String machine, STAFHandle handle, String minRequiredVersion)
986     {
987         // The default is to check the version of STAF running (which is
988         // done by submitting a VERSION request to the MISC service)
989         return compareSTAFVersion(
990             machine, handle, minRequiredVersion, "MISC");
991     }
992 
993     /**
994      * This method gets the version of STAF (or of a STAF service)
995      * running on a specified machine and then checks if the version meets
996      * the minimum required version specified in the
997      * <code>minRequiredVersion</code> parameter.  It gets the version by
998      * submitting a VERSION request to either the MISC service (to get the
999      * version of STAF) or to another STAF service on the specified machine.
1000      * If the version is at or above the required version, a
1001      * <code>STAFResult</code> object is returned with its <code>rc</code>
1002      * field set to 0 and its <code>result</code> field set to the version
1003      * running on the specified machine.  If the version is lower than the
1004      * mininum required version, a <code>STAFResult</code> object is returned
1005      * with its <code>rc</code> field set to
1006      * <code>STAFResult.InvalidSTAFVersion</code> and an error message in its
1007      * <code>result</code> field.  If another error occurs (e.g. RC 16 if
1008      * the machine is not currently running STAF, etc.), that return code
1009      * and possibly an error message will be returned in the
1010      * <code>STAFResult</code> object.
1011      * <p>
1012      * This method should be used by writers of STAF Java services if the
1013      * service they are writing needs a particular version of STAF.
1014      * <p>
1015      * The versions being compared must have the following format unless it is
1016      * blank or "<N/A>", which equates to "no version" and is internally
1017      * represented as 0.0.0.0:
1018      * <p>
1019      *   <code>a[.b[.c[.d]]] [text]</code>
1020      * <p>
1021      * where:
1022      * <ul>
1023      *   <li>a, b, c, and d (if specified) are numeric
1024      *   <li>text is separated by one or more spaces from the version numbers
1025      * </ul>
1026      * <p>
1027      * Versions are compared as follows:
1028      * <ol type="a">
1029      * <li> The numeric versions (a[.b[.c[.d]]]) are numerically compared.
1030      * <li> If the numeric versions are "equal", then the [text] values are
1031      *      compared using a case-insensitive string compare.  Except, note
1032      *      that no text is considered GREATER than any text.  For example,
1033      *      "3.1.0" > "3.1.0 Beta 1").
1034      * </ol>
1035      * <p>
1036      * Examples:
1037      * <ul>
1038      *   <li>"3" = "3.0" = "3.0.0" = "3.0.0.0"
1039      *   <li>"3.0.0" < "3.1.0"
1040      *   <li>"3.0.2" < "3.0.3"
1041      *   <li>"3.0.0" < "3.1"
1042      *   <li>"3.0.9" < "3.0.10"
1043      *   <li>"3.1.0 Beta 1" < "3.1.0"
1044      *   <li>"3.1.0 Alpha 1" < "3.1.0 Beta 1"
1045      * </ul>
1046      *
1047      * @param  machine  endpoint of the machine whose STAF or STAF service
1048      *                  version is to be compared
1049      * @param  handle   STAF handle to use to submit the request
1050      * @param  minRequiredVersion  The minimum required version.
1051      *         The version must have the following format unless it is blank
1052      *         or "<N/A>", which equates to "no version" and is internally
1053      *         represented as 0.0.0.0:  <code>a[.b[.c[.d]]] [text]</code>
1054      * @param  service  Name of the service for which you want the
1055      *                  version of.  Optional.  Defaults to "MISC"
1056      *                  which means that you want to compare the
1057      *                  version of STAF running on the machine.
1058      *                  Or, you can specify the name of a STAF
1059      *                  service (such as STAX, Event, Cron, etc.)
1060      *                  that implements a VERSION request and
1061      *                  follows the STAF version format requirements.
1062      * @return          a <code>STAFResult</code> object.  If the version
1063      *                  is at or above the required version, its
1064      *                  <code>rc</code> field set to 0 and its
1065      *                  <code>result</code> field set to the version
1066      *                  running on the specified machine.  If the version is
1067      *                  lower than the mininum required version, its
1068      *                  <code>rc</code> field is set to
1069      *                  <code>STAFResult.InvalidSTAFVersion</code> and its
1070      *                  <code>result</code> field contains an error message.
1071      *                  Other errors may occur as well such as RC 16 if the
1072      *                  machine is not currently running STAF, etc.), and
1073      *                  then its <code>rc</code> field will be set to it
1074      *                  and the <code>result</code> field will possibly
1075      *                  contain an error message.
1076      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_compareSTAFVersion">
1077      *      Section "3.4.13 Static Method STAFUtil.compareSTAFVersion"
1078      *      in the STAF Java User's Guide</a>
1079      * @since  STAF 3.1.0
1080      */
compareSTAFVersion( String machine, STAFHandle handle, String minRequiredVersion, String service)1081     public static STAFResult compareSTAFVersion(
1082         String machine, STAFHandle handle, String minRequiredVersion,
1083         String service)
1084     {
1085         STAFResult res = handle.submit2(
1086             machine, service, "VERSION");
1087 
1088         if (res.rc != STAFResult.Ok)
1089         {
1090             return new STAFResult(
1091                 res.rc,
1092                 "Request VERSION submitted to the " + service +
1093                 " service on machine " + machine + " failed." +
1094                 "  Additional info: " + res.result);
1095         }
1096         else
1097         {
1098             STAFVersion version;
1099             STAFVersion minReqVersion;
1100 
1101             try
1102             {
1103                 version = new STAFVersion(res.result);
1104             }
1105             catch (NumberFormatException e)
1106             {
1107                 // Should never happen
1108                 return new STAFResult(
1109                     STAFResult.InvalidValue,
1110                     "Invalid value specified for the version: " +
1111                     res.result + ", Exception info: " + e.toString());
1112             }
1113 
1114             try
1115             {
1116                 minReqVersion = new STAFVersion(minRequiredVersion);
1117             }
1118             catch (NumberFormatException e)
1119             {
1120                 return new STAFResult(
1121                     STAFResult.InvalidValue,
1122                     "Invalid value specified for the minimum required " +
1123                     "version: " + minRequiredVersion +
1124                     ", Exception info: " + e.toString());
1125             }
1126 
1127             if (version.compareTo(minReqVersion) < 0)
1128             {
1129                 String servMsg = service + " service";
1130 
1131                 if (service.equalsIgnoreCase("MISC"))
1132                     servMsg = "STAF";
1133 
1134                 return new STAFResult(
1135                     STAFResult.InvalidSTAFVersion,
1136                     "Machine " + machine + " is running " + servMsg +
1137                     " Version " + version + ".  Version " +
1138                     minRequiredVersion + " or later is required.");
1139             }
1140             else
1141             {
1142                 return new STAFResult(STAFResult.Ok, version.toString());
1143             }
1144         }
1145     }
1146 
1147     /**
1148      * This method adds privacy delimiters to a string and returns the updated
1149      * string.
1150      * <p>
1151      * This method should be used by anyone who wants to protect private data
1152      * specified in a STAF command option that supports handling private data.
1153      * <p>
1154      * Examples:
1155      * <ul>
1156      * <li>If the data is "passw0rd", this method would return "!!@passw0rd@!!".
1157      * <li>If the data is "Password: !!@secret@!!", this method would return
1158      *     "!!@Password: ^!!@secret^@!!@!!".
1159      * </ul>
1160      * @param  data  a string containing data you want to protect
1161      * @return       a string containing the data with opening and closing
1162      *               privacy delimiters added and escapes any privacy
1163      *               delimiters already contained in the string with a caret
1164      *               (^). If the string has length 0 or already has an
1165      *               unescaped opening privacy delimiter at the beginning and
1166      *               an unescaped closing privacy delimiter at the end,
1167      *               privacy delimiters are not added.
1168      * @since STAF 3.1.0
1169      * @see <a href="http://staf.sourceforge.net/current/STAFUG.htm#HDRPRIVATEDATA">
1170      *      Section "7.3 Private Data" in the STAF User's Guide</a>
1171      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_addPrivacyDelimiters">
1172      *      Section "3.3.1 Static Method STAFUtil.addPrivacyDelimiters" in the
1173      *      STAF Java User's Guide</a>
1174      */
addPrivacyDelimiters(String data)1175     public static String addPrivacyDelimiters(String data)
1176     {
1177         return STAFUtilAddPrivacyDelimiters(data);
1178     }
1179 
1180     /**
1181      * This method removes all privacy delimiters from the input data and
1182      * returns the updated string.
1183      * <p>
1184      * Examples:
1185      * <ul>
1186      * <li>If the data is "!!@passw0rd@!!", this method would return
1187      *     "passw0rd".
1188      * <li>If the data is "!!@passw^@!!d@!!", this method would return
1189      *     "passw@!!d".
1190      * <li>If the data is "!!@Password=^!!@secret^@!!.@!!", this method
1191      *     would return "Password=secret".
1192      * </ul>
1193      *
1194      * @param  data  a string containing data that may contain privacy
1195      *               delimiters
1196      * @return       a string with the privacy delimiters removed
1197      * @see <a href="http://staf.sourceforge.net/current/STAFUG.htm#HDRPRIVATEDATA">
1198      *      Section "7.3 Private Data" in the STAF User's Guide</a>
1199      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_removePrivacyDelimiters">
1200      *      Section "3.3.3 Static Method STAFUtil.removePrivacyDelimiters" in the
1201      *      STAF Java User's Guide</a>
1202      * @since STAF 3.1.0
1203      */
removePrivacyDelimiters(String data)1204     public static String removePrivacyDelimiters(String data)
1205     {
1206         return STAFUtil.removePrivacyDelimiters(data, 0);
1207     }
1208 
1209     /**
1210      * This method removes privacy delimiters from the input data (allowing
1211      * removal of only a specified number of privacy levels) and returns the
1212      * updated string.
1213      * <p>
1214      * Examples:
1215      * <ul>
1216      * <li>If the data is "!!@passw0rd@!!", this method would return
1217      *     "passw0rd".
1218      * <li>If the data is "!!@passw^@!!d@!!", this method would return
1219      *     "passw@!!d".
1220      * <li>If the data is "!!@Password=^!!@secret^@!!.@!!" and the numLevels
1221      *     is 0, this method would return "Password=secret".
1222      * <li>If the data is "!!@Password=^!!@secret^@!!.@!!" and the numLevels
1223      *     is 1, this method would return "Password=!!@secret@!!".
1224      * </ul>
1225      *
1226      * @param  data       a string containing data that may be contain
1227      *                    privacy delimiters
1228      * @param  numLevels  the number of levels of privacy data to remove.
1229      *                    The default is 0 which indicates to remove all
1230      *                    levels of privacy data.  Note that, generally,
1231      *                    you'll want to remove all levels of privacy
1232      *                    delimiters.
1233      * @return            a string with the specified levels of privacy
1234      *                    delimiters removed
1235      * @see <a href="http://staf.sourceforge.net/current/STAFUG.htm#HDRPRIVATEDATA">
1236      *      Section "7.3 Private Data" in the STAF User's Guide</a>
1237      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_removePrivacyDelimiters">
1238      *      Section "3.3.3 Static Method STAFUtil.removePrivacyDelimiters" in the
1239      *      STAF Java User's Guide</a>
1240      * @since STAF 3.1.0
1241      */
removePrivacyDelimiters(String data, int numLevels)1242     public static String removePrivacyDelimiters(String data, int numLevels)
1243     {
1244         return STAFUtilRemovePrivacyDelimiters(data, numLevels);
1245     }
1246 
1247     /**
1248      * This method masks any private data indicated by the privacy delimiters
1249      * by replacing the private data with asterisks and returns the updated
1250      * string.
1251      * <p>
1252      * Examples:
1253      * <ul>
1254      * <li>If the data is "!!@passw0rd@!!", this method would
1255      *     return "**************".
1256      * <li>If the data is "testA -password !!@secret@!!", this method would
1257      *     return "testA -password ************".
1258      * </ul>
1259      *
1260      * @param  data       a string containing data that may contain privacy
1261      *                    delimiters
1262      * @return            a string with any private data masked
1263      * @see <a href="http://staf.sourceforge.net/current/STAFUG.htm#HDRPRIVATEDATA">
1264      *      Section "7.3 Private Data" in the STAF User's Guide</a>
1265      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_maskPrivateData">
1266      *      Section "3.3.4 Static Method STAFUtil.maskPrivateData" in the
1267      *      STAF Java User's Guide</a>
1268      * @since STAF 3.1.0
1269      */
maskPrivateData(String data)1270     public static String maskPrivateData(String data)
1271     {
1272         return STAFUtilMaskPrivateData(data);
1273     }
1274 
1275     /**
1276      * This method escapes all privacy delimiters (!!@ and @!!) found in the input data,
1277      * and returns the updated string.
1278      * This method escapes all privacy delimiters (!!@ and @!!) found in the
1279      * data with a caret (^) and returns the updated string.
1280      * <p>
1281      * This method should be used before calling the
1282      * <code>addPrivacyDelimiters</code> method for data that needs to be
1283      * protected but may contain substrings !!@ and/or @!! that should not be
1284      * mistaken for privacy delimiters.
1285      * <p>
1286      * For example, if the data is "passw@!!d", this method would return
1287      * "passw^@!!d".
1288      *
1289      * @param  data       a string containing data that may contin substrings
1290      *                    !!@ and/or @!! that should not be mistaken for
1291      *                    privacy delimiters
1292      * @return            a string with all privacy delimiters escaped with
1293      *                    the escape character (^)
1294      * @see <a href="http://staf.sourceforge.net/current/STAFUG.htm#HDRPRIVATEDATA">
1295      *      Section "7.3 Private Data" in the STAF User's Guide</a>
1296      * @see <a href="http://staf.sourceforge.net/current/STAFJava.htm#Header_escapePrivacyDelimiters">
1297      *      Section "3.3.2 Static Method STAFUtil.escapePrivacyDelimiters" in the
1298      *      STAF Java User's Guide</a>
1299      * @since  STAF 3.1.0
1300      */
escapePrivacyDelimiters(String data)1301     public static String escapePrivacyDelimiters(String data)
1302     {
1303         return STAFUtilEscapePrivacyDelimiters(data);
1304     }
1305 
1306     /************************/
1307     /* All the native stuff */
1308     /************************/
1309 
initialize()1310     private static native void initialize();
STAFUtilAddPrivacyDelimiters(String data)1311     private static native String STAFUtilAddPrivacyDelimiters(String data);
STAFUtilRemovePrivacyDelimiters( String data, int numLevels)1312     private static native String STAFUtilRemovePrivacyDelimiters(
1313         String data, int numLevels);
STAFUtilMaskPrivateData(String data)1314     private static native String STAFUtilMaskPrivateData(String data);
STAFUtilEscapePrivacyDelimiters(String data)1315     private static native String STAFUtilEscapePrivacyDelimiters(String data);
1316 
1317     // Static initializer - called first time class is loaded.
1318     static
1319     {
1320         if ((System.getProperty("os.name").toLowerCase().indexOf("aix") == 0) ||
1321            (System.getProperty("os.name").toLowerCase().indexOf("linux") == 0) ||
1322            ((System.getProperty("os.name").toLowerCase().indexOf("sunos") == 0)
1323              && (System.getProperty("os.arch").equals("sparc"))))
1324         {
1325             System.loadLibrary("STAF");
1326         }
1327 
1328         System.loadLibrary("JSTAF");
initialize()1329         initialize();
1330     }
1331 }
1332