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.misc;
019:
020: import java.io.File;
021: import java.util.HashSet;
022: import java.util.Set;
023:
024: import org.apache.cxf.common.i18n.Message;
025: import org.apache.cxf.tools.common.AbstractCXFToolContainer;
026: import org.apache.cxf.tools.common.ToolConstants;
027: import org.apache.cxf.tools.common.ToolContext;
028: import org.apache.cxf.tools.common.ToolException;
029: import org.apache.cxf.tools.common.toolspec.ToolRunner;
030: import org.apache.cxf.tools.common.toolspec.ToolSpec;
031: import org.apache.cxf.tools.common.toolspec.parser.BadUsageException;
032: import org.apache.cxf.tools.common.toolspec.parser.CommandDocument;
033: import org.apache.cxf.tools.common.toolspec.parser.ErrorVisitor;
034: import org.apache.cxf.tools.misc.processor.WSDLToSoapProcessor;
035:
036: public class WSDLToSoap extends AbstractCXFToolContainer {
037:
038: static final String TOOL_NAME = "wsdl2soap";
039: static final String BINDING_NAME_POSFIX = "_Binding";
040: static final String STYLE_DEF_VALUE = "document";
041: static final String USE_DEF_VALUE = "literal";
042:
043: public WSDLToSoap(ToolSpec toolspec) throws Exception {
044: super (TOOL_NAME, toolspec);
045: }
046:
047: private Set getArrayKeys() {
048: return new HashSet<String>();
049: }
050:
051: public void execute(boolean exitOnFinish) {
052: WSDLToSoapProcessor processor = new WSDLToSoapProcessor();
053: try {
054: super .execute(exitOnFinish);
055: if (!hasInfoOption()) {
056: ToolContext env = new ToolContext();
057: env.setParameters(getParametersMap(getArrayKeys()));
058: if (isVerboseOn()) {
059: env.put(ToolConstants.CFG_VERBOSE, Boolean.TRUE);
060: }
061: env.put(ToolConstants.CFG_CMD_ARG, getArgument());
062:
063: validate(env);
064: setEnvParamDefValues(env);
065:
066: processor.setEnvironment(env);
067: processor.process();
068: }
069: } catch (ToolException ex) {
070: if (ex.getCause() instanceof BadUsageException) {
071: printUsageException(TOOL_NAME, (BadUsageException) ex
072: .getCause());
073: }
074: System.err.println();
075: System.err.println("WSDLToSoap Error : " + ex.getMessage());
076: if (isVerboseOn()) {
077: ex.printStackTrace();
078: }
079: } catch (Exception ex) {
080: System.err.println();
081: System.err.println("WSDLToSoap Error : " + ex.getMessage());
082: if (isVerboseOn()) {
083: ex.printStackTrace();
084: }
085: }
086: }
087:
088: private void setEnvParamDefValues(ToolContext env) {
089: if (!env.optionSet(ToolConstants.CFG_BINDING)) {
090: env.put(ToolConstants.CFG_BINDING, env
091: .get(ToolConstants.CFG_PORTTYPE)
092: + BINDING_NAME_POSFIX);
093: }
094: if (!env.optionSet(ToolConstants.CFG_STYLE)) {
095: env.put(ToolConstants.CFG_STYLE, STYLE_DEF_VALUE);
096: }
097: if (!env.optionSet(ToolConstants.CFG_USE)) {
098: env.put(ToolConstants.CFG_USE, USE_DEF_VALUE);
099: }
100: }
101:
102: private void validate(ToolContext env) throws ToolException {
103: String outdir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
104: if (outdir != null) {
105: File dir = new File(outdir);
106: if (!dir.exists()) {
107: Message msg = new Message("DIRECTORY_NOT_EXIST", LOG,
108: outdir);
109: throw new ToolException(msg);
110: }
111: if (!dir.isDirectory()) {
112: Message msg = new Message("NOT_A_DIRECTORY", LOG,
113: outdir);
114: throw new ToolException(msg);
115: }
116: }
117: }
118:
119: public static void main(String[] pargs) {
120: try {
121: ToolRunner
122: .runTool(WSDLToSoap.class, WSDLToSoap.class
123: .getResourceAsStream("wsdl2soap.xml"),
124: false, pargs);
125: } catch (ToolException ex) {
126: System.err.println("WSDL2Soap Error : " + ex.getMessage());
127: System.err.println();
128: ex.printStackTrace();
129: } catch (Exception ex) {
130: System.err.println("WSDL2Soap Error : " + ex.getMessage());
131: System.err.println();
132: ex.printStackTrace();
133: }
134: }
135:
136: public void checkParams(ErrorVisitor errors) throws ToolException {
137: CommandDocument doc = super .getCommandDocument();
138:
139: if (!doc.hasParameter("wsdlurl")) {
140: errors.add(new ErrorVisitor.UserError(
141: "WSDL/SCHEMA URL has to be specified"));
142: }
143: if (errors.getErrors().size() > 0) {
144: Message msg = new Message("PARAMETER_MISSING", LOG);
145: throw new ToolException(msg, new BadUsageException(
146: getUsage(), errors));
147: }
148: }
149:
150: }
|