KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > repository > meta > ArtifactDescriptor


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.repository.meta;
19
20 import java.io.Serializable JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
22 import javax.naming.directory.Attributes JavaDoc;
23 import javax.naming.directory.Attribute JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25
26 import org.apache.avalon.repository.Artifact;
27
28 /**
29  * An abstract descriptor holds attributes about an artifact.
30  *
31  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
32  * @version $Revision: 1.5 $
33  */

34 public class ArtifactDescriptor implements Serializable JavaDoc
35 {
36     //-----------------------------------------------------------
37
// static
38
//-----------------------------------------------------------
39

40     public static final String JavaDoc DOMAIN_KEY = "meta.domain";
41     public static final String JavaDoc VERSION_KEY = "meta.version";
42     public static final String JavaDoc BUILD_KEY =
43       "avalon.artifact.signature";
44
45     //-----------------------------------------------------------
46
// immutable state
47
//-----------------------------------------------------------
48

49     private final String JavaDoc c_domain;
50     private final String JavaDoc c_version;
51
52     private final String JavaDoc m_group;
53     private final String JavaDoc m_name;
54     private final String JavaDoc m_version;
55     private final String JavaDoc m_build;
56
57     private final Artifact m_artifact;
58
59     //-----------------------------------------------------------
60
// constructor
61
//-----------------------------------------------------------
62

63     /**
64      * Creates a new Meta descriptor.
65      *
66      * @param attributes the metadata attributes
67      * @exception NullPointerException if the supplied
68      * attributes argument is null
69      * @exception MetaException if an attribute is inconsitent
70      */

71     public ArtifactDescriptor( Attributes JavaDoc attributes )
72       throws MetaException
73     {
74         if( null == attributes )
75           throw new NullPointerException JavaDoc( "attributes" );
76
77         try
78         {
79             c_domain = getValue( attributes, DOMAIN_KEY );
80             if( null == c_domain )
81             {
82                 final String JavaDoc error =
83                   "Missing attribute: " + DOMAIN_KEY;
84                 throw new MetaException( error );
85             }
86
87             c_version = getValue( attributes, VERSION_KEY );
88             if( null == c_version )
89             {
90                 final String JavaDoc error =
91                   "Missing attribute: " + VERSION_KEY;
92                 throw new MetaException( error );
93             }
94
95             m_group = getAttribute( attributes, Artifact.GROUP_KEY, "" );
96             m_name = getAttribute( attributes, Artifact.NAME_KEY, "" );
97             m_version = getAttribute( attributes, Artifact.VERSION_KEY, "" );
98             m_build = getAttribute( attributes, BUILD_KEY, "" );
99
100             m_artifact = Artifact.createArtifact( m_group, m_name, m_version );
101
102         }
103         catch( NamingException JavaDoc e )
104         {
105             final String JavaDoc error =
106               "Unexpected naming exception during metadata creation.";
107             throw new MetaException( error, e );
108         }
109         catch( NoSuchElementException JavaDoc e )
110         {
111             final String JavaDoc error =
112               "Unexpected exception during metadata creation.";
113             throw new MetaException( error, e );
114         }
115     }
116
117     //-----------------------------------------------------------
118
// public
119
//-----------------------------------------------------------
120

121    /**
122     * Return the artifact reference.
123     * @return the artifact
124     */

125     public Artifact getArtifact()
126     {
127         return m_artifact;
128     }
129
130    /**
131     * Return the meta data domain value.
132     * @return the domain
133     */

134     public String JavaDoc getDomain()
135     {
136         return c_domain;
137     }
138
139    /**
140     * Return the meta data version
141     * @return the version
142     */

143     public String JavaDoc getVersion()
144     {
145         return c_version;
146     }
147
148    /**
149     * Return the build identifier
150     * @return the identifier
151     */

152     public String JavaDoc getBuild()
153     {
154         return m_build;
155     }
156
157    /**
158     * Test is the supplied object is equal to this object.
159     * @param other the obhject to compare this object with
160     * @return true if the objects are equivalent
161     */

162     public boolean equals( Object JavaDoc other )
163     {
164         boolean isEqual = other instanceof ArtifactDescriptor;
165         if ( isEqual )
166         {
167             ArtifactDescriptor meta = (ArtifactDescriptor) other;
168             isEqual = isEqual && c_domain.equals( meta.c_domain );
169             isEqual = isEqual && c_version.equals( meta.c_version );
170             isEqual = isEqual && m_group.equals( meta.m_version );
171             isEqual = isEqual && m_name.equals( meta.m_name );
172             isEqual = isEqual && m_version.equals( meta.m_version );
173         }
174         return isEqual;
175     }
176
177    /**
178     * Return the hashcode for the object.
179     * @return the hashcode value
180     */

181     public int hashCode()
182     {
183         int hash = 1;
184         hash >>>= 13;
185         hash ^= c_domain.hashCode();
186         hash >>>= 13;
187         hash ^= c_version.hashCode();
188         hash >>>= 13;
189         hash ^= m_group.hashCode();
190         hash >>>= 13;
191         hash ^= m_version.hashCode();
192         hash >>>= 13;
193         hash ^= m_build.hashCode();
194         hash >>>= 13;
195         return hash;
196     }
197
198    /**
199     * Return a stringified representation of the instance.
200     * @return the string representation
201     */

202     public String JavaDoc toString()
203     {
204         return "[artifact: " + getDomain()
205           + "::" + getVersion() + "]";
206     }
207
208     //-----------------------------------------------------------
209
// utilities
210
//-----------------------------------------------------------
211

212     private String JavaDoc getAttribute( Attributes JavaDoc attributes, String JavaDoc key, String JavaDoc def )
213     {
214         try
215         {
216             return getValue( attributes, key );
217         }
218         catch( Throwable JavaDoc e )
219         {
220             return def;
221         }
222     }
223
224     protected String JavaDoc getValue( Attributes JavaDoc attributes, String JavaDoc key )
225       throws NamingException JavaDoc, NoSuchElementException JavaDoc
226     {
227         Attribute JavaDoc attribute = attributes.get( key );
228         if( null == attribute ) return null;
229         Object JavaDoc object = attribute.get();
230         if( null == object ) return null;
231         return object.toString();
232     }
233 }
234
Popular Tags