KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > authentication > generation > ConfigurationGenerator


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
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 org.apache.cocoon.webapps.authentication.generation;
17
18 import org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20 import org.apache.avalon.framework.service.ServiceException;
21 import org.apache.avalon.framework.service.ServiceManager;
22
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.components.source.SourceUtil;
25 import org.apache.cocoon.environment.ObjectModelHelper;
26 import org.apache.cocoon.environment.Request;
27 import org.apache.cocoon.environment.Response;
28 import org.apache.cocoon.environment.Session;
29 import org.apache.cocoon.generation.ServiceableGenerator;
30 import org.apache.cocoon.webapps.authentication.AuthenticationManager;
31 import org.apache.cocoon.webapps.authentication.context.AuthenticationContext;
32 import org.apache.cocoon.webapps.authentication.user.RequestState;
33 import org.apache.cocoon.webapps.authentication.user.UserHandler;
34 import org.apache.cocoon.xml.IncludeXMLConsumer;
35 import org.apache.cocoon.xml.XMLConsumer;
36 import org.apache.cocoon.xml.XMLUtils;
37 import org.apache.cocoon.xml.dom.DOMUtil;
38
39 import org.apache.excalibur.source.Source;
40 import org.apache.excalibur.source.SourceException;
41 import org.apache.excalibur.source.SourceParameters;
42 import org.apache.excalibur.xml.xpath.XPathProcessor;
43 import org.w3c.dom.Document JavaDoc;
44 import org.w3c.dom.Element JavaDoc;
45 import org.w3c.dom.Node JavaDoc;
46 import org.w3c.dom.NodeList JavaDoc;
47 import org.xml.sax.SAXException JavaDoc;
48 import org.xml.sax.helpers.DefaultHandler JavaDoc;
49
50 import java.io.IOException JavaDoc;
51 import java.util.Enumeration JavaDoc;
52 import java.util.Map JavaDoc;
53
54 /**
55  * This is the authentication Configuration Generator.
56  *
57  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
58  * @version $Id: ConfigurationGenerator.java 164808 2005-04-26 16:07:03Z vgritsenko $
59 */

60 public final class ConfigurationGenerator extends ServiceableGenerator {
61
62     /** Request parameter */
63     public static final String JavaDoc REQ_PARAMETER_STATE = "authstate";
64     public static final String JavaDoc REQ_PARAMETER_ROLE = "authrole";
65     public static final String JavaDoc REQ_PARAMETER_ID = "authid";
66     public static final String JavaDoc REQ_PARAMETER_USER = "authuser";
67
68     private static final String JavaDoc SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE = "org.apache.cocoon.webapps.generation.ConfigurationGenerator.simple-role";
69
70     /** The XPath Processor */
71     protected XPathProcessor xpathProcessor;
72
73     /* (non-Javadoc)
74      * @see org.apache.avalon.framework.activity.Disposable#dispose()
75      */

76     public void dispose() {
77         if ( this.manager != null ) {
78             this.manager.release( this.xpathProcessor );
79             this.xpathProcessor = null;
80         }
81         super.dispose();
82     }
83
84     /* (non-Javadoc)
85      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
86      */

87     public void service(ServiceManager manager) throws ServiceException {
88         super.service(manager);
89         this.xpathProcessor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE);
90     }
91
92     /**
93      * Generate the configuration
94      */

95     public void generate()
96     throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
97
98         AuthenticationManager authManager = null;
99         RequestState state = null;
100         try {
101             authManager = (AuthenticationManager)this.manager.lookup(AuthenticationManager.ROLE);
102             state = authManager.getState();
103
104         } catch (Exception JavaDoc ignore) {
105         }
106
107         this.xmlConsumer.startDocument();
108         if ( state != null ) {
109             try {
110                 UserHandler userhandler = state.getHandler();
111
112                 Configuration conf = state.getModuleConfiguration("single-role-user-management");
113                 if (conf == null) {
114                     throw new ProcessingException("Module configuration 'single-role-user-management' for authentication user management generator not found.");
115                 }
116                 UserManagementHandler handler = new UserManagementHandler(conf,
117                                                                           state.getApplicationName());
118                 this.showConfiguration(this.xmlConsumer, this.source, handler, userhandler.getContext());
119
120             } catch (ConfigurationException ex) {
121                 throw new ProcessingException("ConfigurationException: " + ex, ex);
122             }
123         }
124
125         this.xmlConsumer.endDocument();
126     }
127
128
129     /**
130      * Show the configuration for the admin.
131      * If <code>src</code> is "admin" or null the admin configuration is shown.
132      * If <code>src</code> is "user" the configuration of the current user
133      * is shown.
134      */

