KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > repository > Artifact


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 ;
19
20
21 import java.io.Serializable JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24
25 /**
26  * Defintion of a artifact that maintains a relative url
27  * to some nominally remote file together with a set of assigned
28  * properties.
29  *
30  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
31  * @version $Revision: 1.9 $
32  */

33 public class Artifact implements Serializable JavaDoc
34 {
35     // ------------------------------------------------------------------------
36
// static
37
// ------------------------------------------------------------------------
38

39     public static final String JavaDoc SEP = "/";
40
41     public static final String JavaDoc GROUP_KEY = "avalon.artifact.group";
42     public static final String JavaDoc NAME_KEY = "avalon.artifact.name";
43     public static final String JavaDoc VERSION_KEY = "avalon.artifact.version";
44     public static final String JavaDoc TYPE_KEY = "avalon.artifact.type";
45
46     public static Artifact createArtifact( String JavaDoc spec )
47     {
48         if ( null == spec ) throw new NullPointerException JavaDoc( "spec" );
49
50         if( spec.startsWith( "artifact:" ) )
51         {
52             try
53             {
54                 URL JavaDoc url = new URL JavaDoc( null, spec, new ArtifactHandler() );
55                 Artifact artifact = (Artifact) url.getContent();
56                 return artifact;
57             }
58             catch( IOException JavaDoc e )
59             {
60                 final String JavaDoc error =
61                   "Bad artifact url [" + spec + "] "
62                   + e.getMessage();
63                 throw new IllegalArgumentException JavaDoc( error );
64             }
65         }
66         else
67         {
68             String JavaDoc version = getVersion( spec );
69             String JavaDoc group = getGroup( spec );
70             String JavaDoc name = getName( spec );
71             return createArtifact( group, name, version );
72         }
73     }
74
75     public static Artifact createArtifact(
76       String JavaDoc group, String JavaDoc name, String JavaDoc version )
77     {
78         return createArtifact( group, name, version, "jar" );
79     }
80
81     public static Artifact createArtifact(
82       String JavaDoc group, String JavaDoc name, String JavaDoc version, String JavaDoc type )
83     {
84         if( group == null ) throw new NullPointerException JavaDoc( "group" );
85         if( name == null ) throw new NullPointerException JavaDoc( "name" );
86
87         return new Artifact( group, name, version, type );
88     }
89
90     // ------------------------------------------------------------------------
91
// state
92
// ------------------------------------------------------------------------
93

94    /**
95     * the artifact base path
96     */

97     private final String JavaDoc m_base;
98
99    /**
100     * the name of the the artifact relative to the base
101     */

102     private final String JavaDoc m_filename;
103
104    /**
105     * the computed path
106     */

107     private final String JavaDoc m_path;
108
109    /**
110     * The artifact group.
111     */

112     private final String JavaDoc m_group;
113
114    /**
115     * The artifact name.
116     */

117     private final String JavaDoc m_name;
118
119    /**
120     * The artifact type.
121     */

122     private final String JavaDoc m_type;
123
124    /**
125     * The artifact version.
126     */

127     private final String JavaDoc m_version;
128
129
130     // ------------------------------------------------------------------------
131
// constructors
132
// ------------------------------------------------------------------------
133

134     /**
135      * Creation of a new classified artifact.
136      *
137      * @param group the artifact group
138      * @param name the artifact name
139      * @param type the artifact type
140      * @param version the artifact version
141      */

142     private Artifact(
143       final String JavaDoc group, final String JavaDoc name,
144       final String JavaDoc version, final String JavaDoc type )
145     {
146         m_group = group;
147         m_name = name;
148         m_version = version;
149         m_type = type;
150
151         String JavaDoc base = createBase( group, type );
152         m_base = getCleanPath( base );
153
154         String JavaDoc filename = createFilename( name, version, type );
155         if( filename.indexOf( SEP ) > 0 )
156         {
157             final String JavaDoc error =
158               "Invalid name - illegal character '/' in filename: " + filename;
159             throw new IllegalArgumentException JavaDoc( error );
160         }
161
162         m_filename = filename;
163         m_path = m_base + SEP + m_filename;
164     }
165
166     /**
167      * Gets the relative artifact URL.
168      *
169      * @return the relative url
170      */

171     public String JavaDoc getURL()
172     {
173         return getURL( null );
174     }
175
176     /**
177      * Gets the URL to the artifact given a base URL for a
178      * remote repository.
179      *
180      * @param host the base repository URL
181      * @return the full URL to the artifact
182      */

183     public String JavaDoc getURL( final String JavaDoc host )
184     {
185         if( null == host )
186         {
187             return getURL( "" );
188         }
189         else
190         {
191             if( host.endsWith( SEP ) )
192             {
193                 return host + getPath();
194             }
195             else
196             {
197                 return host + SEP + getPath();
198             }
199         }
200     }
201
202     
203     // ------------------------------------------------------------------------
204
// accessors
205
// ------------------------------------------------------------------------
206

207    /**
208     * Return the group identifier for this artifact.
209     * @return the group
210     */

211     public String JavaDoc getGroup()
212     {
213         return m_group;
214     }
215
216    /**
217     * Return the name of this artifact.
218     * @return the name
219     */

220     public String JavaDoc getName()
221     {
222         return m_name;
223     }
224
225    /**
226     * Return the artifact type.
227     * @return the type
228     */

229     public String JavaDoc getType()
230     {
231         return m_type;
232     }
233
234    /**
235     * Return the artifact version.
236     * @return the version
237     */

238     public String JavaDoc getVersion()
239     {
240         return m_version;
241     }
242
243     /**
244      * Gets the artifact specification for this Artifact
245      * in the form [group]/[name]#[version].
246      *
247      * @return the artifact specification
248      */

249     public String JavaDoc getSpecification()
250     {
251         final String JavaDoc group = getGroup();
252         final String JavaDoc name = getName();
253
254         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc() ;
255         buffer.append( group );
256         buffer.append( SEP );
257         buffer.append( name );
258         
259         String JavaDoc version = getVersion();
260         if( version != null )
261         {
262             buffer.append( '#' ) ;
263             buffer.append( version ) ;
264         }
265
266         return buffer.toString() ;
267     }
268
269     /**
270      * Return the base path to the artifact. This is equivelent to the
271      * a logic directory path without a leading or trailing seperator.
272      *
273      * @return the base path.
274      */

275     public String JavaDoc getBase()
276     {
277         return m_base;
278     }
279     
280     /**
281      * Return the filename of the artifact.
282      * @return the name.
283      */

284     public String JavaDoc getFilename()
285     {
286         return m_filename ;
287     }
288     
289     /**
290      * Gets the artifact path. The value returned is equal to
291      * the base path, seperator and filename.
292      *
293      * @return the artifact path
294      */

295     public String JavaDoc getPath()
296     {
297         return m_path;
298     }
299
300    /**
301     * Return a stringified representation of the instance.
302     * @return the string representation
303     */

304     public String JavaDoc toString()
305     {
306         if( "block".equals( getType() ) )
307         {
308             return "block:" + getSpecification();
309         }
310         else if( "jar".equals( getType() ) )
311         {
312             return "artifact:" + getSpecification();
313         }
314         else
315         {
316             String JavaDoc path = "artifact:" + getGroup() + "/" + getName();
317             if( getVersion() != null )
318             {
319                 path = path + "#" + getVersion();
320                 if( getType() != null ) path = path + "&type=" + getType();
321                 return path;
322             }
323             else
324             {
325                 if( getType() != null ) path = path + "?type=" + getType();
326                 return path;
327             }
328         }
329     }
330
331     // ------------------------------------------------------------------------
332
// static private
333
// ------------------------------------------------------------------------
334

335     private static String JavaDoc createBase( String JavaDoc group, String JavaDoc type )
336     {
337         if( type == null ) return group;
338         return group + Artifact.SEP + type + "s";
339     }
340
341     private static String JavaDoc createFilename( String JavaDoc name, String JavaDoc version, String JavaDoc type )
342     {
343         if( name == null ) throw new NullPointerException JavaDoc( "name" );
344
345         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc( name );
346         if( version != null )
347         {
348             buffer.append( "-" );
349             buffer.append( version );
350         }
351         if( type != null )
352         {
353             buffer.append( "." );
354             buffer.append( type );
355         }
356         return buffer.toString();
357     }
358
359     private static String JavaDoc getGroup( String JavaDoc spec )
360     {
361         int semiColon = spec.indexOf( ';' ) ;
362         if ( -1 == semiColon )
363         {
364             int colon = spec.indexOf( ':' ) ;
365             if( -1 == colon ) return spec;
366             return spec.substring( 0, colon );
367         }
368         else
369         {
370             return getGroup( spec.substring( 0, semiColon-1 ) );
371         }
372     }
373
374     private static String JavaDoc getName( String JavaDoc spec )
375     {
376         int semiColon = spec.indexOf( ';' ) ;
377         if ( -1 == semiColon )
378         {
379             int colon = spec.indexOf( ':' ) ;
380             if( -1 == colon ) return spec;
381             return spec.substring( colon+1, spec.length() );
382         }
383         else
384         {
385             return getName( spec.substring( 0, semiColon ) );
386         }
387     }
388
389     private static String JavaDoc getVersion( String JavaDoc spec )
390     {
391         int semiColon = spec.indexOf( ';' ) ;
392         if ( -1 == semiColon )
393         {
394             return null;
395         }
396         else
397         {
398             return spec.substring( semiColon+1, spec.length() );
399         }
400     }
401
402    /**
403     * Remove leading and trailing seperators.
404     * @param the path value to clean
405     * @return the clean path
406     */

407     private String JavaDoc getCleanPath( final String JavaDoc path )
408     {
409         if( path.startsWith( SEP ) ) return getCleanPath( path.substring( 1, path.length() ) );
410         if( path.endsWith( SEP ) ) return getCleanPath( path.substring( 0, path.length() -1 ) );
411         return path;
412     }
413
414 }
415
416
Popular Tags