1 2 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 ; 19 import java.io.*; 20 import java.net.MalformedURLException ; 21 22 26 public class PropertiesLoader { 27 28 public static final String RES_NAME_PROP = "this.resource.name"; 29 30 40 public static Properties loadPropertiesForURL(ClassLoader loader, 41 String prefix, String url) throws IOException { 42 int i = url.indexOf(':'); 43 if (i <= 0) { 44 throw new MalformedURLException (url); 45 } 46 String type = url.substring(0, i); 47 return loadProperties(loader, prefix, type); 48 } 49 50 public static Properties loadPropertiesForDB(ClassLoader loader, 51 String prefix, String 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 75 public static Properties loadProperties(ClassLoader loader, 76 String prefix, String type) throws IOException { 77 for (;;) { 78 String resourceName = prefix + "-" + type + ".properties"; 79 Properties 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 98 public static Properties loadProperties(ClassLoader loader, 99 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 } 117 } 118 } 119 120 123 public static Properties loadProperties(String 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 p = new Properties (); 132 p.load(in); 133 return p; 134 } finally { 135 try { 136 in.close(); 137 } catch (IOException e) { 138 } 140 } 141 } 142 143 } 144 145 | Popular Tags |