001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * 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, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.core.webservices;
018:
019: import javax.jws.WebService;
020: import javax.xml.namespace.QName;
021: import javax.xml.ws.BindingType;
022: import javax.xml.ws.WebServiceClient;
023: import javax.xml.ws.WebServiceProvider;
024: import java.lang.reflect.Modifier;
025: import java.util.HashMap;
026: import java.util.Map;
027: import java.util.StringTokenizer;
028:
029: public class JaxWsUtils {
030:
031: private static final Map<String, String> BINDING_MAP = new HashMap<String, String>();
032:
033: static {
034: BINDING_MAP.put("##SOAP11_HTTP",
035: "http://schemas.xmlsoap.org/wsdl/soap/http");
036: BINDING_MAP.put("##SOAP12_HTTP",
037: "http://www.w3.org/2003/05/soap/bindings/HTTP/");
038: BINDING_MAP.put("##SOAP11_HTTP_MTOM",
039: "http://schemas.xmlsoap.org/wsdl/soap/http?mtom=true");
040: BINDING_MAP
041: .put("##SOAP12_HTTP_MTOM",
042: "http://www.w3.org/2003/05/soap/bindings/HTTP/?mtom=true");
043: BINDING_MAP.put("##XML_HTTP",
044: "http://www.w3.org/2004/08/wsdl/http");
045: }
046:
047: private JaxWsUtils() {
048: }
049:
050: public static QName getPortType(Class<?> seiClass) {
051: WebService webService = seiClass
052: .getAnnotation(WebService.class);
053: if (webService != null) {
054: String localName = webService.name();
055: if (localName == null || localName.length() == 0) {
056: localName = seiClass.getName();
057: }
058: String namespace = webService.targetNamespace();
059: return new QName(getNamespace(seiClass, namespace),
060: localName);
061: }
062: return null;
063: }
064:
065: public static String getBindingURI(String token) {
066: if (token != null) {
067: if (token.startsWith("##")) {
068: String uri = BINDING_MAP.get(token);
069: if (uri == null) {
070: throw new IllegalArgumentException(
071: "Unsupported binding token: " + token);
072: }
073: return uri;
074: }
075: return token;
076: }
077: return BINDING_MAP.get("##SOAP11_HTTP");
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).append(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 = clazz.getAnnotation(WebService.class);
165: if (webService != null) {
166: return getServiceQName(clazz, webService.targetNamespace(),
167: webService.serviceName());
168: }
169: WebServiceProvider webServiceProvider = clazz
170: .getAnnotation(WebServiceProvider.class);
171: if (webServiceProvider != null) {
172: return getServiceQName(clazz, webServiceProvider
173: .targetNamespace(), webServiceProvider
174: .serviceName());
175: }
176: WebServiceClient webServiceClient = clazz
177: .getAnnotation(WebServiceClient.class);
178: if (webServiceClient != null) {
179: return getServiceQName(clazz, webServiceClient
180: .targetNamespace(), webServiceClient.name());
181: }
182: throw new IllegalArgumentException("The " + clazz.getName()
183: + " is not annotated");
184: }
185:
186: private static QName getPortQName(Class<?> clazz, String namespace,
187: String name, String portName) {
188: return new QName(getNamespace(clazz, namespace), getPortName(
189: clazz, name, portName));
190: }
191:
192: public static QName getPortQName(Class<?> clazz) {
193: WebService webService = clazz.getAnnotation(WebService.class);
194: if (webService != null) {
195: return getPortQName(clazz, webService.targetNamespace(),
196: webService.name(), webService.portName());
197: }
198:
199: WebServiceProvider webServiceProvider = clazz
200: .getAnnotation(WebServiceProvider.class);
201: if (webServiceProvider != null) {
202: return getPortQName(clazz, webServiceProvider
203: .targetNamespace(), null, webServiceProvider
204: .portName());
205: }
206:
207: throw new IllegalArgumentException("The " + clazz.getName()
208: + " is not annotated");
209: }
210:
211: public static String getName(Class<?> clazz) {
212: WebService webService = clazz.getAnnotation(WebService.class);
213: if (webService != null) {
214: String sei = webService.endpointInterface();
215: if (sei != null && sei.trim().length() != 0) {
216: try {
217: Class seiClass = clazz.getClassLoader().loadClass(
218: sei.trim());
219: return getNameFromInterface(seiClass);
220: } catch (ClassNotFoundException e) {
221: throw new RuntimeException(
222: "Unable to load SEI class: " + sei, e);
223: }
224: }
225: return getName(clazz, webService.name());
226: }
227:
228: WebServiceProvider webServiceProvider = clazz
229: .getAnnotation(WebServiceProvider.class);
230: if (webServiceProvider != null) {
231: return clazz.getName();
232: }
233:
234: throw new IllegalArgumentException("The " + clazz.getName()
235: + " is not annotated");
236: }
237:
238: private static String getNameFromInterface(Class<?> intf) {
239: WebService webService = intf.getAnnotation(WebService.class);
240: if (webService != null) {
241: return getName(intf, webService.name());
242: }
243: throw new IllegalArgumentException("The " + intf.getName()
244: + " is not annotated");
245: }
246:
247: private static String getName(Class clazz, String name) {
248: if (name != null) {
249: name = name.trim();
250: if (name.length() > 0) {
251: return name;
252: }
253: }
254: return clazz.getSimpleName();
255: }
256:
257: private static String getWsdlLocation(Class<?> clazz) {
258: WebService webService = clazz.getAnnotation(WebService.class);
259: if (webService != null) {
260: String wsdlLocation = webService.wsdlLocation().trim();
261: if (wsdlLocation.length() == 0)
262: wsdlLocation = null;
263: return wsdlLocation;
264: }
265:
266: WebServiceClient webServiceClient = clazz
267: .getAnnotation(WebServiceClient.class);
268: if (webServiceClient != null) {
269: String wsdlLocation = webServiceClient.wsdlLocation()
270: .trim();
271: if (wsdlLocation.length() == 0)
272: wsdlLocation = null;
273: return wsdlLocation;
274: }
275:
276: WebServiceProvider webServiceProvider = clazz
277: .getAnnotation(WebServiceProvider.class);
278: if (webServiceProvider != null) {
279: String wsdlLocation = webServiceProvider.wsdlLocation()
280: .trim();
281: if (wsdlLocation.length() == 0)
282: wsdlLocation = null;
283: return wsdlLocation;
284: }
285:
286: return null;
287: }
288:
289: public static String getServiceInterface(Class<?> clazz) {
290: WebService webService = clazz.getAnnotation(WebService.class);
291: if (webService != null
292: && webService.endpointInterface() != null) {
293: String endpointInterface = webService.endpointInterface()
294: .trim();
295: if (endpointInterface.length() == 0)
296: endpointInterface = null;
297: return endpointInterface;
298: }
299:
300: // if the bean implements only one WebService class, that is the SEI
301: String endpointInterface = null;
302: for (Class intf : clazz.getInterfaces()) {
303: webService = clazz.getAnnotation(WebService.class);
304: if (webService != null) {
305: if (endpointInterface == null) {
306: endpointInterface = intf.getName();
307: } else {
308: // multiple endpoint interfaces
309: endpointInterface = null;
310: break;
311: }
312: }
313: }
314:
315: if (endpointInterface != null) {
316: return endpointInterface;
317: }
318:
319: return null;
320: }
321:
322: public static String getServiceWsdlLocation(Class<?> clazz,
323: ClassLoader loader) {
324: String wsdlLocation = getWsdlLocation(clazz);
325: if (wsdlLocation != null && !wsdlLocation.equals("")) {
326: return wsdlLocation;
327: }
328:
329: String serviceInterfaceClassName = getServiceInterface(clazz);
330: if (serviceInterfaceClassName != null
331: && !serviceInterfaceClassName.equals("")) {
332: try {
333: Class serviceInterfaceClass = loader
334: .loadClass(serviceInterfaceClassName);
335: return getWsdlLocation(serviceInterfaceClass);
336: } catch (Exception e) {
337: }
338: }
339: return null;
340: }
341:
342: public static boolean containsWsdlLocation(Class<?> clazz,
343: ClassLoader loader) {
344: String wsdlLocSEIFromAnnotation = getServiceWsdlLocation(clazz,
345: loader);
346: return wsdlLocSEIFromAnnotation != null
347: && !wsdlLocSEIFromAnnotation.equals("");
348: }
349:
350: public static String getBindingUriFromAnn(Class<?> clazz) {
351: BindingType bindingType = clazz
352: .getAnnotation(BindingType.class);
353: if (bindingType != null) {
354: return bindingType.value();
355: }
356: return null;
357: }
358: }
|