KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > meta > info > Type


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6
7  Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
8
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24
25  4. The names "Jakarta", "Apache Avalon", "Avalon Framework" and
26     "Apache Software Foundation" must not be used to endorse or promote
27     products derived from this software without prior written
28     permission. For written permission, please contact apache@apache.org.
29
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48
49 */

50
51 package org.apache.avalon.meta.info;
52
53 import java.io.Serializable JavaDoc;
54
55 import org.apache.avalon.framework.configuration.Configuration;
56
57 /**
58  * This class contains the meta information about a particular
59  * component type. It describes;
60  *
61  * <ul>
62  * <li>Human presentable meta data such as name, version, description etc
63  * useful when assembling the system.</li>
64  * <li>the context object capabilities that this component requires</li>
65  * <li>the services that this component type is capable of providing</li>
66  * <li>the services that this component type requires to operate (and the
67  * names via which services are accessed)</li>
68  * <li>extended lifecycle stages that this component uses</li>
69  * </ul>
70  *
71  * <p><b>UML</b></p>
72  * <p><image SRC="doc-files/Type.gif" border="0"/></p>
73  *
74  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
75  * @version $Revision: 1.13 $ $Date: 2003/09/09 22:00:42 $
76  */

77 public class Type implements Serializable JavaDoc
78 {
79     private final InfoDescriptor m_descriptor;
80     private final ContextDescriptor m_context;
81     private final Configuration m_configuration;
82     private final ServiceDescriptor[] m_services;
83     private final DependencyDescriptor[] m_dependencies;
84     private final CategoryDescriptor[] m_loggers;
85     private final StageDescriptor[] m_stages;
86     private final ExtensionDescriptor[] m_extensions;
87
88     /**
89      * Creation of a new Type instance using a supplied component descriptor,
90      * logging, cotext, services, depedencies, stages and extension descriptors.
91      * @param descriptor a component descriprot that contains information about
92      * the component type
93      * @param loggers a set of logger descriptors the declare the logging channels
94      * required by the type
95      * @param context a component context descriptor that declares the context type
96      * and context entry key and value classnames
97      * @param services a set of service descriptors that detail the service that
98      * this component type is capable of supplying
99      * @param dependencies a set of depedency descriptors that detail the service
100      * that this component type is depedent on
101      * @param stages a set of stage descriprors that detail the extension stage
102      * interfaces that this component requires a handler for
103      * @param extensions a set of lifecycle extension capabilities that this
104      * componet can provide to its container during the process of stage
105      * suppier resolution
106      * @exception NullPointerException if the descriptor, loggers, context, services,
107      * dependencies, stages, or extensions argument is null
108      */

109     public Type( final InfoDescriptor descriptor,
110                  final CategoryDescriptor[] loggers,
111                  final ContextDescriptor context,
112                  final ServiceDescriptor[] services,
113                  final DependencyDescriptor[] dependencies,
114                  final StageDescriptor[] stages,
115                  final ExtensionDescriptor[] extensions )
116             throws NullPointerException JavaDoc
117     {
118         this( descriptor, loggers, context, services, dependencies, stages, extensions, null );
119     }
120
121     /**
122      * Creation of a new Type instance using a supplied component descriptor,
123      * logging, cotext, services, depedencies, stages and extension descriptors.
124      * @param descriptor a component descriprot that contains information about
125      * the component type
126      * @param loggers a set of logger descriptors the declare the logging channels
127      * required by the type
128      * @param context a component context descriptor that declares the context type
129      * and context entry key and value classnames
130      * @param services a set of service descriprors that detail the service that
131      * this component type is capable of supplying
132      * @param dependencies a set of depedency descriprors that detail the service
133      * that this component type is depedent on
134      * @param stages a set of stage descriprors that detail the extensiuon stage
135      * interfaces that this component requires a handler for
136      * @param extensions a set of lifecycle extension capabilities that this
137      * componet can provide to its container during the process of stage
138      * suppier resolution
139      * @exception NullPointerException if the descriptor, loggers, context, services,
140      * dependencies, stages, or extensions argument is null
141      */

142     public Type( final InfoDescriptor descriptor,
143                  final CategoryDescriptor[] loggers,
144                  final ContextDescriptor context,
145                  final ServiceDescriptor[] services,
146                  final DependencyDescriptor[] dependencies,
147                  final StageDescriptor[] stages,
148                  final ExtensionDescriptor[] extensions,
149                  final Configuration defaults )
150             throws NullPointerException JavaDoc
151     {
152         if ( null == descriptor )
153         {
154             throw new NullPointerException JavaDoc( "descriptor" );
155         }
156         if ( null == loggers )
157         {
158             throw new NullPointerException JavaDoc( "loggers" );
159         }
160         if ( null == context )
161         {
162             throw new NullPointerException JavaDoc( "context" );
163         }
164         if ( null == services )
165         {
166             throw new NullPointerException JavaDoc( "services" );
167         }
168         if ( null == dependencies )
169         {
170             throw new NullPointerException JavaDoc( "dependencies" );
171         }
172         if ( null == stages )
173         {
174             throw new NullPointerException JavaDoc( "stages" );
175         }
176         if ( null == extensions )
177         {
178             throw new NullPointerException JavaDoc( "extensions" );
179         }
180
181         m_descriptor = descriptor;
182         m_loggers = loggers;
183         m_context = context;
184         m_services = services;
185         m_dependencies = dependencies;
186         m_stages = stages;
187         m_extensions = extensions;
188         m_configuration = defaults;
189     }
190
191     /**
192      * Return the Component descriptor.
193      *
194      * @return the Component descriptor.
195      */

196     public InfoDescriptor getInfo()
197     {
198         return m_descriptor;
199     }
200
201     /**
202      * Return the set of Logger that this Component will use.
203      *
204      * @return the set of Logger that this Component will use.
205      */

206     public CategoryDescriptor[] getCategories()
207     {
208         return m_loggers;
209     }
210
211     /**
212      * Return TRUE if the set of Logger descriptors includes the supplied name.
213      *
214      * @param name the logging subcategory name
215      * @return TRUE if the logging subcategory is declared.
216      */

217     public boolean isaCategory( String JavaDoc name )
218     {
219         CategoryDescriptor[] loggers = getCategories();
220         for( int i = 0; i < loggers.length; i++ )
221         {
222             CategoryDescriptor logger = loggers[ i ];
223             if( logger.getName().equals( name ) )
224             {
225                 return true;
226             }
227         }
228         return false;
229     }
230
231     /**
232      * Return the ContextDescriptor for component, may be null.
233      * If null then this component does not implement Contextualizable.
234      *
235      * @return the ContextDescriptor for component, may be null.
236      */

237     public ContextDescriptor getContext()
238     {
239         return m_context;
240     }
241
242     /**
243      * Return the set of Services that this component is capable of providing.
244      *
245      * @return the set of Services that this component is capable of providing.
246      */

247     public ServiceDescriptor[] getServices()
248     {
249         return m_services;
250     }
251
252     /**
253      * Retrieve a service with a particular reference.
254      *
255      * @param reference a service reference descriptor
256      * @return the service descriptor or null if it does not exist
257      */

258     public ServiceDescriptor getService( final ReferenceDescriptor reference )
259     {
260         for ( int i = 0; i < m_services.length; i++ )
261         {
262             final ServiceDescriptor service = m_services[i];
263             if ( service.getReference().matches( reference ) )
264             {
265                 return service;
266             }
267         }
268         return null;
269     }
270
271     /**
272      * Retrieve a service with a particular classname.
273      *
274      * @param classname the service classname
275      * @return the service descriptor or null if it does not exist
276      */

277     public ServiceDescriptor getService( final String JavaDoc classname )
278     {
279         for ( int i = 0; i < m_services.length; i++ )
280         {
281             final ServiceDescriptor service = m_services[i];
282             if ( service.getReference().getClassname().equals( classname ) )
283             {
284                 return service;
285             }
286         }
287         return null;
288     }
289
290     /**
291      * Return the set of Dependencies that this component requires to operate.
292      *
293      * @return the set of Dependencies that this component requires to operate.
294      */

295     public DependencyDescriptor[] getDependencies()
296     {
297         return m_dependencies;
298     }
299
300     /**
301      * Retrieve a dependency with a particular role.
302      *
303      * @param key the service key
304      * @return the dependency or null if it does not exist
305      */

306     public DependencyDescriptor getDependency( final String JavaDoc key )
307     {
308         for ( int i = 0; i < m_dependencies.length; i++ )
309         {
310             if ( m_dependencies[i].getKey().equals( key ) )
311             {
312                 return m_dependencies[i];
313             }
314         }
315         return null;
316     }
317
318     /**
319      * Returns the default configuration supplied with the type.
320      *
321      * @return the default configuration or null if no packaged defaults
322      */

323     public Configuration getConfiguration()
324     {
325         return m_configuration;
326     }
327
328     /**
329      * Return the lifecycle stages extensions required by this component type.
330      *
331      * @return an array of stage descriptors.
332      */

333     public StageDescriptor[] getStages()
334     {
335         return m_stages;
336     }
337
338     /**
339      * Return the stages extension handling provided by this extension.
340      *
341      * @return an array of extension descriptors.
342      */

343     public ExtensionDescriptor[] getExtensions()
344     {
345         return m_extensions;
346     }
347
348     /**
349      * Return the extension supporting the supplied stage.
350      *
351      * @param stage the lifecycle stage that this type requires a handler for
352      * @return a matching extension or null if no matching extension
353      */

354     public ExtensionDescriptor getExtension( StageDescriptor stage )
355     {
356         return getExtension( stage.getKey() );
357     }
358
359     /**
360      * Return the extension supporting the supplied stage.
361      *
362      * @param key the lifecycle stage that this type requires a handler for
363      * @return a matching extension or null if no matching extension
364      */

365     public ExtensionDescriptor getExtension( String JavaDoc key )
366     {
367         ExtensionDescriptor[] extensions = getExtensions();
368         for ( int i = 0; i < extensions.length; i++ )
369         {
370             ExtensionDescriptor extension = extensions[i];
371             String JavaDoc ref = extension.getKey();
372             if ( key.equals( ref ) )
373             {
374                 return extension;
375             }
376         }
377         return null;
378     }
379
380     /**
381      * Return a string representation of the type.
382      * @return the stringified type
383      */

384     public String JavaDoc toString()
385     {
386         return getInfo().toString();
387     }
388
389    /**
390     * Test is the supplied object is equal to this object.
391     * @return true if the object are equivalent
392     */

393     public boolean equals(Object JavaDoc other)
394     {
395         boolean isEqual = other instanceof Type;
396         isEqual = isEqual && m_descriptor.equals(((Type)other).m_descriptor);
397         isEqual = isEqual && m_configuration.equals(((Type)other).m_configuration);
398         isEqual = isEqual && m_context.equals(((Type)other).m_context);
399         for( int i=0; i<m_loggers.length; i++ )
400         {
401             isEqual = isEqual && m_loggers[i].equals(((Type)other).m_loggers[i]);
402         }
403         for( int i=0; i<m_services.length; i++ )
404         {
405             isEqual = isEqual && m_services[i].equals(((Type)other).m_services[i]);
406         }
407         for( int i=0; i<m_dependencies.length; i++ )
408         {
409             isEqual = isEqual && m_dependencies[i].equals(((Type)other).m_dependencies[i]);
410         }
411         for( int i=0; i<m_stages.length; i++ )
412         {
413             isEqual = isEqual && m_stages[i].equals(((Type)other).m_stages[i]);
414         }
415         for( int i=0; i<m_extensions.length; i++ )
416         {
417             isEqual = isEqual && m_extensions[i].equals(((Type)other).m_extensions[i]);
418         }
419         return isEqual;
420     }
421
422    /**
423     * Return the hashcode for the object.
424     * @return the hashcode value
425     */

426     public int hashCode()
427     {
428         int hash = m_descriptor.hashCode();
429         hash >>>= 13;
430         hash ^= m_context.hashCode();
431         hash >>>= 13;
432         if( m_configuration != null )
433         {
434             hash ^= m_context.hashCode();
435             hash >>>= 13;
436         }
437         hash >>>= 13;
438         for( int i=0; i<m_services.length; i++ )
439         {
440             hash ^= m_services[i].hashCode();
441             hash >>>= 13;
442         }
443         for( int i=0; i<m_dependencies.length; i++ )
444         {
445             hash ^= m_dependencies[i].hashCode();
446             hash >>>= 13;
447         }
448         for( int i=0; i<m_loggers.length; i++ )
449         {
450             hash ^= m_loggers[i].hashCode();
451             hash >>>= 13;
452         }
453         for( int i=0; i<m_stages.length; i++ )
454         {
455             hash ^= m_stages[i].hashCode();
456             hash >>>= 13;
457         }
458         for( int i=0; i<m_extensions.length; i++ )
459         {
460             hash ^= m_extensions[i].hashCode();
461             hash >>>= 13;
462         }
463         return hash;
464     }
465 }
466
Popular Tags