1 package net.sourceforge.jdbclogger.core.util; 2 18 import net.sourceforge.jdbclogger.core.config.Environment; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.io.Reader ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.Enumeration ; 29 import java.util.Properties ; 30 31 37 public final class ConfigHelper 38 { 39 private static final Log log = LogFactory.getLog(ConfigHelper.class); 40 41 49 public static final URL locateConfig(final String path) { 50 try { 51 return new URL (path); 52 } catch(MalformedURLException e) { 53 return findAsResource(path); 54 } 55 } 56 57 65 public static final URL findAsResource(final String path) { 66 URL url = null; 67 68 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); 71 if (contextClassLoader!=null) { 72 url = contextClassLoader.getResource(path); 73 } 74 if (url != null) 75 return url; 76 77 url = ConfigHelper.class.getClassLoader().getResource(path); 79 if (url != null) 80 return url; 81 82 url = ClassLoader.getSystemClassLoader().getResource(path); 84 85 return url; 87 } 88 89 96 public static final InputStream getConfigStream(final String path) { 97 final URL url = ConfigHelper.locateConfig(path); 98 99 if (url == null) { 100 String msg = "Unable to locate config file: " + path; 101 log.error(msg); 102 return null; 103 } 104 105 try { 106 return url.openStream(); 107 } catch(IOException e) { 108 log.error("Unable to open config file: " + path, e); 109 } 110 return null; 111 } 112 113 121 public static final Reader getConfigStreamReader(final String path) { 122 return new InputStreamReader ( getConfigStream(path) ); 123 } 124 125 130 public static final Properties getConfigProperties(String path) { 131 try 132 { 133 Properties properties = new Properties (); 134 properties.load( getConfigStream(path) ); 135 return properties; 136 } 137 catch(IOException e) { 138 log.error("Unable to load properties from specified config file: " + path, e); 139 } 140 return null; 141 } 142 143 private ConfigHelper() {} 144 145 public static InputStream getResourceAsStream(String resource) { 146 String stripped = resource.startsWith("/") ? 147 resource.substring(1) : resource; 148 149 InputStream stream = null; 150 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 151 if (classLoader!=null) { 152 stream = classLoader.getResourceAsStream( stripped ); 153 } 154 if ( stream == null ) { 155 Environment.class.getResourceAsStream( resource ); 156 } 157 if ( stream == null ) { 158 stream = Environment.class.getClassLoader().getResourceAsStream( stripped ); 159 } 160 if ( stream == null ) { 161 log.error(resource + " not found" ); 162 } 163 return stream; 164 } 165 169 public static Enumeration <URL > getResources(String fileName){ 170 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); 171 try { 172 return contextClassLoader.getResources(fileName); 173 } 174 catch (IOException e) { 175 log.error("No custom drivers found"); 176 } 177 178 return null; 179 } 180 } 181 | Popular Tags |