001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.ws.processor.generator;
038:
039: import com.sun.codemodel.*;
040: import com.sun.tools.ws.processor.model.Model;
041: import com.sun.tools.ws.processor.model.Port;
042: import com.sun.tools.ws.processor.model.Service;
043: import com.sun.tools.ws.processor.model.ModelProperties;
044: import com.sun.tools.ws.processor.model.java.JavaInterface;
045: import com.sun.tools.ws.wscompile.ErrorReceiver;
046: import com.sun.tools.ws.wscompile.Options;
047: import com.sun.tools.ws.wscompile.WsimportOptions;
048: import com.sun.tools.ws.resources.GeneratorMessages;
049: import com.sun.tools.ws.wsdl.document.PortType;
050: import com.sun.xml.bind.api.JAXBRIContext;
051: import com.sun.xml.ws.util.JAXWSUtils;
052:
053: import javax.xml.namespace.QName;
054: import javax.xml.ws.WebEndpoint;
055: import javax.xml.ws.WebServiceClient;
056: import javax.xml.ws.WebServiceFeature;
057: import java.io.IOException;
058: import java.io.File;
059: import java.net.MalformedURLException;
060: import java.net.URL;
061: import java.util.logging.Logger;
062:
063: import org.xml.sax.Locator;
064:
065: /**
066: * @author WS Development Team
067: */
068: public class ServiceGenerator extends GeneratorBase {
069:
070: public static void generate(Model model, WsimportOptions options,
071: ErrorReceiver receiver) {
072: ServiceGenerator serviceGenerator = new ServiceGenerator(model,
073: options, receiver);
074: serviceGenerator.doGeneration();
075: }
076:
077: private ServiceGenerator(Model model, WsimportOptions options,
078: ErrorReceiver receiver) {
079: super (model, options, receiver);
080: }
081:
082: @Override
083: public void visit(Service service) {
084: JavaInterface intf = service.getJavaInterface();
085: String className = Names.customJavaTypeClassName(intf);
086: if (donotOverride
087: && GeneratorUtil.classExists(options, className)) {
088: log("Class " + className + " exists. Not overriding.");
089: return;
090: }
091:
092: JDefinedClass cls;
093: try {
094: cls = getClass(className, ClassType.CLASS);
095: } catch (JClassAlreadyExistsException e) {
096: receiver.error(service.getLocator(), GeneratorMessages
097: .GENERATOR_SERVICE_CLASS_ALREADY_EXIST(className,
098: service.getName()));
099: return;
100: }
101:
102: cls._extends(javax.xml.ws.Service.class);
103: String serviceFieldName = JAXBRIContext.mangleNameToClassName(
104: service.getName().getLocalPart()).toUpperCase();
105: String wsdlLocationName = serviceFieldName + "_WSDL_LOCATION";
106: JFieldVar urlField = cls.field(JMod.PRIVATE | JMod.STATIC
107: | JMod.FINAL, URL.class, wsdlLocationName);
108:
109: cls.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL,
110: Logger.class, "logger", cm.ref(Logger.class)
111: .staticInvoke("getLogger").arg(
112: JExpr.dotclass(cm.ref(className))
113: .invoke("getName")));
114:
115: JClass qNameCls = cm.ref(QName.class);
116: JInvocation inv;
117: inv = JExpr._new(qNameCls);
118: inv.arg("namespace");
119: inv.arg("localpart");
120:
121: JBlock staticBlock = cls.init();
122: JVar urlVar = staticBlock.decl(cm.ref(URL.class), "url", JExpr
123: ._null());
124: JTryBlock tryBlock = staticBlock._try();
125: JVar baseUrl = tryBlock.body().decl(cm.ref(URL.class),
126: "baseUrl");
127: tryBlock.body().assign(
128: baseUrl,
129: JExpr.dotclass(cm.ref(className)).invoke("getResource")
130: .arg("."));
131: tryBlock.body().assign(
132: urlVar,
133: JExpr._new(cm.ref(URL.class)).arg(baseUrl).arg(
134: wsdlLocation));
135: JCatchBlock catchBlock = tryBlock._catch(cm
136: .ref(MalformedURLException.class));
137: catchBlock.param("e");
138:
139: catchBlock.body().directStatement(
140: "logger.warning(\"Failed to create URL for the wsdl Location: "
141: + JExpr.quotify('\'', wsdlLocation)
142: + ", retrying as a local file\");");
143: catchBlock.body().directStatement(
144: "logger.warning(e.getMessage());");
145:
146: staticBlock.assign(urlField, urlVar);
147:
148: //write class comment - JAXWS warning
149: JDocComment comment = cls.javadoc();
150:
151: if (service.getJavaDoc() != null) {
152: comment.add(service.getJavaDoc());
153: comment.add("\n\n");
154: }
155:
156: for (String doc : getJAXWSClassComment()) {
157: comment.add(doc);
158: }
159:
160: JMethod constructor = cls.constructor(JMod.PUBLIC);
161: constructor.param(URL.class, "wsdlLocation");
162: constructor.param(QName.class, "serviceName");
163: constructor.body().directStatement(
164: "super(wsdlLocation, serviceName);");
165:
166: constructor = cls.constructor(JMod.PUBLIC);
167: constructor.body().directStatement(
168: "super(" + wsdlLocationName + ", new QName(\""
169: + service.getName().getNamespaceURI()
170: + "\", \"" + service.getName().getLocalPart()
171: + "\"));");
172:
173: //@WebService
174: JAnnotationUse webServiceClientAnn = cls.annotate(cm
175: .ref(WebServiceClient.class));
176: writeWebServiceClientAnnotation(service, webServiceClientAnn);
177:
178: //@HandlerChain
179: writeHandlerConfig(Names.customJavaTypeClassName(service
180: .getJavaInterface()), cls, options);
181:
182: for (Port port : service.getPorts()) {
183: if (port.isProvider()) {
184: continue; // No getXYZPort() for porvider based endpoint
185: }
186:
187: //Get the SEI class
188: JType retType;
189: try {
190: retType = getClass(port.getJavaInterface().getName(),
191: ClassType.INTERFACE);
192: } catch (JClassAlreadyExistsException e) {
193: QName portTypeName = (QName) port
194: .getProperty(ModelProperties.PROPERTY_WSDL_PORT_TYPE_NAME);
195: Locator loc = null;
196: if (portTypeName != null) {
197: PortType pt = port.portTypes.get(portTypeName);
198: if (pt != null)
199: loc = pt.getLocator();
200: }
201: receiver.error(loc, GeneratorMessages
202: .GENERATOR_SEI_CLASS_ALREADY_EXIST(port
203: .getJavaInterface().getName(),
204: portTypeName));
205: return;
206: }
207:
208: //write getXyzPort()
209: writeDefaultGetPort(port, retType, cls);
210:
211: //write getXyzPort(WebServicesFeature...)
212: if (options.target.isLaterThan(Options.Target.V2_1))
213: writeGetPort(port, retType, cls);
214: }
215: }
216:
217: private void writeGetPort(Port port, JType retType,
218: JDefinedClass cls) {
219: JMethod m = cls.method(JMod.PUBLIC, retType, port
220: .getPortGetter());
221: JDocComment methodDoc = m.javadoc();
222: if (port.getJavaDoc() != null)
223: methodDoc.add(port.getJavaDoc());
224: JCommentPart ret = methodDoc.addReturn();
225: JCommentPart paramDoc = methodDoc.addParam("features");
226: paramDoc.append("A list of ");
227: paramDoc.append("{@link " + WebServiceFeature.class.getName()
228: + "}");
229: paramDoc
230: .append("to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.");
231: ret.add("returns " + retType.name());
232: m.varParam(WebServiceFeature.class, "features");
233: JBlock body = m.body();
234: StringBuffer statement = new StringBuffer("return ");
235: statement.append("super.getPort(new QName(\"").append(
236: port.getName().getNamespaceURI()).append("\", \"")
237: .append(port.getName().getLocalPart()).append("\"), ");
238: statement.append(retType.name());
239: statement.append(".class, features);");
240: body.directStatement(statement.toString());
241: writeWebEndpoint(port, m);
242: }
243:
244: private void writeDefaultGetPort(Port port, JType retType,
245: JDefinedClass cls) {
246: String portGetter = port.getPortGetter();
247: JMethod m = cls.method(JMod.PUBLIC, retType, portGetter);
248: JDocComment methodDoc = m.javadoc();
249: if (port.getJavaDoc() != null)
250: methodDoc.add(port.getJavaDoc());
251: JCommentPart ret = methodDoc.addReturn();
252: ret.add("returns " + retType.name());
253: JBlock body = m.body();
254: StringBuffer statement = new StringBuffer("return ");
255: statement.append("super.getPort(new QName(\"").append(
256: port.getName().getNamespaceURI()).append("\", \"")
257: .append(port.getName().getLocalPart()).append("\"), ");
258: statement.append(retType.name());
259: statement.append(".class);");
260: body.directStatement(statement.toString());
261: writeWebEndpoint(port, m);
262: }
263:
264: private void writeWebServiceClientAnnotation(Service service,
265: JAnnotationUse wsa) {
266: String serviceName = service.getName().getLocalPart();
267: String serviceNS = service.getName().getNamespaceURI();
268: wsa.param("name", serviceName);
269: wsa.param("targetNamespace", serviceNS);
270: wsa.param("wsdlLocation", wsdlLocation);
271: }
272:
273: private void writeWebEndpoint(Port port, JMethod m) {
274: JAnnotationUse webEndpointAnn = m.annotate(cm
275: .ref(WebEndpoint.class));
276: webEndpointAnn.param("name", port.getName().getLocalPart());
277: }
278: }
|