KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > PropertiesStringSource


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.util;
25 import java.io.IOException JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 /**
32  * This class implements IStringSource for the case where the Strings are kept
33  * in a properties file. The file is read when instantiated and cached for
34  * later usage. There are numerous ways to specify a Properties file:
35  * <b><br><br>Filename
36  * <br>File Object
37  * <br>Properties Object
38  *
39  * @see IStringSource
40  */

41 public class PropertiesStringSource implements IStringSource
42 {
43     /** Create an instance from an existing Properties object
44      * @param p A Properties object to use as the source of Strings
45      */

46     public PropertiesStringSource(Properties JavaDoc p)
47     {
48         initialize(p);
49     }
50
51     public PropertiesStringSource(InputStream JavaDoc is) throws IOException JavaDoc
52     {
53         initialize(is);
54     }
55     
56     /** Create an instance from a Properties file.
57      * @param f The File object.
58      * @throws IOException The file probably doesn't exist.
59      */

60     public PropertiesStringSource(File JavaDoc f) throws IOException JavaDoc
61     {
62         initialize(f);
63     }
64     
65     
66     /** Create an instance from the named Properties file
67      * @param filename The name of the Properties file
68      * @throws IOException The file probably doesn't exist
69      */

70     public PropertiesStringSource(String JavaDoc filename) throws IOException JavaDoc
71     {
72         initialize(filename);
73     }
74        
75     
76     /** Get a string referenced by the designated key.
77      *
78      * @param key the key to lookup the string
79      * @return the string, or null if string cannot be found
80      */

81     public String JavaDoc getString(String JavaDoc key)
82     {
83         Assert.assertit((mProperties!=null), "invalid state: mProperties is null");
84         
85         //ArgChecker.check(key, "key");
86

87         return mProperties.getProperty(key);
88     }
89    
90     
91     /* In java, a derived class' constructor must call the base class constructor
92      * in the very first line. Sometimes this can become an insurmountable
93      * problem because the derived class needs to assemble arguments first.
94      * So the real constructor functionality is broken into initialize() methods
95      * But outsiders shouldn't be able to create empty instances or to
96      * call initialize(). So all of these methods are protected.
97      * WBN
98      */

99
100     /** Do-nothing constructor.
101      * This is protected so that derived classes can start with an
102      * empty instance -- and then call initialize() methods in a do-it-yourself
103      * fashion
104      */

105     protected PropertiesStringSource()
106     {
107         /** This is protected so that derived classes can start with an
108          * empty instance -- and then call initialize() methods in a do-it-yourself
109          * fashion
110          */

111     }
112     
113     
114     /** Use the Properties arguments as the source of Strings
115      * @param p A Properties object to use as the source of Strings
116      */

117     protected void initialize(Properties JavaDoc properties)
118     {
119         //ArgChecker.check(properties, "properties");
120
mProperties = properties;
121     }
122
123     
124     /** Load the Properties object from a file.
125      * @param f The File object
126      * @throws IOException The file probably doesn't exist
127      */

128     protected void initialize(File JavaDoc file) throws IOException JavaDoc
129     {
130         //ArgChecker.check(file != null && file.exists(),
131
// "File object doesn't point to an existing file: " + file.getPath());
132

133         //ArgChecker.check(file.canRead(),
134
// "File object can't be read: " + file.getPath());
135

136         //ArgChecker.check(!file.isDirectory(),
137
// "File object is a directory: " + file.getPath());
138
FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
139         initialize(fis);
140     }
141     
142     
143     /** Load the Properties object from a file.
144      * @param filename The name of the Properties file
145      */

146     protected void initialize(String JavaDoc filename) throws IOException JavaDoc
147     {
148         //ArgChecker.check(filename, "filename");
149
initialize(new File JavaDoc(filename));
150     }
151
152     /** Load the Properties object from a file.
153      * @param filename The name of the Properties file
154      */

155     protected void initialize(InputStream JavaDoc is) throws IOException JavaDoc
156     {
157         //ArgChecker.check(filename, "filename");
158
mProperties = new Properties JavaDoc();
159         mProperties.load(is);
160     }
161     
162     private Properties JavaDoc mProperties;
163 }
164
165
Popular Tags