KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > component > DefaultRoleManager


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.component;
9
10 import java.util.Collections JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13 import org.apache.avalon.framework.configuration.Configurable;
14 import org.apache.avalon.framework.configuration.Configuration;
15 import org.apache.avalon.framework.configuration.ConfigurationException;
16 import org.apache.avalon.framework.logger.AbstractLoggable;
17
18 /**
19  * Default RoleManager implementation. It populates the RoleManager
20  * from a configuration file.
21  *
22  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
23  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
24  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
25  * @version CVS $Revision: 1.6 $ $Date: 2001/12/11 09:53:27 $
26  * @since 4.0
27  */

28 public class DefaultRoleManager
29     extends AbstractLoggable
30     implements RoleManager, Configurable
31 {
32     /** Map for shorthand to role mapping */
33     private Map JavaDoc m_shorthands;
34
35     /** Map for role to default classname mapping */
36     private Map JavaDoc m_classNames;
37
38     /** Map for role->hint to classname mapping */
39     private Map JavaDoc m_hintClassNames;
40
41     /** Parent <code>RoleManager</code> for nested resolution */
42     private final RoleManager m_parent;
43
44     /**
45      * Default constructor--this RoleManager has no parent.
46      */

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

58     public DefaultRoleManager(RoleManager parent)
59     {
60         m_parent = parent;
61     }
62
63     /**
64      * Retrieves the real role name from a shorthand name. Usually
65      * the shorthand name refers to a configuration element name. If
66      * this RoleManager does not have the match, and there is a parent
67      * RoleManager, the parent will be asked to resolve the role.
68      *
69      * @param shorthandName The shortname that is an alias for the role.
70      * @return the official role name.
71      */

72     public final String JavaDoc getRoleForName( final String JavaDoc shorthandName )
73     {
74         final String JavaDoc role = (String JavaDoc)m_shorthands.get( shorthandName );
75
76         if( null == role && null != m_parent )
77         {
78             return m_parent.getRoleForName( shorthandName );
79         }
80
81         if (getLogger().isDebugEnabled())
82         {
83             getLogger().debug( "looking up shorthand " + shorthandName +
84                                ", returning " + role );
85         }
86
87         return role;
88     }
89
90     /**
91      * Retrieves the default class name for the specified role. This
92      * is only called when the configuration does not specify the
93      * class explicitly. If this RoleManager does not have the match,
94      * and there is a parent RoleManager, the parent will be asked
95      * to resolve the class name.
96      *
97      * @param role The role that has a default implementation.
98      * @return the Fully Qualified Class Name (FQCN) for the role.
99      */

100     public final String JavaDoc getDefaultClassNameForRole( final String JavaDoc role )
101     {
102         final String JavaDoc className = (String JavaDoc)m_classNames.get( role );
103
104         if( null == className && null != m_parent )
105         {
106             return m_parent.getDefaultClassNameForRole( role );
107         }
108
109         return className;
110     }
111
112     /**
113      * Retrieves a default class name for a role/hint combination.
114      * This is only called when a role is mapped to a
115      * DefaultComponentSelector, and the configuration elements use
116      * shorthand names for the type of component. If this RoleManager
117      * does not have the match, and there is a parent RoleManager, the
118      * parent will be asked to resolve the class name.
119      *
120      * @param role The role that this shorthand refers to.
121      * @param shorthand The shorthand name for the type of Component
122      * @return the FQCN for the role/hint combination.
123      */

124     public final String JavaDoc getDefaultClassNameForHint( final String JavaDoc role,
125                                                     final String JavaDoc shorthand )
126     {
127         if (getLogger().isDebugEnabled())
128         {
129             getLogger().debug( "looking up hintmap for role " + role );
130         }
131
132         final Map JavaDoc hintMap = (Map JavaDoc)m_hintClassNames.get( role );
133
134         if( null == hintMap )
135         {
136             if( null != m_parent )
137             {
138                 return m_parent.getDefaultClassNameForHint( role, shorthand );
139             }
140             else
141             {
142                 return "";
143             }
144         }
145
146         if (getLogger().isDebugEnabled())
147         {
148             getLogger().debug( "looking up classname for hint " + shorthand );
149         }
150
151         return (String JavaDoc)hintMap.get( shorthand );
152     }
153
154     /**
155      * Reads a configuration object and creates the role, shorthand,
156      * and class name mapping.
157      *
158      * @param configuration The configuration object.
159      * @throws ConfigurationException if the configuration is malformed
160      */

161     public final void configure( final Configuration configuration )
162         throws ConfigurationException
163     {
164         final Map JavaDoc shorts = new HashMap JavaDoc();
165         final Map JavaDoc classes = new HashMap JavaDoc();
166         final Map JavaDoc hintclasses = new HashMap JavaDoc();
167
168         final Configuration[] roles = configuration.getChildren( "role" );
169
170         for( int i = 0; i < roles.length; i++ )
171         {
172             final String JavaDoc name = roles[ i ].getAttribute( "name" );
173             final String JavaDoc shorthand = roles[ i ].getAttribute( "shorthand" );
174             final String JavaDoc defaultClassName =
175                 roles[ i ].getAttribute( "default-class", null );
176
177             shorts.put( shorthand, name );
178
179             if( null != defaultClassName )
180             {
181                 classes.put( name, defaultClassName );
182             }
183
184             final Configuration[] hints = roles[ i ].getChildren( "hint" );
185             if( hints.length > 0 )
186             {
187                 HashMap JavaDoc hintMap = new HashMap JavaDoc();
188
189                 for( int j = 0; j < hints.length; j++ )
190                 {
191                     final String JavaDoc shortHand = hints[ j ].getAttribute("shorthand").trim();
192                     final String JavaDoc className = hints[ j ].getAttribute("class").trim();
193
194                     hintMap.put( shortHand, className );
195                     if (getLogger().isDebugEnabled())
196                     {
197                         getLogger().debug( "Adding hint type " + shortHand +
198                                            " associated with role " + name +
199                                            " and class " + className );
200                     }
201                 }
202
203                 hintclasses.put( name, Collections.unmodifiableMap( hintMap ) );
204             }
205
206             if (getLogger().isDebugEnabled())
207             {
208                 getLogger().debug( "added Role " + name + " with shorthand " +
209                                    shorthand + " for " + defaultClassName );
210             }
211         }
212
213         m_shorthands = Collections.unmodifiableMap( shorts );
214         m_classNames = Collections.unmodifiableMap( classes );
215         m_hintClassNames = Collections.unmodifiableMap( hintclasses );
216     }
217 }
218
Popular Tags