135     public void showConfiguration(XMLConsumer consumer,
136                                    String JavaDoc src,
137                                    UserManagementHandler handler,
138                                    AuthenticationContext context)
139     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
140         // synchronized
141
if (this.getLogger().isDebugEnabled() == true) {
142             this.getLogger().debug("BEGIN showConfiguration consumer=" + consumer + ", SRC="+src);
143         }
144
145         // get some important information
146
Request request = ObjectModelHelper.getRequest(this.objectModel);
147         Response response = ObjectModelHelper.getResponse(this.objectModel);
148         Session session = request.getSession();
149
150         boolean isAdmin = (src == null || src.equals("admin"));
151
152         // now start producing xml:
153
consumer.startElement("", "configuration", "configuration", XMLUtils.EMPTY_ATTRIBUTES);
154
155         // set the conf uri:
156
// This is a bug in the servlet 2.2 API!!!
157
// It does not contain the context: String uri = HttpUtils.getRequestURL(this.request).toString();
158
// So: ABSOLUTELY USELESS
159
String JavaDoc uri = response.encodeURL(request.getRequestURI());
160         consumer.startElement("", "uri", "uri", XMLUtils.EMPTY_ATTRIBUTES);
161         consumer.characters(uri.toCharArray(), 0, uri.length());
162         consumer.endElement("", "uri", "uri");
163
164         if (isAdmin == true) {
165             // build the menue
166
consumer.startElement("", "menue", "menue", XMLUtils.EMPTY_ATTRIBUTES);
167
168             if (handler.getNewRoleResource() != null) {
169                 consumer.startElement("", "addrole", "addrole", XMLUtils.EMPTY_ATTRIBUTES);
170                 consumer.endElement("", "addrole", "addrole");
171             }
172             if (handler.getDeleteRoleResource() != null) {
173                 consumer.startElement("", "delrole", "delrole", XMLUtils.EMPTY_ATTRIBUTES);
174                 consumer.endElement("", "delrole", "delrole");
175             }
176
177             consumer.endElement("", "menue", "menue");
178         }
179
180
181         synchronized (session) {
182
183             String JavaDoc state = request.getParameter(REQ_PARAMETER_STATE);
184             if (state == null) {
185                 state = (isAdmin == true ? "main" : "seluser");
186             }
187
188             if (state.equals("addrole") == true) {
189                 String JavaDoc role = request.getParameter(REQ_PARAMETER_ROLE);
190                 if (role != null && role.trim().length() > 0) {
191                     SourceParameters pars = new SourceParameters();
192                     // first include all request parameters
193
Enumeration JavaDoc requestParameters = request.getParameterNames();
194                     String JavaDoc current;
195                     while (requestParameters.hasMoreElements() == true) {
196                         current = (String JavaDoc)requestParameters.nextElement();
197                         pars.setParameter(current, request.getParameter(current));
198                     }
199                     this.addRole(role, pars, handler);
200                 } else {
201                     role = null;
202                 }
203                 session.removeAttribute(SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE);
204             }
205
206             if (state.equals("delrole") == true) {
207                 try {
208                     String JavaDoc role = request.getParameter(REQ_PARAMETER_ROLE);
209                     if (role != null) {
210                         // first delete user
211
Document JavaDoc userDF = this.getUsers(role, null, handler);
212                         NodeList JavaDoc users = null;
213                         if (userDF != null) users = DOMUtil.selectNodeList(userDF, "users/user", this.xpathProcessor);
214                         if (users != null) {
215                             for(int i = 0; i < users.getLength(); i++) {
216                                 this.deleteUser(role, DOMUtil.getValueOf(users.item(i), "ID", this.xpathProcessor), null, handler);
217                             }
218                         }
219                         this.deleteRole(role, null, handler);
220                     }
221                     session.removeAttribute(SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE);
222                 } catch (javax.xml.transform.TransformerException JavaDoc local) {
223                     throw new ProcessingException("TransformerException: " + local, local);
224                 }
225             }
226
227
228             if (state.equals("chguser") == true) {
229                 String JavaDoc role;
230                 String JavaDoc id;
231                 String JavaDoc user;
232
233                 if (isAdmin == false) {
234                     Map JavaDoc pars = context.getContextInfo();
235                     id = (String JavaDoc) pars.get("ID");
236                     role = (String JavaDoc) pars.get("role");
237                     user = "old";
238                 } else {
239                     role = request.getParameter(REQ_PARAMETER_ROLE);
240                     id = request.getParameter(REQ_PARAMETER_ID);
241                     user = request.getParameter(REQ_PARAMETER_USER);
242                 }
243
244                 boolean addingNewUserFailed = false;
245                 if (role != null && id != null && user != null) {
246                     if (user.equals("new") == true) {
247                         SourceParameters pars = new SourceParameters();
248                         // first include all request parameters
249
Enumeration JavaDoc requestParameters = request.getParameterNames();
250                         String JavaDoc current;
251                         while (requestParameters.hasMoreElements() == true) {
252                             current = (String JavaDoc)requestParameters.nextElement();
253                             pars.setParameter(current, request.getParameter(current));
254                         }
255                         addingNewUserFailed = !this.addUser(role, id, pars, handler);
256                         if (addingNewUserFailed == false) {
257                             consumer.startElement("", "addeduser", "addeduser", XMLUtils.EMPTY_ATTRIBUTES);
258                             consumer.characters(id.toCharArray(), 0, id.length());
259                             consumer.endElement("", "addeduser", "addeduser");
260                         }
261                     } else {
262                         String JavaDoc delete = request.getParameter("authdeluser");
263                         if (delete != null && delete.equals("true") == true) {
264                             this.deleteUser(role, id, null, handler);
265                         } else {
266                             SourceParameters pars = new SourceParameters();
267                             // first include all request parameters
268
Enumeration JavaDoc requestParameters = request.getParameterNames();
269                             String JavaDoc current;
270                             while (requestParameters.hasMoreElements() == true) {
271                                 current = (String JavaDoc)requestParameters.nextElement();
272                                 pars.setParameter(current, request.getParameter(current));
273                             }
274                             this.changeUser(role, id, pars, handler);
275                         }
276                     }
277                     session.removeAttribute(SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE);
278                 }
279                 if (addingNewUserFailed == false) {
280                     state = (isAdmin == true ? "adduser" : "seluser");
281                 } else {
282                     state = "erruser";
283                 }
284             }
285
286             if (state.equals("seluser") == true) {
287                 String JavaDoc role;
288                 String JavaDoc id;
289
290                 if (isAdmin == false) {
291                     Map JavaDoc pars = context.getContextInfo();
292                     id = (String JavaDoc) pars.get("ID");
293                     role = (String JavaDoc) pars.get("role");
294                 } else {
295                     role = request.getParameter(REQ_PARAMETER_ROLE);
296                     id = request.getParameter(REQ_PARAMETER_ID);
297                 }
298                 if (role != null && id != null) {
299                     session.setAttribute(SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE, role);
300
301                     // include users
302
Document JavaDoc userDF = this.getUsers(role, id, handler);
303                     Element JavaDoc users = null;
304                     try {
305                         if (userDF != null) users = (Element JavaDoc)DOMUtil.getSingleNode(userDF, "users/user", this.xpathProcessor);
306                     } catch (javax.xml.transform.TransformerException JavaDoc local) {
307                         throw new ProcessingException("TransformerException: " + local, local);
308                     }
309                     consumer.startElement("", "uservalues", "uservalues", XMLUtils.EMPTY_ATTRIBUTES);
310                     if (users != null && users.hasChildNodes() == true) {
311                         NodeList JavaDoc childs = users.getChildNodes();
312                         for(int i = 0; i < childs.getLength(); i++) {
313                             if (childs.item(i).getNodeType() == Node.ELEMENT_NODE)
314                                 IncludeXMLConsumer.includeNode(childs.item(i), consumer, consumer);
315                         }
316                     }
317                     consumer.endElement("", "uservalues", "uservalues");
318                 }
319                 consumer.startElement("", "user", "user", XMLUtils.EMPTY_ATTRIBUTES);
320                 consumer.characters("old".toCharArray(), 0, 3);
321                 consumer.endElement("", "user", "user");
322                 if (isAdmin == false) {
323                     consumer.startElement("", "role", "role", XMLUtils.EMPTY_ATTRIBUTES);
324                     consumer.characters(role.toCharArray(), 0, role.length());
325                     consumer.endElement("", "role", "role");
326                }
327             }
328
329             if (state.equals("erruser") == true) {
330                 String JavaDoc role;
331                 String JavaDoc id;
332
333                 if (isAdmin == false) {
334                     Map JavaDoc pars = context.getContextInfo();
335                     id = (String JavaDoc) pars.get("ID");
336                     role = (String JavaDoc) pars.get("role");
337                 } else {
338                     role = request.getParameter(REQ_PARAMETER_ROLE);
339                     id = request.getParameter(REQ_PARAMETER_ID);
340                 }
341                 if (role != null && id != null) {
342                     session.setAttribute(SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE, role);
343
344                     // include users
345
Document JavaDoc userDF = this.getUsers(role, id, handler);
346                     Element JavaDoc users = null;
347                     try {
348                         if (userDF != null) users = (Element JavaDoc)DOMUtil.getSingleNode(userDF, "users/user", this.xpathProcessor);
349                     } catch (javax.xml.transform.TransformerException JavaDoc local) {
350                         throw new ProcessingException("TransformerException: " + local, local);
351                     }
352                     consumer.startElement("", "uservalues", "uservalues", XMLUtils.EMPTY_ATTRIBUTES);
353                     if (users != null && users.hasChildNodes() == true) {
354                         NodeList JavaDoc childs = users.getChildNodes();
355                         for(int i = 0; i < childs.getLength(); i++) {
356                             if (childs.item(i).getNodeType() == Node.ELEMENT_NODE)
357                                 IncludeXMLConsumer.includeNode(childs.item(i), consumer, consumer);
358                         }
359                     }
360                     consumer.endElement("", "uservalues", "uservalues");
361                 }
362                 consumer.startElement("", "user", "user", XMLUtils.EMPTY_ATTRIBUTES);
363                 consumer.characters("error".toCharArray(), 0, 5);
364                 consumer.endElement("", "user", "user");
365                 if (isAdmin == false) {
366                     consumer.startElement("", "role", "role", XMLUtils.EMPTY_ATTRIBUTES);
367                     consumer.characters(role.toCharArray(), 0, role.length());
368                     consumer.endElement("", "role", "role");
369                }
370             }
371
372             if (state.equals("adduser") == true) {
373                 consumer.startElement("", "user", "user", XMLUtils.EMPTY_ATTRIBUTES);
374                 consumer.characters("new".toCharArray(), 0, 3);
375                 consumer.endElement("", "user", "user");
376             }
377
378             if (state.equals("selrole") == true) {
379                 String JavaDoc role = request.getParameter(REQ_PARAMETER_ROLE);
380                 session.setAttribute(SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE, role);
381                 // include users
382
Document JavaDoc userDF = this.getUsers(role, null, handler);
383                 Node JavaDoc users = null;
384                 try {
385                     if (userDF != null) users = DOMUtil.getSingleNode(userDF, "users", this.xpathProcessor);
386                 } catch (javax.xml.transform.TransformerException JavaDoc local) {
387                     throw new ProcessingException("TransformerException: " + local, local);
388                 }
389                 IncludeXMLConsumer.includeNode(users, consumer, consumer);
390             }
391
392             if (isAdmin == true) {
393                 // include roles
394
Document JavaDoc rolesDF = this.getRoles(handler);
395                 Node JavaDoc roles = null;
396                 try {
397                     if (rolesDF != null) roles = DOMUtil.getSingleNode(rolesDF, "roles", this.xpathProcessor);
398                 } catch (javax.xml.transform.TransformerException JavaDoc local) {
399                     throw new ProcessingException("TransformerException: " + local, local);
400                 }
401                 IncludeXMLConsumer.includeNode(roles, consumer, consumer);
402
403                 // include selected role
404
String JavaDoc role = (String JavaDoc)session.getAttribute(SESSION_CONTEXT_ATTRIBUTE_ADMIN_ROLE);
405                 if (role != null) {
406                     consumer.startElement("", "role", "role", XMLUtils.EMPTY_ATTRIBUTES);
407                     consumer.characters(role.toCharArray(), 0, role.length());
408                     consumer.endElement("", "role", "role");
409                 }
410             }
411         } // end synchronized(context)
412

