Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
ohair@286 | 1 | /* |
ohair@286 | 2 | * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.ws.util; |
ohair@286 | 27 | |
ohair@286 | 28 | import com.sun.xml.internal.ws.api.WSBinding; |
ohair@286 | 29 | import com.sun.xml.internal.ws.api.server.AsyncProvider; |
ohair@286 | 30 | import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory; |
ohair@286 | 31 | import com.sun.xml.internal.ws.handler.HandlerChainsModel; |
ohair@286 | 32 | import com.sun.xml.internal.ws.server.EndpointFactory; |
ohair@286 | 33 | import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil; |
ohair@286 | 34 | import com.sun.istack.internal.NotNull; |
ohair@286 | 35 | |
ohair@286 | 36 | import javax.jws.HandlerChain; |
ohair@286 | 37 | import javax.jws.WebService; |
ohair@286 | 38 | import javax.jws.soap.SOAPMessageHandlers; |
ohair@286 | 39 | import javax.xml.namespace.QName; |
ohair@286 | 40 | import javax.xml.stream.XMLStreamException; |
ohair@286 | 41 | import javax.xml.stream.XMLStreamReader; |
ohair@286 | 42 | import javax.xml.ws.Provider; |
ohair@286 | 43 | import javax.xml.ws.Service; |
ohair@286 | 44 | import java.io.IOException; |
ohair@286 | 45 | import java.io.InputStream; |
ohair@286 | 46 | import java.net.URL; |
ohair@286 | 47 | import java.util.logging.Logger; |
ohair@286 | 48 | |
ohair@286 | 49 | /** |
ohair@286 | 50 | * <p>Used by client and server side to create handler information |
ohair@286 | 51 | * from annotated class. The public methods all return a |
ohair@286 | 52 | * HandlerChainInfo that contains the handlers and role information |
ohair@286 | 53 | * needed at runtime. |
ohair@286 | 54 | * |
ohair@286 | 55 | * <p>All of the handler chain descriptors follow the same schema, |
ohair@286 | 56 | * whether they are wsdl customizations, handler files specified |
ohair@286 | 57 | * by an annotation, or are included in the sun-jaxws.xml file. |
ohair@286 | 58 | * So this class is used for all handler xml information. The |
ohair@286 | 59 | * two public entry points are |
ohair@286 | 60 | * {@link HandlerAnnotationProcessor#buildHandlerInfo}, called |
ohair@286 | 61 | * when you have an annotated class that points to a file. |
ohair@286 | 62 | * |
ohair@286 | 63 | * <p>The methods in the class are static so that it may called |
ohair@286 | 64 | * from the runtime statically. |
ohair@286 | 65 | * |
ohair@286 | 66 | * @see com.sun.xml.internal.ws.util.HandlerAnnotationInfo |
ohair@286 | 67 | * |
ohair@286 | 68 | * @author JAX-WS Development Team |
ohair@286 | 69 | */ |
ohair@286 | 70 | public class HandlerAnnotationProcessor { |
ohair@286 | 71 | |
ohair@286 | 72 | private static final Logger logger = Logger.getLogger( |
ohair@286 | 73 | com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".util"); |
ohair@286 | 74 | |
ohair@286 | 75 | /** |
ohair@286 | 76 | * <p>This method is called by |
ohair@286 | 77 | * {@link EndpointFactory} when |
ohair@286 | 78 | * they have an annotated class. |
ohair@286 | 79 | * |
ohair@286 | 80 | * <p>If there is no handler chain annotation on the class, |
ohair@286 | 81 | * this method will return null. Otherwise it will load the |
ohair@286 | 82 | * class and call the parseHandlerFile method to read the |
ohair@286 | 83 | * information. |
ohair@286 | 84 | * |
ohair@286 | 85 | * @return A HandlerAnnotationInfo object that stores the |
ohair@286 | 86 | * handlers and roles. Will return null if the class passed |
ohair@286 | 87 | * in has no handler chain annotation. |
ohair@286 | 88 | */ |
ohair@286 | 89 | public static HandlerAnnotationInfo buildHandlerInfo(@NotNull |
ohair@286 | 90 | Class<?> clazz, QName serviceName, QName portName, WSBinding binding) { |
ohair@286 | 91 | |
ohair@286 | 92 | // clazz = checkClass(clazz); |
ohair@286 | 93 | HandlerChain handlerChain = |
ohair@286 | 94 | clazz.getAnnotation(HandlerChain.class); |
ohair@286 | 95 | if (handlerChain == null) { |
ohair@286 | 96 | clazz = getSEI(clazz); |
ohair@286 | 97 | if (clazz != null) |
ohair@286 | 98 | handlerChain = |
ohair@286 | 99 | clazz.getAnnotation(HandlerChain.class); |
ohair@286 | 100 | if (handlerChain == null) |
ohair@286 | 101 | return null; |
ohair@286 | 102 | } |
ohair@286 | 103 | |
ohair@286 | 104 | if (clazz.getAnnotation(SOAPMessageHandlers.class) != null) { |
ohair@286 | 105 | throw new UtilException( |
ohair@286 | 106 | "util.handler.cannot.combine.soapmessagehandlers"); |
ohair@286 | 107 | } |
ohair@286 | 108 | InputStream iStream = getFileAsStream(clazz, handlerChain); |
ohair@286 | 109 | XMLStreamReader reader = |
ohair@286 | 110 | XMLStreamReaderFactory.create(null,iStream, true); |
ohair@286 | 111 | XMLStreamReaderUtil.nextElementContent(reader); |
ohair@286 | 112 | HandlerAnnotationInfo handlerAnnInfo = HandlerChainsModel.parseHandlerFile(reader, clazz.getClassLoader(), |
ohair@286 | 113 | serviceName, portName, binding); |
ohair@286 | 114 | try { |
ohair@286 | 115 | reader.close(); |
ohair@286 | 116 | iStream.close(); |
ohair@286 | 117 | } catch (XMLStreamException e) { |
ohair@286 | 118 | e.printStackTrace(); |
ohair@286 | 119 | throw new UtilException(e.getMessage()); |
ohair@286 | 120 | } catch (IOException e) { |
ohair@286 | 121 | e.printStackTrace(); |
ohair@286 | 122 | throw new UtilException(e.getMessage()); |
ohair@286 | 123 | } |
ohair@286 | 124 | return handlerAnnInfo; |
ohair@286 | 125 | } |
ohair@286 | 126 | |
ohair@286 | 127 | public static HandlerChainsModel buildHandlerChainsModel(final Class<?> clazz) { |
ohair@286 | 128 | if(clazz == null) { |
ohair@286 | 129 | return null; |
ohair@286 | 130 | } |
ohair@286 | 131 | HandlerChain handlerChain = |
ohair@286 | 132 | clazz.getAnnotation(HandlerChain.class); |
ohair@286 | 133 | if(handlerChain == null) |
ohair@286 | 134 | return null; |
ohair@286 | 135 | InputStream iStream = getFileAsStream(clazz, handlerChain); |
ohair@286 | 136 | XMLStreamReader reader = |
ohair@286 | 137 | XMLStreamReaderFactory.create(null,iStream, true); |
ohair@286 | 138 | XMLStreamReaderUtil.nextElementContent(reader); |
ohair@286 | 139 | HandlerChainsModel handlerChainsModel = HandlerChainsModel.parseHandlerConfigFile(clazz, reader); |
ohair@286 | 140 | try { |
ohair@286 | 141 | reader.close(); |
ohair@286 | 142 | iStream.close(); |
ohair@286 | 143 | } catch (XMLStreamException e) { |
ohair@286 | 144 | e.printStackTrace(); |
ohair@286 | 145 | throw new UtilException(e.getMessage()); |
ohair@286 | 146 | } catch (IOException e) { |
ohair@286 | 147 | e.printStackTrace(); |
ohair@286 | 148 | throw new UtilException(e.getMessage()); |
ohair@286 | 149 | } |
ohair@286 | 150 | return handlerChainsModel; |
ohair@286 | 151 | } |
ohair@286 | 152 | |
ohair@286 | 153 | static Class getClass(String className) { |
ohair@286 | 154 | try { |
ohair@286 | 155 | return Thread.currentThread().getContextClassLoader().loadClass( |
ohair@286 | 156 | className); |
ohair@286 | 157 | } catch (ClassNotFoundException e) { |
ohair@286 | 158 | throw new UtilException("util.handler.class.not.found", |
ohair@286 | 159 | className); |
ohair@286 | 160 | } |
ohair@286 | 161 | } |
ohair@286 | 162 | |
ohair@286 | 163 | static Class getSEI(Class<?> clazz) { |
ohair@286 | 164 | if (Provider.class.isAssignableFrom(clazz) || AsyncProvider.class.isAssignableFrom(clazz)) { |
ohair@286 | 165 | //No SEI for Provider Implementation |
ohair@286 | 166 | return null; |
ohair@286 | 167 | } |
ohair@286 | 168 | if (Service.class.isAssignableFrom(clazz)) { |
ohair@286 | 169 | //No SEI for Service class |
ohair@286 | 170 | return null; |
ohair@286 | 171 | } |
ohair@286 | 172 | if (!clazz.isAnnotationPresent(WebService.class)) { |
ohair@286 | 173 | throw new UtilException("util.handler.no.webservice.annotation", |
ohair@286 | 174 | clazz.getCanonicalName()); |
ohair@286 | 175 | } |
ohair@286 | 176 | |
ohair@286 | 177 | WebService webService = clazz.getAnnotation(WebService.class); |
ohair@286 | 178 | |
ohair@286 | 179 | String ei = webService.endpointInterface(); |
ohair@286 | 180 | if (ei.length() > 0) { |
ohair@286 | 181 | clazz = getClass(webService.endpointInterface()); |
ohair@286 | 182 | if (!clazz.isAnnotationPresent(WebService.class)) { |
ohair@286 | 183 | throw new UtilException("util.handler.endpoint.interface.no.webservice", |
ohair@286 | 184 | webService.endpointInterface()); |
ohair@286 | 185 | } |
ohair@286 | 186 | return clazz; |
ohair@286 | 187 | } |
ohair@286 | 188 | return null; |
ohair@286 | 189 | } |
ohair@286 | 190 | |
ohair@286 | 191 | static InputStream getFileAsStream(Class clazz, HandlerChain chain) { |
ohair@286 | 192 | URL url = clazz.getResource(chain.file()); |
ohair@286 | 193 | if (url == null) { |
ohair@286 | 194 | url = Thread.currentThread().getContextClassLoader(). |
ohair@286 | 195 | getResource(chain.file()); |
ohair@286 | 196 | } |
ohair@286 | 197 | if (url == null) { |
ohair@286 | 198 | String tmp = clazz.getPackage().getName(); |
ohair@286 | 199 | tmp = tmp.replace('.', '/'); |
ohair@286 | 200 | tmp += "/" + chain.file(); |
ohair@286 | 201 | url = |
ohair@286 | 202 | Thread.currentThread().getContextClassLoader().getResource(tmp); |
ohair@286 | 203 | } |
ohair@286 | 204 | if (url == null) { |
ohair@286 | 205 | throw new UtilException("util.failed.to.find.handlerchain.file", |
ohair@286 | 206 | clazz.getName(), chain.file()); |
ohair@286 | 207 | } |
ohair@286 | 208 | try { |
ohair@286 | 209 | return url.openStream(); |
ohair@286 | 210 | } catch (IOException e) { |
ohair@286 | 211 | throw new UtilException("util.failed.to.parse.handlerchain.file", |
ohair@286 | 212 | clazz.getName(), chain.file()); |
ohair@286 | 213 | } |
ohair@286 | 214 | } |
ohair@286 | 215 | } |