KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > util > defaults > DefaultsBuilder


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.util.defaults;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import org.apache.avalon.util.env.Env;
27
28
29 /**
30  * A utility class that provides support for the establishment
31  * of a set of installation properties.
32  *
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  * @version $Revision: 1.6 $
35  */

36 public class DefaultsBuilder
37 {
38     //--------------------------------------------------------------
39
// static
40
//--------------------------------------------------------------
41

42    /**
43     * Return a home directory taking into account a supplied env symbol,
44     * a property key and a fallback directory.
45     *
46     * If the supplied key references a known system property
47     * value of '[key].home' then that value will be used to establish the home
48     * directory. Otherwise, if the supplied env symbol cannot be resolved
49     * to a value, a directory corresponding to ${user.home}/.[key]
50     * will be returned.
51     *
52     * @param key an application key such as 'merlin'
53     * @return the derived directory
54     */

55     public static File JavaDoc getHomeDirectory( String JavaDoc key ) throws IOException JavaDoc
56     {
57         final String JavaDoc homeKey = key + ".home";
58         final String JavaDoc symbol = key.toUpperCase() + "_HOME";
59         final String JavaDoc home =
60           System.getProperty(
61             homeKey,
62             Env.getEnvVariable( symbol ) );
63
64         if( null != home )
65         {
66             return new File JavaDoc( home ).getCanonicalFile();
67         }
68         else
69         {
70             final File JavaDoc user =
71               new File JavaDoc( System.getProperty( "user.home" ) );
72             final String JavaDoc path = "." + key;
73             return new File JavaDoc( user, path ).getCanonicalFile();
74         }
75     }
76
77    /**
78     * Create a installation properties object. The implementation
79     * will create a new properties object and read in a properties
80     * file if it exists relative to the filename [home]/[key].properties.
81     * Before returning the properties object the home directory will be
82     * assigned as the value of a property name [key].home if the supplied
83     * flag argument is TRUE.
84     *
85     * @param home the home directory
86     * @param key the application key
87     * @param flag if TRUE set the property '[key].home' to the home directory
88     * @return the application properties object
89     */

90     public static Properties JavaDoc getHomeProperties(
91       File JavaDoc home, String JavaDoc key, boolean flag ) throws IOException JavaDoc
92     {
93         Properties JavaDoc properties = getProperties( home, key );
94         if( flag )
95         {
96             final String JavaDoc name = key + ".home";
97             final String JavaDoc path = home.getCanonicalPath();
98             properties.setProperty( name, path );
99         }
100         return properties;
101     }
102
103    /**
104     * Create a user properties object. The implementation
105     * will create a new properties object and read in a properties
106     * file if it exists relative to the filename ${user.home}/[key].properties.
107     *
108     * @param key the application key
109     * @return the user properties object
110     */

111     public static Properties JavaDoc getUserProperties(
112       String JavaDoc key ) throws IOException JavaDoc
113     {
114         final File JavaDoc user = new File JavaDoc( System.getProperty( "user.home" ) );
115         return getProperties( user, key );
116     }
117
118    /**
119     * Create a dir properties object. The implementation
120     * will create a new properties object and read in a properties
121     * file if it exists relative to [dir]/[key].properties.
122     *
123     * @param dir the base directory
124     * @param key the application key
125     * @return the user properties object
126     */

127     public static Properties JavaDoc getProperties(
128       File JavaDoc dir, String JavaDoc key ) throws IOException JavaDoc
129     {
130         final String JavaDoc filename = key + ".properties";
131         final File JavaDoc file = new File JavaDoc( dir, filename );
132         return getProperties( file );
133     }
134
135    /**
136     * Create a properties object from the supplied file. If
137     * the file does not exists an empty property object will be
138     * returned.
139     *
140     * @param file the properties file
141     * @return the properties object
142     */

143     public static Properties JavaDoc getProperties( File JavaDoc file ) throws IOException JavaDoc
144     {
145         if( null == file )
146         {
147             throw new NullPointerException JavaDoc( "file" );
148         }
149
150         Properties JavaDoc properties = new Properties JavaDoc();
151         if( file.exists() )
152         {
153             properties.load(
154               new FileInputStream JavaDoc( file ) );
155         }
156         return properties;
157     }
158
159     public static Properties JavaDoc getProperties(
160       ClassLoader JavaDoc classloader, String JavaDoc path ) throws IOException JavaDoc
161     {
162         Properties JavaDoc properties = new Properties JavaDoc();
163         InputStream JavaDoc input =
164           classloader.getResourceAsStream( path );
165         if( input != null )
166         {
167             properties.load( input );
168         }
169         return properties;
170     }
171
172     //--------------------------------------------------------------
173
// state
174
//--------------------------------------------------------------
175

176     private final String JavaDoc m_key;
177
178     private final File JavaDoc m_work;
179
180     private final File JavaDoc m_root;
181
182     private final Properties JavaDoc m_home;
183
184     private final Properties JavaDoc m_user;
185
186     private final Properties JavaDoc m_dir;
187
188     //--------------------------------------------------------------
189
// constructor
190
//--------------------------------------------------------------
191

192     public DefaultsBuilder( final String JavaDoc key, File JavaDoc work ) throws IOException JavaDoc
193     {
194         m_key = key;
195         m_work = work;
196         m_root = getHomeDirectory( m_key );
197         m_home = getHomeProperties( m_root, m_key, true );
198         m_user = getUserProperties( m_key );
199         m_dir = getProperties( m_work, m_key );
200     }
201
202     //--------------------------------------------------------------
203
// implementation
204
//--------------------------------------------------------------
205

206    /**
207     * Return the application home directory.
208     * @return the home directory
209     */

210     public File JavaDoc getHomeDirectory()
211     {
212         return m_root;
213     }
214
215    /**
216     * Return the application properties. Properties returned are resolved
217     * relative to a properties file named [key].properties in the
218     * application home directory.
219     *
220     * @return the home directory
221     */

222     public Properties JavaDoc getHomeProperties()
223     {
224         return m_home;
225     }
226
227    /**
228     * Return the application specific properties from the users home directory.
229     * @return the user's application properties
230     */

231     public Properties JavaDoc getUserProperties()
232     {
233         return m_user;
234     }
235
236    /**
237     * Return the application specific properties from the current working directory.
238     * @return the working application properties
239     */

240     public Properties JavaDoc getDirProperties()
241     {
242         return m_dir;
243     }
244
245    /**
246     * Return a consolidated set of properties.
247     * @param defaults the source properties
248     * @param keys the simple property keys
249     * @return the consolidated properties
250     */

251     public Properties JavaDoc getConsolidatedProperties(
252       final Properties JavaDoc defaults, final String JavaDoc[] keys ) throws IOException JavaDoc
253     {
254         return getConsolidatedProperties( defaults, keys, new String JavaDoc[0] );
255     }
256
257    /**
258     * Return a consolidated set of properties.
259     * @param defaults the source properties
260     * @param keys the simple property keys
261     * @param sequence the sequence property keys
262     * @return the consolidated properties
263     */

264     public Properties JavaDoc getConsolidatedProperties(
265       final Properties JavaDoc defaults, final String JavaDoc[] keys, String JavaDoc[] sequence ) throws IOException JavaDoc
266     {
267         final Properties JavaDoc[] parameters =
268           new Properties JavaDoc[] {
269             defaults,
270             m_home,
271             m_user,
272             m_dir };
273         final DefaultsFinder[] finders =
274           new DefaultsFinder[]{
275             new SimpleDefaultsFinder(
276               parameters,
277               false ),
278             new SystemDefaultsFinder()
279           };
280         return new Defaults( keys, sequence, finders );
281     }
282 }
283
Popular Tags