KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > AppServWebServiceInfoProvider


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.common;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import com.sun.enterprise.tools.common.dd.webservice.Webservices;
29 import com.sun.enterprise.tools.common.dd.webservice.PortComponentType;
30 import com.sun.enterprise.tools.common.dd.webservice.WebserviceDescriptionType;
31 import com.sun.enterprise.tools.common.dd.webservice.ServiceImplBeanType;
32 import com.sun.enterprise.admin.wsmgmt.repository.spi.WebServiceInfoProvider;
33 import com.sun.enterprise.admin.wsmgmt.repository.spi.RepositoryException;
34 import com.sun.appserv.management.ext.wsmgmt.WebServiceEndpointInfo;
35 import com.sun.appserv.management.ext.wsmgmt.WebServiceEndpointInfoImpl;
36
37 import com.sun.enterprise.tools.common.dd.ejb.SunEjbJar;
38 import com.sun.enterprise.tools.common.dd.ejb.EnterpriseBeans;
39 import com.sun.enterprise.tools.common.dd.ejb.Ejb;
40 import com.sun.enterprise.tools.common.dd.WebserviceEndpoint;
41
42 import com.sun.enterprise.tools.common.dd.webapp.SunWebApp;
43 import com.sun.enterprise.tools.common.dd.webapp.Servlet;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.List JavaDoc;
47 import java.util.Map JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.io.FileNotFoundException JavaDoc;
50 import com.sun.enterprise.admin.mbeans.J2EEModule;
51
52 import org.netbeans.modules.schema2beans.Schema2BeansException;
53 import com.sun.enterprise.config.ConfigContext;
54 import com.sun.enterprise.config.serverbeans.ApplicationHelper;
55 import com.sun.enterprise.config.serverbeans.WebModule;
56 import com.sun.enterprise.config.ConfigBean;
57 import com.sun.enterprise.admin.server.core.AdminService;
58
59 /**
60  * This is the mechanism to provide web service information for a given module.
61  * A WebServiceInfoProvider implementation is a class that extends the
62  * WebServiceInfoProvider abstract class. Some WebServiceInfoProvider can deal
63  * with ejb and web module. Some only deal with web modules.
64  * <br>
65  * A WebServiceInfoProvider implemented is identified by its fully qualified
66  * class name. The default RepositoryProvider is
67  * com.sun.enterprise.admin.repository.spi.impl.AppServWebServiceInfoProvider
68  */

