001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.common.toolspec;
019:
020: import java.io.IOException;
021: import java.io.OutputStream;
022: import java.io.PrintStream;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import org.apache.cxf.common.i18n.Message;
027: import org.apache.cxf.common.logging.LogUtils;
028: import org.apache.cxf.tools.common.ToolContext;
029: import org.apache.cxf.tools.common.ToolException;
030: import org.apache.cxf.tools.common.toolspec.parser.BadUsageException;
031: import org.apache.cxf.tools.common.toolspec.parser.CommandDocument;
032: import org.apache.cxf.tools.common.toolspec.parser.CommandLineParser;
033:
034: public abstract class AbstractToolContainer implements ToolContainer {
035: private static final Logger LOG = LogUtils
036: .getL7dLogger(AbstractToolContainer.class);
037:
038: protected ToolSpec toolspec;
039: protected ToolContext context;
040:
041: private String arguments[];
042: private boolean isVerbose;
043: private boolean isQuiet;
044: private CommandDocument commandDoc;
045: private CommandLineParser parser;
046: private OutputStream outOutputStream;
047: private OutputStream errOutputStream;
048:
049: public class GenericOutputStream extends OutputStream {
050: public void write(int b) throws IOException {
051:
052: }
053: }
054:
055: public AbstractToolContainer() {
056:
057: }
058:
059: public AbstractToolContainer(ToolSpec ts) throws BadUsageException {
060: toolspec = ts;
061: }
062:
063: public void setArguments(String[] args) {
064: if (args == null) {
065: return;
066: }
067: arguments = new String[args.length];
068: System.arraycopy(args, 0, arguments, 0, args.length);
069: setMode(args);
070: if (isQuietMode()) {
071: redirectOutput();
072: }
073: }
074:
075: public void parseCommandLine() throws BadUsageException {
076: if (toolspec != null) {
077: parser = new CommandLineParser(toolspec);
078: commandDoc = parser.parseArguments(arguments);
079: }
080: }
081:
082: public void setMode(String[] args) {
083: for (int i = 0; i < args.length; i++) {
084: if ("-q".equals(args[i])) {
085: isQuiet = true;
086: }
087: if ("-quiet".equals(args[i])) {
088: isQuiet = true;
089: }
090: if ("-V".equals(args[i])) {
091: isVerbose = true;
092: }
093: if ("-verbose".equals(args[i])) {
094: isVerbose = true;
095: }
096: }
097: }
098:
099: public void init() throws ToolException {
100: // initialize
101: if (toolspec == null) {
102: Message message = new Message("TOOLSPEC_NOT_INITIALIZED",
103: LOG);
104: LOG.log(Level.SEVERE, message.toString());
105: throw new ToolException(message);
106: }
107: }
108:
109: public CommandDocument getCommandDocument() {
110: return commandDoc;
111: }
112:
113: public CommandLineParser getCommandLineParser() {
114: return parser;
115: }
116:
117: public void redirectOutput() {
118: outOutputStream = new GenericOutputStream();
119: errOutputStream = new GenericOutputStream();
120: System.setErr(new PrintStream(errOutputStream));
121: System.setOut(new PrintStream(outOutputStream));
122: }
123:
124: public boolean isQuietMode() {
125: return isQuiet;
126: }
127:
128: public boolean isVerboseMode() {
129: return isVerbose;
130: }
131:
132: public String[] getArgument() {
133: return arguments;
134: }
135:
136: public OutputStream getOutOutputStream() {
137: return outOutputStream;
138: }
139:
140: public OutputStream getErrOutputStream() {
141: return errOutputStream;
142: }
143:
144: public void setContext(ToolContext c) {
145: context = c;
146: }
147:
148: public ToolContext getContext() {
149: if (context == null) {
150: context = new ToolContext();
151: }
152: return context;
153: }
154:
155: public void execute(boolean exitOnFinish) throws ToolException {
156: init();
157: try {
158: parseCommandLine();
159: } catch (BadUsageException bue) {
160: throw new ToolException(bue);
161: }
162: }
163:
164: }
|