src/share/jaxws_classes/com/sun/xml/internal/ws/util/HandlerAnnotationProcessor.java

Fri, 14 Feb 2014 11:13:45 +0100

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 515
6cd506508147
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, 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;
alanb@368 29 import com.sun.xml.internal.ws.api.databinding.MetadataReader;
ohair@286 30 import com.sun.xml.internal.ws.api.server.AsyncProvider;
ohair@286 31 import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory;
ohair@286 32 import com.sun.xml.internal.ws.handler.HandlerChainsModel;
alanb@368 33 import com.sun.xml.internal.ws.model.ReflectAnnotationReader;
ohair@286 34 import com.sun.xml.internal.ws.server.EndpointFactory;
ohair@286 35 import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
ohair@286 36 import com.sun.istack.internal.NotNull;
ohair@286 37
ohair@286 38 import javax.jws.HandlerChain;
ohair@286 39 import javax.jws.WebService;
ohair@286 40 import javax.jws.soap.SOAPMessageHandlers;
ohair@286 41 import javax.xml.namespace.QName;
ohair@286 42 import javax.xml.stream.XMLStreamException;
ohair@286 43 import javax.xml.stream.XMLStreamReader;
ohair@286 44 import javax.xml.ws.Provider;
ohair@286 45 import javax.xml.ws.Service;
ohair@286 46 import java.io.IOException;
ohair@286 47 import java.io.InputStream;
ohair@286 48 import java.net.URL;
ohair@286 49 import java.util.logging.Logger;
ohair@286 50
ohair@286 51 /**
ohair@286 52 * <p>Used by client and server side to create handler information
ohair@286 53 * from annotated class. The public methods all return a
ohair@286 54 * HandlerChainInfo that contains the handlers and role information
ohair@286 55 * needed at runtime.
ohair@286 56 *
ohair@286 57 * <p>All of the handler chain descriptors follow the same schema,
ohair@286 58 * whether they are wsdl customizations, handler files specified
ohair@286 59 * by an annotation, or are included in the sun-jaxws.xml file.
ohair@286 60 * So this class is used for all handler xml information. The
ohair@286 61 * two public entry points are
ohair@286 62 * {@link HandlerAnnotationProcessor#buildHandlerInfo}, called
ohair@286 63 * when you have an annotated class that points to a file.
ohair@286 64 *
ohair@286 65 * <p>The methods in the class are static so that it may called
ohair@286 66 * from the runtime statically.
ohair@286 67 *
ohair@286 68 * @see com.sun.xml.internal.ws.util.HandlerAnnotationInfo
ohair@286 69 *
ohair@286 70 * @author JAX-WS Development Team
ohair@286 71 */
ohair@286 72 public class HandlerAnnotationProcessor {
ohair@286 73
ohair@286 74 private static final Logger logger = Logger.getLogger(
ohair@286 75 com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".util");
ohair@286 76
ohair@286 77 /**
ohair@286 78 * <p>This method is called by
ohair@286 79 * {@link EndpointFactory} when
ohair@286 80 * they have an annotated class.
ohair@286 81 *
ohair@286 82 * <p>If there is no handler chain annotation on the class,
ohair@286 83 * this method will return null. Otherwise it will load the
ohair@286 84 * class and call the parseHandlerFile method to read the
ohair@286 85 * information.
ohair@286 86 *
ohair@286 87 * @return A HandlerAnnotationInfo object that stores the
ohair@286 88 * handlers and roles. Will return null if the class passed
ohair@286 89 * in has no handler chain annotation.
ohair@286 90 */
ohair@286 91 public static HandlerAnnotationInfo buildHandlerInfo(@NotNull
ohair@286 92 Class<?> clazz, QName serviceName, QName portName, WSBinding binding) {
ohair@286 93
alanb@368 94 MetadataReader metadataReader = EndpointFactory.getExternalMetadatReader(clazz, binding);
alanb@368 95 if (metadataReader == null) {
alanb@368 96 metadataReader = new ReflectAnnotationReader();
alanb@368 97 }
alanb@368 98
ohair@286 99 // clazz = checkClass(clazz);
alanb@368 100 HandlerChain handlerChain = metadataReader.getAnnotation(HandlerChain.class, clazz);
ohair@286 101 if (handlerChain == null) {
alanb@368 102 clazz = getSEI(clazz, metadataReader);
ohair@286 103 if (clazz != null)
alanb@368 104 handlerChain = metadataReader.getAnnotation(HandlerChain.class, clazz);
ohair@286 105 if (handlerChain == null)
ohair@286 106 return null;
ohair@286 107 }
ohair@286 108
ohair@286 109 if (clazz.getAnnotation(SOAPMessageHandlers.class) != null) {
ohair@286 110 throw new UtilException(
ohair@286 111 "util.handler.cannot.combine.soapmessagehandlers");
ohair@286 112 }
ohair@286 113 InputStream iStream = getFileAsStream(clazz, handlerChain);
ohair@286 114 XMLStreamReader reader =
ohair@286 115 XMLStreamReaderFactory.create(null,iStream, true);
ohair@286 116 XMLStreamReaderUtil.nextElementContent(reader);
ohair@286 117 HandlerAnnotationInfo handlerAnnInfo = HandlerChainsModel.parseHandlerFile(reader, clazz.getClassLoader(),
ohair@286 118 serviceName, portName, binding);
ohair@286 119 try {
ohair@286 120 reader.close();
ohair@286 121 iStream.close();
ohair@286 122 } catch (XMLStreamException e) {
ohair@286 123 e.printStackTrace();
ohair@286 124 throw new UtilException(e.getMessage());
ohair@286 125 } catch (IOException e) {
ohair@286 126 e.printStackTrace();
ohair@286 127 throw new UtilException(e.getMessage());
ohair@286 128 }
ohair@286 129 return handlerAnnInfo;
ohair@286 130 }
ohair@286 131
ohair@286 132 public static HandlerChainsModel buildHandlerChainsModel(final Class<?> clazz) {
ohair@286 133 if(clazz == null) {
ohair@286 134 return null;
ohair@286 135 }
ohair@286 136 HandlerChain handlerChain =
ohair@286 137 clazz.getAnnotation(HandlerChain.class);
ohair@286 138 if(handlerChain == null)
ohair@286 139 return null;
ohair@286 140 InputStream iStream = getFileAsStream(clazz, handlerChain);
ohair@286 141 XMLStreamReader reader =
ohair@286 142 XMLStreamReaderFactory.create(null,iStream, true);
ohair@286 143 XMLStreamReaderUtil.nextElementContent(reader);
ohair@286 144 HandlerChainsModel handlerChainsModel = HandlerChainsModel.parseHandlerConfigFile(clazz, reader);
ohair@286 145 try {
ohair@286 146 reader.close();
ohair@286 147 iStream.close();
ohair@286 148 } catch (XMLStreamException e) {
ohair@286 149 e.printStackTrace();
ohair@286 150 throw new UtilException(e.getMessage());
ohair@286 151 } catch (IOException e) {
ohair@286 152 e.printStackTrace();
ohair@286 153 throw new UtilException(e.getMessage());
ohair@286 154 }
ohair@286 155 return handlerChainsModel;
ohair@286 156 }
ohair@286 157
ohair@286 158 static Class getClass(String className) {
ohair@286 159 try {
ohair@286 160 return Thread.currentThread().getContextClassLoader().loadClass(
ohair@286 161 className);
ohair@286 162 } catch (ClassNotFoundException e) {
ohair@286 163 throw new UtilException("util.handler.class.not.found",
ohair@286 164 className);
ohair@286 165 }
ohair@286 166 }
ohair@286 167
alanb@368 168 static Class getSEI(Class<?> clazz, MetadataReader metadataReader) {
alanb@368 169 if (metadataReader == null) {
alanb@368 170 metadataReader = new ReflectAnnotationReader();
alanb@368 171 }
alanb@368 172
ohair@286 173 if (Provider.class.isAssignableFrom(clazz) || AsyncProvider.class.isAssignableFrom(clazz)) {
ohair@286 174 //No SEI for Provider Implementation
ohair@286 175 return null;
ohair@286 176 }
ohair@286 177 if (Service.class.isAssignableFrom(clazz)) {
ohair@286 178 //No SEI for Service class
ohair@286 179 return null;
ohair@286 180 }
alanb@368 181
alanb@368 182 WebService webService = metadataReader.getAnnotation(WebService.class, clazz);
alanb@368 183 if (webService == null) {
alanb@368 184 throw new UtilException("util.handler.no.webservice.annotation", clazz.getCanonicalName());
ohair@286 185 }
ohair@286 186
ohair@286 187 String ei = webService.endpointInterface();
ohair@286 188 if (ei.length() > 0) {
ohair@286 189 clazz = getClass(webService.endpointInterface());
alanb@368 190 WebService ws = metadataReader.getAnnotation(WebService.class, clazz);
alanb@368 191 if (ws == null) {
ohair@286 192 throw new UtilException("util.handler.endpoint.interface.no.webservice",
ohair@286 193 webService.endpointInterface());
ohair@286 194 }
ohair@286 195 return clazz;
ohair@286 196 }
ohair@286 197 return null;
ohair@286 198 }
ohair@286 199
ohair@286 200 static InputStream getFileAsStream(Class clazz, HandlerChain chain) {
ohair@286 201 URL url = clazz.getResource(chain.file());
ohair@286 202 if (url == null) {
ohair@286 203 url = Thread.currentThread().getContextClassLoader().
ohair@286 204 getResource(chain.file());
ohair@286 205 }
ohair@286 206 if (url == null) {
ohair@286 207 String tmp = clazz.getPackage().getName();
ohair@286 208 tmp = tmp.replace('.', '/');
ohair@286 209 tmp += "/" + chain.file();
ohair@286 210 url =
ohair@286 211 Thread.currentThread().getContextClassLoader().getResource(tmp);
ohair@286 212 }
ohair@286 213 if (url == null) {
ohair@286 214 throw new UtilException("util.failed.to.find.handlerchain.file",
ohair@286 215 clazz.getName(), chain.file());
ohair@286 216 }
ohair@286 217 try {
ohair@286 218 return url.openStream();
ohair@286 219 } catch (IOException e) {
ohair@286 220 throw new UtilException("util.failed.to.parse.handlerchain.file",
ohair@286 221 clazz.getName(), chain.file());
ohair@286 222 }
ohair@286 223 }
ohair@286 224 }

mercurial