1 17 18 package org.apache.avalon.repository.util ; 19 20 21 import java.net.URL ; 22 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileNotFoundException ; 28 29 import java.util.ArrayList ; 30 import java.util.Properties ; 31 import java.util.Enumeration ; 32 33 import javax.naming.directory.Attribute ; 34 import javax.naming.directory.Attributes ; 35 import javax.naming.directory.BasicAttribute ; 36 import javax.naming.directory.BasicAttributes ; 37 38 import org.apache.avalon.repository.Artifact; 39 import org.apache.avalon.repository.RepositoryException; 40 41 48 public class RepositoryUtils 49 { 50 51 public static final String META = "meta" ; 52 53 63 public static Attributes getAsAttributes( Properties properties ) 64 { 65 if( null == properties ) 66 throw new NullPointerException ( "properties" ); 67 68 Attributes l_attrs = new BasicAttributes ( false ) ; 69 Enumeration l_list = properties.propertyNames() ; 70 71 while ( l_list.hasMoreElements() ) 72 { 73 String l_key = ( String ) l_list.nextElement() ; 74 75 if ( isEnumerated( l_key ) ) 76 { 77 String l_keyBase = getEnumeratedBase( l_key ) ; 78 Attribute l_attr = l_attrs.get( l_keyBase ) ; 79 80 if ( null == l_attr ) 81 { 82 l_attr = new BasicAttribute ( 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 105 public static Attributes getAttributes( 106 String [] repositories, Artifact artifact ) 107 throws RepositoryException 108 { 109 return getAsAttributes( getProperties( repositories, artifact ) ) ; 110 } 111 112 120 public static Attributes getAttributes( 121 File cache, Artifact artifact ) 122 throws RepositoryException 123 { 124 return getAsAttributes( getProperties( cache, artifact ) ) ; 125 } 126 127 136 public static Properties getProperties( 137 File cache, Artifact artifact ) 138 throws RepositoryException 139 { 140 File local = new File ( cache, artifact.getPath() + "." + META ); 141 if( !local.exists() ) 142 { 143 final String error = "Cannot load metadata due to missing resurce."; 144 Throwable cause = new FileNotFoundException ( local.toString() ); 145 throw new RepositoryException( error, cause ); 146 } 147 148 try 149 { 150 Properties properties = new Properties (); 151 InputStream input = new FileInputStream ( local ); 152 properties.load( input ); 153 return properties; 154 } 155 catch( Throwable e ) 156 { 157 final String 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 173 public static Properties getProperties( 174 String [] repositories, Artifact artifact ) 175 throws RepositoryException 176 { 177 if( null == repositories ) 178 throw new NullPointerException ( "repositories" ); 179 if( null == artifact ) 180 throw new NullPointerException ( "artifact" ); 181 182 Throwable l_throwable = null ; 183 Properties l_props = null ; 184 185 for( int ii = 0; ii < repositories.length; ii++ ) 186 { 187 StringBuffer l_buf = new StringBuffer () ; 188 l_buf.append( artifact.getURL( repositories[ii] ) ) ; 189 l_buf.append( "." ) ; 190 l_buf.append( META ) ; 191 192 try 193 { 194 URL l_url = new URL ( l_buf.toString() ) ; 195 l_props = getProperties( l_url ) ; 196 return l_props ; 197 } 198 catch ( Throwable e ) 199 { 200 l_throwable = e ; 201 } 202 } 203 204 StringBuffer l_repos = new StringBuffer () ; 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 223 public static Properties getProperties( URL url ) throws IOException 224 { 225 InputStream l_in = null ; 226 Properties l_props = new Properties () ; 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 247 public static boolean isEnumerated( String key ) 248 { 249 int l_lastDot = key.lastIndexOf( '.' ) ; 250 String 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 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 e ) 270 { 271 return false ; 272 } 273 274 return true ; 275 } 276 277 278 285 public static String getEnumeratedBase( String 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 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 [] getDelimited( char a_delim, String a_substrate ) 309 { 310 int l_start = 0, l_end = 0 ; 311 ArrayList l_list = new ArrayList () ; 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 [] ) l_list.toArray( new String [0] ) ; 334 } 335 336 341 public static URL [] convertToURLs( String [] hosts ) 342 { 343 ArrayList list = new ArrayList (); 344 for( int i=0; i<hosts.length; i++ ) 345 { 346 URL url = convertToURL( hosts[i] ); 347 if( url != null ) list.add( url ); 348 } 349 return (URL []) list.toArray( new URL [0] ); 350 } 351 352 359 public static URL convertToURL( String host ) 360 throws IllegalArgumentException 361 { 362 try 363 { 364 return new URL ( host ); 365 } 366 catch( Throwable e ) 367 { 368 final String error = 369 "Unable to convert a supplied host spec to a url: " 370 + host; 371 throw new IllegalArgumentException ( error ); 372 } 373 } 374 375 381 public static String [] getCleanPaths( String [] hosts ) 382 { 383 String [] paths = new String [ hosts.length ]; 384 for( int i=0; i<hosts.length; i++ ) 385 { 386 String 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 |