001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.util;
027:
028: import java.io.OutputStream;
029: import java.io.PrintStream;
030:
031: import com.sun.xml.internal.ws.util.localization.Localizable;
032: import com.sun.xml.internal.ws.util.localization.LocalizableMessageFactory;
033: import com.sun.xml.internal.ws.util.localization.Localizer;
034:
035: /**
036: * A base class for command-line tools.
037: *
038: * @author WS Development Team
039: */
040: public abstract class ToolBase {
041:
042: public ToolBase(OutputStream out, String program) {
043: this .out = out;
044: this .program = program;
045: initialize();
046: }
047:
048: protected void initialize() {
049: messageFactory = new LocalizableMessageFactory(
050: getResourceBundleName());
051: localizer = new Localizer();
052: }
053:
054: public boolean run(String[] args) {
055: if (!parseArguments(args)) {
056: return false;
057: }
058:
059: try {
060: run();
061: return wasSuccessful();
062: } catch (Exception e) {
063: if (e instanceof Localizable) {
064: report((Localizable) e);
065: } else {
066: report(getMessage(getGenericErrorMessage(), e
067: .toString()));
068: }
069: printStackTrace(e);
070: return false;
071: }
072: }
073:
074: public boolean wasSuccessful() {
075: return true;
076: }
077:
078: protected abstract boolean parseArguments(String[] args);
079:
080: protected abstract void run() throws Exception;
081:
082: public void runProcessorActions() {
083: }
084:
085: protected abstract String getGenericErrorMessage();
086:
087: protected abstract String getResourceBundleName();
088:
089: public void printStackTrace(Throwable t) {
090: PrintStream outstream = out instanceof PrintStream ? (PrintStream) out
091: : new PrintStream(out, true);
092: t.printStackTrace(outstream);
093: outstream.flush();
094: }
095:
096: protected void report(String msg) {
097: PrintStream outstream = out instanceof PrintStream ? (PrintStream) out
098: : new PrintStream(out, true);
099: outstream.println(msg);
100: outstream.flush();
101: }
102:
103: protected void report(Localizable msg) {
104: report(localizer.localize(msg));
105: }
106:
107: public Localizable getMessage(String key) {
108: return getMessage(key, (Object[]) null);
109: }
110:
111: public Localizable getMessage(String key, String arg) {
112: return messageFactory.getMessage(key, new Object[] { arg });
113: }
114:
115: public Localizable getMessage(String key, String arg1, String arg2) {
116: return messageFactory.getMessage(key,
117: new Object[] { arg1, arg2 });
118: }
119:
120: public Localizable getMessage(String key, String arg1, String arg2,
121: String arg3) {
122: return messageFactory.getMessage(key, new Object[] { arg1,
123: arg2, arg3 });
124: }
125:
126: public Localizable getMessage(String key, Localizable localizable) {
127: return messageFactory.getMessage(key,
128: new Object[] { localizable });
129: }
130:
131: public Localizable getMessage(String key, Object[] args) {
132: return messageFactory.getMessage(key, args);
133: }
134:
135: protected OutputStream out;
136: protected String program;
137: protected Localizer localizer;
138: protected LocalizableMessageFactory messageFactory;
139:
140: protected final static String TRUE = "true";
141: protected final static String FALSE = "false";
142:
143: }
|