KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > utility > PropertyHelper


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
25 package com.sun.jdo.spi.persistence.utility;
26
27 import com.sun.jdo.spi.persistence.utility.logging.Logger;
28
29 import java.util.Properties JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31
32 import java.io.BufferedInputStream JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.FileNotFoundException JavaDoc;
36 import java.io.IOException JavaDoc;
37
38 import java.security.AccessController JavaDoc;
39 import java.security.PrivilegedAction JavaDoc;
40 import java.security.PrivilegedExceptionAction JavaDoc;
41 import java.security.PrivilegedActionException JavaDoc;
42
43
44
45 /**
46  * @author Mitesh Meswani
47  * This class provides helper to load reources into property object.
48  *
49  */

50 public class PropertyHelper {
51
52     /**
53      * The logger.
54      */

55     private static final Logger logger = LogHelperUtility.getLogger();
56     
57     /**
58      * I18N message handler.
59      */

60     private final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
61             "com.sun.jdo.spi.persistence.utility.Bundle", // NOI18N
62
PropertyHelper.class.getClassLoader());
63     
64
65     /**
66      * Loads properties list from the specified resource into specified Properties object.
67      * @param properties Properties object to load
68      * @param resourceName Name of resource.
69      * @param classLoader The class loader that should be used to load the resource. If null,primordial
70      * class loader is used.
71      */

72     public static void loadFromResource(Properties JavaDoc properties, String JavaDoc resourceName, ClassLoader JavaDoc classLoader)
73             throws IOException JavaDoc {
74         load(properties, resourceName, false, classLoader);
75     }
76
77
78     /**
79      * Loads properties list from the specified file into specified Properties object.
80      * @param properties Properties object to load
81      * @param fileName Fully qualified path name to the file.
82      */

83     public static void loadFromFile(Properties JavaDoc properties, String JavaDoc fileName)
84             throws IOException JavaDoc {
85         load(properties, fileName, true, null);
86     }
87
88     /**
89      * Loads properties list from the specified resource
90      * into specified Properties object.
91      * @param resourceName Name of resource.
92      * If loadFromFile is true, this is fully qualified path name to a file.
93      * param classLoader is ignored.
94      * If loadFromFile is false,this is resource name.
95      * @param classLoader The class loader that should be used to load the resource. If null,primordial
96      * class loader is used.
97      * @param properties Properties object to load
98      * @param loadFromFile true if resourcename is to be treated as file name.
99      */

100     private static void load(Properties JavaDoc properties, final String JavaDoc resourceName,
101                             final boolean loadFromFile, final ClassLoader JavaDoc classLoader)
102                             throws IOException JavaDoc {
103
104         InputStream JavaDoc bin = null;
105         InputStream JavaDoc in = null;
106         boolean debug = logger.isLoggable();
107
108         if (debug) {
109             Object JavaDoc[] items = new Object JavaDoc[] {resourceName,Boolean.valueOf(loadFromFile)};
110             logger.fine("utility.PropertyHelper.load",items); // NOI18N
111
}
112
113         in = loadFromFile ? openFileInputStream(resourceName) :
114                                 openResourceInputStream(resourceName,classLoader);
115         if (in == null) {
116             throw new IOException JavaDoc(I18NHelper.getMessage(messages,
117                     "utility.PropertyHelper.failedToLoadResource", resourceName));// NOI18N
118
}
119         bin = new BufferedInputStream JavaDoc(in);
120         try {
121             properties.load(bin);
122         } finally {
123             try {
124                 bin.close();
125             } catch (Exception JavaDoc e) {
126                 // no action
127
}
128         }
129     }
130     
131     /**
132      * Open fileName as input stream inside doPriviledged block
133      */

134     private static InputStream JavaDoc openFileInputStream(final String JavaDoc fileName) throws java.io.FileNotFoundException JavaDoc {
135         try {
136             return (InputStream JavaDoc) AccessController.doPrivileged(
137                 new PrivilegedExceptionAction JavaDoc() {
138                     public Object JavaDoc run() throws FileNotFoundException JavaDoc {
139                             return new FileInputStream JavaDoc(fileName);
140                     }
141                 }
142            );
143         } catch (PrivilegedActionException JavaDoc e) {
144             // e.getException() should be an instance of FileNotFoundException,
145
// as only "checked" exceptions will be "wrapped" in a
146
// PrivilegedActionException.
147
throw (FileNotFoundException JavaDoc) e.getException();
148         }
149     
150     }
151
152     /**
153      * Open resourcenName as input stream inside doPriviledged block
154      */

155     private static InputStream JavaDoc openResourceInputStream(final String JavaDoc resourceName, final ClassLoader JavaDoc classLoader)
156                                                                         throws java.io.FileNotFoundException JavaDoc {
157         return (InputStream JavaDoc) AccessController.doPrivileged(
158             new PrivilegedAction JavaDoc() {
159                 public Object JavaDoc run() {
160                     if (classLoader != null) {
161                         return classLoader.getResourceAsStream(resourceName);
162                     } else {
163                         return ClassLoader.getSystemResourceAsStream(resourceName);
164                     }
165                 }
166             }
167         );
168     }
169
170 }
Popular Tags