KickJava   Java API By Example, From Geeks To Geeks.

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


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.RoleEntry;
21 import org.apache.avalon.fortress.RoleManager;
22 import org.apache.avalon.fortress.impl.handler.PerThreadComponentHandler;
23 import org.apache.avalon.framework.logger.AbstractLogEnabled;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * The Excalibur Role Manager is used for Excalibur Role Mappings. All of
30  * the information is hard-coded.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  * @version CVS $Revision: 1.13 $ $Date: 2004/02/28 15:16:25 $
34  * @since 4.1
35  */

36 public abstract class AbstractRoleManager
37     extends AbstractLogEnabled
38     implements RoleManager
39 {
40     /**
41      * The classloader used to load and check roles and components.
42      */

43     private final ClassLoader JavaDoc m_loader;
44
45     /**
46      * Map for shorthand to RoleEntry.
47      */

48     private final Map JavaDoc m_shorthands = new HashMap JavaDoc();
49
50     /**
51      * Map for classname to RoleEntry.
52      */

53     private final Map JavaDoc m_classnames = new HashMap JavaDoc();
54
55     /**
56      * Parent <code>RoleManager</code> for nested resolution.
57      */

58     private final RoleManager m_parent;
59
60     /**
61      * Default constructor--this RoleManager has no parent.
62      */

63     public AbstractRoleManager()
64     {
65         this( null );
66     }
67
68     /**
69      * Alternate constructor--this RoleManager has the specified
70      * parent.
71      *
72      * @param parent The parent <code>RoleManager</code>.
73      */

74     public AbstractRoleManager( final RoleManager parent )
75     {
76         this( parent, Thread.currentThread().getContextClassLoader() );
77     }
78
79     /**
80      * Create an AbstractRoleManager with the specified parent manager and the
81      * supplied classloader.
82      *
83      * @param parent The parent <code>RoleManager</code>
84      * @param loader The class loader
85      */

86     public AbstractRoleManager( final RoleManager parent,
87                                 final ClassLoader JavaDoc loader )
88     {
89         ClassLoader JavaDoc thisLoader = loader;
90         if ( null == thisLoader )
91         {
92             thisLoader = Thread.currentThread().getContextClassLoader();
93         }
94
95         m_loader = thisLoader;
96         m_parent = parent;
97     }
98
99     /**
100      * Addition of a role to the role manager.
101      * @param shortName the shor name for the role
102      * @param role the role
103      * @param className the class name
104      * @param handlerClassName the handler classname
105      */

106     protected final boolean addRole( final String JavaDoc shortName,
107                                   final String JavaDoc role,
108                                   final String JavaDoc className,
109                                   final String JavaDoc handlerClassName )
110     {
111         final Class JavaDoc clazz;
112         final Class JavaDoc handlerKlass;
113
114         try
115         {
116             clazz = m_loader.loadClass( className );
117         }
118         catch ( final Exception JavaDoc e )
119         {
120             final String JavaDoc message =
121                 "Unable to load class " + className + ". Skipping.";
122             getLogger().warn( message );
123             return false;
124         }
125
126         if ( null != handlerClassName )
127         {
128             try
129             {
130                 handlerKlass = m_loader.loadClass( handlerClassName );
131             }
132             catch ( final Exception JavaDoc e )
133             {
134                 final String JavaDoc message = "Unable to load handler " +
135                     handlerClassName + " for class " + className + ". Skipping.";
136                 getLogger().warn( message );
137                 return false;
138             }
139         }
140         else
141         {
142             handlerKlass = getDefaultHandler();
143         }
144
145         if ( getLogger().isDebugEnabled() )
146         {
147             getLogger().debug( "addRole role: name='" + shortName + "', role='" + role + "', "
148                 + "class='" + className + "', handler='" + handlerClassName + "'" );
149         }
150
151         final RoleEntry entry = new RoleEntry( role, shortName, clazz, handlerKlass );
152         m_shorthands.put( shortName, entry );
153         m_classnames.put( className, entry );
154
155         return true;
156     }
157
158     /**
159      * Get the default component handler.
160      *
161      * @return the class for {@link PerThreadComponentHandler}
162      */

163     protected final Class JavaDoc getDefaultHandler()
164     {
165         return PerThreadComponentHandler.class;
166     }
167
168     public final RoleEntry getRoleForClassname( final String JavaDoc classname )
169     {
170         final RoleEntry roleEntry = (RoleEntry) m_classnames.get( classname );
171         if ( null != roleEntry )
172         {
173             return roleEntry;
174         }
175         else if ( null != m_parent )
176         {
177             return m_parent.getRoleForClassname( classname );
178         }
179         else
180         {
181             return null;
182         }
183     }
184
185     /**
186      * Return a role name relative to a supplied short name.
187      *
188      * @param shortname the short name
189      * @return the role entry
190      */

191     public final RoleEntry getRoleForShortName( final String JavaDoc shortname )
192     {
193         final RoleEntry roleEntry = (RoleEntry) m_shorthands.get( shortname );
194         if ( null != roleEntry )
195         {
196             return roleEntry;
197         }
198         else if ( null != m_parent )
199         {
200             return m_parent.getRoleForShortName( shortname );
201         }
202         else
203         {
204             return null;
205         }
206     }
207
208     /**
209      * Get the classloader used for the RoleManager for any class that
210      * extends this one.
211      *
212      * @return ClassLoader
213      */

214     protected final ClassLoader JavaDoc getLoader()
215     {
216         return m_loader;
217     }
218 }
219
220
Popular Tags