001: package org.objectweb.celtix.bus.jaxws;
002:
003: import java.lang.reflect.Method;
004: import java.lang.reflect.ParameterizedType;
005: import java.lang.reflect.Type;
006: import java.util.ArrayList;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.logging.Level;
010: import java.util.logging.Logger;
011:
012: import javax.jws.WebMethod;
013: import javax.jws.WebService;
014: import javax.xml.namespace.QName;
015: import javax.xml.ws.Provider;
016: import javax.xml.ws.ServiceMode;
017: import javax.xml.ws.WebServiceProvider;
018:
019: import org.objectweb.celtix.common.logging.LogUtils;
020:
021: public final class EndpointUtils {
022:
023: private static final Logger LOG = LogUtils
024: .getL7dLogger(EndpointUtils.class);
025:
026: private EndpointUtils() {
027: // Utility class - never constructed
028: }
029:
030: public static ServiceMode getServiceMode(EndpointImpl endpoint) {
031: assert null != endpoint;
032: Class<?> implementorClass = endpoint.getImplementorClass();
033: if (implementorClass.isAssignableFrom(Provider.class)) {
034: return implementorClass.getAnnotation(ServiceMode.class);
035: }
036: return null;
037: }
038:
039: /**
040: * Returns the method in the <code>Endpoint</code>'s implementor that
041: * implements the specified operation. This assumes that the
042: * <code>Endpoint</code>'s implementor is annotated with the
043: * <code>WebService</code> annotation. The implementor's (public) methods
044: * need not necessarily be annotated. REVISIT: Does the implementor have to
045: * implement an SEI, or does it even have to implement the Remote interface?
046: *
047: * @param endpoint
048: * @param operationName
049: * @return the <code>Method</code> in the <code>Endpoint</code>'s implementor.
050: */
051:
052: public static Method getMethod(EndpointImpl endpoint,
053: QName operationName) {
054: Class<?> iClass = endpoint.getImplementorClass();
055: return getMethod(iClass, operationName);
056: }
057:
058: public static Method getMethod(Class<?> iClass, QName operationName) {
059: // determine the (fully annoated) SEI
060: List<Class<?>> list = getWebServiceAnnotatedClass(iClass);
061:
062: // determine the method in the SEI
063: String methodName = operationName.getLocalPart();
064: Method iMethod = null;
065:
066: Iterator<Class<?>> iter = list.iterator();
067: boolean strictMatch = false;
068: while (iter.hasNext() && !strictMatch) {
069: Class<?> sei = iter.next();
070: Method[] iMethods = sei.getMethods();
071:
072: for (Method m : iMethods) {
073: WebMethod wm = m.getAnnotation(WebMethod.class);
074:
075: if (wm != null && !"".equals(wm.operationName())) {
076: if (methodName.equals(wm.operationName())
077: && methodName.equalsIgnoreCase(m.getName())) {
078: iMethod = m;
079: strictMatch = true;
080: break;
081: }
082: } else if (methodName.equals(m.getName())) {
083: iMethod = m;
084: break;
085: }
086: }
087: }
088:
089: if (null == iMethod) {
090: LOG.log(Level.SEVERE, "METHOD_NOT_DEFINED_MSG", methodName);
091: return null;
092: }
093:
094: return iMethod;
095: }
096:
097: public static Class<?> getProviderParameterType(
098: EndpointImpl endpoint) {
099: //The Provider Implementor inherits out of Provier<T>
100: Type intfTypes[] = endpoint.getImplementorClass()
101: .getGenericInterfaces();
102: for (Type t : intfTypes) {
103: Class<?> clazz = JAXBEncoderDecoder.getClassFromType(t);
104: if (Provider.class == clazz) {
105: Type paramTypes[] = ((ParameterizedType) t)
106: .getActualTypeArguments();
107: return JAXBEncoderDecoder
108: .getClassFromType(paramTypes[0]);
109: }
110: }
111:
112: return null;
113: }
114:
115: private static boolean hasWebServiceAnnotation(Class<?> cls) {
116: if (cls == null) {
117: return false;
118: }
119: if (null != cls.getAnnotation(WebService.class)) {
120: return true;
121: }
122: for (Class<?> inf : cls.getInterfaces()) {
123: if (null != inf.getAnnotation(WebService.class)) {
124: return true;
125: }
126: }
127:
128: return hasWebServiceAnnotation(cls.getSuperclass());
129: }
130:
131: private static boolean hasWebServiceProviderAnnotation(Class<?> cls) {
132: if (cls != null) {
133: return cls.isAnnotationPresent(WebServiceProvider.class);
134: }
135:
136: return false;
137: }
138:
139: public static boolean isValidImplementor(Object implementor) {
140: if (Provider.class.isAssignableFrom(implementor.getClass())
141: && hasWebServiceProviderAnnotation(implementor
142: .getClass())) {
143: return true;
144: }
145:
146: // implementor MUST be an instance of a class with a WebService
147: // annotation
148: // (that implements an SEI) OR a Provider
149:
150: if (hasWebServiceAnnotation(implementor.getClass())) {
151: return true;
152: }
153:
154: LOG
155: .info("Implementor is not annotated with WebService annotation.");
156: return false;
157: }
158:
159: public static List<Class<?>> getWebServiceAnnotatedClass(
160: Class<?> cls) {
161: List<Class<?>> list = new ArrayList<Class<?>>();
162:
163: Class<?>[] interfaces = cls.getInterfaces();
164: for (Class<?> c : interfaces) {
165: list.addAll(getWebServiceAnnotatedClass(c));
166: }
167:
168: if (!cls.isInterface()) {
169: Class<?> super Class = cls.getSuperclass();
170: if (super Class != null) {
171: list.addAll(getWebServiceAnnotatedClass(super Class));
172: }
173: }
174:
175: if (cls.isAnnotationPresent(WebService.class)) {
176: list.add(cls);
177: }
178:
179: return list;
180: }
181:
182: }
|