View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans.
5    *
6    * CosyBeans is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.application;
21  
22  import java.io.BufferedInputStream;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.InputStream;
26  import java.net.URL;
27  import java.util.Properties;
28  import java.util.logging.ConsoleHandler;
29  import java.util.logging.Level;
30  import java.util.logging.Logger;
31  
32  
33  
34  /**
35   * This class searches for configuration file in several location in well defined order.
36   * 
37   * @author ikriznar
38   *
39   */
40  public final class ConfigurationLoader {
41  	
42  	private ConfigurationLoader() {
43  		super();
44  	}
45  	
46  	/**
47  	 * Tries to return class loader from provided class. If not possible, 
48  	 * tries to return default class loader. If no usable class loader is found, then <code>null</code>
49  	 * is returned.
50  	 * 
51  	 * @param c class for witch class loader is searched
52  	 * @return class loader instance or <code>null</code>
53  	 */
54  	public static ClassLoader findClassLoader(Class c) {
55  		Logger l= Logger.getLogger(ConfigurationLoader.class.getName());
56  		ClassLoader cl=null;
57  		
58  		if (c == null) {
59  			c= ConfigurationLoader.class;
60  		}
61  			
62  		try {
63  			cl= c.getClassLoader();
64  		} catch (Exception e) {
65  			l.log(Level.FINEST,"Failed to load class loader for '"+c.getName()+"'",e);
66  		}
67  		
68  		if (cl!=null) {
69  			return cl;
70  		}
71  		
72  		try {
73  			cl= ClassLoader.getSystemClassLoader();
74  		} catch (Exception e) {
75  			l.log(Level.FINEST,"Failed to load system class loader for '"+c.getName()+"'",e);
76  		}
77  		
78  		return cl;
79  	}
80  	
81  	/**
82  	 * Loads the configuration from the file <code>name</code>.properties
83  	 * @param c the class loader supplier
84  	 * @param name the name of the file (without extension)
85  	 * @return properties as read from the file
86  	 */
87  	public static Properties loadConfiguration(Class c, String name) {
88  		return loadConfigurationFile(c,name+".properties");
89  	}
90  	
91  	/**
92  	 * Loads configuration from the file <code>name</code>.properties using
93  	 * default class loader.
94  	 * 
95  	 * @param name the name of the file (without extension)
96  	 * @return properties as read from the file
97  	 */
98  	public static Properties loadConfiguration(String name) {
99  		return loadConfigurationFile(null,name+".properties");
100 	}
101 	
102 	/**
103 	 * Loads the configuration from the file with the given name using
104 	 * the supplied class's classloader.
105 	 * 
106 	 * @param c class loader source
107 	 * @param propFileName the name of the properties file (with extension)
108 	 * @return properties as read from the file
109 	 */
110 	public static Properties loadConfigurationFile(Class c, String propFileName) {
111 		Logger l= Logger.getLogger(ConfigurationLoader.class.getName());
112 		
113 		Properties p= new Properties();
114 		
115 		/*
116 		 * Tries to load from classpath (eg. configuration in jar)
117 		 */
118 		
119 		ClassLoader cl = findClassLoader(c);
120 		if (cl!=null) {
121 			
122 			URL url= cl.getResource(".");
123 			InputStream is=null;
124 			File f= null;
125 			try {
126 				f= new File(url.toURI());
127 				f= new File(f,propFileName);
128 				if (f.exists()) {
129 					is= new BufferedInputStream(new FileInputStream(f));
130 					p.load(is);
131 					is.close();
132 					l.log(Level.CONFIG,"Configuration '"+propFileName+"' loaded from file '"+f.getAbsolutePath()+"'.");
133 				} else {
134 					l.log(Level.FINEST,"Configuration '"+propFileName+"' does not exist as file '"+f.getAbsolutePath()+"'.");
135 				}
136 			} catch (Exception e) {
137 				l.log(Level.FINEST,"Failed to load '"+propFileName+"' from file '"+f+"'.",e);
138 			} finally {
139 				try {
140 					if (is!=null) is.close();
141 				} catch (Exception e) {
142 					// ignore
143 				}
144 			}
145 			
146 			is= cl.getResourceAsStream(propFileName);
147 			l.log(Level.FINE,"Configuration '"+propFileName+"' not found on classpath.");
148 			if (is!=null) {
149 				try {
150 					p.load(is);
151 					l.log(Level.CONFIG,"Configuration '"+propFileName+"' loaded from classpath.");
152 					is.close();
153 				} catch (Exception e) {
154 					l.log(Level.FINEST,"Failed to load '"+propFileName+"' from classpath.",e);
155 				} finally {
156 					try {
157 						if (is!=null) is.close();
158 					} catch (Exception e) {
159 						// ignore
160 					}
161 				}
162 			}
163 		}
164 		
165 		/*
166 		 * Tries to load from current directory.
167 		 */
168 		File f= new File(System.getProperty("user.dir"));
169 		InputStream is=null;
170 		try {
171 			if (f.exists()) {
172 				f= new File(f,propFileName);
173 				if (f.exists()) {
174 					is= new BufferedInputStream(new FileInputStream(f));
175 					p.load(is);
176 					is.close();
177 					l.log(Level.CONFIG,"Configuration '"+propFileName+"' loaded from file '"+f.getAbsolutePath()+"'.");
178 				}
179 			} else {
180 				l.log(Level.FINEST,"Configuration '"+propFileName+"' does not exist as file '"+f.getAbsolutePath()+"'.");
181 			}
182 		} catch (Exception e) {
183 			l.log(Level.FINEST,"Failed to load '"+propFileName+"' from file '"+f.getAbsolutePath()+"'.",e);
184 		} finally {
185 			try {
186 				if (is!=null) is.close();
187 			} catch (Exception e) {
188 				// ignore
189 			}
190 		}
191 		
192 		
193 		return p;
194 	}
195 	
196 	public static void main(String[] args) {
197 		Logger l= Logger.getLogger(ConfigurationLoader.class.getName());
198 		l.setLevel(Level.ALL);
199 		ConsoleHandler c= new ConsoleHandler();
200 		c.setLevel(Level.ALL);
201 		l.addHandler(c);
202 		
203 		Properties p= ConfigurationLoader.loadConfiguration("project");
204 		c.flush();
205 		p.list(System.out);
206 		
207 	}
208 
209 }