| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DefaultConfigurationProvider |
|
| 4.0;4 |
| 1 | /** | |
| 2 | * Copyright 2012 Sven Diedrichsen | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an | |
| 12 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | |
| 13 | * express or implied. See the License for the specific language | |
| 14 | * governing permissions and limitations under the License. | |
| 15 | */ | |
| 16 | package de.jollyday.configuration.impl; | |
| 17 | ||
| 18 | import java.io.IOException; | |
| 19 | import java.io.InputStream; | |
| 20 | import java.util.Properties; | |
| 21 | import java.util.logging.Logger; | |
| 22 | ||
| 23 | import de.jollyday.configuration.ConfigurationProvider; | |
| 24 | import de.jollyday.util.ResourceUtil; | |
| 25 | ||
| 26 | /** | |
| 27 | * Provider which adds jollydays default configuration file | |
| 28 | * 'jollyday.properties' by reading it from the classpath by using the currents | |
| 29 | * threads classloader. | |
| 30 | * | |
| 31 | * @author Sven Diedrichsen | |
| 32 | */ | |
| 33 | 7 | public class DefaultConfigurationProvider implements ConfigurationProvider { |
| 34 | ||
| 35 | 2 | private static final Logger LOG = Logger |
| 36 | 1 | .getLogger(DefaultConfigurationProvider.class.getName()); |
| 37 | ||
| 38 | /** | |
| 39 | * The name of the configuration file. | |
| 40 | */ | |
| 41 | private static final String CONFIG_FILE = "jollyday.properties"; | |
| 42 | /** | |
| 43 | * The utility to load resources. | |
| 44 | */ | |
| 45 | 7 | private ResourceUtil resourceUtil = new ResourceUtil(); |
| 46 | ||
| 47 | /* | |
| 48 | * (non-Javadoc) | |
| 49 | * | |
| 50 | * @see | |
| 51 | * de.jollyday.configuration.ConfigurationProvider#addConfiguration(java | |
| 52 | * .util.Properties) | |
| 53 | */ | |
| 54 | @Override | |
| 55 | public Properties getProperties() { | |
| 56 | 1261 | Properties properties = new Properties(); |
| 57 | try { | |
| 58 | 1263 | InputStream stream = null; |
| 59 | try { | |
| 60 | 1260 | stream = resourceUtil.getResource(CONFIG_FILE).openStream(); |
| 61 | 1246 | if (stream != null) { |
| 62 | 1251 | properties.load(stream); |
| 63 | } else { | |
| 64 | 0 | LOG.warning("Could not load default properties file '" |
| 65 | + CONFIG_FILE + "' from classpath."); | |
| 66 | } | |
| 67 | } finally { | |
| 68 | 1263 | closeStream(stream); |
| 69 | 1263 | } |
| 70 | 1263 | return properties; |
| 71 | 0 | } catch (IOException e) { |
| 72 | 0 | throw new IllegalStateException( |
| 73 | "Could not load default properties from classpath.", e); | |
| 74 | } | |
| 75 | } | |
| 76 | ||
| 77 | private void closeStream(InputStream stream) { | |
| 78 | 1264 | if (stream != null) { |
| 79 | try { | |
| 80 | 1264 | stream.close(); |
| 81 | 0 | } catch (IOException e) { |
| 82 | 0 | LOG.warning("Could not close configurations input stream."); |
| 83 | 1264 | } |
| 84 | } | |
| 85 | 1263 | } |
| 86 | ||
| 87 | } |