KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
54 import java.util.Arrays JavaDoc;
55 import java.util.List JavaDoc;
56 import java.util.Properties JavaDoc;
57
58 /**
59  * A descriptor describing the Context that the Component
60  * is passed to describe information about Runtime environment
61  * of Component. It contains information such as;
62  * <ul>
63  * <li>classname: the classname of the Context type if it
64  * differs from base Context class (ie BlockContext).</li>
65  * <li>entries: a list of entries contained in context</li>
66  * </ul>
67  *
68  * <p>Also associated with each Context is a set of arbitrary
69  * attributes that can be used to store extra information
70  * about Context requirements.</p>
71  *
72  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
73  * @version $Revision: 1.9 $ $Date: 2003/09/09 22:00:42 $
74  */

75 public class ContextDescriptor extends Descriptor
76 {
77     //---------------------------------------------------------
78
// static
79
//---------------------------------------------------------
80

81    /**
82     * The context entry key for accessing a component name.
83     */

84     public static final String JavaDoc NAME_KEY =
85             "urn:avalon:name";
86
87    /**
88     * The context entry key for accessing a component partition name.
89     */

90     public static final String JavaDoc PARTITION_KEY =
91             "urn:avalon:partition";
92
93    /**
94     * The context entry key for accessing a component home directory.
95     */

96     public static final String JavaDoc HOME_KEY =
97             "urn:avalon:home";
98
99    /**
100     * The context entry key for accessing a component temporary directory.
101     */

102     public static final String JavaDoc TEMP_KEY =
103             "urn:avalon:temp";
104
105    /**
106     * The context entry key for accessing a component classloader.
107     */

108     public static final String JavaDoc CLASSLOADER_KEY =
109             "urn:avalon:classloader";
110
111    /**
112     * Context attribute key used to declare a custom contextualization
113     * interface.
114     */

115     public static final String JavaDoc STRATEGY_KEY =
116             "urn:avalon:context.strategy";
117
118    /**
119     * Context interface classname.
120     */

121     public static final String JavaDoc AVALON_CONTEXT_CLASSNAME =
122             "org.apache.avalon.framework.context.Context";
123
124     //---------------------------------------------------------
125
// immutable state
126
//---------------------------------------------------------
127

128     private final String JavaDoc m_classname;
129
130     private final EntryDescriptor[] m_entries;
131
132     //---------------------------------------------------------
133
// constructors
134
//---------------------------------------------------------
135

136     /**
137      * Create a standard descriptor without attributes.
138      * @param entries the set of entries required within the context
139      */

140     public ContextDescriptor( final EntryDescriptor[] entries )
141     {
142         this( null, entries, null );
143     }
144
145     /**
146      * Create a descriptor without attributes.
147      * @param classname the classname of a castable interface
148      * @param entries the set of entries required within the context
149      */

150     public ContextDescriptor( final String JavaDoc classname,
151                               final EntryDescriptor[] entries )
152     {
153         this( classname, entries, null );
154     }
155
156     /**
157      * Create a descriptor.
158      * @param classname the classname of a castable interface
159      * @param entries the set of entries required within the context
160      * @param attributes supplimentary attributes associated with the context
161      * @exception NullPointerException if the entries argument is null
162      */

163     public ContextDescriptor( final String JavaDoc classname,
164                               final EntryDescriptor[] entries,
165                               final Properties JavaDoc attributes )
166             throws NullPointerException JavaDoc, IllegalArgumentException JavaDoc
167     {
168         super( attributes );
169
170         if ( null == entries )
171         {
172             throw new NullPointerException JavaDoc( "entries" );
173         }
174
175         if ( null == classname )
176         {
177             m_classname = AVALON_CONTEXT_CLASSNAME;
178         }
179         else
180         {
181             m_classname = classname;
182         }
183         m_entries = entries;
184     }
185
186     //---------------------------------------------------------
187
// implementation
188
//---------------------------------------------------------
189

190     /**
191      * Return the classname of the context
192      * object interface that the supplied context argument
193      * supports under a type-safe cast.
194      *
195      * @return the reference descriptor.
196      */

197     public String JavaDoc getContextInterfaceClassname()
198     {
199         return m_classname;
200     }
201
202     /**
203      * Return the local entries contained in the context.
204      *
205      * @return the entries contained in the context.
206      */

207     public EntryDescriptor[] getEntries()
208     {
209         return m_entries;
210     }
211
212     /**
213      * Return the entry with specified alias. If the entry
214      * does not declare an alias the method will return an
215      * entry with the matching key.
216      *
217      * @param alias the context entry key to lookup
218      * @return the entry with specified key.
219      */

220     public EntryDescriptor getEntry( final String JavaDoc alias )
221     {
222         if ( null == alias )
223         {
224             throw new NullPointerException JavaDoc( "alias" );
225         }
226
227         for ( int i = 0; i < m_entries.length; i++ )
228         {
229             final EntryDescriptor entry = m_entries[i];
230             if( entry.getAlias().equals( alias ) )
231             {
232                 return entry;
233             }
234         }
235
236         for ( int i = 0; i < m_entries.length; i++ )
237         {
238             final EntryDescriptor entry = m_entries[i];
239             if( entry.getKey().equals( alias ) )
240             {
241                 return entry;
242             }
243         }
244
245         return null;
246     }
247
248     /**
249      * Returns a set of entry descriptors resulting from a merge of the descriptors
250      * container in this descriptor with the supplied descriptors.
251      *
252      * @param entries the entries to merge
253      * @return the mergerged set of entries
254      * @exception IllegalArgumentException if a entry conflict occurs
255      */

256     public EntryDescriptor[] merge( EntryDescriptor[] entries )
257             throws IllegalArgumentException JavaDoc
258     {
259         for ( int i = 0; i < entries.length; i++ )
260         {
261             EntryDescriptor entry = entries[i];
262             final String JavaDoc key = entry.getKey();
263             EntryDescriptor local = getEntry( entry.getKey() );
264             if ( local != null )
265             {
266                 if ( !entry.getClassname().equals( local.getClassname() ) )
267                 {
268                     final String JavaDoc error =
269                             "Conflicting entry type for key: " + key;
270                     throw new IllegalArgumentException JavaDoc( error );
271                 }
272             }
273         }
274
275         return join( entries, getEntries() );
276     }
277
278     private EntryDescriptor[] join( EntryDescriptor[] primary, EntryDescriptor[] secondary )
279     {
280         List JavaDoc list = new ArrayList JavaDoc( primary.length + secondary.length );
281         list.addAll( Arrays.asList( primary ) );
282         list.addAll( Arrays.asList( secondary ) );
283         return (EntryDescriptor[]) list.toArray( new EntryDescriptor[0] );
284     }
285
286    /**
287     * Test is the supplied object is equal to this object.
288     * @return true if the object are equivalent
289     */

290     public boolean equals( Object JavaDoc other )
291     {
292         boolean isEqual = super.equals( other );
293         if( isEqual ) isEqual = other instanceof ContextDescriptor;
294         if( isEqual )
295         {
296             ContextDescriptor entity = (ContextDescriptor) other;
297             isEqual = isEqual && m_classname.equals( entity.m_classname );
298             for( int i=0; i<m_entries.length; i++ )
299             {
300                 isEqual = isEqual && m_entries[i].equals( entity.m_entries[i] );
301             }
302         }
303         return isEqual;
304     }
305
306    /**
307     * Return the hashcode for the object.
308     * @return the hashcode value
309     */

310     public int hashCode()
311     {
312         int hash = super.hashCode();
313         hash >>>= 7;
314         hash ^= m_classname.hashCode();
315         for( int i=0; i<m_entries.length; i++ )
316         {
317             hash >>>= 7;
318             hash ^= m_entries[i].hashCode();
319         }
320         return hash;
321     }
322 }
323
Popular Tags