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.java2wsdl.processor;
019:
020: import java.io.File;
021: import java.util.ArrayList;
022: import java.util.List;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025: import javax.xml.namespace.QName;
026: import javax.xml.ws.BindingType;
027: import javax.xml.ws.soap.SOAPBinding;
028:
029: import org.apache.cxf.Bus;
030: import org.apache.cxf.BusFactory;
031: import org.apache.cxf.common.i18n.Message;
032: import org.apache.cxf.common.logging.LogUtils;
033: import org.apache.cxf.common.util.StringUtils;
034: import org.apache.cxf.service.ServiceBuilder;
035: import org.apache.cxf.service.model.EndpointInfo;
036: import org.apache.cxf.service.model.ServiceInfo;
037: import org.apache.cxf.tools.common.Processor;
038: import org.apache.cxf.tools.common.ToolConstants;
039: import org.apache.cxf.tools.common.ToolContext;
040: import org.apache.cxf.tools.common.ToolException;
041: import org.apache.cxf.tools.java2wsdl.generator.AbstractGenerator;
042: import org.apache.cxf.tools.java2wsdl.generator.WSDLGeneratorFactory;
043: import org.apache.cxf.tools.java2wsdl.generator.wsdl11.FaultBeanGenerator;
044: import org.apache.cxf.tools.java2wsdl.generator.wsdl11.WrapperBeanGenerator;
045: import org.apache.cxf.tools.java2wsdl.processor.internal.ServiceBuilderFactory;
046: import org.apache.cxf.tools.util.AnnotationUtil;
047: import org.apache.cxf.wsdl.WSDLConstants;
048:
049: public class JavaToProcessor implements Processor {
050: private static final Logger LOG = LogUtils
051: .getL7dLogger(JavaToProcessor.class);
052: private static final String DEFAULT_ADDRESS = "http://localhost:9090/hello";
053: private static final String JAVA_CLASS_PATH = "java.class.path";
054: private ToolContext context;
055: private final List<AbstractGenerator> generators = new ArrayList<AbstractGenerator>();
056:
057: private void customize(ServiceInfo service) {
058: if (context.containsKey(ToolConstants.CFG_TNS)) {
059: String ns = (String) context.get(ToolConstants.CFG_TNS);
060: service.setTargetNamespace(ns);
061: }
062:
063: if (context.containsKey(ToolConstants.CFG_PORT)) {
064: String portName = (String) context
065: .get(ToolConstants.CFG_PORT);
066: EndpointInfo einfo = service.getEndpoints().iterator()
067: .next();
068: QName qn = new QName(einfo.getName().getNamespaceURI(),
069: portName);
070: einfo.setName(qn);
071: }
072:
073: if (context.containsKey(ToolConstants.CFG_SERVICENAME)) {
074: String svName = getServiceName();
075: service.setName(new QName(service.getName()
076: .getNamespaceURI(), svName));
077: }
078: }
079:
080: public void process() throws ToolException {
081: String oldClassPath = System.getProperty(JAVA_CLASS_PATH);
082: LOG.log(Level.INFO, "OLD_CP", oldClassPath);
083: if (context.get(ToolConstants.CFG_CLASSPATH) != null) {
084: String newCp = (String) context
085: .get(ToolConstants.CFG_CLASSPATH);
086: System.setProperty(JAVA_CLASS_PATH, newCp
087: + File.pathSeparator + oldClassPath);
088: LOG.log(Level.INFO, "NEW_CP", newCp);
089: }
090:
091: ServiceBuilder builder = getServiceBuilder();
092: ServiceInfo service = builder.createService();
093:
094: customize(service);
095:
096: File wsdlFile = getOutputFile(builder.getOutputFile(), service
097: .getName().getLocalPart()
098: + ".wsdl");
099:
100: File outputDir = getOutputDir(wsdlFile);
101:
102: generators.add(getWSDLGenerator(wsdlFile));
103: generators.add(getWrapperBeanGenerator());
104: generators.add(getFaultBeanGenerator());
105:
106: generate(service, outputDir);
107: System.setProperty(JAVA_CLASS_PATH, oldClassPath);
108: LOG.log(Level.INFO, "RESUME_CP", oldClassPath);
109: }
110:
111: private AbstractGenerator getWrapperBeanGenerator() {
112: WrapperBeanGenerator generator = new WrapperBeanGenerator();
113: generator.setOutputBase(getSourceDir());
114: generator.setCompileToDir(getClassesDir());
115: return generator;
116: }
117:
118: private AbstractGenerator getFaultBeanGenerator() {
119: FaultBeanGenerator generator = new FaultBeanGenerator();
120: generator.setOutputBase(getSourceDir());
121: generator.setCompileToDir(getClassesDir());
122: return generator;
123: }
124:
125: private AbstractGenerator getWSDLGenerator(final File wsdlFile) {
126: WSDLGeneratorFactory factory = new WSDLGeneratorFactory();
127: factory.setWSDLVersion(getWSDLVersion());
128:
129: AbstractGenerator generator = factory.newGenerator();
130: generator.setAllowImports(context
131: .containsKey(ToolConstants.CFG_CREATE_XSD_IMPORTS));
132: generator.setOutputBase(wsdlFile);
133: return generator;
134: }
135:
136: public void generate(ServiceInfo service, File output)
137: throws ToolException {
138: for (AbstractGenerator generator : generators) {
139: generator.setServiceModel(service);
140: generator.setBus(getBus());
141: generator.generate(output);
142: }
143: }
144:
145: public ServiceBuilder getServiceBuilder() throws ToolException {
146:
147: ServiceBuilderFactory builderFactory = ServiceBuilderFactory
148: .getInstance();
149: builderFactory.setServiceClass(getServiceClass());
150: // TODO check if user specify the style from cli arguments
151: // builderFactory.setStyle(style/from/command/line);
152: ServiceBuilder builder = builderFactory.newBuilder();
153:
154: builder.validate();
155:
156: if (context.get(ToolConstants.CFG_ADDRESS) != null) {
157: String address = (String) context
158: .get(ToolConstants.CFG_ADDRESS);
159: builder.setAddress(address);
160: } else {
161: builder.setAddress(DEFAULT_ADDRESS);
162: }
163: builder.setTransportId(getTransportId());
164: builder.setBus(getBus());
165: builder.setBindingId(getBindingId());
166:
167: return builder;
168: }
169:
170: protected String getTransportId() {
171: if (isSOAP12()) {
172: return WSDLConstants.SOAP12_NAMESPACE;
173: }
174: return WSDLConstants.SOAP11_NAMESPACE;
175: }
176:
177: protected String getBindingId() {
178: if (isSOAP12()) {
179: return WSDLConstants.SOAP12_NAMESPACE;
180: } else {
181: return WSDLConstants.SOAP11_NAMESPACE;
182: }
183: }
184:
185: protected boolean isSOAP12() {
186: if (!this .context.optionSet(ToolConstants.CFG_SOAP12)) {
187: BindingType bType = getServiceClass().getAnnotation(
188: BindingType.class);
189: if (bType != null) {
190: return SOAPBinding.SOAP12HTTP_BINDING.equals(bType
191: .value());
192: }
193: return false;
194: }
195: return true;
196: }
197:
198: protected File getOutputDir(File wsdlLocation) {
199: String dir = (String) context.get(ToolConstants.CFG_OUTPUTDIR);
200: if (dir == null) {
201: if (wsdlLocation == null) {
202: dir = "./";
203: } else {
204: dir = wsdlLocation.getParent();
205: }
206: }
207: return new File(dir);
208: }
209:
210: protected File getOutputFile(File nameFromClz,
211: String defaultOutputFile) {
212: String output = (String) context
213: .get(ToolConstants.CFG_OUTPUTFILE);
214: String dir = (String) context.get(ToolConstants.CFG_OUTPUTDIR);
215: if (dir == null) {
216: dir = "./";
217: }
218:
219: File result;
220: if (output != null) {
221: result = new File(output);
222: if (!result.isAbsolute()) {
223: result = new File(new File(dir), output);
224: }
225: } else {
226: result = new File(new File(dir), defaultOutputFile);
227: }
228: if (nameFromClz != null) {
229: result = nameFromClz;
230: }
231:
232: // rename the exising wsdl file
233: if (result.exists()
234: && !result.renameTo(new File(result.getParent(), result
235: .getName()))) {
236: throw new ToolException(new Message("OUTFILE_EXISTS", LOG));
237: }
238: return result;
239: }
240:
241: public Class<?> getServiceClass() {
242: return AnnotationUtil.loadClass((String) context
243: .get(ToolConstants.CFG_CLASSNAME), getClass()
244: .getClassLoader());
245: }
246:
247: public WSDLConstants.WSDLVersion getWSDLVersion() {
248: String version = (String) context
249: .get(ToolConstants.CFG_WSDL_VERSION);
250: WSDLConstants.WSDLVersion wsVersion = WSDLConstants
251: .getVersion(version);
252: if (wsVersion == WSDLConstants.WSDLVersion.UNKNOWN) {
253: wsVersion = WSDLConstants.WSDLVersion.WSDL11;
254: }
255: return wsVersion;
256: }
257:
258: public String getServiceName() {
259: return (String) this .context.get(ToolConstants.CFG_SERVICENAME);
260: }
261:
262: File getSourceDir() {
263: String dir = (String) this .context
264: .get(ToolConstants.CFG_SOURCEDIR);
265: if (StringUtils.isEmpty(dir)) {
266: return null;
267: }
268: return new File(dir);
269: }
270:
271: File getClassesDir() {
272: String dir = (String) this .context
273: .get(ToolConstants.CFG_CLASSDIR);
274: if (StringUtils.isEmpty(dir)) {
275: return null;
276: }
277: return new File(dir);
278: }
279:
280: public Bus getBus() {
281: return BusFactory.getDefaultBus();
282: }
283:
284: public void setEnvironment(ToolContext env) {
285: this .context = env;
286: }
287:
288: public ToolContext getEnvironment() {
289: return this.context;
290: }
291:
292: }
|