001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: CmdUtil.java,v 1.21.2.3 2008/01/07 15:14:18 cwl Exp $
007: */
008:
009: package com.sleepycat.je.utilint;
010:
011: import java.io.File;
012:
013: import com.sleepycat.je.DatabaseException;
014: import com.sleepycat.je.EnvironmentConfig;
015: import com.sleepycat.je.config.EnvironmentParams;
016: import com.sleepycat.je.dbi.EnvironmentImpl;
017:
018: /**
019: * Convenience methods for command line utilities.
020: */
021: public class CmdUtil {
022: public static String getArg(String[] argv, int whichArg)
023: throws IllegalArgumentException {
024:
025: if (whichArg < argv.length) {
026: return argv[whichArg];
027: } else {
028: throw new IllegalArgumentException();
029: }
030: }
031:
032: /**
033: * Parse a string into a long. If the string starts with 0x, this is a hex
034: * number, else it's decimal.
035: */
036: public static long readLongNumber(String longVal) {
037: if (longVal.startsWith("0x")) {
038: return Long.parseLong(longVal.substring(2), 16);
039: } else {
040: return Long.parseLong(longVal);
041: }
042: }
043:
044: private static final String printableChars = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
045: + "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
046:
047: public static void formatEntry(StringBuffer sb, byte[] entryData,
048: boolean formatUsingPrintable) {
049: for (int i = 0; i < entryData.length; i++) {
050: int b = entryData[i] & 0xff;
051: if (formatUsingPrintable) {
052: if (isPrint(b)) {
053: if (b == 0134) { /* backslash */
054: sb.append('\\');
055: }
056: sb.append(printableChars.charAt(b - 33));
057: } else {
058: sb.append('\\');
059: String hex = Integer.toHexString(b);
060: if (b < 16) {
061: sb.append('0');
062: }
063: sb.append(hex);
064: }
065: } else {
066: String hex = Integer.toHexString(b);
067: if (b < 16) {
068: sb.append('0');
069: }
070: sb.append(hex);
071: }
072: }
073: }
074:
075: private static boolean isPrint(int b) {
076: return (b < 0177) && (040 < b);
077: }
078:
079: /**
080: * Create an environment suitable for utilities. Utilities should in
081: * general send trace output to the console and not to the db log.
082: */
083: public static EnvironmentImpl makeUtilityEnvironment(File envHome,
084: boolean readOnly) throws DatabaseException {
085:
086: EnvironmentConfig config = new EnvironmentConfig();
087: config.setReadOnly(readOnly);
088:
089: /* Don't debug log to the database log. */
090: config.setConfigParam(EnvironmentParams.JE_LOGGING_DBLOG
091: .getName(), "false");
092: /* Do debug log to the console. */
093: config.setConfigParam(EnvironmentParams.JE_LOGGING_CONSOLE
094: .getName(), "true");
095:
096: /* Set logging level to only show errors. */
097: config.setConfigParam(EnvironmentParams.JE_LOGGING_LEVEL
098: .getName(), "SEVERE");
099:
100: /* Don't run recovery. */
101: config.setConfigParam(EnvironmentParams.ENV_RECOVERY.getName(),
102: "false");
103:
104: EnvironmentImpl envImpl = new EnvironmentImpl(envHome, config);
105: return envImpl;
106: }
107:
108: /**
109: * Returns a description of the java command for running a utility, without
110: * arguments. For utilities the last name of the class name can be
111: * specified when "-jar je.jar" is used.
112: */
113: public static String getJavaCommand(Class cls) {
114:
115: String clsName = cls.getName();
116: String lastName = clsName
117: .substring(clsName.lastIndexOf('.') + 1);
118:
119: return "java { " + cls.getName() + " | -jar je-<version>.jar "
120: + lastName + " }";
121: }
122: }
|