KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > property > FilePropertyReader


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.property;
23
24 import java.util.Properties JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.BufferedInputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 import org.jboss.util.NullArgumentException;
34
35 /**
36  * Reads properties from one or more files.
37  *
38  * @version <tt>$Revision: 1958 $</tt>
39  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
40  */

41 public class FilePropertyReader
42    implements PropertyReader
43 {
44    /** Array of filenames to load properties from */
45    protected String JavaDoc[] filenames;
46
47    /**
48     * Construct a FilePropertyReader with an array of filenames
49     * to read from.
50     *
51     * @param filenames Filenames to load properties from
52     */

53    public FilePropertyReader(String JavaDoc[] filenames) {
54       if (filenames == null)
55          throw new NullArgumentException("filenames");
56
57       this.filenames = filenames;
58    }
59
60    /**
61     * Construct a FilePropertyReader with a single filename to read from.
62     *
63     * @param filename Filename to load properties from
64     */

65    public FilePropertyReader(String JavaDoc filename) {
66       this(new String JavaDoc[] { filename });
67    }
68
69    /**
70     * Get an input stream for the given filename.
71     *
72     * @param filename File name to get input stream for.
73     * @return Input stream for file.
74     *
75     * @throws IOException Failed to get input stream for file.
76     */

77    protected InputStream JavaDoc getInputStream(String JavaDoc filename) throws IOException JavaDoc {
78       File JavaDoc file = new File JavaDoc(filename);
79       return new FileInputStream JavaDoc(file);
80    }
81
82    /**
83     * Load properties from a file into a properties map.
84     *
85     * @param props Properties map to load properties into.
86     * @param filename Filename to read properties from.
87     *
88     * @throws IOException Failed to load properties from filename.
89     * @throws IllegalArgumentException Filename is invalid.
90     */

91    protected void loadProperties(Properties JavaDoc props, String JavaDoc filename)
92       throws IOException JavaDoc
93    {
94       if (filename == null)
95          throw new NullArgumentException("filename");
96       if (filename.equals(""))
97          throw new IllegalArgumentException JavaDoc("filename");
98
99       InputStream JavaDoc in = new BufferedInputStream JavaDoc(getInputStream(filename));
100       props.load(in);
101       in.close();
102    }
103
104    /**
105     * Read properties from each specified filename
106     *
107     * @return Read properties
108     *
109     * @throws PropertyException Failed to read properties.
110     * @throws IOException I/O error while reading properties.
111     */

112    public Map JavaDoc readProperties()
113       throws PropertyException, IOException JavaDoc
114    {
115       Properties JavaDoc props = new Properties JavaDoc();
116       
117       // load each specified property file
118
for (int i=0; i<filenames.length; i++) {
119          loadProperties(props, filenames[i]);
120       }
121
122       return props;
123    }
124 }
125
Popular Tags