KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > wsgen > ddmodifier > WsEndpointDDModifier


1 /**
2  * JOnAS : Java(TM) OpenSource Application Server
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Initial Developer : Guillaume Sauthier
20  * --------------------------------------------------------------------------
21  * $Id: WsEndpointDDModifier.java,v 1.11 2005/07/18 23:53:30 mwringe Exp $
22  * --------------------------------------------------------------------------
23  */

24
25 package org.objectweb.jonas_ws.wsgen.ddmodifier;
26
27 import org.objectweb.util.monolog.api.BasicLevel;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32 import org.w3c.dom.Text JavaDoc;
33
34 /**
35  * Modify a Deployment Desc for Endpoint. Wrapper around a web.xml DOM.
36  * @author Guillaume Sauthier
37  */

38 public class WsEndpointDDModifier extends DeploymentDescModifier {
39
40     /**
41      * servlet Element name
42      */

43     private static final String JavaDoc SERVLET = "servlet";
44
45     /**
46      * servlet-name Element name
47      */

48     private static final String JavaDoc SERVLET_NAME = "servlet-name";
49
50     /**
51      * servlet-class Element name
52      */

53     private static final String JavaDoc SERVLET_CLASS = "servlet-class";
54
55     /**
56      * servlet-mapping Element name
57      */

58     private static final String JavaDoc SERVLET_MAPPING = "servlet-mapping";
59
60     /**
61      * url-pattern Element name
62      */

63     private static final String JavaDoc URL_MAPPING = "url-pattern";
64
65     /**
66      * init-param Element name
67      */

68     private static final String JavaDoc INIT_PARAM = "init-param";
69
70     /**
71      * param-name Element name
72      */

73     private static final String JavaDoc PARAM_NAME = "param-name";
74
75     /**
76      * param-value Element name
77      */

78     private static final String JavaDoc PARAM_VALUE = "param-value";
79
80     /**
81      * security-role-ref Element name
82      */

83     private static final String JavaDoc SECURITY_ROLE_REF = "security-role-ref";
84
85     /**
86      * url-pattern Element name
87      */

88     private static final String JavaDoc URL_PATTERN = "url-pattern";
89
90     /**
91      * security-constraint Element name
92      */

93     private static final String JavaDoc SECURITY_CONSTRAINT = "security-constraint";
94
95     /**
96      * login-config Element name
97      */

98     private static final String JavaDoc LOGIN_CONFIG = "login-config";
99
100     /**
101      * security-role Element name
102      */

103     private static final String JavaDoc SECURITY_ROLE = "security-role";
104
105     /**
106      * Creates a new WsEndpointDDModifier object.
107      * @param web web.xml document
108      */

109     public WsEndpointDDModifier(Document JavaDoc web) {
110         super(web.getDocumentElement(), web);
111     }
112
113     /**
114      * Add a new <code>servlet</code> element in the web.xml.
115      * @param name servlet name.
116      * @param classname servlet fully qualified classname.
117      */

118     public void addServlet(String JavaDoc name, String JavaDoc classname) {
119         Element JavaDoc servlet = newJ2EEElement(SERVLET);
120         Element JavaDoc servletName = newJ2EEElement(SERVLET_NAME, name);
121         Element JavaDoc servletClass = newJ2EEElement(SERVLET_CLASS, classname);
122
123         servlet.appendChild(servletName);
124         servlet.appendChild(servletClass);
125
126         // add servlet in the webapp Element
127
getElement().appendChild(servlet);
128
129     }
130
131     /**
132      * Add a new security-constraint element into the web.xml
133      *
134      * @param securityConstraint A node containing the security-constraint setting
135      */

136     public void addEndpointSecurityConstraint(Node JavaDoc securityConstraint) {
137         Element JavaDoc sConstraintElement = newJ2EEElement (SECURITY_CONSTRAINT);
138
139         // securityConstraint comes from another document, therefore have to import first
140
Node JavaDoc newSecurityConstraint = getDocument().importNode(securityConstraint, true);
141
142         while (newSecurityConstraint.hasChildNodes()) {
143             sConstraintElement.appendChild(newSecurityConstraint.getFirstChild());
144         }
145
146         //add security-constraint to webapp
147
getElement().appendChild(sConstraintElement);
148     }
149
150     /**
151      * Add a new login-config element into the web.xml
152      *
153      * @param loginConfig A node constaining the login-config setting
154      */

155     public void addEndpointLoginConfig (Node JavaDoc loginConfig) {
156         Element JavaDoc lConfigElement = newJ2EEElement (LOGIN_CONFIG);
157
158         // loginConfig comes from another document, therefore have to import first
159
Node JavaDoc newLoginConfig = getDocument().importNode(loginConfig, true);
160
161         while (newLoginConfig.hasChildNodes()) {
162             lConfigElement.appendChild(newLoginConfig.getFirstChild());
163         }
164
165         //add login-config to webapp
166
getElement().appendChild(lConfigElement);
167     }
168
169     /**
170      * Add a new security-role element into the web.xml
171      * @param securityRole A node containing the login-config setting
172      */

173     public void addSecurityRole (Node JavaDoc securityRole) {
174         Element JavaDoc sRole = newJ2EEElement (SECURITY_ROLE);
175
176         // securityRole comes from another document, therefor have to import first
177
Node JavaDoc newSecurityRole = getDocument().importNode(securityRole, true);
178
179         while (newSecurityRole.hasChildNodes()) {
180             sRole.appendChild(newSecurityRole.getFirstChild());
181         }
182
183         //add security-role to webapp
184
getElement().appendChild(sRole);
185     }
186
187     /**
188      * Remove a <code>servlet</code> element from the web.xml.
189      * @param name servlet name.
190      */

191     public void removeServlet(String JavaDoc name) {
192         Element JavaDoc servlet = findServlet(name);
193
194         if (servlet != null) {
195             getElement().removeChild(servlet);
196         }
197     }
198
199     /**
200      * Remove a <code>servlet</code> element from the web.xml.
201      * @param name servlet name.
202      * @return security-role elements for this servlet if such elements exist
203      */

204     public NodeList JavaDoc removeServletWithSecurity(String JavaDoc name) {
205         Element JavaDoc servlet = findServlet(name);
206         NodeList JavaDoc elements = null;
207
208         if (servlet != null) {
209
210             elements = servlet.getElementsByTagNameNS(J2EE_NS, SECURITY_ROLE_REF);
211             // if the list is empty, return null
212
if (elements.getLength() == 0) {
213                 elements = null;
214             }
215             getElement().removeChild(servlet);
216         }
217         return elements;
218     }
219
220     /**
221      * Add a new <code>servlet-mapping</code> element in the web.xml.
222      * @param name servlet name.
223      * @param mapping <code>url-mapping</code> value
224      */

225     public void addServletMapping(String JavaDoc name, String JavaDoc mapping) {
226         Element JavaDoc servletMapping = newJ2EEElement(SERVLET_MAPPING);
227         Element JavaDoc servletName = newJ2EEElement(SERVLET_NAME, name);
228         Element JavaDoc urlMapping = newJ2EEElement(URL_MAPPING, mapping);
229
230         servletMapping.appendChild(servletName);
231         servletMapping.appendChild(urlMapping);
232
233         // add servletMapping in the webapp Element
234
getElement().appendChild(servletMapping);
235     }
236
237     /**
238      * Add a new <code>init-param</code> element in the web.xml.
239      * @param servletName the servlet name where init-param will be added.
240      * @param pName parameter name
241      * @param pValue parameter value
242      */

243     public void addServletParam(String JavaDoc servletName, String JavaDoc pName, String JavaDoc pValue) {
244         Element JavaDoc ip = newJ2EEElement(INIT_PARAM);
245         Element JavaDoc pn = newJ2EEElement(PARAM_NAME, pName);
246         Element JavaDoc pv = newJ2EEElement(PARAM_VALUE, pValue);
247
248         ip.appendChild(pn);
249         ip.appendChild(pv);
250
251         Element JavaDoc servlet = findServlet(servletName);
252         servlet.appendChild(ip);
253
254     }
255
256     /**
257      * Add a new <code>security-role-ref</code> element in the web.xml.
258      * @param servletName the servlet name where security-role-ref will be
259      * added.
260      * @param securityRoleRefs security-role elements for a servlet
261      */

262     public void addServletSecurityRoleRefs(String JavaDoc servletName, NodeList JavaDoc securityRoleRefs) {
263         // find the servlet
264
Element JavaDoc servlet = findServlet(servletName);
265         // add all security-role-ref
266
for (int i = 0; i < securityRoleRefs.getLength(); i++) {
267             Node JavaDoc securityRoleRefItem = securityRoleRefs.item(i);
268             servlet.appendChild(securityRoleRefItem);
269         }
270     }
271
272     /**
273      * search webapp element for servlet named with the given name.
274      * @param name the searched servlet name
275      * @return the found element or null if element is not found (should'nt
276      * occurs).
277      */

278     private Element JavaDoc findServlet(String JavaDoc name) {
279         NodeList JavaDoc nl = getElement().getElementsByTagNameNS(J2EE_NS, SERVLET);
280         Element JavaDoc servlet = null;
281
282         for (int i = 0; (i < nl.getLength()) && (servlet == null); i++) {
283             Element JavaDoc e = (Element JavaDoc) nl.item(i);
284
285             NodeList JavaDoc names = e.getElementsByTagNameNS(J2EE_NS, SERVLET_NAME);
286
287             // test servlet/servlet-name/#text-node.value
288
if (names.item(0).getFirstChild().getNodeValue().equals(name)) {
289                 servlet = e;
290             }
291         }
292
293         return servlet;
294     }
295
296     /**
297      * Remove servlet-mapping tag associated to a given servlet-name
298      * @param sName servlet-name
299      * @return url-pattern element's value in the servelet-mapping
300      */

301     public String JavaDoc removeServletMapping(String JavaDoc sName) {
302         NodeList JavaDoc nl = getElement().getElementsByTagNameNS(J2EE_NS, SERVLET_MAPPING);
303         Element JavaDoc mapping = null;
304         String JavaDoc urlPatternValue = null;
305
306         for (int i = 0; (i < nl.getLength()) && (mapping == null); i++) {
307             Element JavaDoc e = (Element JavaDoc) nl.item(i);
308
309             NodeList JavaDoc names = e.getElementsByTagNameNS(J2EE_NS, SERVLET_NAME);
310
311             // test servlet-mapping/servlet-name/#text-node.value
312
if (names.item(0).getFirstChild().getNodeValue().equals(sName)) {
313                 mapping = e;
314             }
315         }
316
317         if (mapping != null) {
318             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
319                 getLogger().log(BasicLevel.DEBUG, "mapping element found : " + mapping);
320             }
321             // pick up the url-pattern before removing
322
NodeList JavaDoc urlPatterns = mapping.getElementsByTagNameNS(J2EE_NS, URL_PATTERN);
323             urlPatternValue = urlPatterns.item(0).getFirstChild().getNodeValue();
324
325             getElement().removeChild(mapping);
326         }
327
328         return urlPatternValue;
329     }
330
331     /**
332      * Update the security-constraint element having url-pattern equal with
333      * oldPattern by replacing this old pattern with the newUrlPattern
334      * @param oldUrlPatter url-pattern to be replaced
335      * @param newUrlPatterValue url-pattern to replace with
336      */

