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