413         consumer.endElement("", "configuration", "configuration");
414         if (this.getLogger().isDebugEnabled() == true) {
415             this.getLogger().debug("END showConfiguration");
416         }
417     }
418
419     /**
420      * Get all users in a document fragment with the following children:
421      * <users>
422      * <user>
423      * <ID>...</ID>
424      * <role>...</role> <!-- optional -->
425      * <data>
426      * ...
427      * </data>
428      * </user>
429      * ....
430      * </users>
431      * The document fragment might contain further nodes at the root!
432      * If <code>role</code> is <code>null</code> all users are fetched,
433      * otherwise only the users for this role.
434      * If also ID is not null only the single user is fetched.
435      */

436     public Document JavaDoc getUsers(String JavaDoc role, String JavaDoc ID, UserManagementHandler handler)
437     throws IOException JavaDoc, ProcessingException, SAXException JavaDoc {
438         // calling method is syned
439
if (this.getLogger().isDebugEnabled() == true) {
440             this.getLogger().debug("BEGIN getUsers role="+role+", ID="+ID);
441         }
442         Document JavaDoc frag = null;
443
444         if (handler.getLoadUsersResource() != null) {
445             final String JavaDoc loadUsersResource = handler.getLoadUsersResource();
446             final SourceParameters loadParameters = handler.getLoadUsersResourceParameters();
447             SourceParameters parameters = (loadParameters == null) ? new SourceParameters()
448                                                                      : (SourceParameters)loadParameters;
449             if (handler.getApplicationName() != null) {
450                 parameters.setSingleParameterValue("application", handler.getApplicationName());
451             }
452             if (ID != null) {
453                 parameters.setSingleParameterValue("type", "user");
454                 parameters.setSingleParameterValue("ID", ID);
455             } else {
456                 parameters.setSingleParameterValue("type", "users");
457             }
458             if (role != null) parameters.setSingleParameterValue("role", role);
459             frag = this.loadResource(loadUsersResource, parameters);
460
461         }
462
463         if (this.getLogger().isDebugEnabled() == true) {
464             this.getLogger().debug("END getUsers fragment="+(frag == null ? "null" : XMLUtils.serializeNode(frag, XMLUtils.createPropertiesForXML(false))));
465         }
466         return frag;
467     }
468
469     /**
470      * Get all roles in a document fragment with the following children:
471      * <roles>
472      * <role>...</role>
473      * ....
474      * </roles>
475      * The document fragment might contain further nodes at the root!
476      */

