1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveutils.service.impl; |
16 | |
17 | import java.io.File; |
18 | import java.io.FileInputStream; |
19 | import java.io.IOException; |
20 | import java.io.InputStream; |
21 | import java.util.List; |
22 | import java.util.Properties; |
23 | |
24 | import org.apache.commons.logging.Log; |
25 | import org.apache.hivemind.SymbolSource; |
26 | import org.apache.hivemind.impl.BaseLocatable; |
27 | |
28 | /** |
29 | * Implementation of {@link org.apache.hivemind.SymbolSource} driven |
30 | * off of a property file. |
31 | * |
32 | * @author Jean-Francois Poilpret |
33 | */ |
34 | public class PropertyFileSymbolSource |
35 | extends BaseLocatable |
36 | implements SymbolSource |
37 | { |
38 | public PropertyFileSymbolSource(Log logger, List<PropertyFileContribution> parameters) |
39 | { |
40 | for (PropertyFileContribution contrib: parameters) |
41 | { |
42 | addProperties(logger, contrib.getFile(), contrib.getProperty()); |
43 | } |
44 | } |
45 | |
46 | private void addProperties(Log logger, String path, String property) |
47 | { |
48 | String actualPath = path; |
49 | if (actualPath == null) |
50 | { |
51 | // Configuration does not define path but a system property where |
52 | // to find actual path |
53 | actualPath = System.getProperty(property); |
54 | } |
55 | Properties properties = new Properties(_allProperties); |
56 | try |
57 | { |
58 | File file = new File(actualPath); |
59 | if (!file.exists()) |
60 | { |
61 | logger.error("Problem reading property file [" + actualPath + "]"); |
62 | return; |
63 | } |
64 | InputStream input = new FileInputStream(file); |
65 | properties.load(input); |
66 | input.close(); |
67 | } |
68 | catch (IOException e) |
69 | { |
70 | logger.error("Problem reading property file [" + actualPath + "]", e); |
71 | } |
72 | _allProperties = properties; |
73 | } |
74 | |
75 | public String valueForSymbol(String name) |
76 | { |
77 | return _allProperties.getProperty(name); |
78 | } |
79 | |
80 | private Properties _allProperties = new Properties(); |
81 | } |