KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > system > AbstractRoleManager


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.system;
9
10 import org.apache.avalon.framework.component.Component;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 /**
19  * The Excalibur Role Manager is used for Excalibur Role Mappings. All of the
20  * information is hard-coded.
21  *
22  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
23  * @version CVS $Revision: 1.1 $ $Date: 2002/01/30 15:44:06 $
24  * @since 4.1
25  */

26 public abstract class AbstractRoleManager
27     implements RoleManager
28 {
29     protected static final String JavaDoc EMPTY_STRING = "";
30
31     protected final ClassLoader JavaDoc m_loader;
32
33     /** Map for shorthand to class mapping */
34     protected Map JavaDoc m_shorthands;
35
36     /** Map for role to classname mapping */
37     protected Map JavaDoc m_classNames;
38
39     /** Map for role to handler classname mapping */
40     protected Map JavaDoc m_handlerNames;
41
42     /** Parent <code>RoleManager</code> for nested resolution */
43     protected final RoleManager m_parent;
44
45     /**
46      * Default constructor--this RoleManager has no parent.
47      */

48     public AbstractRoleManager()
49     {
50         this( null );
51     }
52
53     /**
54      * Alternate constructor--this RoleManager has the specified
55      * parent.
56      *
57      * @param parent The parent <code>RoleManager</code>.
58      */

59     public AbstractRoleManager(RoleManager parent)
60     {
61         this( parent, Thread.currentThread().getContextClassLoader() );
62     }
63
64     /**
65      * Alternate constructor--this RoleManager has the specified
66      * parent.
67      *
68      * @param parent The parent <code>RoleManager</code>.
69      */

70     public AbstractRoleManager(RoleManager parent, ClassLoader JavaDoc loader)
71     {
72         ClassLoader JavaDoc thisLoader = loader;
73
74         if ( null == thisLoader )
75         {
76             thisLoader = Thread.currentThread().getContextClassLoader();
77         }
78
79         m_loader = thisLoader;
80         m_parent = parent;
81     }
82
83     protected void setup( Map JavaDoc shorts, Map JavaDoc classes, Map JavaDoc handlers,
84                         String JavaDoc shortName, String JavaDoc role, String JavaDoc className,
85                         String JavaDoc handlerClassName )
86     {
87         final Class JavaDoc klass;
88         Class JavaDoc handlerKlass;
89
90         try
91         {
92             klass = m_loader.loadClass( className );
93
94             if ( ! Component.class.isAssignableFrom( klass ) )
95             {
96                 // Do not store reference if it is not a Component
97
return;
98             }
99         }
100         catch ( Exception JavaDoc e )
101         {
102             // Do not store reference if class does not exist.
103
return;
104         }
105
106         try
107         {
108             handlerKlass = m_loader.loadClass( handlerClassName );
109         }
110         catch ( Exception JavaDoc e )
111         {
112             handlerKlass = org.apache.avalon.excalibur.system.handler.PerThreadComponentHandler.class;
113         }
114
115         shorts.put( shortName, klass );
116         shorts.put( klass, shortName );
117         classes.put( klass,
118                      role );
119
120         List JavaDoc classList = (List JavaDoc) classes.get( role );
121
122         if ( null == classList )
123         {
124             classList = new ArrayList JavaDoc(5);
125         }
126
127         classList.add( klass );
128         classes.put( role, classList );
129
130         handlers.put( klass, handlerKlass );
131     }
132
133     /**
134      * Find the Class for the given shorthand name. If there is no
135      * correspondence between the class and the shorthand name, the method
136      * returns <code>null</code>. If this RoleManager does not have the match,
137      * and there is a parent RoleManager, the parent will be asked to resolve
138      * the request.
139      */

140     public final Class JavaDoc getClassForName( final String JavaDoc shorthandName )
141     {
142         final Class JavaDoc component = (Class JavaDoc)m_shorthands.get( shorthandName );
143
144         if( null == component && null != m_parent )
145         {
146             return m_parent.getClassForName( shorthandName );
147         }
148
149         return component;
150     }
151
152     /**
153      * Retrieves the real role name from a shorthand name. Usually
154      * the shorthand name refers to a configuration element name. If
155      * this RoleManager does not have the match, and there is a parent
156      * RoleManager, the parent will be asked to resolve the role.
157      *
158      * @param shorthandName The shortname that is an alias for the role.
159      * @return the official role name.
160      */

161     public final String JavaDoc getNameForClass( final Class JavaDoc klass )
162     {
163         final String JavaDoc shorthandName = (String JavaDoc)m_shorthands.get( klass );
164
165         if( null == shorthandName && null != m_parent )
166         {
167             return m_parent.getNameForClass( klass );
168         }
169
170         return shorthandName;
171     }
172
173     /**
174      * Retrieves the handler class name for the specified class name. This
175      * is called for every ComponentImplementation. If this RoleManager does
176      * not have the match, and there is a parent RoleManager, the parent will be
177      * asked to resolve the handler's class name.
178      *
179      * @param role The role that has a default implementation.
180      * @return the Fully Qualified Class Name (FQCN) for the role.
181      */

182     public final Class JavaDoc getHandlerClassForClass( final Class JavaDoc className )
183     {
184         final Class JavaDoc handler = (Class JavaDoc)m_handlerNames.get( className );
185
186         if( null == handler && null != m_parent )
187         {
188             return m_parent.getHandlerClassForClass( className );
189         }
190
191         return handler;
192     }
193
194     /**
195      * Retrieves the default class name for the specified role. This
196      * is only called when the configuration does not specify the
197      * class explicitly. If this RoleManager does not have the match,
198      * and there is a parent RoleManager, the parent will be asked
199      * to resolve the class name.
200      *
201      * @param role The role that has a default implementation.
202      * @return the Fully Qualified Class Name (FQCN) for the role.
203      */

204     public final Class JavaDoc[] getClassesForRole( final String JavaDoc role )
205     {
206         final List JavaDoc classes = (List JavaDoc)m_classNames.get( role );
207
208         if( null == classes && null != m_parent )
209         {
210             return m_parent.getClassesForRole( role );
211         }
212
213         return (Class JavaDoc []) classes.toArray( new Class JavaDoc[] {} );
214     }
215
216     /**
217      * Retrieves a default class name for a role/hint combination.
218      * This is only called when a role is mapped to a
219      * DefaultComponentSelector, and the configuration elements use
220      * shorthand names for the type of component. If this RoleManager
221      * does not have the match, and there is a parent RoleManager, the
222      * parent will be asked to resolve the class name.
223      *
224      * @param role The role that this shorthand refers to.
225      * @param shorthand The shorthand name for the type of Component
226      * @return the FQCN for the role/hint combination.
227      */

228     public final String JavaDoc getRoleForClass( final Class JavaDoc klass )
229     {
230         final String JavaDoc role = (String JavaDoc)m_classNames.get( klass );
231
232         if( null == role )
233         {
234             if( null != m_parent )
235             {
236                 return m_parent.getRoleForClass( klass );
237             }
238             else
239             {
240                 return EMPTY_STRING;
241             }
242         }
243
244         return role;
245     }
246 }
247
Popular Tags