477     public Document JavaDoc getRoles(UserManagementHandler handler)
478     throws IOException JavaDoc, ProcessingException, SAXException JavaDoc {
479         // calling method is syned
480
if (this.getLogger().isDebugEnabled() == true) {
481             this.getLogger().debug("BEGIN getRoles");
482         }
483         Document JavaDoc frag = null;
484
485         if (handler.getLoadRolesResource() != null) {
486             final String JavaDoc loadRolesResource = handler.getLoadRolesResource();
487             final SourceParameters loadParameters = handler.getLoadRolesResourceParameters();
488             SourceParameters parameters = (loadParameters == null) ? new SourceParameters()
489                                                                      : (SourceParameters)loadParameters.clone();
490             if (handler.getApplicationName() != null)
491                 parameters.setSingleParameterValue("application", handler.getApplicationName());
492             parameters.setSingleParameterValue("type", "roles");
493             frag = this.loadResource(loadRolesResource, parameters);
494         }
495
496         if (this.getLogger().isDebugEnabled() == true) {
497             this.getLogger().debug("END getRoles fragment="+frag);
498         }
499         return frag;
500     }
501
502     /**
503      * Add a role
504      */

505     private void addRole(String JavaDoc name, SourceParameters parameters, UserManagementHandler handler)
506     throws IOException JavaDoc, ProcessingException, SAXException JavaDoc {
507         // calling method is syned
508
if (this.getLogger().isDebugEnabled() == true) {
509             this.getLogger().debug("BEGIN addRole role="+name+", parameters="+parameters);
510         }
511         if (handler.getNewRoleResource() != null) {
512             final String JavaDoc newRoleResource = handler.getNewRoleResource();
513             final SourceParameters handlerPars = handler.getNewRoleResourceParameters();
514             if (parameters == null) parameters = new SourceParameters();
515             parameters.add(handlerPars);
516
517             if (handler.getApplicationName() != null)
518                 parameters.setSingleParameterValue("application", handler.getApplicationName());
519             parameters.setSingleParameterValue("type", "role");
520             parameters.setSingleParameterValue("role", name);
521
522             this.invokeResource(newRoleResource, parameters);
523         }
524
525         if (this.getLogger().isDebugEnabled() == true) {
526             this.getLogger().debug("END addRole");
527         }
528     }
529
530     /**
531      * Add a user.
532      * @return If a user with ID already exists <code>false</code> is returned.
533      */

