KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > repository > impl > DefaultRepositoryCriteria


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.impl;
19
20 import java.io.IOException JavaDoc;
21 import java.io.File JavaDoc;
22 import java.util.Properties JavaDoc;
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 /**
37  * A Criteria is a class holding the values supplied by a user
38  * for application to a factory.
39  *
40  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
41  * @version $Revision: 1.10 $
42  */

43 public class DefaultRepositoryCriteria extends Criteria implements RepositoryCriteria
44 {
45     //--------------------------------------------------------------
46
// static
47
//--------------------------------------------------------------
48

49    /**
50     * The factory parameters template.
51     * @return the set of parameters constraining the criteria
52     */

53     private static Parameter[] buildParameters( InitialContext context )
54     {
55         return new Parameter[]{
56           new Parameter(
57             REPOSITORY_ONLINE_MODE,
58             Boolean JavaDoc.class, new Boolean JavaDoc( context.getOnlineMode() ) ),
59           new Parameter(
60             REPOSITORY_CACHE_DIR,
61             File JavaDoc.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     //--------------------------------------------------------------
74
// constructor
75
//--------------------------------------------------------------
76

77     /**
78      * Creation of a new criteria.
79      * @param context the initial context
80      */

81     public DefaultRepositoryCriteria( InitialContext context )
82       throws RepositoryException
83     {
84         super( buildParameters( context ) );
85
86         //
87
// create the consolidated properties
88
//
89

90         try
91         {
92
93             final String JavaDoc key = context.getApplicationKey();
94             final File JavaDoc work = context.getInitialWorkingDirectory();
95             Properties JavaDoc defaults = getDefaultProperties();
96             DefaultsBuilder builder = new DefaultsBuilder( key, work );
97             Properties JavaDoc properties =
98               builder.getConsolidatedProperties( defaults, getKeys() );
99
100             //
101
// Populate the empty repository criteria using
102
// the values from the consilidated defaults.
103
//
104

105             String JavaDoc[] keys = super.getKeys();
106             for( int i=0; i<keys.length; i++ )
107             {
108                 final String JavaDoc propertyKey = keys[i];
109                 final String JavaDoc value = properties.getProperty( propertyKey );
110                 if( null != value )
111                 {
112                     put( propertyKey, value );
113                 }
114             }
115         }
116         catch( IOException JavaDoc ioe )
117         {
118             final String JavaDoc error =
119               "Failed to resolve repository parameters.";
120             throw new RepositoryException( error, ioe );
121         }
122     }
123
124     //--------------------------------------------------------------
125
// RepositoryCriteria
126
//--------------------------------------------------------------
127

128     public void setOnlineMode( boolean mode )
129     {
130         put( REPOSITORY_ONLINE_MODE, new Boolean JavaDoc( mode ) );
131     }
132
133     public void setCacheDirectory( File JavaDoc cache )
134     {
135         put( REPOSITORY_CACHE_DIR, cache );
136     }
137
138     public void setHosts( String JavaDoc[] 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     //--------------------------------------------------------------
149
// Object
150
//--------------------------------------------------------------
151

152     public String JavaDoc toString()
153     {
154         return "[repository: " + super.toString() + "]";
155     }
156
157     //--------------------------------------------------------------
158
// private
159
//--------------------------------------------------------------
160

161     private Properties JavaDoc getDefaultProperties() throws RepositoryException
162     {
163         try
164         {
165             return Defaults.getStaticProperties( DefaultRepositoryCriteria.class );
166         }
167         catch ( IOException JavaDoc 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