| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package de.jollyday.configuration.impl; |
| 17 | |
|
| 18 | |
import java.io.InputStream; |
| 19 | |
import java.net.URL; |
| 20 | |
import java.util.Properties; |
| 21 | |
import java.util.logging.Level; |
| 22 | |
import java.util.logging.Logger; |
| 23 | |
|
| 24 | |
import de.jollyday.configuration.ConfigurationProvider; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | 10 | public class URLConfigurationProvider implements ConfigurationProvider { |
| 34 | |
|
| 35 | 2 | private static final Logger LOG = Logger |
| 36 | 1 | .getLogger(URLConfigurationProvider.class.getName()); |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
@Override |
| 43 | |
public Properties getProperties() { |
| 44 | 1257 | Properties properties = new Properties(); |
| 45 | 1260 | Properties systemProps = System.getProperties(); |
| 46 | 1260 | String configURLs = systemProps.getProperty(CONFIG_URLS_PROPERTY); |
| 47 | 1267 | if (configURLs != null) { |
| 48 | 1235 | String[] strConfigURLs = configURLs.split(","); |
| 49 | 1235 | if (strConfigURLs != null) { |
| 50 | 2467 | for (String strURL : strConfigURLs) { |
| 51 | 1232 | readPropertiesFromURL(properties, strURL); |
| 52 | |
} |
| 53 | |
} |
| 54 | |
} |
| 55 | 1264 | return properties; |
| 56 | |
} |
| 57 | |
|
| 58 | |
private void readPropertiesFromURL(Properties properties, String strURL) { |
| 59 | 1228 | if (strURL == null || "".equals(strURL)) |
| 60 | 1 | return; |
| 61 | 1228 | InputStream inputStream = null; |
| 62 | |
try { |
| 63 | 1227 | URL configURL = new URL(strURL.trim()); |
| 64 | 1228 | inputStream = configURL.openStream(); |
| 65 | 1225 | properties.load(inputStream); |
| 66 | 1 | } catch (Exception e) { |
| 67 | 1 | LOG.log(Level.WARNING, "Cannot read configuration from '" + strURL |
| 68 | |
+ "'.", e); |
| 69 | |
} finally { |
| 70 | 1233 | closeStreamFromURL(strURL, inputStream); |
| 71 | 1231 | } |
| 72 | 1231 | } |
| 73 | |
|
| 74 | |
private void closeStreamFromURL(String strURL, InputStream inputStream) { |
| 75 | 1233 | if (inputStream != null) { |
| 76 | |
try { |
| 77 | 1232 | inputStream.close(); |
| 78 | 0 | } catch (Exception e) { |
| 79 | 0 | LOG.warning("Cannot close stream for configuration URL " |
| 80 | |
+ strURL + "."); |
| 81 | 1230 | } |
| 82 | |
} |
| 83 | 1231 | } |
| 84 | |
|
| 85 | |
} |