534     public boolean addUser(String JavaDoc role, String JavaDoc ID, SourceParameters parameters, UserManagementHandler handler)
535     throws IOException JavaDoc, ProcessingException, SAXException JavaDoc {
536         // calling method is syned
537
if (this.getLogger().isDebugEnabled() == true) {
538             this.getLogger().debug("BEGIN addUser role="+role+", ID="+ID+", parameters="+parameters);
539         }
540         boolean result = false;
541
542         if (handler.getNewUserResource() != null
543             && ID != null
544             && ID.trim().length() > 0) {
545             // first test if a user with this ID already exists
546
Document JavaDoc user = this.getUsers(null, null, handler);
547             Node JavaDoc node = null;
548             if (user != null) {
549                 try {
550                     node = DOMUtil.getSingleNode(user, "users/user/ID[text()='"+ID+"']", this.xpathProcessor);
551                 } catch (javax.xml.transform.TransformerException JavaDoc local) {
552                     throw new ProcessingException("Transformer exception: " + local, local);
553                 }
554             }
555             if (user == null || node == null) {
556                 final String JavaDoc newUserResource = handler.getNewUserResource();
557                 final SourceParameters newUsersPars = handler.getNewUserResourceParameters();
558                 if (parameters == null) parameters = new SourceParameters();
559                 parameters.add(newUsersPars);
560
561                 if (handler.getApplicationName() != null)
562                     parameters.setSingleParameterValue("application", handler.getApplicationName());
563                 parameters.setSingleParameterValue("type", "user");
564                 parameters.setSingleParameterValue("role", role);
565                 parameters.setSingleParameterValue("ID", ID);
566
567                 this.invokeResource(newUserResource, parameters);
568                 result = true;
569             }
570         }
571
572         if (this.getLogger().isDebugEnabled() == true) {
573             this.getLogger().debug("END addUser success="+result);
574         }
575         return result;
576     }
577
578     /**
579      * Delete a role
580      */

