Index: modules/tools/src/main/java/org/apache/harmony/tools/jarsigner/UserInteractor.java =================================================================== --- modules/tools/src/main/java/org/apache/harmony/tools/jarsigner/UserInteractor.java (revision 442109) +++ modules/tools/src/main/java/org/apache/harmony/tools/jarsigner/UserInteractor.java (working copy) @@ -18,6 +18,10 @@ import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStream; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.logging.StreamHandler; /** * Class to interact with user - ask for confirmations, and necessary parameters @@ -37,14 +41,81 @@ // when ENTER is pressed. private static int newLineLength = 2; + // an instance of Logger, used to manage message output + private final static Logger logger = Logger.getLogger("JarSignerLogger"); + + // log handler + private static StreamHandler logHandler = new StreamHandler(System.out, + new JSLogFormatter()); + + static { + // Do not send massages to another handlers. + logger.setUseParentHandlers(false); + // log to System.out by default. + logger.addHandler(logHandler); + } + // Prints prompt and waits the user to enter the needed data, // tha data is returned. - static char [] getDataFromUser(String prompt) throws IOException{ - System.out.print(prompt); + static char[] getDataFromUser(String prompt) throws IOException { + print(prompt); charsRead = in.read(readData); char[] password = new char[charsRead - newLineLength]; System.arraycopy(readData, 0, password, 0, charsRead - newLineLength); return password; } + + static void setOutputStream(OutputStream out) { + logger.removeHandler(logHandler); + // reuse the Formatter from old handler + logHandler = new StreamHandler(out, logHandler.getFormatter()); + logger.addHandler(logHandler); + } + + // prints the given message, if the output is not turned off with + // setNoOutput() + static void print(String msg) { + logger.info(msg); + logHandler.flush(); + } + + // prints the given message and a new line symbol, if the output is not + // turned off with setNoOutput() + static void println(String msg) { + logger.info(msg + "\n"); + logHandler.flush(); + } + + // prints the given message, if verbose output is set with + // setVerboseOutput() + static void printIfVerbose(String msg) { + logger.fine(msg); + logHandler.flush(); + } + + // prints the given message and a new line symbol, if verbose output is set + // with setVerboseOutput() + static void printlnIfVerbose(String msg) { + logger.fine(msg + "\n"); + logHandler.flush(); + } + + // turns off the output + static void setNoOutput() { + logger.setLevel(Level.OFF); + logger.setLevel(Level.OFF); + } + + // sets the verbose output + static void setVerboseOutput() { + logger.setLevel(Level.ALL); + logHandler.setLevel(Level.ALL); + } + + // sets the normal output + static void setNormalOutput() { + logger.setLevel(Level.INFO); + logHandler.setLevel(Level.INFO); + } } Index: modules/tools/src/main/java/org/apache/harmony/tools/jarsigner/Main.java =================================================================== --- modules/tools/src/main/java/org/apache/harmony/tools/jarsigner/Main.java (revision 442109) +++ modules/tools/src/main/java/org/apache/harmony/tools/jarsigner/Main.java (working copy) @@ -16,7 +16,9 @@ package org.apache.harmony.tools.jarsigner; +import java.io.OutputStream; + /** * The main class that bundles command line parsing, interaction with the user, * exception handling and JAR signing and verification work. @@ -39,8 +41,12 @@ * @param args - * command line with options. */ - public static void run(String[] args) throws Exception { + public static void run(String[] args, OutputStream out) throws Exception { // TODO + if (out != System.out){ + UserInteractor.setOutputStream(out); + } + } @@ -52,7 +58,7 @@ */ public static void main(String[] args) { try { - run(args); + run(args, System.out); } catch (Exception e) { // System.out.println("JarSigner error: " + e); e.printStackTrace(); Index: modules/tools/src/main/java/org/apache/harmony/tools/jarsigner/JSLogFormatter.java =================================================================== --- modules/tools/src/main/java/org/apache/harmony/tools/jarsigner/JSLogFormatter.java (revision 0) +++ modules/tools/src/main/java/org/apache/harmony/tools/jarsigner/JSLogFormatter.java (revision 0) @@ -0,0 +1,31 @@ +/* + * Copyright 2006 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package org.apache.harmony.tools.jarsigner; + +import java.util.logging.LogRecord; +import java.util.logging.SimpleFormatter; + +/** + * Simple formatter to output given Strings without formatting. + */ +class JSLogFormatter extends SimpleFormatter { + @Override + public String format(LogRecord r) { + return r.getMessage(); + } +} +