KickJava   Java API By Example, From Geeks To Geeks.

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


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

19
20 package org.apache.avalon.fortress.impl.role;
21
22 import org.apache.avalon.fortress.MetaInfoEntry;
23 import org.apache.avalon.fortress.RoleManager;
24 import org.apache.avalon.fortress.util.Service;
25 import org.apache.avalon.framework.configuration.Configurable;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28 import org.apache.avalon.framework.thread.SingleThreaded;
29 import org.apache.avalon.framework.thread.ThreadSafe;
30
31 /**
32  * This role manager implementation is able to read ECM based role files.
33  *
34  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
35  * @version CVS $Revision: 1.3 $ $Date: 2004/04/05 10:16:56 $
36  */

37 public class ECMRoleManager
38         extends AbstractRoleManager
39         implements Configurable
40 {
41
42     /**
43      * Default constructor--this RoleManager has no parent.
44      */

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

56     public ECMRoleManager( final ClassLoader JavaDoc loader )
57     {
58         super( null, loader );
59     }
60
61     /**
62      * Alternate constructor--this RoleManager has the specified
63      * parent.
64      *
65      * @param parent The parent <code>RoleManager</code>.
66      */

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

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

92     public final void configure( final Configuration configuration )
93     throws ConfigurationException
94     {
95         final Configuration[] roles = configuration.getChildren( "role" );
96
97         for ( int i = 0; i < roles.length; i++ )
98         {
99             final String JavaDoc role = roles[i].getAttribute( "name" );
100             final String JavaDoc shorthand = roles[i].getAttribute( "shorthand" );
101             final String JavaDoc defaultClassName = roles[i].getAttribute( "default-class", null );
102
103             if ( ! addRole( shorthand, role, defaultClassName, getComponentHandlerClassName(defaultClassName)) )
104             {
105
106                 final String JavaDoc message = "Configuration error on invalid entry:\n\tRole: " + role +
107                         "\n\tShorthand: " + shorthand +
108                         "\n\tDefault Class: " + defaultClassName;
109
110                 getLogger().warn(message);
111             }
112
113         }
114     }
115
116     protected String JavaDoc getComponentHandlerClassName(final String JavaDoc defaultClassName)
117     {
118         if ( defaultClassName == null )
119         {
120             return null;
121         }
122         Class JavaDoc clazz;
123         try
124         {
125             clazz = getLoader().loadClass( defaultClassName );
126         }
127         catch ( final Exception JavaDoc e )
128         {
129             final String JavaDoc message =
130                 "Unable to load class " + defaultClassName + ". Using dfault component handler.";
131             getLogger().warn( message );
132             return null;
133         }
134
135         if ( ThreadSafe.class.isAssignableFrom( clazz ) )
136         {
137             return MetaInfoEntry.THREADSAFE_HANDLER;
138         }
139         else if ( Service.isClassPoolable( clazz ) )
140         {
141             return MetaInfoEntry.POOLABLE_HANDLER;
142         }
143         else if ( SingleThreaded.class.isAssignableFrom( clazz) )
144         {
145             return MetaInfoEntry.FACTORY_HANDLER;
146         }
147
148         // Don't know, use default
149
return null ;
150     }
151 }
152
Popular Tags