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.WSDLToServiceProcessor;
035:
036: public class WSDLToService extends AbstractCXFToolContainer {
037:
038: static final String TOOL_NAME = "wsdl2service";
039:
040: public WSDLToService(ToolSpec toolspec) throws Exception {
041: super (TOOL_NAME, toolspec);
042: }
043:
044: private Set getArrayKeys() {
045: return new HashSet<String>();
046: }
047:
048: public void execute(boolean exitOnFinish) {
049: WSDLToServiceProcessor processor = new WSDLToServiceProcessor();
050: try {
051: super .execute(exitOnFinish);
052: if (!hasInfoOption()) {
053: ToolContext env = new ToolContext();
054: env.setParameters(getParametersMap(getArrayKeys()));
055:
056: if (isVerboseOn()) {
057: env.put(ToolConstants.CFG_VERBOSE, Boolean.TRUE);
058: }
059:
060: env.put(ToolConstants.CFG_CMD_ARG, getArgument());
061:
062: validate(env);
063:
064: processor.setEnvironment(env);
065: processor.process();
066: }
067: } catch (ToolException ex) {
068: if (ex.getCause() instanceof BadUsageException) {
069: printUsageException(TOOL_NAME, (BadUsageException) ex
070: .getCause());
071: }
072: System.err.println();
073: System.err.println("WSDLToService Error : "
074: + ex.getMessage());
075: if (isVerboseOn()) {
076: ex.printStackTrace();
077: }
078: } catch (Exception ex) {
079: System.err.println();
080: System.err.println("WSDLToService Error : "
081: + ex.getMessage());
082: if (isVerboseOn()) {
083: ex.printStackTrace();
084: }
085: }
086: }
087:
088: private void validate(ToolContext env) throws ToolException {
089: String outdir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
090: if (outdir != null) {
091: File dir = new File(outdir);
092: if (!dir.exists()) {
093: Message msg = new Message("DIRECTORY_NOT_EXIST", LOG,
094: outdir);
095: throw new ToolException(msg);
096: }
097: if (!dir.isDirectory()) {
098: Message msg = new Message("NOT_A_DIRECTORY", LOG,
099: outdir);
100: throw new ToolException(msg);
101: }
102: }
103: }
104:
105: public static void main(String[] pargs) {
106: try {
107: ToolRunner.runTool(WSDLToService.class, WSDLToService.class
108: .getResourceAsStream("wsdl2service.xml"), false,
109: pargs);
110: } catch (Exception ex) {
111: System.err.println("Error : " + ex.getMessage());
112: System.err.println();
113: ex.printStackTrace();
114: }
115: }
116:
117: public void checkParams(ErrorVisitor errors) throws ToolException {
118: CommandDocument doc = super .getCommandDocument();
119:
120: if (!doc.hasParameter("wsdlurl")) {
121: errors.add(new ErrorVisitor.UserError(
122: "WSDL/SCHEMA URL has to be specified"));
123: }
124: if (errors.getErrors().size() > 0) {
125: Message msg = new Message("PARAMETER_MISSING", LOG);
126: throw new ToolException(msg, new BadUsageException(
127: getUsage(), errors));
128: }
129: }
130: }
|