KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > axis > handlers > AnnotatedWebServiceDeploymentHandler


1 package org.apache.beehive.wsm.axis.handlers;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */

20
21 import java.io.File JavaDoc;
22 import java.io.FilenameFilter JavaDoc;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31
32
33 import javax.xml.namespace.QName JavaDoc;
34 import javax.xml.rpc.handler.HandlerInfo JavaDoc;
35
36 import org.apache.axis.AxisFault;
37 import org.apache.axis.Constants;
38 import org.apache.axis.MessageContext;
39 import org.apache.axis.client.Call;
40 import org.apache.axis.components.logger.LogFactory;
41 import org.apache.axis.constants.Use;
42 import org.apache.axis.description.ServiceDesc;
43 import org.apache.axis.handlers.BasicHandler;
44 import org.apache.axis.handlers.HandlerInfoChainFactory;
45 import org.apache.axis.handlers.soap.SOAPService;
46 import org.apache.axis.providers.java.RPCProvider;
47 import org.apache.axis.utils.ClassUtils;
48
49
50 import org.apache.beehive.wsm.axis.AxisHook;
51 import org.apache.beehive.wsm.axis.ControlProvider;
52 import org.apache.beehive.wsm.model.BeehiveWsSOAPMessageHandlerInfo;
53 import org.apache.beehive.wsm.model.BeehiveWsTypeMetadata;
54 import org.apache.beehive.wsm.model.jsr181.Jsr181ObjectModelStore;
55 import org.w3c.dom.Element JavaDoc;
56
57 import org.apache.log4j.Logger;
58 /**
59  * ****************************************************************************
60  * A few annotation specific things added and lots of code copied from the
61  * JWSHandler class by
62  *
63  * @author Jonathan Colwell (jcolwell@bea.com) but most of the original code was
64  * written by:
65  * @author Glen Daniels (gdaniels@allaire.com)
66  * @author Doug Davis (dug@us.ibm.com)
67  * @author Sam Ruby (rubys@us.ibm.com) todo: fix directory
68  * structure/classloaders for output files
69  */

