01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.tools.common.toolspec;
19:
20: import java.io.InputStream;
21: import java.lang.reflect.Constructor;
22: import java.util.logging.Level;
23: import java.util.logging.Logger;
24:
25: import org.apache.cxf.common.i18n.Message;
26: import org.apache.cxf.common.logging.LogUtils;
27: import org.apache.cxf.tools.common.ToolContext;
28: import org.apache.cxf.tools.common.ToolException;
29:
30: public final class ToolRunner {
31: private static final Logger LOG = LogUtils
32: .getL7dLogger(ToolRunner.class);
33:
34: private ToolRunner() {
35: // utility class - never constructed
36: }
37:
38: public static void runTool(Class<? extends ToolContainer> clz,
39: InputStream toolspecStream, boolean validate, String[] args)
40: throws Exception {
41: runTool(clz, toolspecStream, validate, args, true);
42: }
43:
44: public static void runTool(Class<? extends ToolContainer> clz,
45: InputStream toolspecStream, boolean validate,
46: String[] args, ToolContext context) throws Exception {
47: runTool(clz, toolspecStream, validate, args, true, context);
48: }
49:
50: public static void runTool(Class<? extends ToolContainer> clz,
51: InputStream toolspecStream, boolean validate,
52: String[] args, boolean exitOnFinish) throws Exception {
53: runTool(clz, toolspecStream, validate, args, true, null);
54: }
55:
56: public static void runTool(Class<? extends ToolContainer> clz,
57: InputStream toolspecStream, boolean validate,
58: String[] args, boolean exitOnFinish, ToolContext context)
59: throws Exception {
60:
61: ToolContainer container = null;
62:
63: try {
64: Constructor<? extends ToolContainer> cons = clz
65: .getConstructor(new Class[] { ToolSpec.class });
66: container = cons.newInstance(new Object[] { new ToolSpec(
67: toolspecStream, validate) });
68: } catch (Exception ex) {
69: Message message = new Message("CLZ_CANNOT_BE_CONSTRUCTED",
70: LOG, clz.getName());
71: LOG.log(Level.SEVERE, message.toString());
72: throw new ToolException(message, ex);
73: }
74:
75: try {
76: container.setArguments(args);
77: container.setContext(context);
78: container.execute(exitOnFinish);
79: } catch (Exception ex) {
80: throw ex;
81: }
82:
83: }
84:
85: }
|