581     private void deleteRole(String JavaDoc name, SourceParameters parameters, UserManagementHandler handler)
582     throws IOException JavaDoc, ProcessingException, SAXException JavaDoc {
583         // calling method is syned
584
if (this.getLogger().isDebugEnabled() == true) {
585             this.getLogger().debug("BEGIN deleteRole role="+name+", parameters="+parameters);
586         }
587         if (handler.getDeleteRoleResource() != null) {
588             final String JavaDoc deleteRoleResource = handler.getDeleteRoleResource();
589             final SourceParameters handlerPars = handler.getDeleteRoleResourceParameters();
590             if (parameters == null) parameters = new SourceParameters();
591             parameters.add(handlerPars);
592
593             if (handler.getApplicationName() != null)
594                 parameters.setSingleParameterValue("application", handler.getApplicationName());
595             parameters.setSingleParameterValue("type", "role");
596             parameters.setSingleParameterValue("role", name);
597
598             this.invokeResource(deleteRoleResource, parameters);
599         }
600
601         if (this.getLogger().isDebugEnabled() == true) {
602             this.getLogger().debug("END deleteRole");
603         }
604     }
605
606     /**
607      * Delete a user
608      */

609     private void deleteUser(String JavaDoc role, String JavaDoc name, SourceParameters parameters, UserManagementHandler handler)
610     throws IOException JavaDoc, ProcessingException, SAXException JavaDoc {
611         // calling method is syned
612
if (this.getLogger().isDebugEnabled() == true) {
613             this.getLogger().debug("BEGIN deleteUser role="+role+", ID="+name+", parameters="+parameters);
614         }
615         if (handler.getDeleteUserResource() != null) {
616             final String JavaDoc deleteUserResource = handler.getDeleteUserResource();
617             final SourceParameters handlerPars = handler.getDeleteUserResourceParameters();
618             if (parameters == null) parameters = new SourceParameters();
619             parameters.add(handlerPars);
620
621             if (handler.getApplicationName() != null)
622                 parameters.setSingleParameterValue("application", handler.getApplicationName());
623             parameters.setSingleParameterValue("type", "user");
624             parameters.setSingleParameterValue("role", role);
625             parameters.setSingleParameterValue("ID", name);
626
627             this.invokeResource(deleteUserResource, parameters);
628         }
629
630         if (this.getLogger().isDebugEnabled() == true) {
631             this.getLogger().debug("END deleteUser");
632         }
633     }
634
635     /**
636      * Change a user
637      */

