001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.xml.ws.util;
037:
038: import com.sun.xml.ws.api.WSBinding;
039: import com.sun.xml.ws.api.server.AsyncProvider;
040: import com.sun.xml.ws.api.streaming.XMLStreamReaderFactory;
041: import com.sun.xml.ws.handler.HandlerChainsModel;
042: import com.sun.xml.ws.server.EndpointFactory;
043: import com.sun.xml.ws.streaming.XMLStreamReaderUtil;
044: import com.sun.istack.NotNull;
045:
046: import javax.jws.HandlerChain;
047: import javax.jws.WebService;
048: import javax.jws.soap.SOAPMessageHandlers;
049: import javax.xml.namespace.QName;
050: import javax.xml.stream.XMLStreamException;
051: import javax.xml.stream.XMLStreamReader;
052: import javax.xml.ws.Provider;
053: import javax.xml.ws.Service;
054: import java.io.IOException;
055: import java.io.InputStream;
056: import java.net.URL;
057: import java.util.logging.Logger;
058:
059: /**
060: * <p>Used by client and server side to create handler information
061: * from annotated class. The public methods all return a
062: * HandlerChainInfo that contains the handlers and role information
063: * needed at runtime.
064: *
065: * <p>All of the handler chain descriptors follow the same schema,
066: * whether they are wsdl customizations, handler files specified
067: * by an annotation, or are included in the sun-jaxws.xml file.
068: * So this class is used for all handler xml information. The
069: * two public entry points are
070: * {@link HandlerAnnotationProcessor#buildHandlerInfo}, called
071: * when you have an annotated class that points to a file.
072: *
073: * <p>The methods in the class are static so that it may called
074: * from the runtime statically.
075: *
076: * @see com.sun.xml.ws.util.HandlerAnnotationInfo
077: *
078: * @author JAX-WS Development Team
079: */
080: public class HandlerAnnotationProcessor {
081:
082: private static final Logger logger = Logger
083: .getLogger(com.sun.xml.ws.util.Constants.LoggingDomain
084: + ".util");
085:
086: /**
087: * <p>This method is called by
088: * {@link EndpointFactory} when
089: * they have an annotated class.
090: *
091: * <p>If there is no handler chain annotation on the class,
092: * this method will return null. Otherwise it will load the
093: * class and call the parseHandlerFile method to read the
094: * information.
095: *
096: * @return A HandlerAnnotationInfo object that stores the
097: * handlers and roles. Will return null if the class passed
098: * in has no handler chain annotation.
099: */
100: public static HandlerAnnotationInfo buildHandlerInfo(@NotNull
101: Class<?> clazz, QName serviceName, QName portName, WSBinding binding) {
102:
103: // clazz = checkClass(clazz);
104: HandlerChain handlerChain = clazz
105: .getAnnotation(HandlerChain.class);
106: if (handlerChain == null) {
107: clazz = getSEI(clazz);
108: if (clazz != null)
109: handlerChain = clazz.getAnnotation(HandlerChain.class);
110: if (handlerChain == null)
111: return null;
112: }
113:
114: if (clazz.getAnnotation(SOAPMessageHandlers.class) != null) {
115: throw new UtilException(
116: "util.handler.cannot.combine.soapmessagehandlers");
117: }
118: InputStream iStream = getFileAsStream(clazz, handlerChain);
119: XMLStreamReader reader = XMLStreamReaderFactory.create(null,
120: iStream, true);
121: XMLStreamReaderUtil.nextElementContent(reader);
122: HandlerAnnotationInfo handlerAnnInfo = HandlerChainsModel
123: .parseHandlerFile(reader, clazz.getClassLoader(),
124: serviceName, portName, binding);
125: try {
126: reader.close();
127: iStream.close();
128: } catch (XMLStreamException e) {
129: e.printStackTrace();
130: throw new UtilException(e.getMessage());
131: } catch (IOException e) {
132: e.printStackTrace();
133: throw new UtilException(e.getMessage());
134: }
135: return handlerAnnInfo;
136: }
137:
138: public static HandlerChainsModel buildHandlerChainsModel(
139: final Class<?> clazz) {
140: if (clazz == null) {
141: return null;
142: }
143: HandlerChain handlerChain = clazz
144: .getAnnotation(HandlerChain.class);
145: if (handlerChain == null)
146: return null;
147: InputStream iStream = getFileAsStream(clazz, handlerChain);
148: XMLStreamReader reader = XMLStreamReaderFactory.create(null,
149: iStream, true);
150: XMLStreamReaderUtil.nextElementContent(reader);
151: HandlerChainsModel handlerChainsModel = HandlerChainsModel
152: .parseHandlerConfigFile(clazz, reader);
153: try {
154: reader.close();
155: iStream.close();
156: } catch (XMLStreamException e) {
157: e.printStackTrace();
158: throw new UtilException(e.getMessage());
159: } catch (IOException e) {
160: e.printStackTrace();
161: throw new UtilException(e.getMessage());
162: }
163: return handlerChainsModel;
164: }
165:
166: static Class getClass(String className) {
167: try {
168: return Thread.currentThread().getContextClassLoader()
169: .loadClass(className);
170: } catch (ClassNotFoundException e) {
171: throw new UtilException("util.handler.class.not.found",
172: className);
173: }
174: }
175:
176: static Class getSEI(Class<?> clazz) {
177: if (Provider.class.isAssignableFrom(clazz)
178: || AsyncProvider.class.isAssignableFrom(clazz)) {
179: //No SEI for Provider Implementation
180: return null;
181: }
182: if (Service.class.isAssignableFrom(clazz)) {
183: //No SEI for Service class
184: return null;
185: }
186: if (!clazz.isAnnotationPresent(WebService.class)) {
187: throw new UtilException(
188: "util.handler.no.webservice.annotation", clazz
189: .getCanonicalName());
190: }
191:
192: WebService webService = clazz.getAnnotation(WebService.class);
193:
194: String ei = webService.endpointInterface();
195: if (ei.length() > 0) {
196: clazz = getClass(webService.endpointInterface());
197: if (!clazz.isAnnotationPresent(WebService.class)) {
198: throw new UtilException(
199: "util.handler.endpoint.interface.no.webservice",
200: webService.endpointInterface());
201: }
202: return clazz;
203: }
204: return null;
205: }
206:
207: static InputStream getFileAsStream(Class clazz, HandlerChain chain) {
208: URL url = clazz.getResource(chain.file());
209: if (url == null) {
210: url = Thread.currentThread().getContextClassLoader()
211: .getResource(chain.file());
212: }
213: if (url == null) {
214: String tmp = clazz.getPackage().getName();
215: tmp = tmp.replace('.', '/');
216: tmp += "/" + chain.file();
217: url = Thread.currentThread().getContextClassLoader()
218: .getResource(tmp);
219: }
220: if (url == null) {
221: throw new UtilException(
222: "util.failed.to.find.handlerchain.file", clazz
223: .getName(), chain.file());
224: }
225: try {
226: return url.openStream();
227: } catch (IOException e) {
228: throw new UtilException(
229: "util.failed.to.parse.handlerchain.file", clazz
230: .getName(), chain.file());
231: }
232: }
233: }
|