KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Properties JavaDoc;
54
55 import org.apache.avalon.framework.Version;
56
57 /**
58  * This class is used to provide explicit information to assembler
59  * and administrator about the Component. It includes information
60  * such as;
61  *
62  * <ul>
63  * <li>a symbolic name</li>
64  * <li>classname</li>
65  * <li>version</li>
66  * </ul>
67  *
68  * <p>The InfoDescriptor also includes an arbitrary set
69  * of attributes about component. Usually these are container
70  * specific attributes that can store arbitrary information.
71  * The attributes should be stored with keys based on package
72  * name of container.
73  *
74  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
75  * @version $Revision: 1.7 $ $Date: 2003/07/24 12:12:00 $
76  */

77 public final class InfoDescriptor extends Descriptor
78 {
79     //-------------------------------------------------------------------
80
// static
81
//-------------------------------------------------------------------
82

83     public static final String JavaDoc TRANSIENT = "transient";
84
85     public static final String JavaDoc SINGLETON = "singleton";
86
87     public static final String JavaDoc THREAD = "thread";
88
89     public static final String JavaDoc POOLED = "pooled";
90
91     //-------------------------------------------------------------------
92
// immutable state
93
//-------------------------------------------------------------------
94

95     /**
96      * The short name of the Component Type. Useful for displaying
97      * human readable strings describing the type in
98      * assembly tools or generators.
99      */

100     private final String JavaDoc m_name;
101
102     /**
103      * The implementation classname.
104      */

105     private final String JavaDoc m_classname;
106
107     /**
108      * The version of component that descriptor describes.
109      */

110     private final Version m_version;
111
112     /**
113      * The component lifestyle.
114      */

115     private final String JavaDoc m_lifestyle;
116
117     /**
118      * The component configuration schema.
119      */

120     private final String JavaDoc m_schema;
121
122     //-------------------------------------------------------------------
123
// constructor
124
//-------------------------------------------------------------------
125

126     /**
127      * Creation of a new component descriptor using a supplied name, key, version
128      * and attribute set.
129      *
130      * @param classname the implemetation classname
131      * @exception IllegalArgumentException if the implementation key is not a classname
132      */

133     public InfoDescriptor( final String JavaDoc classname )
134             throws IllegalArgumentException JavaDoc
135     {
136         this( null, classname, null, null, null, null );
137     }
138
139     /**
140      * Creation of a new info descriptor using a supplied name, key, version
141      * and attribute set.
142      *
143      * @param name the component name
144      * @param classname the implemetation classname
145      * @param version the implementation version
146      * @param attributes a set of attributes associated with the component type
147      * @exception IllegalArgumentException if the implementation key is not a classname
148      */

149     public InfoDescriptor( final String JavaDoc name,
150                            final String JavaDoc classname,
151                            final Version version,
152                            final String JavaDoc lifestyle,
153                            final String JavaDoc schema,
154                            final Properties JavaDoc attributes )
155             throws IllegalArgumentException JavaDoc
156     {
157         super( attributes );
158
159         if ( null == classname ) throw new NullPointerException JavaDoc( "classname" );
160
161         if ( classname.indexOf( "/" ) > -1 )
162         {
163             throw new IllegalArgumentException JavaDoc( "classname: " + classname );
164         }
165
166         m_classname = classname;
167         m_version = version;
168         m_schema = schema;
169
170         if ( lifestyle == null )
171         {
172             m_lifestyle = TRANSIENT;
173         }
174         else
175         {
176             validateLifestyle( lifestyle );
177             m_lifestyle = lifestyle;
178         }
179
180         if ( name != null )
181         {
182             m_name = name;
183         }
184         else
185         {
186             m_name = getClassName( classname );
187         }
188     }
189
190     private void validateLifestyle( String JavaDoc lifestyle ) throws IllegalArgumentException JavaDoc
191     {
192         if ( lifestyle.equals( TRANSIENT )
193                 || lifestyle.equals( SINGLETON )
194                 || lifestyle.equals( THREAD )
195                 || lifestyle.equals( POOLED ) )
196         {
197             return;
198         }
199         final String JavaDoc error = "Lifestyle policy not recognized: " + lifestyle;
200         throw new IllegalArgumentException JavaDoc( error );
201     }
202
203     private String JavaDoc getClassName( String JavaDoc classname )
204     {
205         int i = classname.lastIndexOf( "." );
206         if ( i == -1 )
207         {
208             return classname.toLowerCase();
209         }
210         else
211         {
212             return classname.substring( i + 1, classname.length() ).toLowerCase();
213         }
214     }
215
216     /**
217      * Return the symbolic name of component.
218      *
219      * @return the symbolic name of component.
220      */

221     public String JavaDoc getName()
222     {
223         return m_name;
224     }
225
226     /**
227      * Return the configuration schema.
228      *
229      * @return the schema declaration (possibly null)
230      */

231     public String JavaDoc getConfigurationSchema()
232     {
233         return m_schema;
234     }
235
236     /**
237      * Return the implementation class name for the component type.
238      *
239      * @return the implementation class name
240      */

241     public String JavaDoc getClassname()
242     {
243         return m_classname;
244     }
245
246     /**
247      * Return the version of component.
248      *
249      * @return the version of component.
250      */

251     public Version getVersion()
252     {
253         return m_version;
254     }
255
256     /**
257      * Return the component lifestyle.
258      *
259      * @return the lifestyle
260      */

261     public String JavaDoc getLifestyle()
262     {
263         return m_lifestyle;
264     }
265
266     /**
267      * Return a string representation of the info descriptor.
268      * @return the stringified type
269      */

270     public String JavaDoc toString()
271     {
272         return "[" + getName() + "] " + getClassname() + ":" + getVersion();
273     }
274
275    /**
276     * Test is the supplied object is equal to this object.
277     * @return true if the object are equivalent
278     */

279     public boolean equals(Object JavaDoc other)
280     {
281         boolean isEqual = super.equals(other) && other instanceof InfoDescriptor;
282
283         if (isEqual)
284         {
285             InfoDescriptor info = (InfoDescriptor)other;
286             isEqual = isEqual && m_classname.equals( info.m_classname );
287             isEqual = isEqual && m_name.equals( info.m_name );
288             isEqual = isEqual && m_lifestyle.equals( info.m_lifestyle );
289
290             if ( null == m_version )
291             {
292                 isEqual = isEqual && null == info.m_version;
293             }
294             else
295             {
296                 isEqual = isEqual && m_version.equals(info.m_version);
297             }
298         }
299
300         return isEqual;
301     }
302
303    /**
304     * Return the hashcode for the object.
305     * @return the hashcode value
306     */

307     public int hashCode()
308     {
309         int hash = super.hashCode();
310
311         hash >>>= 7;
312         hash ^= m_classname.hashCode();
313         hash >>>= 7;
314
315         if ( null != m_name )
316         {
317             hash >>>= 7;
318             hash ^= m_name.hashCode();
319         }
320
321         if ( null != m_lifestyle )
322         {
323             hash >>>= 7;
324             hash ^= m_lifestyle.hashCode();
325         }
326
327         if ( null != m_version )
328         {
329             hash >>>= 7;
330             hash ^= m_version.hashCode();
331         }
332
333         return hash;
334     }
335 }
336
Popular Tags