638     private void changeUser(String JavaDoc role, String JavaDoc name, SourceParameters parameters, UserManagementHandler handler)
639     throws IOException JavaDoc, ProcessingException, SAXException JavaDoc {
640         // calling method is syned
641
if (this.getLogger().isDebugEnabled() == true) {
642             this.getLogger().debug("BEGIN changeUser role="+role+", ID="+name+", parameters="+parameters);
643         }
644         if (handler.getChangeUserResource() != null) {
645             final String JavaDoc changeUserResource = handler.getChangeUserResource();
646             final SourceParameters handlerPars = handler.getChangeUserResourceParameters();
647             if (parameters == null) parameters = new SourceParameters();
648             parameters.add(handlerPars);
649
650             if (handler.getApplicationName() != null)
651                 parameters.setSingleParameterValue("application", handler.getApplicationName());
652             parameters.setSingleParameterValue("type", "user");
653             parameters.setSingleParameterValue("role", role);
654             parameters.setSingleParameterValue("ID", name);
655
656             this.invokeResource(changeUserResource, parameters);
657         }
658
659         if (this.getLogger().isDebugEnabled() == true) {
660             this.getLogger().debug("END changeUser");
661         }
662     }
663
664     /**
665      * Invoke resource
666      */

667     private void invokeResource(String JavaDoc resource,
668                                  SourceParameters parameters)
669     throws IOException JavaDoc, ProcessingException, SAXException JavaDoc {
670         Source source = null;
671         try {
672             source = SourceUtil.getSource(resource,
673                                           null,
674                                           parameters,
675                                           this.resolver);
676             SourceUtil.parse(this.manager, source, new DefaultHandler JavaDoc());
677         } catch (SourceException se) {
678             throw SourceUtil.handle(se);
679         } finally {
680             this.resolver.release(source);
681         }
682     }
683
684     /**
685      * Load XML resource
686      */

