1 17 18 package org.apache.avalon.repository.main; 19 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.ArrayList ; 25 import java.util.Properties ; 26 import java.util.StringTokenizer ; 27 28 import org.apache.avalon.repository.Artifact; 29 import org.apache.avalon.repository.RepositoryRuntimeException; 30 import org.apache.avalon.repository.provider.InitialContext; 31 import org.apache.avalon.repository.provider.InitialContextFactory; 32 33 import org.apache.avalon.util.defaults.DefaultsBuilder; 34 import org.apache.avalon.util.defaults.Defaults; 35 36 37 54 public class DefaultInitialContextFactory implements InitialContextFactory 55 { 56 60 65 private static final String AVALON_PROPERTIES = "avalon.properties"; 66 67 71 private final String m_key; 72 73 private final File m_work; 74 75 private final DefaultsBuilder m_defaults; 76 77 private final Properties m_properties; 78 79 83 private File m_cache; 84 85 private Artifact m_artifact; 86 87 private ClassLoader m_classloader; 88 89 private String [] m_hosts; 90 91 private String m_proxyHost; 92 93 private int m_proxyPort; 94 95 private String m_proxyUsername; 96 97 private String m_proxyPassword; 98 99 private boolean m_online = true; 100 101 private Artifact[] m_registry; 102 103 107 122 public DefaultInitialContextFactory( String key ) 123 throws IOException 124 { 125 this( key, new File ( System.getProperty( "user.dir" ) ) ); 126 } 127 128 161 public DefaultInitialContextFactory( String key, File work ) 162 throws IOException 163 { 164 if( null == key ) throw new NullPointerException ( "key" ); 165 if( null == work ) throw new NullPointerException ( "work" ); 166 167 m_key = key; 168 m_work = work; 169 170 m_defaults = new DefaultsBuilder( key, work ); 171 172 177 m_properties = 178 m_defaults.getConsolidatedProperties( 179 getDefaultProperties(), KEYS ); 180 Defaults.macroExpand( 181 m_properties, 182 new Properties []{ m_properties } ); 183 184 189 String spec = m_properties.getProperty( 190 InitialContext.IMPLEMENTATION_KEY ); 191 if( null != spec ) 192 { 193 m_artifact = Artifact.createArtifact( spec ); 194 } 195 else 196 { 197 final String error = 198 "Required implementation key [" 199 + InitialContext.IMPLEMENTATION_KEY 200 + "] not found."; 201 throw new IllegalStateException ( error ); 202 } 203 } 204 205 209 213 public void setFactoryArtifacts( Artifact[] artifacts ) 214 { 215 m_registry = artifacts; 216 } 217 218 225 public void setOnlineMode( boolean policy ) 226 { 227 m_online = policy; 228 } 229 230 236 public void setParentClassLoader( ClassLoader classloader ) 237 { 238 m_classloader = classloader; 239 } 240 241 251 public void setImplementation( Artifact artifact ) 252 { 253 m_artifact = artifact; 254 } 255 256 262 public void setCacheDirectory( File cache ) 263 { 264 m_cache = cache; 265 } 266 267 276 public void setHosts( String [] hosts ) 277 { 278 m_hosts = hosts; 279 } 280 281 287 public void setProxyHost( String host ) 288 { 289 m_proxyHost = host; 290 } 291 292 297 public void setProxyPort( int port ) 298 { 299 m_proxyPort = port; 300 } 301 302 307 public void setProxyUsername( String username ) 308 { 309 m_proxyUsername = username; 310 } 311 312 317 public void setProxyPassword( String password ) 318 { 319 m_proxyPassword = password; 320 } 321 322 330 public InitialContext createInitialContext() 331 { 332 try 333 { 334 return new DefaultInitialContext( 335 getApplicationKey(), 336 getParentClassLoader(), 337 getImplementation(), 338 getRegisteredArtifacts(), 339 getWorkingDirectory(), 340 getCacheDirectory(), 341 getProxyHost(), 342 getProxyPort(), 343 getProxyUsername(), 344 getProxyPassword(), 345 getHosts(), 346 getOnlineMode() ); 347 } 348 catch( Throwable e ) 349 { 350 final String error = 351 "Could not create initial context."; 352 throw new RepositoryRuntimeException( error, e ); 353 } 354 } 355 356 359 public Artifact[] getRegisteredArtifacts() 360 { 361 if( null != m_registry ) return m_registry; 362 return new Artifact[0]; 363 } 364 365 368 public boolean getOnlineMode() 369 { 370 return m_online; 371 } 372 373 376 public String getApplicationKey() 377 { 378 return m_key; 379 } 380 381 385 public File getHomeDirectory() 386 { 387 return m_defaults.getHomeDirectory(); 388 } 389 390 394 public File getWorkingDirectory() 395 { 396 return m_work; 397 } 398 399 405 public ClassLoader getParentClassLoader() 406 { 407 if( null != m_classloader ) return m_classloader; 408 return DefaultInitialContext.class.getClassLoader(); 409 } 410 411 417 public Artifact getImplementation() 418 { 419 return m_artifact; 420 } 421 422 428 public File getCacheDirectory() 429 { 430 if( null != m_cache ) return m_cache; 431 String value = m_properties.getProperty( InitialContext.CACHE_KEY ); 432 if( null != value ) return new File ( value ); 433 return new File ( getHomeDirectory(), "repository" ); 434 } 435 436 440 public String [] getHosts() 441 { 442 if( null != m_hosts ) return m_hosts; 443 String value = m_properties.getProperty( InitialContext.HOSTS_KEY ); 444 if( null == value ) return new String [0]; 445 return expandHosts( value ); 446 } 447 448 453 public String getProxyHost() 454 { 455 if( null != m_proxyHost ) return m_proxyHost; 456 return m_properties.getProperty( InitialContext.PROXY_HOST_KEY ); 457 } 458 459 464 public int getProxyPort() 465 { 466 if( m_proxyPort > -1 ) return m_proxyPort; 467 String value = m_properties.getProperty( InitialContext.PROXY_PORT_KEY ); 468 if( value != null ) return Integer.parseInt( value ); 469 return -1; 470 } 471 472 477 public String getProxyUsername() 478 { 479 if( null != m_proxyUsername ) return m_proxyUsername; 480 return m_properties.getProperty( InitialContext.PROXY_USERNAME_KEY ); 481 } 482 483 488 public String getProxyPassword() 489 { 490 if( null != m_proxyPassword ) return m_proxyPassword; 491 return m_properties.getProperty( InitialContext.PROXY_PASSWORD_KEY ); 492 } 493 494 498 private Properties getDefaultProperties() throws IOException 499 { 500 Properties properties = new Properties (); 501 ClassLoader classloader = 502 DefaultInitialContextFactory.class.getClassLoader(); 503 InputStream input = 504 classloader.getResourceAsStream( AVALON_PROPERTIES ); 505 if( input == null ) 506 { 507 final String error = 508 "Missing resource: [" + AVALON_PROPERTIES + "]"; 509 throw new Error ( error ); 510 } 511 properties.load( input ); 512 return properties; 513 } 514 515 private static String [] expandHosts( String arg ) 516 { 517 ArrayList list = new ArrayList (); 518 StringTokenizer tokenizer = new StringTokenizer ( arg, "," ); 519 while( tokenizer.hasMoreTokens() ) 520 { 521 list.add( tokenizer.nextToken() ); 522 } 523 return (String []) list.toArray( new String [0] ); 524 } 525 } 526 | Popular Tags |