KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > util > PropertiesLoader


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.util;
13
14 import com.versant.core.common.BindingSupportImpl;
15 import com.versant.core.common.Utils;
16 import com.versant.core.common.config.ConfigParser;
17
18 import java.util.Properties JavaDoc;
19 import java.io.*;
20 import java.net.MalformedURLException JavaDoc;
21
22 /**
23  * Utility methods to load Properties instances as resources from a ClassLoader
24  * based on a name prefix and type String.
25  */

26 public class PropertiesLoader {
27
28     public static final String JavaDoc RES_NAME_PROP = "this.resource.name";
29
30     /**
31      * Look for a resorce named prefix-type.properties and load it. If
32      * the resource contains a property named alias.for then replace type
33      * with that String and attempt to load that resource instead.
34      * If sucessful the name of the resource loaded is inserted into the
35      * Properties instance using the key {@link #RES_NAME_PROP}.
36      *
37      * @exception FileNotFoundException if no resource could be loaded
38      * @exception IOException on read errors
39      */

40     public static Properties JavaDoc loadPropertiesForURL(ClassLoader JavaDoc loader,
41             String JavaDoc prefix, String JavaDoc url) throws IOException {
42         int i = url.indexOf(':');
43         if (i <= 0) {
44             throw new MalformedURLException JavaDoc(url);
45         }
46         String JavaDoc type = url.substring(0, i);
47         return loadProperties(loader, prefix, type);
48     }
49
50     public static Properties JavaDoc loadPropertiesForDB(ClassLoader JavaDoc loader,
51             String JavaDoc prefix, String JavaDoc db) throws IOException {
52         if (Utils.isStringEmpty(db)){
53             throw BindingSupportImpl.getInstance().runtime("Unable to guess " +
54                     "database type. use the " + ConfigParser.STORE_DB +
55                     " property to set the database type");
56         }
57
58         if (Utils.isVersantDatabaseType(db)){
59             return loadProperties(loader, prefix, "versant");
60         } else {
61             return loadProperties(loader, prefix, "jdbc");
62         }
63     }
64
65     /**
66      * Look for a resorce named prefix-type.properties and load it. If
67      * the resource contains a property named alias.for then replace type
68      * with that String and attempt to load that resource instead.
69      * If sucessful the name of the resource loaded is inserted into the
70      * Properties instance using the key {@link #RES_NAME_PROP}.
71      *
72      * @exception FileNotFoundException if no resource could be loaded
73      * @exception IOException on read errors
74      */

75     public static Properties JavaDoc loadProperties(ClassLoader JavaDoc loader,
76             String JavaDoc prefix, String JavaDoc type) throws IOException {
77         for (;;) {
78             String JavaDoc resourceName = prefix + "-" + type + ".properties";
79             Properties JavaDoc p = loadProperties(loader, resourceName);
80             if (p == null) {
81                 throw new FileNotFoundException("Resource not found: " +
82                         resourceName);
83             }
84             type = p.getProperty("alias.for");
85             if (type == null) {
86                 p.put(RES_NAME_PROP, resourceName);
87                 return p;
88             }
89         }
90     }
91
92     /**
93      * Load Properties from resource name. If the first attempt fails then
94      * '/' is prepended to the resource name and another try is made.
95      * Returns null if not found.
96      *
97      */

98     public static Properties JavaDoc loadProperties(ClassLoader JavaDoc loader,
99             String JavaDoc resourceName) throws IOException {
100         InputStream in = loader.getResourceAsStream(resourceName);
101         if (in == null) {
102             in = loader.getResourceAsStream("/" + resourceName);
103         }
104         if (in == null) {
105             return null;
106         }
107         try {
108             Properties JavaDoc p = new Properties JavaDoc();
109             p.load(in);
110             return p;
111         } finally {
112             try {
113                 in.close();
114             } catch (IOException e) {
115                 // ignore
116
}
117         }
118     }
119
120     /**
121      * Load Properties from a file. Returns null if not found.
122      */

123     public static Properties JavaDoc loadProperties(String JavaDoc fileName) throws IOException {
124         InputStream in;
125         try {
126             in = new FileInputStream(fileName);
127         } catch (FileNotFoundException e) {
128             return null;
129         }
130         try {
131             Properties JavaDoc p = new Properties JavaDoc();
132             p.load(in);
133             return p;
134         } finally {
135             try {
136                 in.close();
137             } catch (IOException e) {
138                 // ignore
139
}
140         }
141     }
142
143 }
144
145
Popular Tags