001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.jaxws;
017:
018: import java.lang.reflect.Modifier;
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.StringTokenizer;
022:
023: import javax.jws.WebService;
024: import javax.xml.namespace.QName;
025: import javax.xml.ws.BindingType;
026: import javax.xml.ws.WebServiceProvider;
027:
028: public class JAXWSUtils {
029:
030: private static final Map<String, String> BINDING_MAP = new HashMap<String, String>();
031:
032: static {
033: BINDING_MAP.put("##SOAP11_HTTP",
034: "http://schemas.xmlsoap.org/wsdl/soap/http");
035: BINDING_MAP.put("##SOAP12_HTTP",
036: "http://www.w3.org/2003/05/soap/bindings/HTTP/");
037: BINDING_MAP.put("##SOAP11_HTTP_MTOM",
038: "http://schemas.xmlsoap.org/wsdl/soap/http?mtom=true");
039: BINDING_MAP
040: .put("##SOAP12_HTTP_MTOM",
041: "http://www.w3.org/2003/05/soap/bindings/HTTP/?mtom=true");
042: BINDING_MAP.put("##XML_HTTP",
043: "http://www.w3.org/2004/08/wsdl/http");
044: }
045:
046: private JAXWSUtils() {
047: }
048:
049: public static QName getPortType(Class seiClass) {
050: WebService webService = (WebService) seiClass
051: .getAnnotation(WebService.class);
052: if (webService != null) {
053: String localName = webService.name();
054: if (localName == null || localName.length() == 0) {
055: localName = seiClass.getName();
056: }
057: String namespace = webService.targetNamespace();
058: return new QName(getNamespace(seiClass, namespace),
059: localName);
060: }
061: return null;
062: }
063:
064: public static String getBindingURI(String token) {
065: if (token == null) {
066: // return the default
067: return BINDING_MAP.get("##SOAP11_HTTP");
068: } else if (token.startsWith("##")) {
069: String uri = BINDING_MAP.get(token);
070: if (uri == null) {
071: throw new IllegalArgumentException(
072: "Unsupported binding token: " + token);
073: }
074: return uri;
075: } else {
076: return token;
077: }
078: }
079:
080: public static boolean isWebService(Class clazz) {
081: return ((clazz.isAnnotationPresent(WebService.class) || clazz
082: .isAnnotationPresent(WebServiceProvider.class)) && isProperWebService(clazz));
083: }
084:
085: private static boolean isProperWebService(Class clazz) {
086: int modifiers = clazz.getModifiers();
087: return (Modifier.isPublic(modifiers)
088: && !Modifier.isFinal(modifiers) && !Modifier
089: .isAbstract(modifiers));
090: }
091:
092: public static String getServiceName(Class clazz) {
093: return getServiceQName(clazz).getLocalPart();
094: }
095:
096: private static String getServiceName(Class clazz, String name) {
097: if (name == null || name.trim().length() == 0) {
098: return clazz.getSimpleName() + "Service";
099: } else {
100: return name.trim();
101: }
102: }
103:
104: private static String getPortName(Class clazz, String name,
105: String portName) {
106: if (portName == null || portName.trim().length() == 0) {
107: if (name == null || name.trim().length() == 0) {
108: return clazz.getSimpleName() + "Port";
109: } else {
110: return name + "Port";
111: }
112: } else {
113: return portName.trim();
114: }
115: }
116:
117: private static String getNamespace(Class clazz, String namespace) {
118: if (namespace == null || namespace.trim().length() == 0) {
119: Package pkg = clazz.getPackage();
120: if (pkg == null) {
121: return null;
122: } else {
123: return getNamespace(pkg.getName());
124: }
125: } else {
126: return namespace.trim();
127: }
128: }
129:
130: private static String getNamespace(String packageName) {
131: if (packageName == null || packageName.length() == 0) {
132: return null;
133: }
134: StringTokenizer tokenizer = new StringTokenizer(packageName,
135: ".");
136: String[] tokens;
137: if (tokenizer.countTokens() == 0) {
138: tokens = new String[0];
139: } else {
140: tokens = new String[tokenizer.countTokens()];
141: for (int i = tokenizer.countTokens() - 1; i >= 0; i--) {
142: tokens[i] = tokenizer.nextToken();
143: }
144: }
145: StringBuffer namespace = new StringBuffer("http://");
146: String dot = "";
147: for (int i = 0; i < tokens.length; i++) {
148: if (i == 1) {
149: dot = ".";
150: }
151: namespace.append(dot + tokens[i]);
152: }
153: namespace.append('/');
154: return namespace.toString();
155: }
156:
157: private static QName getServiceQName(Class clazz, String namespace,
158: String name) {
159: return new QName(getNamespace(clazz, namespace),
160: getServiceName(clazz, name));
161: }
162:
163: public static QName getServiceQName(Class clazz) {
164: WebService webService = (WebService) clazz
165: .getAnnotation(WebService.class);
166: if (webService == null) {
167: WebServiceProvider webServiceProvider = (WebServiceProvider) clazz
168: .getAnnotation(WebServiceProvider.class);
169: if (webServiceProvider == null) {
170: throw new IllegalArgumentException("The "
171: + clazz.getName() + " is not annotated");
172: }
173: return getServiceQName(clazz, webServiceProvider
174: .targetNamespace(), webServiceProvider
175: .serviceName());
176: } else {
177: return getServiceQName(clazz, webService.targetNamespace(),
178: webService.serviceName());
179: }
180: }
181:
182: private static QName getPortQName(Class clazz, String namespace,
183: String name, String portName) {
184: return new QName(getNamespace(clazz, namespace), getPortName(
185: clazz, name, portName));
186: }
187:
188: public static QName getPortQName(Class clazz) {
189: WebService webService = (WebService) clazz
190: .getAnnotation(WebService.class);
191: if (webService == null) {
192: WebServiceProvider webServiceProvider = (WebServiceProvider) clazz
193: .getAnnotation(WebServiceProvider.class);
194: if (webServiceProvider == null) {
195: throw new IllegalArgumentException("The "
196: + clazz.getName() + " is not annotated");
197: }
198: return getPortQName(clazz, webServiceProvider
199: .targetNamespace(), null, webServiceProvider
200: .portName());
201: } else {
202: return getPortQName(clazz, webService.targetNamespace(),
203: webService.name(), webService.portName());
204: }
205: }
206:
207: public static String getName(Class clazz) {
208: WebService webService = (WebService) clazz
209: .getAnnotation(WebService.class);
210: if (webService == null) {
211: WebServiceProvider webServiceProvider = (WebServiceProvider) clazz
212: .getAnnotation(WebServiceProvider.class);
213: if (webServiceProvider == null) {
214: throw new IllegalArgumentException("The "
215: + clazz.getName() + " is not annotated");
216: }
217: return clazz.getSimpleName();
218: } else {
219: String sei = webService.endpointInterface();
220: if (sei == null || sei.trim().length() == 0) {
221: return getName(clazz, webService.name());
222: } else {
223: try {
224: Class seiClass = clazz.getClassLoader().loadClass(
225: sei.trim());
226: return getNameFromSEI(seiClass);
227: } catch (ClassNotFoundException e) {
228: throw new RuntimeException(
229: "Unable to load SEI class: " + sei, e);
230: }
231: }
232: }
233: }
234:
235: private static String getNameFromSEI(Class seiClass) {
236: WebService webService = (WebService) seiClass
237: .getAnnotation(WebService.class);
238: if (webService == null) {
239: throw new IllegalArgumentException("The "
240: + seiClass.getName() + " is not annotated");
241: }
242: return getName(seiClass, webService.name());
243: }
244:
245: private static String getName(Class clazz, String name) {
246: if (name == null || name.trim().length() == 0) {
247: return clazz.getSimpleName();
248: } else {
249: return name.trim();
250: }
251: }
252:
253: private static String getWsdlLocation(Class clazz) {
254: WebService webService = (WebService) clazz
255: .getAnnotation(WebService.class);
256: if (webService == null) {
257: WebServiceProvider webServiceProvider = (WebServiceProvider) clazz
258: .getAnnotation(WebServiceProvider.class);
259: if (webServiceProvider == null) { //no WebService or WebServiceProvider annotation
260: return "";
261: } else {
262: return webServiceProvider.wsdlLocation().trim();
263: }
264: } else {
265: return webService.wsdlLocation().trim();
266: }
267: }
268:
269: private static String getServiceInterface(Class clazz) {
270: WebService webService = (WebService) clazz
271: .getAnnotation(WebService.class);
272: if (webService == null) {
273: //WebServiceProvider doesn't support endpointInterface property (JAX-WS 2.0 sec 7.7)
274: return "";
275: } else {
276: if (webService.endpointInterface() == null
277: || webService.endpointInterface().trim().equals("")) {
278: return "";
279: } else {
280: return webService.endpointInterface().trim();
281: }
282: }
283: }
284:
285: public static String getServiceWsdlLocation(Class clazz,
286: ClassLoader loader) {
287: String wsdlLocation = getWsdlLocation(clazz);
288: if (wsdlLocation != null && !wsdlLocation.equals("")) {
289: return wsdlLocation;
290: } else { //check if the interface contains the wsdlLocation value
291: String serviceInterfaceClassName = getServiceInterface(clazz);
292: if (serviceInterfaceClassName != null
293: && !serviceInterfaceClassName.equals("")) {
294: try {
295: Class serviceInterfaceClass = loader
296: .loadClass(serviceInterfaceClassName);
297: return getWsdlLocation(serviceInterfaceClass);
298: } catch (Exception e) {
299: return "";
300: }
301: }
302: }
303: return "";
304: }
305:
306: public static boolean containsWsdlLocation(Class clazz,
307: ClassLoader loader) {
308: String wsdlLocSEIFromAnnotation = getServiceWsdlLocation(clazz,
309: loader);
310: if (wsdlLocSEIFromAnnotation != null
311: && !wsdlLocSEIFromAnnotation.equals("")) {
312: return true;
313: } else {
314: return false;
315: }
316: }
317:
318: public static String getBindingURIFromAnnot(Class clazz,
319: ClassLoader loader) {
320: BindingType bindingType = (BindingType) clazz
321: .getAnnotation(BindingType.class);
322: if (bindingType == null) {
323: return "";
324: } else {
325: return bindingType.value();
326: }
327: }
328:
329: }
|