KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > authentication > components > DefaultHandlerManager


1 /*
2  * Copyright 1999-2004 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.components;
17
18 import java.util.Collections JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.cocoon.ProcessingException;
25 import org.apache.cocoon.components.ChainedConfiguration;
26 import org.apache.cocoon.components.SitemapConfigurationHolder;
27 import org.apache.cocoon.environment.ObjectModelHelper;
28 import org.apache.cocoon.webapps.authentication.configuration.HandlerConfiguration;
29
30
31 /**
32  * This is a utility class managing the authentication handlers
33  *
34  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
35  * @version CVS $Id: DefaultHandlerManager.java 30932 2004-07-29 17:35:38Z vgritsenko $
36 */

37 public final class DefaultHandlerManager {
38
39     /**
40      * Get the current handler configuration
41      */

42     static public Map JavaDoc prepareHandlerConfiguration(Map JavaDoc objectModel,
43                                                   SitemapConfigurationHolder holder)
44     throws ConfigurationException {
45         Map JavaDoc configs = (Map JavaDoc)holder.getPreparedConfiguration();
46         if ( null == configs ) {
47             ChainedConfiguration chainedConfig = holder.getConfiguration();
48             configs = prepare( objectModel, holder, chainedConfig );
49         }
50         return configs;
51     }
52     /**
53      * Prepare the handler configuration
54      */

55     static private Map JavaDoc prepare( Map JavaDoc objectModel,
56                                 SitemapConfigurationHolder holder,
57                                 ChainedConfiguration conf)
58     throws ConfigurationException {
59         // test for handlers
60
boolean found = false;
61         Configuration[] handlers = null;
62         Configuration handlersWrapper = conf.getChild("handlers", false);
63         if ( null != handlersWrapper ) {
64             handlers = handlersWrapper.getChildren("handler");
65             if ( null != handlers && handlers.length > 0) {
66                 found = true;
67             }
68         }
69
70         Map JavaDoc values = null;
71         final ChainedConfiguration parent = conf.getParent();
72         if ( null != parent ) {
73             values = prepare( objectModel, holder, parent );
74             if ( found ) {
75                 values = new HashMap JavaDoc( values );
76             }
77         } else if ( found ){
78             values = new HashMap JavaDoc(10);
79         } else {
80             values = Collections.EMPTY_MAP;
81         }
82
83         if ( found ) {
84             for(int i=0; i<handlers.length;i++) {
85                 // check unique name
86
final String JavaDoc name = handlers[i].getAttribute("name");
87                 if ( null != values.get(name) ) {
88                     throw new ConfigurationException("Handler names must be unique: " + name);
89                 }
90
91                 addHandler( objectModel, handlers[i], values );
92             }
93         }
94         holder.setPreparedConfiguration( conf, values );
95         
96         return values;
97     }
98
99     /**
100      * Add one handler configuration
101      */

102     static private void addHandler(Map JavaDoc objectModel,
103                                    Configuration configuration,
104                                    Map JavaDoc values)
105     throws ConfigurationException {
106         // get handler name
107
final String JavaDoc name = configuration.getAttribute("name");
108
109         // create handler
110
HandlerConfiguration currentHandler = new HandlerConfiguration(name);
111
112         try {
113             currentHandler.configure(ObjectModelHelper.getRequest(objectModel), configuration);
114         } catch (ProcessingException se) {
115             throw new ConfigurationException("Exception during configuration of handler: " + name, se);
116         }
117         values.put( name, currentHandler );
118     }
119
120
121 }
122
Popular Tags