KickJava   Java API By Example, From Geeks To Geeks.

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


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.Version;
56
57 /**
58  * This reference defines the type of interface required
59  * by a component. The type corresponds to the class name of the
60  * interface implemented by component. Associated with each
61  * classname is a version object so that different versions of same
62  * interface can be represented.
63  *
64  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
65  * @version $Revision: 1.9 $ $Date: 2003/07/12 21:12:06 $
66  */

67 public final class ReferenceDescriptor
68         implements Serializable JavaDoc
69 {
70     /**
71      * The name of service class.
72      */

73     private final String JavaDoc m_classname;
74
75     /**
76      * The version of service class.
77      */

78     private final Version m_version;
79
80     /**
81      * Construct a service with specified type.
82      *
83      * @param type the service type spec
84      * @exception NullPointerException if the classname is null
85      */

86     public ReferenceDescriptor( final String JavaDoc type ) throws NullPointerException JavaDoc
87     {
88         this( parseClassname(type), parseVersion(type) );
89     }
90
91     /**
92      * Construct a service with specified name, version and attributes.
93      *
94      * @param classname the name of the service
95      * @param version the version of service
96      * @exception NullPointerException if the classname or version is null
97      * @exception IllegalArgumentException if the classname string is invalid
98      */

99     public ReferenceDescriptor( final String JavaDoc classname,
100                                 final Version version ) throws NullPointerException JavaDoc
101     {
102         if ( null == classname )
103         {
104             throw new NullPointerException JavaDoc( "classname" );
105         }
106         if( classname.equals( "" ) )
107         {
108             throw new IllegalArgumentException JavaDoc( "classname" );
109         }
110         if( classname.indexOf( "/" ) > -1 )
111         {
112             throw new IllegalArgumentException JavaDoc( "classname" );
113         }
114
115         m_classname = classname;
116
117         if ( null == version )
118         {
119             m_version = Version.getVersion( "" );
120         }
121         else
122         {
123             m_version = version;
124         }
125     }
126
127     /**
128      * Return classname of interface this reference refers to.
129      *
130      * @return the classname of the Service
131      */

132     public String JavaDoc getClassname()
133     {
134         return m_classname;
135     }
136
137     /**
138      * Return the version of interface.
139      *
140      * @return the version of interface
141      */

142     public Version getVersion()
143     {
144         return m_version;
145     }
146
147     /**
148      * Determine if specified service will match this service.
149      * To match a service has to have same name and must comply with version.
150      *
151      * @param other the other ServiceInfo
152      * @return true if matches, false otherwise
153      */

154     public boolean matches( final ReferenceDescriptor other )
155     {
156         return m_classname.equals( other.m_classname )
157                 && other.getVersion().complies( getVersion() );
158     }
159
160     /**
161      * Convert to a string of format name:version
162      *
163      * @return string describing service
164      */

165     public String JavaDoc toString()
166     {
167         return getClassname() + ":" + getVersion();
168     }
169
170     /**
171      * Compare this object with another for equality.
172      * @param other the object to compare this object with
173      * @return TRUE if the supplied object is a reference, service, or service
174      * descriptor that matches this objct in terms of classname and version
175      */

176     public boolean equals( Object JavaDoc other )
177     {
178         boolean match = false;
179
180         //
181
// TODO: check validity of the following - this is
182
// assuming the equality is equivalent to compliance
183
// which is not true
184
//
185

186         if ( other instanceof ReferenceDescriptor )
187         {
188             match = ( (ReferenceDescriptor) other ).matches( this );
189         }
190         else if ( other instanceof Service )
191         {
192             match = ( (Service) other ).matches( this );
193         }
194         else if ( other instanceof ServiceDescriptor )
195         {
196             match = ( (ServiceDescriptor) other ).getReference().matches( this );
197         }
198
199         return match;
200     }
201
202     /**
203      * Returns the cashcode.
204      * @return the hascode value
205      */

206     public int hashCode()
207     {
208         return getClassname().hashCode() ^ getVersion().hashCode();
209     }
210
211     private static final String JavaDoc parseClassname( final String JavaDoc type )
212     {
213         if( type == null ) throw new NullPointerException JavaDoc( "type" );
214
215         int index = type.indexOf( ":" );
216         if( index == -1 )
217         {
218             return type;
219         }
220         else
221         {
222             return type.substring( 0, index );
223         }
224     }
225
226     private static final Version parseVersion( final String JavaDoc type )
227     {
228         if( type.indexOf( ":" ) == -1 )
229         {
230             return Version.getVersion( "" );
231         }
232         else
233         {
234             return Version.getVersion( type.substring( getColonIndex( type ) + 1) );
235         }
236     }
237
238     private static final int getColonIndex( final String JavaDoc type )
239     {
240         if ( null == type ) throw new NullPointerException JavaDoc( "type" );
241         return Math.min( type.length(), Math.max( 0, type.indexOf( ":" ) ) );
242     }
243 }
244
Popular Tags