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.wsdlto;
019:
020: import java.io.InputStream;
021: import java.util.Arrays;
022: import java.util.List;
023:
024: import org.apache.cxf.common.util.StringUtils;
025: import org.apache.cxf.tools.common.ToolConstants;
026: import org.apache.cxf.tools.common.ToolContext;
027: import org.apache.cxf.tools.common.ToolException;
028: import org.apache.cxf.tools.common.toolspec.ToolContainer;
029: import org.apache.cxf.tools.common.toolspec.ToolRunner;
030: import org.apache.cxf.tools.wsdlto.core.DataBindingProfile;
031: import org.apache.cxf.tools.wsdlto.core.FrontEndProfile;
032: import org.apache.cxf.tools.wsdlto.core.PluginLoader;
033:
034: public class WSDLToJava {
035:
036: public static final String DEFAULT_FRONTEND_NAME = "jaxws";
037: public static final String DEFAULT_DATABINDING_NAME = "jaxb";
038:
039: private String[] args;
040:
041: private PluginLoader pluginLoader = PluginLoader.getInstance();
042:
043: public WSDLToJava() {
044: args = new String[0];
045: }
046:
047: public WSDLToJava(String pargs[]) {
048: args = pargs;
049: }
050:
051: private FrontEndProfile loadFrontEnd(String name) {
052: if (StringUtils.isEmpty(name)) {
053: name = DEFAULT_FRONTEND_NAME;
054: }
055: if (isVerbose()) {
056: System.out.println("Loading FrontEnd " + name + " ...");
057: }
058: return pluginLoader.getFrontEndProfile(name);
059: }
060:
061: private DataBindingProfile loadDataBinding(String name) {
062: if (StringUtils.isEmpty(name)) {
063: name = DEFAULT_DATABINDING_NAME;
064: }
065: if (isVerbose()) {
066: System.out.println("Loading DataBinding " + name + " ...");
067: }
068: return pluginLoader.getDataBindingProfile(name);
069: }
070:
071: private boolean isExitOnFinish() {
072: String exit = System.getProperty("exitOnFinish");
073: if (StringUtils.isEmpty(exit)) {
074: return false;
075: }
076: return "YES".equalsIgnoreCase(exit)
077: || "TRUE".equalsIgnoreCase(exit);
078: }
079:
080: public void run(ToolContext context) throws Exception {
081: FrontEndProfile frontend = null;
082: if (args != null) {
083: context.put(ToolConstants.CFG_CMD_ARG, args);
084: frontend = loadFrontEnd(getFrontEndName(args));
085: } else {
086: frontend = loadFrontEnd("");
087: }
088:
089: context.put(FrontEndProfile.class, frontend);
090:
091: DataBindingProfile databinding = loadDataBinding(getDataBindingName(args));
092:
093: context.put(DataBindingProfile.class, databinding);
094:
095: Class<? extends ToolContainer> containerClass = frontend
096: .getContainerClass();
097:
098: InputStream toolspecStream = getResourceAsStream(
099: containerClass, frontend.getToolspec());
100:
101: ToolRunner.runTool(containerClass, toolspecStream, false, args,
102: isExitOnFinish(), context);
103: }
104:
105: protected boolean isVerbose() {
106: return isSet(new String[] { "-V", "-verbose" });
107: }
108:
109: private boolean isSet(String[] keys) {
110: if (args == null) {
111: return false;
112: }
113: List<String> pargs = Arrays.asList(args);
114:
115: for (String key : keys) {
116: if (pargs.contains(key)) {
117: return true;
118: }
119: }
120: return false;
121: }
122:
123: private String parseArguments(String[] pargs, String key) {
124: if (pargs == null) {
125: return null;
126: }
127: List<String> largs = Arrays.asList(pargs);
128:
129: int index = 0;
130: if (largs.contains(key)) {
131: index = largs.indexOf(key);
132: if (index + 1 < largs.size()) {
133: return largs.get(index + 1);
134: }
135: }
136: return null;
137: }
138:
139: private String getOptionValue(String[] pargs, String[] keys) {
140: for (String key : keys) {
141: String value = parseArguments(pargs, key);
142: if (!StringUtils.isEmpty(value)) {
143: return value.trim();
144: }
145: }
146: return null;
147: }
148:
149: protected String getFrontEndName(String[] pargs) {
150: return getOptionValue(pargs,
151: new String[] { "-frontend", "-fe" });
152: }
153:
154: protected String getDataBindingName(String[] pargs) {
155: return getOptionValue(pargs, new String[] { "-databinding",
156: "-db" });
157: }
158:
159: public void setArguments(String[] pargs) {
160: args = pargs;
161: }
162:
163: public static void main(String[] pargs) {
164:
165: WSDLToJava w2j = new WSDLToJava(pargs);
166: try {
167:
168: w2j.run(new ToolContext());
169:
170: } catch (ToolException ex) {
171: System.err.println();
172: System.err.println("WSDLToJava Error : " + ex.getMessage());
173: System.err.println();
174: if (w2j.isVerbose()) {
175: ex.printStackTrace();
176: }
177: if (w2j.isExitOnFinish()) {
178: System.exit(1);
179: }
180: } catch (Exception ex) {
181: System.err.println("WSDLToJava Error : " + ex.getMessage());
182: System.err.println();
183: if (w2j.isVerbose()) {
184: ex.printStackTrace();
185: }
186: if (w2j.isExitOnFinish()) {
187: System.exit(1);
188: }
189: }
190: if (w2j.isExitOnFinish()) {
191: System.exit(0);
192: }
193: }
194:
195: private static InputStream getResourceAsStream(Class clz,
196: String file) {
197: return clz.getResourceAsStream(file);
198: }
199: }
|