KickJava   Java API By Example, From Geeks To Geeks.

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


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  * A descriptor that describes dependency information for
59  * a particular Component. This class contains information
60  * about;
61  * <ul>
62  * <li>role: the name component uses to look up dependency</li>
63  * <li>service: the class/interface that the dependency must provide</li>
64  * </ul>
65  *
66  * <p>Also associated with each dependency is a set of arbitrary
67  * attributes that can be used to store extra information
68  * about dependency. See {@link InfoDescriptor} for example
69  * of how to declare the container specific attributes.</p>
70  *
71  * <p>Possible uses for the attributes are to declare container
72  * specific constraints of component. For example a dependency on
73  * a Corba ORB may also require that the Corba ORB contain the
74  * TimeServer and PersistenceStateService at initialization. Or it
75  * may require that the componenet be multi-thread safe or that
76  * it is persistent etc. These are all container specific
77  * demands.</p>
78  *
79  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
80  * @version $Revision: 1.3 $ $Date: 2003/07/12 13:34:27 $
81  */

82 public final class DependencyDescriptor extends Descriptor
83 {
84
85     private static final Version DEFAULT_VERSION = Version.getVersion( "1.0" );
86
87
88     /**
89      * The name the component uses to lookup dependency.
90      */

91     private final String JavaDoc m_key;
92
93     /**
94      * The service class/interface that the dependency must provide.
95      */

96     private final ReferenceDescriptor m_service;
97
98     /**
99      * True if dependency is optional, false otherwise.
100      */

101     private final boolean m_optional;
102
103     /**
104      * Creation of a new dependency descriptor using the default 1.0 version
105      * @param role the role name that will be used by the type when looking up a service
106      * @param service the interface service
107      */

108     public DependencyDescriptor( final String JavaDoc role, String JavaDoc service )
109     {
110         this( role, service, DEFAULT_VERSION );
111     }
112
113     /**
114      * Creation of a new dependency descriptor.
115      * @param role the role name that will be used by the type when looking up a service
116      * @param service the version insterface service reference
117      */

118     public DependencyDescriptor( final String JavaDoc role, String JavaDoc service, Version version )
119     {
120         this( role, new ReferenceDescriptor( service, version ), false, null );
121     }
122
123     /**
124      * Creation of a new dependency descriptor.
125      * @param role the role name that will be used by the type when looking up a service
126      * @param service the version insterface service reference
127      */

128     public DependencyDescriptor( final String JavaDoc role,
129                                  final ReferenceDescriptor service )
130     {
131         this( role, service, false, null );
132     }
133
134     /**
135      * Creation of a new dependency descriptor.
136      * @param role the role name that will be used by the type when looking up a service
137      * @param service the version insterface service reference
138      * @param optional TRUE if this depedency is optional
139      * @param attributes a set of attributes to associate with the dependency
140      */

141     public DependencyDescriptor( final String JavaDoc role,
142                                  final ReferenceDescriptor service,
143                                  final boolean optional,
144                                  final Properties JavaDoc attributes )
145     {
146         super( attributes );
147
148         if ( null == role )
149         {
150             throw new NullPointerException JavaDoc( "role" );
151         }
152
153         if ( null == service )
154         {
155             throw new NullPointerException JavaDoc( "service" );
156         }
157
158         m_key = role;
159         m_service = service;
160         m_optional = optional;
161     }
162
163     /**
164      * Return the name the component uses to lookup the dependency.
165      *
166      * @return the name the component uses to lookup the dependency.
167      */

168     public String JavaDoc getKey()
169     {
170         return m_key;
171     }
172
173     /**
174      * Return the service class/interface descriptor that describes the
175      * dependency that the provider provides.
176      *
177      * @return a reference to service reference that describes the fulfillment
178      * obligations that must be met by a service provider.
179      * @deprecated use getReference()
180      */

181     public ReferenceDescriptor getService()
182     {
183         return m_service;
184     }
185
186     /**
187      * Return the service class/interface descriptor that describes the
188      * dependency must fulfilled by a provider.
189      *
190      * @return a reference to service reference that describes the fulfillment
191      * obligations that must be met by a service provider.
192      */

193     public ReferenceDescriptor getReference()
194     {
195         return m_service;
196     }
197
198     /**
199      * Return true if dependency is optional, false otherwise.
200      *
201      * @return true if dependency is optional, false otherwise.
202      */

203     public boolean isOptional()
204     {
205         return m_optional;
206     }
207
208     /**
209      * Return true if dependency is required, false otherwise.
210      *
211      * @return true if dependency is required, false otherwise.
212      */

213     public boolean isRequired()
214     {
215         return !isOptional();
216     }
217
218     public String JavaDoc toString()
219     {
220         return "[" + getKey() + "] " + getReference();
221     }
222
223     /**
224      * Compare this object with another for equality.
225      * @param other the object to compare this object with
226      * @return TRUE if the supplied object is a reference, service, or service
227      * descriptor that matches this objct in terms of classname and version
228      */

229     public boolean equals( Object JavaDoc other )
230     {
231         boolean isEqual = super.equals( other ) && other instanceof DependencyDescriptor;
232         if ( other instanceof DependencyDescriptor )
233         {
234             DependencyDescriptor dep = (DependencyDescriptor) other;
235
236             isEqual = isEqual && m_optional == dep.m_optional;
237             isEqual = isEqual && m_service.equals( dep.m_service );
238         }
239
240         return isEqual;
241     }
242
243    /**
244     * Return the hashcode for the object.
245     * @return the hashcode value
246     */

247     public int hashCode()
248     {
249         int hash = super.hashCode();
250         hash >>>= 13;
251         hash ^= m_service.hashCode();
252         hash >>>= ( m_optional ) ? 1 : 0;
253
254         return hash;
255     }
256
257 }
258
Popular Tags