1 17 18 package org.apache.avalon.repository.impl; 19 20 import java.io.IOException ; 21 import java.io.File ; 22 import java.util.Properties ; 23 24 import org.apache.avalon.repository.Artifact; 25 import org.apache.avalon.repository.RepositoryException; 26 import org.apache.avalon.repository.provider.InitialContext; 27 import org.apache.avalon.repository.provider.RepositoryCriteria; 28 29 import org.apache.avalon.util.criteria.Criteria; 30 import org.apache.avalon.util.criteria.Parameter; 31 import org.apache.avalon.util.criteria.PackedParameter; 32 import org.apache.avalon.util.defaults.Defaults; 33 import org.apache.avalon.util.defaults.DefaultsBuilder; 34 35 36 43 public class DefaultRepositoryCriteria extends Criteria implements RepositoryCriteria 44 { 45 49 53 private static Parameter[] buildParameters( InitialContext context ) 54 { 55 return new Parameter[]{ 56 new Parameter( 57 REPOSITORY_ONLINE_MODE, 58 Boolean .class, new Boolean ( context.getOnlineMode() ) ), 59 new Parameter( 60 REPOSITORY_CACHE_DIR, 61 File .class, 62 context.getInitialCacheDirectory() ), 63 new PackedParameter( 64 REPOSITORY_REMOTE_HOSTS, 65 ",", 66 context.getInitialHosts() ), 67 new ArtifactSequenceParameter( 68 REPOSITORY_FACTORY_ARTIFACTS, 69 ",", 70 new Artifact[0] ) }; 71 } 72 73 77 81 public DefaultRepositoryCriteria( InitialContext context ) 82 throws RepositoryException 83 { 84 super( buildParameters( context ) ); 85 86 90 try 91 { 92 93 final String key = context.getApplicationKey(); 94 final File work = context.getInitialWorkingDirectory(); 95 Properties defaults = getDefaultProperties(); 96 DefaultsBuilder builder = new DefaultsBuilder( key, work ); 97 Properties properties = 98 builder.getConsolidatedProperties( defaults, getKeys() ); 99 100 105 String [] keys = super.getKeys(); 106 for( int i=0; i<keys.length; i++ ) 107 { 108 final String propertyKey = keys[i]; 109 final String value = properties.getProperty( propertyKey ); 110 if( null != value ) 111 { 112 put( propertyKey, value ); 113 } 114 } 115 } 116 catch( IOException ioe ) 117 { 118 final String error = 119 "Failed to resolve repository parameters."; 120 throw new RepositoryException( error, ioe ); 121 } 122 } 123 124 128 public void setOnlineMode( boolean mode ) 129 { 130 put( REPOSITORY_ONLINE_MODE, new Boolean ( mode ) ); 131 } 132 133 public void setCacheDirectory( File cache ) 134 { 135 put( REPOSITORY_CACHE_DIR, cache ); 136 } 137 138 public void setHosts( String [] hosts ) 139 { 140 put( REPOSITORY_REMOTE_HOSTS, hosts ); 141 } 142 143 public void setFactoryArtifacts( Artifact[] artifacts ) 144 { 145 put( REPOSITORY_FACTORY_ARTIFACTS, artifacts ); 146 } 147 148 152 public String toString() 153 { 154 return "[repository: " + super.toString() + "]"; 155 } 156 157 161 private Properties getDefaultProperties() throws RepositoryException 162 { 163 try 164 { 165 return Defaults.getStaticProperties( DefaultRepositoryCriteria.class ); 166 } 167 catch ( IOException e ) 168 { 169 throw new RepositoryException( 170 "Failed to load implementation defaults resource for the class: " 171 + DefaultRepositoryCriteria.class.getName(), e ); 172 } 173 } 174 } 175 | Popular Tags |