70 public class AnnotatedWebServiceDeploymentHandler extends BasicHandler {
71
72     protected static Logger logger = Logger.getLogger(AnnotatedWebServiceDeploymentHandler.class);
73
74     protected Map JavaDoc<String JavaDoc, SOAPService> soapServices = new HashMap JavaDoc<String JavaDoc, SOAPService>();
75
76     /**
77      * @param mc
78      * @throws AxisFault
79      */

80     public void invoke(MessageContext mc) throws AxisFault {
81
82         try {
83             SOAPService ss = getSOAPService(getWebServiceClass(mc));
84             if (null != ss) {
85                 ss.setEngine(mc.getAxisEngine());
86                 mc.setService(ss);
87                 /*
88                  * NOTE jcolwell@bea.com 2004-Oct-22 -- not sure why, but xsi
89                  * types were still being sent even with literal Use. Forcing it
90                  * to not send the types unless encoded.
91                  */

92                 mc.setProperty(Call.SEND_TYPE_ATTR, new Boolean JavaDoc(Use.ENCODED
93                         .equals(ss.getUse())));
94
95                 // blow away the real path to bypass the regular JWSHandler
96
mc.removeProperty(Constants.MC_REALPATH);
97             }
98         } catch (Exception JavaDoc e) {
99             throw AxisFault.makeFault(e);
100         }
101     }
102
103     /**
104      * @param mc
105      * @throws AxisFault
106      */

107     public void generateWSDL(MessageContext mc) throws AxisFault {
108         invoke(mc);
109     }
110
111     /**
112      * @param clazz
113      * @return SOAPService
114      * @throws Exception
115      */

116     protected SOAPService getSOAPService(Class JavaDoc clazz) throws Exception JavaDoc {
117         SOAPService ss = null;
118         if (null != clazz) {
119             ss = soapServices.get(clazz.getName());
120             if (null == ss) {
121                 ss = createSOAPService(Jsr181ObjectModelStore.load(clazz),
122                         clazz);
123             }
124         }
125         return ss;
126     }
127
128     /**
129      * @param wsm
130      * @param cls
131      * @return SOAPService
132      * @throws Exception
133      */

134     protected SOAPService createSOAPService(BeehiveWsTypeMetadata wsm, Class JavaDoc cls)
135             throws Exception JavaDoc {
136
137         SOAPService ss = null;
138         if (null != wsm) {
139             // HandlerHandler hh = new HandlerHandler(wsm);
140
// hh.init();
141
// // NOTE jcolwell@bea.com 2004-Nov-01 --
142
// // not calling ss.init() since ControlProvider won't do anything
143
// // with it and I don't want the HandlerHandler initialized twice.
144
// ss = new SOAPService(hh, new ControlProvider(), hh);
145

146             // create soap service without any handlers.
147
ss = new SOAPService(null, new ControlProvider(), null);
148
149             // Add jaxrpc handlers if there are any.
150
HandlerInfoChainFactory jaxrpcChain;
151             if (null != (jaxrpcChain = getJaxRPCHandlerChainFactory(wsm))) {
152                 ss.setOption(Constants.ATTR_HANDLERINFOCHAIN, jaxrpcChain);
153             }
154             ss.setName(wsm.getWsName());
155             ss.setOption(RPCProvider.OPTION_CLASSNAME, cls.getName());
156
157             // Take the setting for the scope option from the handler
158
// parameter named "scope"
159
String JavaDoc scope = (String JavaDoc) getOption(RPCProvider.OPTION_SCOPE);
160             if (null == scope) {
161                 scope = "Request";
162             }
163             ss.setOption(RPCProvider.OPTION_SCOPE, scope);
164             ServiceDesc sd = AxisHook.createServiceDesc(wsm, cls.getClassLoader());
165             ss.setServiceDescription(sd);
166             List JavaDoc allowedMethodNames = sd.getAllowedMethods();
167             Iterator JavaDoc allowIt = allowedMethodNames.iterator();
168             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
169             boolean firstPass = true;
170             while (allowIt.hasNext()) {
171                 if (firstPass) {
172                     sb.append(' ');
173                     firstPass = false;
174                 }
175                 sb.append(((String JavaDoc) allowIt.next()));
176             }
177             ss.setOption(RPCProvider.OPTION_ALLOWEDMETHODS, sb.toString());
178             ss.setOption(RPCProvider.OPTION_WSDL_PORTTYPE, wsm.getWsName());
179             ss.setOption(RPCProvider.OPTION_WSDL_SERVICEPORT, wsm.getWsName());
180             ss.setOption(RPCProvider.OPTION_WSDL_SERVICEELEMENT, wsm
181                     .getWsServiceName());
182             ss.setOption(RPCProvider.OPTION_WSDL_TARGETNAMESPACE, wsm
183                     .getWsTargetNamespace());
184             ss.setStyle(sd.getStyle());
185             ss.setUse(sd.getUse());
186             soapServices.put(cls.getName(), ss);
187         }
188         return ss;
189     }
190
191     protected HandlerInfoChainFactory getJaxRPCHandlerChainFactory(
192             BeehiveWsTypeMetadata wsm) throws ClassNotFoundException JavaDoc {
193         HandlerInfoChainFactory res = null;
194         List JavaDoc< ? extends BeehiveWsSOAPMessageHandlerInfo> handlers = wsm
195                 .getSoapHandlers();
196         if (handlers.size() > 0) { // there are handlers to install
197
ArrayList JavaDoc<HandlerInfo JavaDoc> infoList = new ArrayList JavaDoc<HandlerInfo JavaDoc>();
198             HashSet JavaDoc<String JavaDoc> roles = new HashSet JavaDoc<String JavaDoc>();
199
200             for (BeehiveWsSOAPMessageHandlerInfo handler : handlers) {
201                 Class JavaDoc handlerClass = ClassUtils.forName(handler.getClassName());
202                 QName JavaDoc[] handlerHeaders = handler.getHeaders().toArray(
203                         new QName JavaDoc[0]);
204                 infoList.add(new HandlerInfo JavaDoc(handlerClass, (Map JavaDoc) handler
205                         .getParameterMap(), handlerHeaders));
206                 roles.addAll(handler.getRoles());
207             }
208             res = new HandlerInfoChainFactory(infoList);
209             String JavaDoc[] roleArray = roles.toArray(new String JavaDoc[0]);
210             res.setRoles(roleArray);
211         }
212         return res;
213     }
214
215     /**
216      * @param mc
217      * @return web service Class
218      * @throws Exception
219      */

220     protected Class JavaDoc getWebServiceClass(MessageContext mc) throws Exception JavaDoc {
221         // check message context
222
if (null == mc) {
223             throw new IllegalArgumentException JavaDoc("message context not set");
224         }
225
226         // retrieve & check relative path
227
String JavaDoc relativePath = mc.getStrProp(Constants.MC_RELATIVE_PATH);
228         if (null == relativePath) {
229             throw new Exception JavaDoc(
230                     "relative path not set properly in message context");
231         }
232
233         // turn relative path into fq class name
234
final String JavaDoc JWS_EXTENSION = ".jws";
235
236 /*
237         if ((! relativePath.startsWith("/")) || (! relativePath.endsWith(JWS_EXTENSION))) {
238             throw new Exception ("invalid relative path: " + relativePath);
239         }
240         String className = relativePath.substring(1,
241                 relativePath.length() - JWS_EXTENSION.length()).replaceAll("/",
242                 ".");
243 */

244
245         if (! relativePath.startsWith("/")) {
246             throw new Exception JavaDoc ("invalid relative path: " + relativePath);
247         }
248         String JavaDoc className = null;
249         if (relativePath.endsWith(JWS_EXTENSION)) {
250             className = relativePath.substring(1, relativePath.length() - JWS_EXTENSION.length()).replaceAll("/", ".");
251         }
252         else {
253             className = relativePath.substring(1, relativePath.length()).replaceAll("/", ".");
254         }
255         
256         // load class
257
if (logger.isDebugEnabled()) {
258             logger.debug("trying to load web service class: " + className);
259         }
260         Class JavaDoc clazz = Jsr181ObjectModelStore.loadWebServiceClass(className);
261         if (null == clazz) {
262             if (logger.isDebugEnabled()) {
263                 logger.debug("failed to load web service class: " + className);
264             }
265         }
266
267         return clazz;
268     }
269 }
270
Popular Tags