69 public class AppServWebServiceInfoProvider implements WebServiceInfoProvider {
70
71     /**
72      * Returns the unique identifier for this WebServiceInfoProvider object.
73      *
74      * @return fully qualified class name of this WebServiceInfoProvider
75      */

76     public String JavaDoc getProviderID() {
77         return PROVIDER_ID;
78     }
79
80     /**
81      * Returns the List of WebServiceInfos for the provided EJB module.
82      * @param moduleInfo Descriptor file (sun-ejb-jar.xml or sun-web-app.xml)
83      * location
84      * @param propMap Additional properties passed
85      *
86      * @return the List of WebServiceInfos
87      * @throws when the descriptor can not be read or file is not of
88      * sun-ejb-jar.xml or sun-web-app.xml type.
89      */

90     public List JavaDoc getWebServiceInfo(String JavaDoc moduleInfo, Map JavaDoc propMap)
91                     throws RepositoryException {
92         String JavaDoc moduleType = null,appId =null, bundleName = null,
93                     webservices = null, bundleRoot = null;
94         if (propMap != null) {
95             moduleType = (String JavaDoc)
96                 propMap.get(WebServiceInfoProvider.MOD_TYPE_PROP_NAME);
97             appId = (String JavaDoc)
98                propMap.get(WebServiceInfoProvider.APP_ID_PROP_NAME);
99             bundleName = (String JavaDoc) propMap.get(
100                WebServiceInfoProvider.BUNDLE_NAME_PROP_NAME);
101             webservices = (String JavaDoc) propMap.get(
102                 WebServiceInfoProvider.WS_XML_LOCATION_PROP_NAME);
103             bundleRoot = (String JavaDoc)
104              propMap.get(WebServiceInfoProvider.BUNDLE_ROOT_LOCATION_PROP_NAME);
105         }
106
107         // validate mandatory arguments
108
if ( (moduleType==null) || (appId==null)
109                 || (webservices==null) || (bundleRoot==null) ) {
110             throw new IllegalArgumentException JavaDoc();
111         }
112
113         return getWebServiceInfoInternal(moduleInfo, appId,
114                         bundleName, webservices, bundleRoot, propMap);
115     }
116
117     /**
118      * Returns list of WebServiceEndpointInfo ojbects for
119      * all available web serivce end points in this module.
120      *
121      * @param moduleInfo location to sun-web.xml or sun-ejb-jar.xml descriptor
122      * @param appId name of application or stand alone module
123      * @param bundleName name of embedded module name. This is null
124      * for stand alone module
125      * @param webservices location of webservices.xml file
126      * @param bundleRoot location of module root. For embedded module, this
127      * would point to generated/xml/j2ee-applications/moduleName.
128      * For stand alone module, this would point to
129      * generated/xml/j2ee-modules/moduleName
130      *
131      * @return list of WebServiceEndpointInfo
132      *
133      * @throws RepositoryException if an error during parsing descriptor
134      */

135     private List JavaDoc getWebServiceInfoInternal(String JavaDoc moduleInfo,
136                     String JavaDoc appId, String JavaDoc bundleName, String JavaDoc
137                     webservices, String JavaDoc bundleRoot, Map JavaDoc propMap)
138                     throws RepositoryException {
139
140         // load webservices.xml beans
141
FileInputStream JavaDoc in = null;
142         try {
143             in = new FileInputStream JavaDoc(webservices);
144         } catch(FileNotFoundException JavaDoc fne) {
145             throw new RepositoryException(fne);
146         }
147         Webservices webServices = null;
148         webServices = Webservices.createGraph(in);
149
150         // bundle name is null for stand alone module
151
boolean isAppStandAloneModule = (bundleName==null) ? true : false;
152
153         // all web service endpoints
154
WebserviceDescriptionType[] wsdts =
155                 webServices.getWebserviceDescription();
156
157         ArrayList JavaDoc aList = new ArrayList JavaDoc();
158         String JavaDoc uri = null, implName = null, implType = null, implClass = null;
159         Map JavaDoc wsWebMap = null, wsEjbMap = null;
160
161         // get all web services in this module
162
for (int wsCnt =0; wsCnt < wsdts.length; wsCnt++) {
163                 WebserviceDescriptionType wsdt = wsdts[wsCnt];
164
165                 String JavaDoc wsdl = null;
166                 String JavaDoc wsdlFile = null;
167                 String JavaDoc mappingFileName = null;
168                 String JavaDoc mapping = null;
169                 if (wsdt != null) {
170                     wsdl = bundleRoot + File.separator + wsdt.getWsdlFile();
171                     String JavaDoc mapFile = wsdt.getJaxrpcMappingFile();
172                     if (mapFile == null) {
173                         mappingFileName = null;
174                     } else {
175                         mappingFileName = bundleRoot + File.separator + mapFile;
176                     }
177                 }
178
179                 J2EEModule j2eeModule = new J2EEModule();
180                 try {
181                     if (mappingFileName != null) {
182                         mapping = j2eeModule.getStringForDDxml(mappingFileName);
183                     }
184                 } catch (Exception JavaDoc e ) {
185                   //_logger.log(Level.FINE,"Error reading dd file contents", e);
186
}
187
188                 try {
189                     if(wsdl != null) {
190                         wsdlFile = j2eeModule.getStringForDDxml(wsdl);
191                     }
192                 } catch (Exception JavaDoc e ) {
193                   //_logger.log(Level.FINE,"Error reading dd file contents", e);
194
}
195
196                 // get all ports inside this web service.
197
PortComponentType pts[] = wsdt.getPortComponent();
198                 for (int portCnt =0; portCnt < pts.length; portCnt++) {
199                     PortComponentType pt = pts[portCnt];
200                     ServiceImplBeanType beanType = pt.getServiceImplBean();
201                     
202                     WebServiceDescrInfo wsDescrInfo = null;
203                     implName = beanType.getServletLink();
204                     if ( implName != null) {
205                         implType = "SERVLET";
206
207                         // cache uri and sevlet impl class for all endpoints
208
if (wsWebMap == null) {
209                             wsWebMap = getWebServiceInfoForWebModule(moduleInfo,
210                                 appId, bundleName, webservices);
211                         }
212
213                         // uri, servlet impl class from cache
214
if (wsWebMap != null) {
215                             wsDescrInfo = (WebServiceDescrInfo) wsWebMap.get(
216                                 (String JavaDoc)pt.getPortComponentName());
217                         }
218                     } else {
219                         implName = beanType.getEjbLink();
220                         if ( implName != null) {
221                             implType = "EJB";
222
223                             // cache uri
224
if (wsEjbMap == null) {
225                                 wsEjbMap = getWebServiceInfoForEjbModule(
226                                     moduleInfo, appId, bundleName, webservices);
227                             }
228
229                             // uri
230
if ( wsEjbMap != null) {
231                                 wsDescrInfo = (WebServiceDescrInfo)wsEjbMap.get(
232                                     (String JavaDoc)pt.getPortComponentName());
233                             }
234                         } else {
235                             // throw warning, unknown type set
236
}
237                     }
238                     if (wsDescrInfo != null) {
239                         uri = wsDescrInfo.getUri();
240                         implClass = wsDescrInfo.getImplClass();
241                     }
242
243                     final String JavaDoc wsFile = (String JavaDoc) propMap.get(
244                         WebServiceInfoProvider.WS_XML_PROP_NAME);
245
246                     // populate web service endpoint info object
247
WebServiceEndpointInfoImpl wsInfo = new
248                         WebServiceEndpointInfoImpl(pt.getPortComponentName(),
249                             uri, appId, bundleName, isAppStandAloneModule,
250                             wsdlFile, mapping, wsFile,implType,implName,
251                             implClass, wsDescrInfo.isSecure());
252
253                     wsInfo.putField(
254                     WebServiceEndpointInfo.SUN_WEB_XML_KEY, (Serializable JavaDoc)propMap.get(
255                         WebServiceInfoProvider.SUN_WEB_XML_PROP_NAME));
256
257                     wsInfo.putField(
258                     WebServiceEndpointInfo.WEB_XML_KEY, (Serializable JavaDoc)propMap.get(
259                         WebServiceInfoProvider.WEB_XML_PROP_NAME));
260
261                     wsInfo.putField(
262                     WebServiceEndpointInfo.SUN_EJB_XML_KEY, (Serializable JavaDoc)propMap.get(
263                         WebServiceInfoProvider.SUN_EJB_JAR_XML_PROP_NAME));
264
265                     wsInfo.putField(
266                     WebServiceEndpointInfo.EJB_XML_KEY, (Serializable JavaDoc)propMap.get(
267                         WebServiceInfoProvider.EJB_JAR_XML_PROP_NAME));
268
269                     wsInfo.putField(
270                     WebServiceEndpointInfo.APPLICATION_XML_KEY, (Serializable JavaDoc)propMap.get(
271                         WebServiceInfoProvider.APPLICATION_XML_PROP_NAME));
272
273                     wsInfo.putField(
274                     WebServiceEndpointInfo.MAPPING_FILE_LOCATION_KEY,
275                         (Serializable JavaDoc)mappingFileName);
276
277                     wsInfo.putField(
278                     WebServiceEndpointInfo.WSDL_FILE_LOCATION_KEY,
279                         (Serializable JavaDoc)wsdl);
280
281                     // add to the list
282
aList.add(wsInfo);
283                 }
284         }
285         return aList;
286     }
287
288     /**
289      * Returns the List of WebServiceEndpointInfos for the provided WEB module.
290      *
291      * @param moduleInfo path to sun-web.xml location
292      * @param appId name of the application or module
293      * @param bundleName name of the bundle
294      * @param webservices path to the webservices.xml location
295      *
296      * @return the Map Web Service Names and WebServiceDescrInfos
297      */

298     private Map JavaDoc getWebServiceInfoForWebModule(String JavaDoc moduleInfo,
299                     String JavaDoc appId, String JavaDoc bundleName, String JavaDoc webservices)
300                     throws RepositoryException {
301
302         // load sun-web.xml beans
303
FileInputStream JavaDoc in = null;
304         try {
305             in = new FileInputStream JavaDoc(moduleInfo);
306         } catch(FileNotFoundException JavaDoc fne) {
307             throw new RepositoryException (fne);
308         }
309         SunWebApp sunWebApp = null;
310         try {
311             sunWebApp = SunWebApp.createGraph(in);
312         } catch (Schema2BeansException sce) {
313             throw new RepositoryException (sce);
314         }
315
316         // bundle name is null for stand alone module
317
boolean isAppStandAloneModule = (bundleName==null) ? true : false;
318
319         // all available servlets
320
Servlet[] sLets = sunWebApp.getServlet();
321
322         Map JavaDoc wsMap = new HashMap JavaDoc();
323         for (int sCnt =0; sCnt < sLets.length; sCnt++) {
324             Servlet sLet = sLets[sCnt];
325
326             // all end points for this servlet
327
WebserviceEndpoint[] webSvcEps = sLet.getWebserviceEndpoint();
328
329             for ( int wsCnt = 0; wsCnt < webSvcEps.length; wsCnt++) {
330                 WebserviceEndpoint webSvc = webSvcEps[wsCnt];
331
332                 // context root for web service endpoint
333
String JavaDoc ctxRoot = sunWebApp.getContextRoot();
334                 String JavaDoc uriInConfig = getUriInDomainConfig(appId);
335                 if (uriInConfig != null) {
336                     ctxRoot = uriInConfig;
337                 }
338                 String JavaDoc uri;
339                 String JavaDoc wsUri = webSvc.getEndpointAddressUri();
340                 if ((wsUri != null) && (wsUri.length() > 0)
341                                     && (wsUri.charAt(0) != '/')) {
342                     wsUri = "/" + wsUri;
343                 }
344
345                 // FIXME: Do we need to read domain.xml stand alone module?
346
if (ctxRoot != null) {
347                     uri = ctxRoot + wsUri;
348                 } else {
349                     uri = wsUri;
350                 }
351
352                 boolean isSec = false;
353                 String JavaDoc trans = webSvc.getTransportGuarantee();
354                 if (( trans != null) && ("NONE".equals(trans) == false)) {
355                     isSec = true;
356                 } else if ((webSvc.getLoginConfig() != null) ||
357                     (webSvc.getMessageSecurityBinding() != null)) {
358                     isSec = true;
359                 }
360                 // web service uri, endpoint name and servlet impl class
361
WebServiceDescrInfo wsdInfo =
362                     new WebServiceDescrInfo(webSvc.getPortComponentName(),
363                                        uri, webSvc.getServletImplClass(),isSec);
364
365                 wsMap.put(wsdInfo.getName(),wsdInfo);
366             }
367        }
368
369         return wsMap;
370     }
371
372     /**
373      * Returns the webservice-description-type for the given
374      * port-component-name. WSDL and jaxrpc-mapping file
375      * location are available in this object.
376      *
377      * @param webservcesXML path to webservices.xml file
378      * @param pcName port component name
379      */

380     private WebserviceDescriptionType getWSDT(String JavaDoc webservicesXML,
381             String JavaDoc pcName) {
382
383         FileInputStream JavaDoc fis = null;
384         try {
385             fis = new FileInputStream JavaDoc(new File JavaDoc(webservicesXML));
386             Webservices ws = Webservices.createGraph(fis);
387             WebserviceDescriptionType[] wsdt = ws.getWebserviceDescription();
388             for (int i=0; i<wsdt.length; i++) {
389                 PortComponentType[] pct = wsdt[i].getPortComponent();
390                 for (int j=0; j<pct.length; j++) {
391                     String JavaDoc name = pct[j].getPortComponentName();
392                     if (pcName.equals(name)) {
393                         return wsdt[i];
394                     }
395                 }
396             }
397         } catch (Exception JavaDoc e) {
398         } finally {
399             if (fis != null) {
400                 try {
401                     fis.close();
402                 } catch (Exception JavaDoc e) { }
403             }
404         }
405         return null;
406     }
407
408
409     /**
410      * Returns the List of WebServiceEndpointInfos for the provided EJB module.
411      *
412      * @param moduleInfo path to sun-ejb-jar.xml location
413      * @param appId name of the application or module
414      * @param bundleName name of the bundle
415      * @param webservices path to the webservices.xml location
416      *
417      * @return the Map of Web Service Names and WebServiceDescrInfos
418      */

419     private Map JavaDoc getWebServiceInfoForEjbModule(String JavaDoc moduleInfo,String JavaDoc appId,
420             String JavaDoc bundleName, String JavaDoc webservices)
421             throws RepositoryException {
422
423         // load sun-ejb-jar.xml beans
424
FileInputStream JavaDoc in = null;
425         try {
426             in = new FileInputStream JavaDoc(moduleInfo);
427         } catch(FileNotFoundException JavaDoc fne) {
428             throw new RepositoryException (fne);
429         }
430         SunEjbJar sunEjbJar = null;
431         try {
432             sunEjbJar = SunEjbJar.createGraph(in);
433         } catch (Schema2BeansException sce) {
434             throw new RepositoryException (sce);
435         }
436
437         // bundle name is null for stand alone module
438
boolean isAppStandAloneModule = (bundleName==null) ? true : false;
439
440         // all ejbs in this module
441
EnterpriseBeans eBeans = sunEjbJar.getEnterpriseBeans();
442         Ejb[] ejbs = eBeans.getEjb();
443         HashMap JavaDoc wsMap = new HashMap JavaDoc();
444
445         for (int ejbCnt =0; ejbCnt < ejbs.length; ejbCnt++) {
446             Ejb ejb = ejbs[ejbCnt];
447
448             // all web service endpoints for this ejb
449
WebserviceEndpoint[] webSvcEps = ejb.getWebserviceEndpoint();
450
451             for ( int wsCnt = 0; wsCnt < webSvcEps.length; wsCnt++) {
452                 WebserviceEndpoint webSvc = webSvcEps[wsCnt];
453
454                 boolean isSec = false;
455                 String JavaDoc trans = webSvc.getTransportGuarantee();
456                 if (( trans != null) && ("NONE".equals(trans) == false)) {
457                     isSec = true;
458                 } else if ((webSvc.getLoginConfig() != null) ||
459                     (webSvc.getMessageSecurityBinding() != null)) {
460                     isSec = true;
461                 }
462                 // uri
463
WebServiceDescrInfo wsdInfo = new
464                     WebServiceDescrInfo(webSvc.getPortComponentName(),
465                         webSvc.getEndpointAddressUri(),
466                         webSvc.getTieClass(), isSec);
467
468                 wsMap.put(wsdInfo.getName(),wsdInfo);
469             }
470         }
471
472         return wsMap;
473     }
474
475     private String JavaDoc getUriInDomainConfig(String JavaDoc appId) {
476             ConfigContext configCtx = AdminService.getAdminService().
477                 getAdminContext().getAdminConfigContext();
478         ConfigBean cb = null;
479         
480         try {
481             cb = ApplicationHelper.findApplication(configCtx, appId);
482         } catch( Exception JavaDoc e) {
483             //String msg = "Could not find a deployed application/module by name "
484
// + appId;
485
//_logger.log(Level.FINE, msg);
486
return null;
487         }
488         
489         if (cb instanceof WebModule) {
490             return ((WebModule)cb).getContextRoot();
491         } else {
492             return null;
493         }
494
495     }
496
497     /**
498      * Data structure to hold URI and impl class for end point.
499      */

500     class WebServiceDescrInfo {
501         
502         WebServiceDescrInfo (String JavaDoc cName, String JavaDoc URI, String JavaDoc iClass, boolean
503         isSec) {
504
505                 compName = cName;
506                 uri = URI;
507                 implClass = iClass;
508                 isSecure = isSec;
509         }
510
511         String JavaDoc getName () {
512             return compName;
513         }
514
515         String JavaDoc getUri() {
516             return uri;
517         }
518         
519         String JavaDoc getImplClass() {
520             return implClass;
521         }
522
523         boolean isSecure() {
524             return isSecure;
525         }
526
527         private String JavaDoc compName;
528         private String JavaDoc uri;
529         private String JavaDoc implClass;
530         private boolean isSecure;
531     }
532
533     /** provider id for the default web server info provider */
534     public static final String JavaDoc PROVIDER_ID =
535         "com.sun.enterprise.tools.common.AppServWebServiceInfoProvider";
536 }
537
Popular Tags