KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > utilint > CmdUtil


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: CmdUtil.java,v 1.21 2006/10/30 21:14:28 bostic Exp $
7  */

8
9 package com.sleepycat.je.utilint;
10
11 import java.io.File JavaDoc;
12
13 import com.sleepycat.je.DatabaseException;
14 import com.sleepycat.je.EnvironmentConfig;
15 import com.sleepycat.je.config.EnvironmentParams;
16 import com.sleepycat.je.dbi.EnvironmentImpl;
17
18 /**
19  * Convenience methods for command line utilities.
20  */

21 public class CmdUtil {
22     public static String JavaDoc getArg(String JavaDoc [] argv, int whichArg)
23         throws IllegalArgumentException JavaDoc {
24
25         if (whichArg < argv.length) {
26             return argv[whichArg];
27         } else {
28             throw new IllegalArgumentException JavaDoc();
29         }
30     }
31
32     /**
33      * Parse a string into a long. If the string starts with 0x, this is a hex
34      * number, else it's decimal.
35      */

36     public static long readLongNumber(String JavaDoc longVal) {
37         if (longVal.startsWith("0x")) {
38             return Long.parseLong(longVal.substring(2), 16);
39         } else {
40             return Long.parseLong(longVal);
41         }
42     }
43
44     private static final String JavaDoc printableChars =
45     "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
46     "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
47
48     public static void formatEntry(StringBuffer JavaDoc sb,
49                                    byte[] entryData,
50                                    boolean formatUsingPrintable) {
51     for (int i = 0; i < entryData.length; i++) {
52         int b = entryData[i] & 0xff;
53         if (formatUsingPrintable) {
54         if (isPrint(b)) {
55             if (b == 0134) { /* backslash */
56             sb.append('\\');
57             }
58             sb.append(printableChars.charAt(b - 33));
59         } else {
60             sb.append('\\');
61             String JavaDoc hex = Integer.toHexString(b);
62             if (b < 16) {
63             sb.append('0');
64             }
65             sb.append(hex);
66         }
67         } else {
68         String JavaDoc hex = Integer.toHexString(b);
69         if (b < 16) {
70             sb.append('0');
71         }
72         sb.append(hex);
73         }
74     }
75     }
76
77     private static boolean isPrint(int b) {
78     return (b < 0177) && (040 < b);
79     }
80
81     /**
82      * Create an environment suitable for utilities. Utilities should in
83      * general send trace output to the console and not to the db log.
84      */

85     public static EnvironmentImpl makeUtilityEnvironment(File JavaDoc envHome,
86                              boolean readOnly)
87         throws DatabaseException {
88         
89         EnvironmentConfig config = new EnvironmentConfig();
90         config.setReadOnly(readOnly);
91         
92         /* Don't debug log to the database log. */
93         config.setConfigParam(EnvironmentParams.JE_LOGGING_DBLOG.getName(),
94                   "false");
95         /* Do debug log to the console. */
96         config.setConfigParam(EnvironmentParams.JE_LOGGING_CONSOLE.getName(),
97                   "true");
98
99         /* Set logging level to only show errors. */
100         config.setConfigParam(EnvironmentParams.JE_LOGGING_LEVEL.getName(),
101                   "SEVERE");
102
103         /* Don't run recovery. */
104         config.setConfigParam(EnvironmentParams.ENV_RECOVERY.getName(),
105                   "false");
106
107     EnvironmentImpl envImpl = new EnvironmentImpl(envHome, config);
108     return envImpl;
109     }
110
111     /**
112      * Returns a description of the java command for running a utility, without
113      * arguments. For utilities the last name of the class name can be
114      * specified when "-jar je.jar" is used.
115      */

116     public static String JavaDoc getJavaCommand(Class JavaDoc cls) {
117
118         String JavaDoc clsName = cls.getName();
119         String JavaDoc lastName = clsName.substring(clsName.lastIndexOf('.') + 1);
120
121         return "java { " + cls.getName() + " | -jar je-<version>.jar " + lastName + " }";
122     }
123 }
124
Popular Tags