337     public void updateSecurityConstraint(String JavaDoc oldUrlPatter, String JavaDoc newUrlPatterValue) {
338
339         NodeList JavaDoc nl = getElement().getElementsByTagNameNS(J2EE_NS, SECURITY_CONSTRAINT);
340
341         // loop over security constraints
342
for (int i = 0; i < nl.getLength(); i++) {
343             Element JavaDoc e = (Element JavaDoc) nl.item(i);
344
345             // look at nested url-pattern Element(s)
346
NodeList JavaDoc urlPatternCollection = e.getElementsByTagNameNS(J2EE_NS, URL_PATTERN);
347             for (int j = 0; j < urlPatternCollection.getLength(); j++) {
348
349                 Element JavaDoc urlPatternElement = (Element JavaDoc) urlPatternCollection.item(j);
350                 Text JavaDoc urlPatternText = (Text JavaDoc) urlPatternElement.getFirstChild();
351
352                 // if found urlPattern Element, replace its value
353
if (urlPatternText.getNodeValue().equals(oldUrlPatter)) {
354                     urlPatternText.setNodeValue(newUrlPatterValue);
355                 }
356             }
357         }
358     }
359
360     /**
361      * DOCUMENT ME!
362      * @param name DOCUMENT ME!
363      * @param home DOCUMENT ME!
364      * @param remote DOCUMENT ME!
365      * @param link DOCUMENT ME!
366      */

