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.wscompile;
038:
039: import com.sun.mirror.apt.Filer;
040: import com.sun.tools.ws.resources.WscompileMessages;
041: import com.sun.xml.ws.api.BindingID;
042:
043: import javax.jws.WebService;
044: import javax.xml.namespace.QName;
045: import java.io.File;
046: import java.util.ArrayList;
047: import java.util.List;
048:
049: /**
050: * @author Vivek Pandey
051: */
052: public class WsgenOptions extends Options {
053: /**
054: * -servicename
055: */
056: public QName serviceName;
057:
058: /**
059: * -portname
060: */
061: public QName portName;
062:
063: /**
064: * -r
065: */
066: public File nonclassDestDir;
067:
068: /**
069: * -wsdl
070: */
071: public boolean genWsdl;
072:
073: /**
074: * protocol value
075: */
076: public String protocol = "soap1.1";
077: public String transport;
078:
079: /**
080: * -XwsgenReport
081: */
082: public File wsgenReport;
083:
084: /**
085: * -Xdonotoverwrite
086: */
087: public boolean doNotOverWrite;
088:
089: public Filer filer;
090:
091: /**
092: * Tells if user specified a specific protocol
093: */
094: public boolean protocolSet = false;
095:
096: private static final String SERVICENAME_OPTION = "-servicename";
097: private static final String PORTNAME_OPTION = "-portname";
098: private static final String HTTP = "http";
099: private static final String SOAP11 = "soap1.1";
100: public static final String X_SOAP12 = "Xsoap1.2";
101:
102: @Override
103: protected int parseArguments(String[] args, int i)
104: throws BadCommandLineException {
105: int j = super .parseArguments(args, i);
106: if (args[i].equals(SERVICENAME_OPTION)) {
107: serviceName = QName.valueOf(requireArgument(
108: SERVICENAME_OPTION, args, ++i));
109: if (serviceName.getNamespaceURI() == null
110: || serviceName.getNamespaceURI().length() == 0) {
111: throw new BadCommandLineException(WscompileMessages
112: .WSGEN_SERVICENAME_MISSING_NAMESPACE(args[i]));
113: }
114: if (serviceName.getLocalPart() == null
115: || serviceName.getLocalPart().length() == 0) {
116: throw new BadCommandLineException(WscompileMessages
117: .WSGEN_SERVICENAME_MISSING_LOCALNAME(args[i]));
118: }
119: return 2;
120: } else if (args[i].equals(PORTNAME_OPTION)) {
121: portName = QName.valueOf(requireArgument(PORTNAME_OPTION,
122: args, ++i));
123: if (portName.getNamespaceURI() == null
124: || portName.getNamespaceURI().length() == 0) {
125: throw new BadCommandLineException(WscompileMessages
126: .WSGEN_PORTNAME_MISSING_NAMESPACE(args[i]));
127: }
128: if (portName.getLocalPart() == null
129: || portName.getLocalPart().length() == 0) {
130: throw new BadCommandLineException(WscompileMessages
131: .WSGEN_PORTNAME_MISSING_LOCALNAME(args[i]));
132: }
133: return 2;
134: } else if (args[i].equals("-r")) {
135: nonclassDestDir = new File(requireArgument("-r", args, ++i));
136: if (!nonclassDestDir.exists()) {
137: throw new BadCommandLineException(WscompileMessages
138: .WSCOMPILE_NO_SUCH_DIRECTORY(nonclassDestDir
139: .getPath()));
140: }
141: return 2;
142: } else if (args[i].equals("-classpath")
143: || args[i].equals("-cp")) {
144: classpath = requireArgument("-classpath", args, ++i)
145: + File.pathSeparator
146: + System.getProperty("java.class.path");
147: return 2;
148: } else if (args[i].startsWith("-wsdl")) {
149: genWsdl = true;
150: //String value = requireArgument("-wsdl", args, ++i).substring(5);
151: String value = args[i].substring(5);
152: int index = value.indexOf(':');
153: if (index == 0) {
154: value = value.substring(1);
155: index = value.indexOf('/');
156: if (index == -1) {
157: protocol = value;
158: transport = HTTP;
159: } else {
160: protocol = value.substring(0, index);
161: transport = value.substring(index + 1);
162: }
163: protocolSet = true;
164: }
165: return 1;
166: } else if (args[i].equals("-XwsgenReport")) {
167: // undocumented switch for the test harness
168: wsgenReport = new File(requireArgument("-XwsgenReport",
169: args, ++i));
170: return 2;
171: } else if (args[i].equals("-Xdonotoverwrite")) {
172: doNotOverWrite = true;
173: return 1;
174: }
175: return j;
176: }
177:
178: @Override
179: protected void addFile(String arg) {
180: endpoints.add(arg);
181: }
182:
183: List<String> endpoints = new ArrayList<String>();
184:
185: public Class endpoint;
186:
187: private boolean isImplClass;
188: private boolean noWebServiceEndpoint;
189:
190: public void validate() throws BadCommandLineException {
191: if (nonclassDestDir == null)
192: nonclassDestDir = destDir;
193: if (!protocol.equalsIgnoreCase(SOAP11)
194: && !protocol.equalsIgnoreCase(X_SOAP12)) {
195: throw new BadCommandLineException(WscompileMessages
196: .WSGEN_INVALID_PROTOCOL(protocol, SOAP11 + ", "
197: + X_SOAP12));
198: }
199:
200: if (transport != null && !transport.equalsIgnoreCase(HTTP)) {
201: throw new BadCommandLineException(WscompileMessages
202: .WSGEN_INVALID_TRANSPORT(transport, HTTP));
203: }
204:
205: if (endpoints.isEmpty()) {
206: throw new BadCommandLineException(WscompileMessages
207: .WSGEN_MISSING_FILE());
208: }
209: if (protocol == null || protocol.equalsIgnoreCase(X_SOAP12)
210: && !isExtensionMode()) {
211: throw new BadCommandLineException(WscompileMessages
212: .WSGEN_SOAP_12_WITHOUT_EXTENSION());
213: }
214:
215: validateEndpointClass();
216: validateArguments();
217: }
218:
219: /**
220: * Get an implementation class annotated with @WebService annotation.
221: */
222: private void validateEndpointClass() throws BadCommandLineException {
223: Class clazz = null;
224: for (String cls : endpoints) {
225: clazz = getClass(cls);
226: if (clazz == null)
227: continue;
228:
229: if (clazz.isEnum() || clazz.isInterface()
230: || clazz.isPrimitive()) {
231: continue;
232: }
233: isImplClass = true;
234: WebService webService = (WebService) clazz
235: .getAnnotation(WebService.class);
236: if (webService == null)
237: continue;
238: break;
239: }
240: if (clazz == null) {
241: throw new BadCommandLineException(WscompileMessages
242: .WSGEN_CLASS_NOT_FOUND(endpoints.get(0)));
243: }
244: if (!isImplClass) {
245: throw new BadCommandLineException(WscompileMessages
246: .WSGEN_CLASS_MUST_BE_IMPLEMENTATION_CLASS(clazz
247: .getName()));
248: }
249: if (noWebServiceEndpoint) {
250: throw new BadCommandLineException(WscompileMessages
251: .WSGEN_NO_WEBSERVICES_CLASS(clazz.getName()));
252: }
253: endpoint = clazz;
254: validateBinding();
255: }
256:
257: private void validateBinding() throws BadCommandLineException {
258: if (genWsdl) {
259: BindingID binding = BindingID.parse(endpoint);
260: if ((binding.equals(BindingID.SOAP12_HTTP) || binding
261: .equals(BindingID.SOAP12_HTTP_MTOM))
262: && !(protocol.equals(X_SOAP12) && isExtensionMode())) {
263: throw new BadCommandLineException(WscompileMessages
264: .WSGEN_CANNOT_GEN_WSDL_FOR_SOAP_12_BINDING(
265: binding.toString(), endpoint.getName()));
266: }
267: if (binding.equals(BindingID.XML_HTTP)) {
268: throw new BadCommandLineException(WscompileMessages
269: .WSGEN_CANNOT_GEN_WSDL_FOR_NON_SOAP_BINDING(
270: binding.toString(), endpoint.getName()));
271: }
272: }
273: }
274:
275: private void validateArguments() throws BadCommandLineException {
276: if (!genWsdl) {
277: if (serviceName != null) {
278: throw new BadCommandLineException(WscompileMessages
279: .WSGEN_WSDL_ARG_NO_GENWSDL(SERVICENAME_OPTION));
280: }
281: if (portName != null) {
282: throw new BadCommandLineException(WscompileMessages
283: .WSGEN_WSDL_ARG_NO_GENWSDL(PORTNAME_OPTION));
284: }
285: }
286: }
287:
288: public static BindingID getBindingID(String protocol) {
289: if (protocol.equals(SOAP11))
290: return BindingID.SOAP11_HTTP;
291: if (protocol.equals(X_SOAP12))
292: return BindingID.SOAP12_HTTP;
293: return null;
294: }
295:
296: private Class getClass(String className) {
297: try {
298: return getClassLoader().loadClass(className);
299: } catch (ClassNotFoundException e) {
300: return null;
301: }
302: }
303:
304: }
|