KickJava   Java API By Example, From Geeks To Geeks.

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


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.MetaInfoEntry;
21 import org.apache.avalon.fortress.MetaInfoManager;
22 import org.apache.avalon.fortress.RoleManager;
23 import org.apache.avalon.framework.logger.AbstractLogEnabled;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 /**
31  * Provides the foundation for MetaInfoManagers.
32  *
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  * @version CVS $Revision: 1.9 $
35  */

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

41     private final ClassLoader JavaDoc m_loader;
42
43     /**
44      * Map for shorthand to MetaInfoEntry.
45      */

46     private final Map JavaDoc m_shorthands = new HashMap JavaDoc();
47
48     /**
49      * Map for classname to MetaInfoEntry.
50      */

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

56     private final MetaInfoManager m_parent;
57
58     /**
59      * Default constructor--this RoleManager has no parent.
60      */

61     public AbstractMetaInfoManager()
62     {
63         this( (MetaInfoManager) null );
64     }
65
66     /**
67      * Create a MetaInfoManager with a parent manager.
68      *
69      * @param parent The parent <code>RoleManager</code>.
70      */

71     public AbstractMetaInfoManager( final RoleManager parent )
72     {
73         this( new Role2MetaInfoManager( parent ) );
74     }
75
76     /**
77      * Create a MetaInfoManager with a parent manager.
78      *
79      * @param parent The parent <code>MetaInfoManager</code>.
80      */

81     public AbstractMetaInfoManager( final MetaInfoManager parent )
82     {
83         this( parent, Thread.currentThread().getContextClassLoader() );
84     }
85
86     /**
87      * Alternate constructor--this RoleManager has the specified
88      * parent.
89      *
90      * @param parent The parent <code>MetaInfoManager</code>
91      * @param loader The class loader
92      */

93     public AbstractMetaInfoManager( final MetaInfoManager parent,
94                                     final ClassLoader JavaDoc loader )
95     {
96         ClassLoader JavaDoc thisLoader = loader;
97         if ( null == thisLoader )
98         {
99             thisLoader = Thread.currentThread().getContextClassLoader();
100         }
101
102         m_loader = thisLoader;
103         m_parent = parent;
104     }
105
106     /**
107      * Addition of a component to the meta info manager.
108      * @param role the role associated with the component
109      * @param className the class name
110      * @param meta the properties object for the meta info
111      */

112     protected void addComponent( final String JavaDoc role,
113                                  final String JavaDoc className,
114                                  final Properties JavaDoc meta,
115                                  final List JavaDoc deps )
116     {
117         final Class JavaDoc klass;
118
119         MetaInfoEntry entry = (MetaInfoEntry) m_classnames.get( className );
120
121         if ( null != entry )
122         {
123             entry.addRole( role );
124             return;
125         }
126
127         if ( getLogger().isDebugEnabled() )
128         {
129             getLogger().debug( "addComponent component: type='" + className +
130                 "', meta='" + meta.toString() + "', role='" + role + "', with deps=" + deps );
131         }
132
133         try
134         {
135             klass = m_loader.loadClass( className );
136         }
137         catch ( final ClassNotFoundException JavaDoc e )
138         {
139             final String JavaDoc message =
140                 "Unable to load class " + className + ". Skipping.";
141             getLogger().warn( message );
142             // Do not store reference if class does not exist.
143
return;
144         }
145
146         try
147         {
148             entry = new MetaInfoEntry( klass, meta, deps );
149             entry.addRole( role );
150
151             m_shorthands.put( entry.getConfigurationName(), entry );
152             m_classnames.put( className, entry );
153         }
154         catch ( ClassNotFoundException JavaDoc cfne )
155         {
156             final String JavaDoc message =
157                 "Unable to load the handler class for " + className + ". Skipping.";
158             getLogger().warn( message );
159         }
160     }
161
162     /**
163      * Get a <code>MetaInfoEntry</code> for a component type. This facilitates
164      * self-healing configuration files where the impl reads the
165      * configuration and translates all <code>&lt;component/&gt;</code>
166      * entries to use the short hand name for readability.
167      *
168      * @param classname The component type name
169      *
170      * @return the proper {@link MetaInfoEntry}
171      */

172     public MetaInfoEntry getMetaInfoForClassname( final String JavaDoc classname )
173     {
174         final MetaInfoEntry metaEntry = (MetaInfoEntry) m_classnames.get( classname );
175         if ( null != metaEntry )
176         {
177             return metaEntry;
178         }
179         else if ( null != m_parent )
180         {
181             return m_parent.getMetaInfoForClassname( classname );
182         }
183         else
184         {
185             return null;
186         }
187     }
188
189     /**
190      * Return the meta info relative to a supplied short name.
191      *
192      * @param shortname the short name
193      * @return the proper {@link MetaInfoEntry}
194      */

195     public MetaInfoEntry getMetaInfoForShortName( final String JavaDoc shortname )
196     {
197         final MetaInfoEntry metaEntry = (MetaInfoEntry) m_shorthands.get( shortname );
198         if ( null != metaEntry )
199         {
200             return metaEntry;
201         }
202         else if ( null != m_parent )
203         {
204             return m_parent.getMetaInfoForShortName( shortname );
205         }
206         else
207         {
208             return null;
209         }
210     }
211
212     /**
213      * Get the classloader used for the RoleManager for any class that
214      * extends this one.
215      *
216      * @return ClassLoader
217      */

218     protected ClassLoader JavaDoc getLoader()
219     {
220         return m_loader;
221     }
222     
223     /**
224      * Let us know that the meta and dependency info has already been
225      * loaded for a given class name.
226      *
227      * @param className The name of the class to check
228      * @return <code>true</code> if it has been added
229      */

230     protected boolean isAlreadyAdded( String JavaDoc className )
231     {
232         return m_classnames.containsKey( className );
233     }
234 }
235
Popular Tags