KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > util > core > DescriptorParser


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

16 package com.blandware.atleap.webapp.util.core;
17
18 import org.apache.commons.digester.Digester;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.xml.sax.SAXException JavaDoc;
22
23 import javax.servlet.ServletContext JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * <p>Parser for deployment descriptor of our web application.</p>
34  * <p><a HREF="DescriptorParser.java.htm"><i>View Source</i></a></p>
35  *
36  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
37  * @version $Revision: 1.3 $ $Date: 2005/05/27 16:03:47 $
38  */

39 public class DescriptorParser {
40
41     protected transient final Log log = LogFactory.getLog(getClass());
42
43     /**
44      * Public ID of webapp 2.3 DTD
45      */

46     private static final String JavaDoc WEB_APP_2_3_DTD_PUBLIC_ID = "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN";
47
48     /**
49      * Location of webapp 2.3 DTD
50      */

51     private static final String JavaDoc WEB_APP_2_3_DTD_LOCATION = "/org/apache/struts/resources/web-app_2_3.dtd";
52
53     /**
54      * Default path to deployment descriptor
55      */

56     protected String JavaDoc configPath = "/WEB-INF/web.xml";
57
58     /**
59      * Servlet context of our web application
60      */

61     protected ServletContext JavaDoc servletContext;
62
63     /**
64      * Set of security roles
65      */

66     protected Map JavaDoc securityRoles = null;
67
68     /**
69      * Creates new instance of DescriptorParser
70      *
71      * @param servletContext Servlet context of our web application
72      */

73     public DescriptorParser(ServletContext JavaDoc servletContext) {
74         this.servletContext = servletContext;
75     }
76
77     /**
78      * Returns set of security roles presented in web application deployment descriptor
79      *
80      * @return Set of security roles
81      */

82     public Map JavaDoc getSecurityRoles() {
83
84         Digester digester = new Digester();
85         digester.push(this);
86         digester.setNamespaceAware(true);
87         digester.setValidating(false);
88         digester.setUseContextClassLoader(true);
89
90         // register DTD
91
URL JavaDoc dtdUrl = getClass().getResource(WEB_APP_2_3_DTD_LOCATION);
92         digester.register(WEB_APP_2_3_DTD_PUBLIC_ID, dtdUrl.toString());
93
94         digester.addCallMethod("web-app/security-role", "addSecurityRole", 2, new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class});
95         digester.addCallParam("web-app/security-role/role-name", 0);
96         digester.addCallParam("web-app/security-role/description", 1);
97
98         InputStream JavaDoc input = servletContext.getResourceAsStream(configPath);
99
100         if ( input == null ) {
101             if ( log.isErrorEnabled() ) {
102                 log.error("Failed to retrieve deployment descriptor by path: " + configPath);
103             }
104             return null;
105         }
106
107         try {
108             digester.parse(input);
109         } catch ( IOException JavaDoc e ) {
110             if ( log.isErrorEnabled() ) {
111                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
112                 e.printStackTrace(new PrintWriter JavaDoc(sw));
113                 log.error(sw.toString());
114             }
115
116         } catch ( SAXException JavaDoc e ) {
117             if ( log.isErrorEnabled() ) {
118                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
119                 e.printStackTrace(new PrintWriter JavaDoc(sw));
120                 log.error(sw.toString());
121             }
122         } finally {
123             try {
124                 input.close();
125             } catch ( IOException JavaDoc e ) {
126                 if ( log.isErrorEnabled() ) {
127                     StringWriter JavaDoc sw = new StringWriter JavaDoc();
128                     e.printStackTrace(new PrintWriter JavaDoc(sw));
129                     log.error(sw.toString());
130                 }
131             }
132         }
133
134         return securityRoles;
135     }
136
137     /**
138      * Adds security role to the set
139      *
140      * @param roleName Name of role to add
141      * @param description Role description
142      */

143     public void addSecurityRole(String JavaDoc roleName, String JavaDoc description) {
144         if ( securityRoles == null ) {
145             securityRoles = new HashMap JavaDoc();
146         }
147         securityRoles.put(roleName, description);
148     }
149
150     /**
151      * Sets config path
152      *
153      * @param configPath Config path to use
154      */

155     public void setConfigPath(String JavaDoc configPath) {
156         this.configPath = configPath;
157     }
158
159 }
160
Popular Tags