001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.util;
012:
013: import com.versant.core.common.BindingSupportImpl;
014: import com.versant.core.common.Utils;
015: import com.versant.core.common.config.ConfigParser;
016:
017: import java.util.Properties;
018: import java.io.*;
019: import java.net.MalformedURLException;
020:
021: /**
022: * Utility methods to load Properties instances as resources from a ClassLoader
023: * based on a name prefix and type String.
024: */
025: public class PropertiesLoader {
026:
027: public static final String RES_NAME_PROP = "this.resource.name";
028:
029: /**
030: * Look for a resorce named prefix-type.properties and load it. If
031: * the resource contains a property named alias.for then replace type
032: * with that String and attempt to load that resource instead.
033: * If sucessful the name of the resource loaded is inserted into the
034: * Properties instance using the key {@link #RES_NAME_PROP}.
035: *
036: * @exception FileNotFoundException if no resource could be loaded
037: * @exception IOException on read errors
038: */
039: public static Properties loadPropertiesForURL(ClassLoader loader,
040: String prefix, String url) throws IOException {
041: int i = url.indexOf(':');
042: if (i <= 0) {
043: throw new MalformedURLException(url);
044: }
045: String type = url.substring(0, i);
046: return loadProperties(loader, prefix, type);
047: }
048:
049: public static Properties loadPropertiesForDB(ClassLoader loader,
050: String prefix, String db) throws IOException {
051: if (Utils.isStringEmpty(db)) {
052: throw BindingSupportImpl.getInstance().runtime(
053: "Unable to guess " + "database type. use the "
054: + ConfigParser.STORE_DB
055: + " property to set the database type");
056: }
057:
058: if (Utils.isVersantDatabaseType(db)) {
059: return loadProperties(loader, prefix, "versant");
060: } else {
061: return loadProperties(loader, prefix, "jdbc");
062: }
063: }
064:
065: /**
066: * Look for a resorce named prefix-type.properties and load it. If
067: * the resource contains a property named alias.for then replace type
068: * with that String and attempt to load that resource instead.
069: * If sucessful the name of the resource loaded is inserted into the
070: * Properties instance using the key {@link #RES_NAME_PROP}.
071: *
072: * @exception FileNotFoundException if no resource could be loaded
073: * @exception IOException on read errors
074: */
075: public static Properties loadProperties(ClassLoader loader,
076: String prefix, String type) throws IOException {
077: for (;;) {
078: String resourceName = prefix + "-" + type + ".properties";
079: Properties p = loadProperties(loader, resourceName);
080: if (p == null) {
081: throw new FileNotFoundException("Resource not found: "
082: + resourceName);
083: }
084: type = p.getProperty("alias.for");
085: if (type == null) {
086: p.put(RES_NAME_PROP, resourceName);
087: return p;
088: }
089: }
090: }
091:
092: /**
093: * Load Properties from resource name. If the first attempt fails then
094: * '/' is prepended to the resource name and another try is made.
095: * Returns null if not found.
096: *
097: */
098: public static Properties loadProperties(ClassLoader loader,
099: String 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 p = new Properties();
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 loadProperties(String fileName)
124: throws IOException {
125: InputStream in;
126: try {
127: in = new FileInputStream(fileName);
128: } catch (FileNotFoundException e) {
129: return null;
130: }
131: try {
132: Properties p = new Properties();
133: p.load(in);
134: return p;
135: } finally {
136: try {
137: in.close();
138: } catch (IOException e) {
139: // ignore
140: }
141: }
142: }
143:
144: }
|