KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > impl > role > ConfigurableRoleManager


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

17
18 package org.apache.avalon.fortress.impl.role;
19
20 import org.apache.avalon.fortress.RoleManager;
21 import org.apache.avalon.framework.configuration.Configurable;
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24
25 /**
26  * Configurable RoleManager implementation. It populates the RoleManager
27  * from a configuration hierarchy. This is based on the DefaultRoleManager
28  * in the org.apache.avalon.component package.
29  *
30  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
31  * @version CVS $Revision: 1.13 $ $Date: 2004/02/28 15:16:25 $
32  * @since 4.1
33  */

34 public class ConfigurableRoleManager
35         extends AbstractRoleManager
36         implements Configurable
37 {
38     /**
39      * Default constructor--this RoleManager has no parent.
40      */

41     public ConfigurableRoleManager()
42     {
43         super( null, null );
44     }
45
46     /**
47      * Alternate constructor--this RoleManager has the specified
48      * classloader.
49      *
50      * @param loader The <code>ClassLoader</code> used to resolve class names.
51      */

52     public ConfigurableRoleManager( final ClassLoader JavaDoc loader )
53     {
54         super( null, loader );
55     }
56
57     /**
58      * Alternate constructor--this RoleManager has the specified
59      * parent.
60      *
61      * @param parent The parent <code>RoleManager</code>.
62      */

63     public ConfigurableRoleManager( final RoleManager parent )
64     {
65         super( parent, null );
66     }
67
68     /**
69      * Alternate constructor--this RoleManager has the specified
70      * parent and a classloader.
71      *
72      * @param parent The parent <code>RoleManager</code>.
73      * @param loader the classloader
74      */

75     public ConfigurableRoleManager( final RoleManager parent, final ClassLoader JavaDoc loader )
76     {
77         super( parent, loader );
78     }
79
80     /**
81      * Reads a configuration object and creates the role, shorthand,
82      * and class name mapping.
83      *
84      * @param configuration The configuration object.
85      * @throws ConfigurationException if the configuration is malformed
86      */

87     public final void configure( final Configuration configuration )
88             throws ConfigurationException
89     {
90         final Configuration[] roles = configuration.getChildren( "role" );
91
92         for ( int i = 0; i < roles.length; i++ )
93         {
94             final String JavaDoc role = roles[i].getAttribute( "name" );
95             final Configuration[] components = roles[i].getChildren( "component" );
96
97             for ( int j = 0; j < components.length; j++ )
98             {
99                 final String JavaDoc shorthand = components[j].getAttribute( "shorthand" );
100                 final String JavaDoc className =
101                         components[j].getAttribute( "class", null );
102                 final String JavaDoc handlerClassName =
103                         components[j].getAttribute( "handler",
104                                 org.apache.avalon.fortress.impl.handler.PerThreadComponentHandler.class.getName() );
105
106                 if ( ! addRole( shorthand, role, className, handlerClassName ) )
107                 {
108                     final String JavaDoc message = "Skipping invalid entry:\n\tRole: " + role +
109                             "\n\tShorthand: " + shorthand +
110                             "\n\tClass Name: " + className +
111                             "\n\tHandler Class: " + handlerClassName;
112
113                     getLogger().error( message );
114                 }
115             }
116         }
117     }
118 }
119
Popular Tags