687     private Document JavaDoc loadResource(String JavaDoc resource,
688                                    SourceParameters parameters)
689     throws IOException JavaDoc, ProcessingException, SAXException JavaDoc {
690         Source source = null;
691         try {
692             source = SourceUtil.getSource(resource,
693                                           null,
694                                           parameters,
695                                           this.resolver);
696             return SourceUtil.toDOM(source);
697         } catch (SourceException se) {
698             throw SourceUtil.handle(se);
699         } finally {
700             this.resolver.release(source);
701         }
702     }
703 }
704
705 final class UserManagementHandler {
706
707     /** The name of the current application */
708     private String JavaDoc applicationName;
709
710     /** The load-users resource */
711     private String JavaDoc loadUsersResource;
712     private SourceParameters loadUsersResourceParameters;
713
714     /** The load-roles resource */
715     private String JavaDoc loadRolesResource;
716     private SourceParameters loadRolesResourceParameters;
717
718     /** The new-user resource */
719     private String JavaDoc newUserResource;
720     private SourceParameters newUserResourceParameters;
721
722     /** The new-role resource */
723     private String JavaDoc newRoleResource;
724     private SourceParameters newRoleResourceParameters;
725
726     /** The delete-role resource */
727     private String JavaDoc deleteRoleResource;
728     private SourceParameters deleteRoleResourceParameters;
729
730     /** The delete-user resource */
731     private String JavaDoc deleteUserResource;
732     private SourceParameters deleteUserResourceParameters;
733
734     /** The change-user resource */
735     private String JavaDoc changeUserResource;
736     private SourceParameters changeUserResourceParameters;
737
738     /**
739      * Create a new handler object.
740      */

741     public UserManagementHandler(Configuration conf,
742                                   String JavaDoc appName)
743     throws ConfigurationException {
744         Configuration child;
745
746         this.applicationName = appName;
747
748         // get load-users resource (optional)
749
child = conf.getChild("load-users", false);
750         if (child != null) {
751             this.loadUsersResource = child.getAttribute("uri");
752             this.loadUsersResourceParameters = SourceParameters.create(child);
753         }
754
755         // get load-roles resource (optional)
756
child = conf.getChild("load-roles", false);
757         if (child != null) {
758             this.loadRolesResource = child.getAttribute("uri");
759             this.loadRolesResourceParameters = SourceParameters.create(child);
760         }
761
762         // get new user resource (optional)
763
child = conf.getChild("new-user", false);
764         if (child != null) {
765             this.newUserResource = child.getAttribute("uri");
766             this.newUserResourceParameters = SourceParameters.create(child);
767         }
768
769         // get new role resource (optional)
770
child = conf.getChild("new-role", false);
771         if (child != null) {
772             this.newRoleResource = child.getAttribute("uri");
773             this.newRoleResourceParameters = SourceParameters.create(child);
774         }
775
776         // get delete user resource (optional)
777
child = conf.getChild("delete-user", false);
778         if (child != null) {
779             this.deleteUserResource = child.getAttribute("uri");
780             this.deleteUserResourceParameters = SourceParameters.create(child);
781         }
782
783         // get delete role resource (optional)
784
child = conf.getChild("delete-role", false);
785         if (child != null) {
786             this.deleteRoleResource = child.getAttribute("uri");
787             this.deleteRoleResourceParameters = SourceParameters.create(child);
788         }
789
790         // get change user resource (optional)
791
child = conf.getChild("change-user", false);
792         if (child != null) {
793             this.changeUserResource = child.getAttribute("uri");
794             this.changeUserResourceParameters = SourceParameters.create(child);
795         }
796     }
797
798     /**
799      * Get the name of the current application
800      */

801     public String JavaDoc getApplicationName() { return this.applicationName; }
802
803     /**
804      * Get the load users resource
805      */

806     public String JavaDoc getLoadUsersResource() { return this.loadUsersResource; }
807     public SourceParameters getLoadUsersResourceParameters() { return this.loadUsersResourceParameters; }
808
809     /**
810      * Get the load roles resource
811      */

812     public String JavaDoc getLoadRolesResource() { return this.loadRolesResource; }
813     public SourceParameters getLoadRolesResourceParameters() { return this.loadRolesResourceParameters; }
814
815     /**
816      * Get the new user resource
817      */

818     public String JavaDoc getNewUserResource() { return this.newUserResource; }
819     public SourceParameters getNewUserResourceParameters() { return this.newUserResourceParameters; }
820
821     /**
822      * Get the new role resource
823      */

824     public String JavaDoc getNewRoleResource() { return this.newRoleResource; }
825     public SourceParameters getNewRoleResourceParameters() { return this.newRoleResourceParameters; }
826
827     /** Get the delete user resource */
828     public String JavaDoc getDeleteUserResource() { return this.deleteUserResource; }
829     public SourceParameters getDeleteUserResourceParameters() { return this.deleteUserResourceParameters; }
830
831     /** Get the delete role resource */
832     public String JavaDoc getDeleteRoleResource() { return this.deleteRoleResource; }
833     public SourceParameters getDeleteRoleResourceParameters() { return this.deleteRoleResourceParameters; }
834
835     /** Get the change user resource */
836     public String JavaDoc getChangeUserResource() { return this.changeUserResource; }
837     public SourceParameters getChangeUserResourceParameters() { return this.changeUserResourceParameters; }
838 }
839
Popular Tags