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     */
017    package org.apache.geronimo.jaxws;
018    
019    import java.lang.reflect.Modifier;
020    import java.util.HashMap;
021    import java.util.Map;
022    import java.util.StringTokenizer;
023    
024    import javax.jws.WebService;
025    import javax.xml.namespace.QName;
026    import javax.xml.ws.BindingType;
027    import javax.xml.ws.WebServiceProvider;
028    
029    public class JAXWSUtils {
030        
031        private static final Map<String, String> BINDING_MAP = 
032            new HashMap<String, String>();
033        
034        static {
035            BINDING_MAP.put("##SOAP11_HTTP", "http://schemas.xmlsoap.org/wsdl/soap/http");
036            BINDING_MAP.put("##SOAP12_HTTP", "http://www.w3.org/2003/05/soap/bindings/HTTP/");
037            BINDING_MAP.put("##SOAP11_HTTP_MTOM", "http://schemas.xmlsoap.org/wsdl/soap/http?mtom=true");
038            BINDING_MAP.put("##SOAP12_HTTP_MTOM", "http://www.w3.org/2003/05/soap/bindings/HTTP/?mtom=true");
039            BINDING_MAP.put("##XML_HTTP", "http://www.w3.org/2004/08/wsdl/http");
040        }
041        
042        private JAXWSUtils() {
043        }
044    
045        public static QName getPortType(Class seiClass) {
046            WebService webService = (WebService) seiClass.getAnnotation(WebService.class);        
047            if (webService != null) {
048                String localName = webService.name();
049                if (localName == null || localName.length() == 0) {
050                    localName = seiClass.getName();
051                }
052                String namespace = webService.targetNamespace();
053                return new QName(getNamespace(seiClass, namespace), localName);
054            }
055            return null;
056        }
057    
058        public static String getBindingURI(String token) {
059            if (token == null) {
060                // return the default
061                return BINDING_MAP.get("##SOAP11_HTTP");
062            } else if (token.startsWith("##")) {
063                String uri = BINDING_MAP.get(token);
064                if (uri == null) {
065                    throw new IllegalArgumentException("Unsupported binding token: " + token);
066                }
067                return uri;
068            } else {
069                return token;            
070            }
071        }
072        
073        public static boolean isWebService(Class clazz) {
074            return ((clazz.isAnnotationPresent(WebService.class) || 
075                     clazz.isAnnotationPresent(WebServiceProvider.class)) &&
076                     isProperWebService(clazz));
077        }
078        
079        private static boolean isProperWebService(Class clazz) {
080            int modifiers = clazz.getModifiers();
081            return (Modifier.isPublic(modifiers) &&
082                    !Modifier.isFinal(modifiers) &&
083                    !Modifier.isAbstract(modifiers));
084        }
085        
086        public static String getServiceName(Class clazz) {
087            return getServiceQName(clazz).getLocalPart();
088        }
089        
090        private static String getServiceName(Class clazz, String name) {
091            if (name == null || name.trim().length() == 0) {
092                return clazz.getSimpleName() + "Service";
093            } else {
094                return name.trim();
095            }       
096        }
097        
098        private static String getPortName(Class clazz, String name, String portName) {
099            if (portName == null || portName.trim().length() == 0) {
100                if (name == null || name.trim().length() == 0) {
101                    return clazz.getSimpleName() + "Port";
102                } else {
103                    return name + "Port";
104                }
105            } else {
106                return portName.trim();
107            }
108        }
109        
110        private static String getNamespace(Class clazz, String namespace) {
111            if (namespace == null || namespace.trim().length() == 0) {
112                Package pkg = clazz.getPackage();
113                if (pkg == null) {
114                    return null;
115                } else {
116                    return getNamespace(pkg.getName());
117                }
118            } else {
119                return namespace.trim();
120            }
121        }
122        
123        private static String getNamespace(String packageName) {
124            if (packageName == null || packageName.length() == 0) {
125                return null;
126            }
127            StringTokenizer tokenizer = new StringTokenizer(packageName, ".");
128            String[] tokens;
129            if (tokenizer.countTokens() == 0) {
130                tokens = new String[0];
131            } else {
132                tokens = new String[tokenizer.countTokens()];
133                for (int i = tokenizer.countTokens() - 1; i >= 0; i--) {
134                    tokens[i] = tokenizer.nextToken();
135                }
136            }
137            StringBuffer namespace = new StringBuffer("http://");
138            String dot = "";
139            for (int i = 0; i < tokens.length; i++) {
140                if (i == 1) {
141                    dot = ".";
142                }
143                namespace.append(dot + tokens[i]);
144            }
145            namespace.append('/');
146            return namespace.toString();
147        }
148        
149        private static QName getServiceQName(Class clazz, String namespace, String name) {
150            return new QName(getNamespace(clazz, namespace), getServiceName(clazz, name));         
151        }
152        
153        public static QName getServiceQName(Class clazz) {
154            WebService webService = 
155                (WebService)clazz.getAnnotation(WebService.class);
156            if (webService == null) {
157                WebServiceProvider webServiceProvider = 
158                    (WebServiceProvider)clazz.getAnnotation(WebServiceProvider.class);
159                if (webServiceProvider == null) {
160                    throw new IllegalArgumentException("The " + clazz.getName() + " is not annotated");
161                }
162                return getServiceQName(clazz, webServiceProvider.targetNamespace(), webServiceProvider.serviceName());
163            } else {
164                return getServiceQName(clazz, webService.targetNamespace(), webService.serviceName());
165            }
166        }
167        
168        private static QName getPortQName(Class clazz, String namespace, String name, String portName) {
169            return new QName(getNamespace(clazz, namespace), getPortName(clazz, name, portName));         
170        }
171        
172        public static QName getPortQName(Class clazz) {
173            WebService webService = 
174                (WebService)clazz.getAnnotation(WebService.class);
175            if (webService == null) {
176                WebServiceProvider webServiceProvider = 
177                    (WebServiceProvider)clazz.getAnnotation(WebServiceProvider.class);
178                if (webServiceProvider == null) {
179                    throw new IllegalArgumentException("The " + clazz.getName() + " is not annotated");
180                }
181                return getPortQName(clazz, webServiceProvider.targetNamespace(), null, webServiceProvider.portName());
182            } else {
183                return getPortQName(clazz, webService.targetNamespace(), webService.name(), webService.portName());
184            }
185        }
186            
187        public static String getName(Class clazz) {
188            WebService webService = 
189                (WebService)clazz.getAnnotation(WebService.class);
190            if (webService == null) {
191                WebServiceProvider webServiceProvider = 
192                    (WebServiceProvider)clazz.getAnnotation(WebServiceProvider.class);
193                if (webServiceProvider == null) {
194                    throw new IllegalArgumentException("The " + clazz.getName() + " is not annotated");
195                } 
196                return clazz.getSimpleName();         
197            } else {
198                String sei = webService.endpointInterface();
199                if (sei == null || sei.trim().length() == 0) {
200                    return getName(clazz, webService.name());
201                } else {
202                    try {
203                        Class seiClass = clazz.getClassLoader().loadClass(sei.trim());
204                        return getNameFromSEI(seiClass);
205                    } catch (ClassNotFoundException e) {
206                        throw new RuntimeException("Unable to load SEI class: " + sei, e);
207                    }
208                }
209            }        
210        }
211            
212        private static String getNameFromSEI(Class seiClass) {
213            WebService webService = 
214                (WebService)seiClass.getAnnotation(WebService.class);
215            if (webService == null) {
216                throw new IllegalArgumentException("The " + seiClass.getName() + " is not annotated");
217            } 
218            return getName(seiClass, webService.name());
219        }
220        
221        private static String getName(Class clazz, String name) {
222            if (name == null || name.trim().length() == 0) {
223                return clazz.getSimpleName();
224            } else {
225                return name.trim();
226            }  
227        }
228    
229        private static String getWsdlLocation(Class clazz) {
230            WebService webService = (WebService) clazz.getAnnotation(WebService.class); 
231            if (webService == null) {
232                WebServiceProvider webServiceProvider = 
233                    (WebServiceProvider)clazz.getAnnotation(WebServiceProvider.class);
234                if (webServiceProvider == null) //no WebService or WebServiceProvider annotation
235                    return "";
236                return webServiceProvider.wsdlLocation().trim();
237                
238            } else
239                return webService.wsdlLocation().trim();
240        } 
241        
242        private static String getServiceInterface(Class clazz) {
243            WebService webService = (WebService) clazz.getAnnotation(WebService.class); 
244            if (webService == null) {
245                //WebServiceProvider doesn't support endpointInterface property (JAX-WS 2.0 sec 7.7)
246                return "";  
247            } else {
248                if (webService.endpointInterface() != null & !webService.endpointInterface().equals("")) 
249                    return webService.endpointInterface();
250                else 
251                    return "";
252            }
253    
254        }
255        public static String getServiceWsdlLocation(Class clazz, ClassLoader loader) {
256            if (getWsdlLocation(clazz) != null && !getWsdlLocation(clazz).equals("")) {
257                return getWsdlLocation(clazz);
258            }
259            else { //check if the interface contains the wsdlLocation value
260                String serviceInterfaceClassName = getServiceInterface(clazz);
261                if (serviceInterfaceClassName != null && !serviceInterfaceClassName.equals("")) {
262                    try {
263                        Class serviceInterfaceClass = loader.loadClass(getServiceInterface(clazz));
264                        return getWsdlLocation(serviceInterfaceClass);                    
265                    } catch (Exception e) {
266                        return "";
267                    }
268                } 
269            }
270            return "";
271            
272        }
273        
274        public static boolean containsWsdlLocation(Class clazz, ClassLoader loader) {
275            String wsdlLocSEIFromAnnotation = getServiceWsdlLocation(clazz, loader);
276            if (wsdlLocSEIFromAnnotation != null && !wsdlLocSEIFromAnnotation.equals(""))
277                return true;
278            return false;
279        }
280        
281        public static String getBindingURIFromAnnot(Class clazz, ClassLoader loader) {
282            BindingType bindingType = (BindingType) clazz.getAnnotation(BindingType.class);
283            if (bindingType == null) {
284                return ""; 
285            } else {
286                return bindingType.value();
287            }
288        }
289        
290    }