367     public void addEjbRef(String JavaDoc name, String JavaDoc home, String JavaDoc remote, String JavaDoc link) {
368
369         Element JavaDoc ejbRef = newJ2EEElement("ejb-ref");
370         Element JavaDoc ejbRefName = newJ2EEElement("ejb-ref-name", name);
371         Element JavaDoc ejbRefType = newJ2EEElement("ejb-ref-type", "Session");
372         Element JavaDoc ejbHome = newJ2EEElement("home", home);
373         Element JavaDoc ejbRemote = newJ2EEElement("remote", remote);
374         Element JavaDoc ejbLink = newJ2EEElement("ejb-link", link);
375
376         ejbRef.appendChild(ejbRefName);
377         ejbRef.appendChild(ejbRefType);
378         ejbRef.appendChild(ejbHome);
379         ejbRef.appendChild(ejbRemote);
380         ejbRef.appendChild(ejbLink);
381
382         getElement().appendChild(ejbRef);
383
384     }
385
386     /**
387      * DOCUMENT ME!
388      * @param name DOCUMENT ME!
389      * @param home DOCUMENT ME!
390      * @param remote DOCUMENT ME!
391      * @param link DOCUMENT ME!
392      */

393     public void addEjbLocalRef(String JavaDoc name, String JavaDoc home, String JavaDoc remote, String JavaDoc link) {
394
395         Element JavaDoc ejbRef = newJ2EEElement("ejb-local-ref");
396         Element JavaDoc ejbRefName = newJ2EEElement("ejb-ref-name", name);
397         Element JavaDoc ejbRefType = newJ2EEElement("ejb-ref-type", "Session");
398         Element JavaDoc ejbHome = newJ2EEElement("local-home", home);
399         Element JavaDoc ejbRemote = newJ2EEElement("local", remote);
400         Element JavaDoc ejbLink = newJ2EEElement("ejb-link", link);
401
402         ejbRef.appendChild(ejbRefName);
403         ejbRef.appendChild(ejbRefType);
404         ejbRef.appendChild(ejbHome);
405         ejbRef.appendChild(ejbRemote);
406         ejbRef.appendChild(ejbLink);
407
408         getElement().appendChild(ejbRef);
409
410     }
411 }
412
Popular Tags