1 17 18 package org.apache.avalon.repository ; 19 20 21 import java.io.Serializable ; 22 import java.io.IOException ; 23 import java.net.URL ; 24 25 33 public class Artifact implements Serializable 34 { 35 39 public static final String SEP = "/"; 40 41 public static final String GROUP_KEY = "avalon.artifact.group"; 42 public static final String NAME_KEY = "avalon.artifact.name"; 43 public static final String VERSION_KEY = "avalon.artifact.version"; 44 public static final String TYPE_KEY = "avalon.artifact.type"; 45 46 public static Artifact createArtifact( String spec ) 47 { 48 if ( null == spec ) throw new NullPointerException ( "spec" ); 49 50 if( spec.startsWith( "artifact:" ) ) 51 { 52 try 53 { 54 URL url = new URL ( null, spec, new ArtifactHandler() ); 55 Artifact artifact = (Artifact) url.getContent(); 56 return artifact; 57 } 58 catch( IOException e ) 59 { 60 final String error = 61 "Bad artifact url [" + spec + "] " 62 + e.getMessage(); 63 throw new IllegalArgumentException ( error ); 64 } 65 } 66 else 67 { 68 String version = getVersion( spec ); 69 String group = getGroup( spec ); 70 String name = getName( spec ); 71 return createArtifact( group, name, version ); 72 } 73 } 74 75 public static Artifact createArtifact( 76 String group, String name, String version ) 77 { 78 return createArtifact( group, name, version, "jar" ); 79 } 80 81 public static Artifact createArtifact( 82 String group, String name, String version, String type ) 83 { 84 if( group == null ) throw new NullPointerException ( "group" ); 85 if( name == null ) throw new NullPointerException ( "name" ); 86 87 return new Artifact( group, name, version, type ); 88 } 89 90 94 97 private final String m_base; 98 99 102 private final String m_filename; 103 104 107 private final String m_path; 108 109 112 private final String m_group; 113 114 117 private final String m_name; 118 119 122 private final String m_type; 123 124 127 private final String m_version; 128 129 130 134 142 private Artifact( 143 final String group, final String name, 144 final String version, final String type ) 145 { 146 m_group = group; 147 m_name = name; 148 m_version = version; 149 m_type = type; 150 151 String base = createBase( group, type ); 152 m_base = getCleanPath( base ); 153 154 String filename = createFilename( name, version, type ); 155 if( filename.indexOf( SEP ) > 0 ) 156 { 157 final String error = 158 "Invalid name - illegal character '/' in filename: " + filename; 159 throw new IllegalArgumentException ( error ); 160 } 161 162 m_filename = filename; 163 m_path = m_base + SEP + m_filename; 164 } 165 166 171 public String getURL() 172 { 173 return getURL( null ); 174 } 175 176 183 public String getURL( final String 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 207 211 public String getGroup() 212 { 213 return m_group; 214 } 215 216 220 public String getName() 221 { 222 return m_name; 223 } 224 225 229 public String getType() 230 { 231 return m_type; 232 } 233 234 238 public String getVersion() 239 { 240 return m_version; 241 } 242 243 249 public String getSpecification() 250 { 251 final String group = getGroup(); 252 final String name = getName(); 253 254 StringBuffer buffer = new StringBuffer () ; 255 buffer.append( group ); 256 buffer.append( SEP ); 257 buffer.append( name ); 258 259 String version = getVersion(); 260 if( version != null ) 261 { 262 buffer.append( '#' ) ; 263 buffer.append( version ) ; 264 } 265 266 return buffer.toString() ; 267 } 268 269 275 public String getBase() 276 { 277 return m_base; 278 } 279 280 284 public String getFilename() 285 { 286 return m_filename ; 287 } 288 289 295 public String getPath() 296 { 297 return m_path; 298 } 299 300 304 public String 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 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 335 private static String createBase( String group, String type ) 336 { 337 if( type == null ) return group; 338 return group + Artifact.SEP + type + "s"; 339 } 340 341 private static String createFilename( String name, String version, String type ) 342 { 343 if( name == null ) throw new NullPointerException ( "name" ); 344 345 StringBuffer buffer = new StringBuffer ( 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 getGroup( String 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 getName( String 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 getVersion( String 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 407 private String getCleanPath( final String 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
|