KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > jaxws > EndpointUtils


1 package org.objectweb.celtix.bus.jaxws;
2
3 import java.lang.reflect.Method JavaDoc;
4 import java.lang.reflect.ParameterizedType JavaDoc;
5 import java.lang.reflect.Type JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.logging.Level JavaDoc;
10 import java.util.logging.Logger JavaDoc;
11
12 import javax.jws.WebMethod;
13 import javax.jws.WebService;
14 import javax.xml.namespace.QName JavaDoc;
15 import javax.xml.ws.Provider;
16 import javax.xml.ws.ServiceMode;
17 import javax.xml.ws.WebServiceProvider;
18
19 import org.objectweb.celtix.common.logging.LogUtils;
20
21 public final class EndpointUtils {
22
23     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(EndpointUtils.class);
24
25     private EndpointUtils() {
26         // Utility class - never constructed
27
}
28
29     public static ServiceMode getServiceMode(EndpointImpl endpoint) {
30         assert null != endpoint;
31         Class JavaDoc<?> implementorClass = endpoint.getImplementorClass();
32         if (implementorClass.isAssignableFrom(Provider.class)) {
33             return implementorClass.getAnnotation(ServiceMode.class);
34         }
35         return null;
36     }
37
38     /**
39      * Returns the method in the <code>Endpoint</code>'s implementor that
40      * implements the specified operation. This assumes that the
41      * <code>Endpoint</code>'s implementor is annotated with the
42      * <code>WebService</code> annotation. The implementor's (public) methods
43      * need not necessarily be annotated. REVISIT: Does the implementor have to
44      * implement an SEI, or does it even have to implement the Remote interface?
45      *
46      * @param endpoint
47      * @param operationName
48      * @return the <code>Method</code> in the <code>Endpoint</code>'s implementor.
49      */

50
51     public static Method JavaDoc getMethod(EndpointImpl endpoint, QName JavaDoc operationName) {
52         Class JavaDoc<?> iClass = endpoint.getImplementorClass();
53         return getMethod(iClass, operationName);
54     }
55     public static Method JavaDoc getMethod(Class JavaDoc<?> iClass, QName JavaDoc operationName) {
56         // determine the (fully annoated) SEI
57
List JavaDoc<Class JavaDoc<?>> list = getWebServiceAnnotatedClass(iClass);
58
59         // determine the method in the SEI
60
String JavaDoc methodName = operationName.getLocalPart();
61         Method JavaDoc iMethod = null;
62         
63         Iterator JavaDoc<Class JavaDoc<?>> iter = list.iterator();
64         boolean strictMatch = false;
65         while (iter.hasNext() && !strictMatch) {
66             Class JavaDoc<?> sei = iter.next();
67             Method JavaDoc[] iMethods = sei.getMethods();
68
69             for (Method JavaDoc m : iMethods) {
70                 WebMethod wm = m.getAnnotation(WebMethod.class);
71
72                 if (wm != null && !"".equals(wm.operationName())) {
73                     if (methodName.equals(wm.operationName())
74                         && methodName.equalsIgnoreCase(m.getName())) {
75                         iMethod = m;
76                         strictMatch = true;
77                         break;
78                     }
79                 } else if (methodName.equals(m.getName())) {
80                     iMethod = m;
81                     break;
82                 }
83             }
84         }
85         
86         if (null == iMethod) {
87             LOG.log(Level.SEVERE, "METHOD_NOT_DEFINED_MSG", methodName);
88             return null;
89         }
90
91         return iMethod;
92     }
93
94     public static Class JavaDoc<?> getProviderParameterType(EndpointImpl endpoint) {
95         //The Provider Implementor inherits out of Provier<T>
96
Type JavaDoc intfTypes[] = endpoint.getImplementorClass().getGenericInterfaces();
97         for (Type JavaDoc t : intfTypes) {
98             Class JavaDoc<?> clazz = JAXBEncoderDecoder.getClassFromType(t);
99             if (Provider.class == clazz) {
100                 Type JavaDoc paramTypes[] = ((ParameterizedType JavaDoc)t).getActualTypeArguments();
101                 return JAXBEncoderDecoder.getClassFromType(paramTypes[0]);
102             }
103         }
104         
105         return null;
106     }
107
108     private static boolean hasWebServiceAnnotation(Class JavaDoc<?> cls) {
109         if (cls == null) {
110             return false;
111         }
112         if (null != cls.getAnnotation(WebService.class)) {
113             return true;
114         }
115         for (Class JavaDoc<?> inf : cls.getInterfaces()) {
116             if (null != inf.getAnnotation(WebService.class)) {
117                 return true;
118             }
119         }
120         
121         return hasWebServiceAnnotation(cls.getSuperclass());
122     }
123     
124     private static boolean hasWebServiceProviderAnnotation(Class JavaDoc<?> cls) {
125         if (cls != null) {
126             return cls.isAnnotationPresent(WebServiceProvider.class);
127         }
128         
129         return false;
130     }
131     
132     public static boolean isValidImplementor(Object JavaDoc implementor) {
133         if (Provider.class.isAssignableFrom(implementor.getClass())
134             && hasWebServiceProviderAnnotation(implementor.getClass())) {
135             return true;
136         }
137
138         // implementor MUST be an instance of a class with a WebService
139
// annotation
140
// (that implements an SEI) OR a Provider
141

142         if (hasWebServiceAnnotation(implementor.getClass())) {
143             return true;
144         }
145
146         LOG.info("Implementor is not annotated with WebService annotation.");
147         return false;
148     }
149     
150     public static List JavaDoc<Class JavaDoc<?>> getWebServiceAnnotatedClass(Class JavaDoc<?> cls) {
151         List JavaDoc<Class JavaDoc<?>> list = new ArrayList JavaDoc<Class JavaDoc<?>>();
152
153         Class JavaDoc<?>[] interfaces = cls.getInterfaces();
154         for (Class JavaDoc<?> c : interfaces) {
155             list.addAll(getWebServiceAnnotatedClass(c));
156         }
157         
158         if (!cls.isInterface()) {
159             Class JavaDoc<?> superClass = cls.getSuperclass();
160             if (superClass != null) {
161                 list.addAll(getWebServiceAnnotatedClass(superClass));
162             }
163         }
164         
165         if (cls.isAnnotationPresent(WebService.class)) {
166             list.add(cls);
167         }
168         
169         return list;
170     }
171     
172     
173 }
174
Popular Tags