KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > repository > util > RepositoryUtils


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.util ;
19
20
21 import java.net.URL JavaDoc ;
22
23 import java.io.IOException JavaDoc ;
24 import java.io.InputStream JavaDoc ;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc ;
27 import java.io.FileNotFoundException JavaDoc ;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Properties JavaDoc ;
31 import java.util.Enumeration JavaDoc ;
32
33 import javax.naming.directory.Attribute JavaDoc;
34 import javax.naming.directory.Attributes JavaDoc ;
35 import javax.naming.directory.BasicAttribute JavaDoc;
36 import javax.naming.directory.BasicAttributes JavaDoc ;
37
38 import org.apache.avalon.repository.Artifact;
39 import org.apache.avalon.repository.RepositoryException;
40
41 /**
42  * Various static utility methods used throughout repository related programing
43  * interfaces.
44  *
45  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
46  * @version $Revision: 1.5 $
47  */

48 public class RepositoryUtils
49 {
50     /** meta extension tag for meta-data containing artifacts */
51     public static final String JavaDoc META = "meta" ;
52     
53     /**
54      * Transforms a Properties into a Attributes using a simple enumeration
55      * convention for property names which appends a numeric enumeration name
56      * component to the dotted property key. Note that changes to the
57      * Attributes object do not have any effect on the Properties object and
58      * vice versa. All values are copied.
59      *
60      * @param properties the properties to be transformed
61      * @return the Attributes representing the properties
62      */

63     public static Attributes JavaDoc getAsAttributes( Properties JavaDoc properties )
64     {
65         if( null == properties )
66           throw new NullPointerException JavaDoc( "properties" );
67
68         Attributes JavaDoc l_attrs = new BasicAttributes JavaDoc( false ) ;
69         Enumeration JavaDoc l_list = properties.propertyNames() ;
70         
71         while ( l_list.hasMoreElements() )
72         {
73             String JavaDoc l_key = ( String JavaDoc ) l_list.nextElement() ;
74             
75             if ( isEnumerated( l_key ) )
76             {
77                 String JavaDoc l_keyBase = getEnumeratedBase( l_key ) ;
78                 Attribute JavaDoc l_attr = l_attrs.get( l_keyBase ) ;
79                 
80                 if ( null == l_attr )
81                 {
82                     l_attr = new BasicAttribute JavaDoc( l_keyBase, false ) ;
83                 }
84                 
85                 l_attr.add( properties.getProperty( l_key ) ) ;
86                 l_attrs.put( l_attr ) ;
87             }
88             else
89             {
90                 l_attrs.put( l_key, properties.getProperty( l_key ) ) ;
91             }
92         }
93         
94         return l_attrs ;
95     }
96
97     /**
98      * Gets the Attribues in a remote artifact.
99      *
100      * @param repositories the reprositories to search against
101      * @param artifact the artifact to load meta data from
102      * @return the meta data as attributes
103      * @throws RepositoryException if there is execution failure
104      */

105     public static Attributes JavaDoc getAttributes(
106       String JavaDoc [] repositories, Artifact artifact )
107       throws RepositoryException
108     {
109         return getAsAttributes( getProperties( repositories, artifact ) ) ;
110     }
111     
112     /**
113      * Gets the Attribues from the cache.
114      *
115      * @param cache the reprository cache
116      * @param artifact the artifact to load meta data from
117      * @return the meta data as attributes
118      * @throws RepositoryException if there is execution failure
119      */

120     public static Attributes JavaDoc getAttributes(
121       File JavaDoc cache, Artifact artifact )
122       throws RepositoryException
123     {
124         return getAsAttributes( getProperties( cache, artifact ) ) ;
125     }
126
127     /**
128      * Gets the Properties in the local cache.
129      *
130      * @param cache the local cache
131      * @param artifact the artifact to load meta data from
132      * @return the loaded properties
133      * @throws RepositoryException if there is any problem loading the
134      * properties
135      */

136     public static Properties JavaDoc getProperties(
137       File JavaDoc cache, Artifact artifact )
138       throws RepositoryException
139     {
140         File JavaDoc local = new File JavaDoc( cache, artifact.getPath() + "." + META );
141         if( !local.exists() )
142         {
143             final String JavaDoc error = "Cannot load metadata due to missing resurce.";
144             Throwable JavaDoc cause = new FileNotFoundException JavaDoc( local.toString() );
145             throw new RepositoryException( error, cause );
146         }
147
148         try
149         {
150             Properties JavaDoc properties = new Properties JavaDoc();
151             InputStream JavaDoc input = new FileInputStream JavaDoc( local );
152             properties.load( input );
153             return properties;
154         }
155         catch( Throwable JavaDoc e )
156         {
157             final String JavaDoc error =
158               "Unexpected error while attempting to load properties from local meta: "
159               + local.toString();
160             throw new RepositoryException( error, e );
161         }
162     }
163
164     /**
165      * Gets the Properties in a remote properties file.
166      *
167      * @param repositories the reprositories to search against
168      * @param artifact the artifact to load meta data from
169      * @return the loaded properties
170      * @throws RepositoryException if there is any problem loading the
171      * properties
172      */

173     public static Properties JavaDoc getProperties(
174       String JavaDoc [] repositories, Artifact artifact )
175       throws RepositoryException
176     {
177         if( null == repositories )
178           throw new NullPointerException JavaDoc( "repositories" );
179         if( null == artifact )
180           throw new NullPointerException JavaDoc( "artifact" );
181
182         Throwable JavaDoc l_throwable = null ;
183         Properties JavaDoc l_props = null ;
184
185         for( int ii = 0; ii < repositories.length; ii++ )
186         {
187             StringBuffer JavaDoc l_buf = new StringBuffer JavaDoc() ;
188             l_buf.append( artifact.getURL( repositories[ii] ) ) ;
189             l_buf.append( "." ) ;
190             l_buf.append( META ) ;
191
192             try
193             {
194                 URL JavaDoc l_url = new URL JavaDoc( l_buf.toString() ) ;
195                 l_props = getProperties( l_url ) ;
196                 return l_props ;
197             }
198             catch ( Throwable JavaDoc e )
199             {
200                 l_throwable = e ;
201             }
202         }
203
204         StringBuffer JavaDoc l_repos = new StringBuffer JavaDoc() ;
205         for ( int ii = 0; ii < repositories.length; ii++ )
206         {
207             l_repos.append( repositories[ii] ).append( ',' ) ;
208         }
209
210         throw new RepositoryException(
211             "None of the repositories [" + l_repos.toString()
212             + "] contained the metadata properties for "
213             + artifact, l_throwable ) ;
214     }
215     
216     /**
217      * Gets the Properties in a remote properties file.
218      *
219      * @param url the url to the properties file
220      * @return the loaded properties for the file
221      * @throws IOException indicating a IO error during property loading
222      */

223     public static Properties JavaDoc getProperties( URL JavaDoc url ) throws IOException JavaDoc
224     {
225         InputStream JavaDoc l_in = null ;
226         Properties JavaDoc l_props = new Properties JavaDoc() ;
227         l_in = url.openStream() ;
228         l_props.load( l_in ) ;
229         
230         if ( l_in != null )
231         {
232             l_in.close() ;
233         }
234         return l_props ;
235     }
236     
237     /**
238      * Detects whether or not a property key is of the multivalued enumeration
239      * kind. A multivalued key simply enumerates values by appending a '.' and
240      * a number after the dot: i.e. artifact.dependency.2 and artifact.alias.23
241      * et. cetera.
242      *
243      * @param key the property name or key
244      * @return true if the property conforms to the enumerated property
245      * convention, false otherwise
246      */

247     public static boolean isEnumerated( String JavaDoc key )
248     {
249         int l_lastDot = key.lastIndexOf( '.' ) ;
250         String JavaDoc l_lastComponent = null ;
251         
252         if ( -1 == l_lastDot )
253         {
254             return false ;
255         }
256     
257         l_lastComponent = key.substring( l_lastDot + 1 ) ;
258         
259         // names like .123 are not really considered enumerated without a base
260
if ( key.equals( key.substring( l_lastDot ) ) )
261         {
262             return false ;
263         }
264         
265         try
266         {
267             Integer.parseInt( l_lastComponent ) ;
268         }
269         catch ( NumberFormatException JavaDoc e )
270         {
271             return false ;
272         }
273         
274         return true ;
275     }
276
277     
278     /**
279      * Gets the key base of an enumerated property using the multivalued
280      * property key naming convention.
281      *
282      * @param key the enumerated key whose last name component is a number
283      * @return the base name of the enumerated property
284      */

285     public static String JavaDoc getEnumeratedBase( String JavaDoc key )
286     {
287         if ( null == key )
288         {
289             return null ;
290         }
291         
292         if ( ! isEnumerated( key ) )
293         {
294             return key ;
295         }
296         
297         int l_lastDot = key.lastIndexOf( '.' ) ;
298         String JavaDoc l_base = null ;
299         
300         if ( -1 == l_lastDot )
301         {
302             return key ;
303         }
304     
305         return key.substring( 0, l_lastDot ) ;
306     }
307
308     public static String JavaDoc [] getDelimited( char a_delim, String JavaDoc a_substrate )
309     {
310         int l_start = 0, l_end = 0 ;
311         ArrayList JavaDoc l_list = new ArrayList JavaDoc() ;
312         
313         if ( null == a_substrate || a_substrate.equals( "" ) )
314         {
315             return null ;
316         }
317
318         while( l_end < a_substrate.length() )
319         {
320             l_end = a_substrate.indexOf( ',', l_start ) ;
321             
322             if ( -1 == l_end )
323             {
324                 l_end = a_substrate.length() ;
325                 l_list.add( a_substrate.substring( l_start, l_end ) ) ;
326                 break ;
327             }
328
329             l_list.add( a_substrate.substring( l_start, l_end ) ) ;
330             l_start = l_end + 1 ;
331         }
332         
333         return ( String JavaDoc [] ) l_list.toArray( new String JavaDoc[0] ) ;
334     }
335
336    /**
337     * Convert a set of host path statements to formal urls.
338     * @param hosts the set of host names
339     * @return the equivalent URL array
340     */

341     public static URL JavaDoc[] convertToURLs( String JavaDoc[] hosts )
342     {
343         ArrayList JavaDoc list = new ArrayList JavaDoc();
344         for( int i=0; i<hosts.length; i++ )
345         {
346             URL JavaDoc url = convertToURL( hosts[i] );
347             if( url != null ) list.add( url );
348         }
349         return (URL JavaDoc[]) list.toArray( new URL JavaDoc[0] );
350     }
351
352    /**
353     * Convert a path to a url.
354     * @param host the host address
355     * @return the equivalent url
356     * @exception IllegalArgumentException if the path cannot
357     * be converted to a url
358     */

359     public static URL JavaDoc convertToURL( String JavaDoc host )
360       throws IllegalArgumentException JavaDoc
361     {
362         try
363         {
364             return new URL JavaDoc( host );
365         }
366         catch( Throwable JavaDoc e )
367         {
368             final String JavaDoc error =
369              "Unable to convert a supplied host spec to a url: "
370              + host;
371             throw new IllegalArgumentException JavaDoc( error );
372         }
373     }
374
375    /**
376     * For a given set of host paths, returns an equivalent set
377     * ensuring that the path ends with the "/" character.
378     * @param hosts the set of host path names
379     * @return the equivalent host path names
380     */

381     public static String JavaDoc[] getCleanPaths( String JavaDoc[] hosts )
382     {
383         String JavaDoc[] paths = new String JavaDoc[ hosts.length ];
384         for( int i=0; i<hosts.length; i++ )
385         {
386             String JavaDoc path = hosts[i];
387             if( !path.endsWith( "/" ) )
388             {
389                 paths[i] = path + "/";
390             }
391             else
392             {
393                 paths[i] = path;
394             }
395         }
396         return paths;
397     }
398
399 }
400
Popular Tags