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

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 637
9c07ef4934dd
permissions
-rw-r--r--

merge

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

mercurial