| 1 | package org.jtoolkit.essence.app.pojo.impl; |
| 2 | |
| 3 | import org.apache.commons.logging.Log; |
| 4 | import static org.apache.commons.logging.LogFactory.getLog; |
| 5 | import org.jetbrains.annotations.NotNull; |
| 6 | import org.jtoolkit.essence.utils.IOUtils; |
| 7 | import org.jtoolkit.essence.utils.Named; |
| 8 | |
| 9 | import java.io.FileNotFoundException; |
| 10 | import java.io.IOException; |
| 11 | import java.util.LinkedHashMap; |
| 12 | import java.util.Map; |
| 13 | import java.util.Properties; |
| 14 | import java.util.Set; |
| 15 | |
| 16 | /* |
| 17 | Copyright 2006 Peter Lawrey |
| 18 | |
| 19 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 20 | you may not use this file except in compliance with the License. |
| 21 | You may obtain a copy of the License at |
| 22 | |
| 23 | http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | |
| 25 | Unless required by applicable law or agreed to in writing, software |
| 26 | distributed under the License is distributed on an "AS IS" BASIS, |
| 27 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | See the License for the specific language governing permissions and |
| 29 | limitations under the License. |
| 30 | */ |
| 31 | |
| 32 | /** |
| 33 | * This define a context for building data value objects and components. |
| 34 | * Its package.path is used to lookup class names and properties are used to expand ${} in values. |
| 35 | * |
| 36 | * @author Peter Lawrey |
| 37 | */ |
| 38 | @SuppressWarnings("unchecked") |
| 39 | public class PojoContext { |
| 40 | private static final Log LOG = getLog(PojoContext.class); |
| 41 | private static final String[] BASE_PACKAGE_PATH = {"org.jtoolkit", "java.lang", ""}; |
| 42 | |
| 43 | public static final PojoContext EMPTY = new PojoContext("", new String[] { "" }); |
| 44 | |
| 45 | private final Map<String, String> propertiesMap; |
| 46 | public final Named.Source<String> properties; |
| 47 | |
| 48 | public PojoContext(String name, String[] contexts) { |
| 49 | propertiesMap = new LinkedHashMap<String, String>(); |
| 50 | properties = new Named.ContextSource<String>(name, new Named.MapSource<String>(propertiesMap), contexts); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Add unset proeprties to this context. However, properties once set do not change. |
| 55 | */ |
| 56 | public void putProperties(@NotNull Map map) { |
| 57 | for (Map.Entry entry : (Set<Map.Entry>) map.entrySet()) { |
| 58 | String key = String.valueOf(entry.getKey()); |
| 59 | String value = entry.getValue() == null ? null : String.valueOf(entry.getValue()); |
| 60 | putProperty(key, value); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public void putProperty(String key, String value) { |
| 65 | Object prev = propertiesMap.get(key); |
| 66 | if (prev != null && !prev.equals(value)) { |
| 67 | if (LOG.isDebugEnabled()) |
| 68 | LOG.debug(Thread.currentThread().getName() + ": Property " + key + " remains " + prev + " not overriden to " + value); |
| 69 | } else { |
| 70 | propertiesMap.put(key, value); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return the base search path for packages. This is a list of prefix strings and can be class names to refer to inner classes. |
| 76 | */ |
| 77 | @NotNull public String[] getPackagePath() { |
| 78 | Object path = propertiesMap.get("package.path"); |
| 79 | if (path == null) return BASE_PACKAGE_PATH; |
| 80 | return (path + "::java.lang").split("[,:;] ?"); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Add unset proeprties from a properties file. |
| 85 | */ |
| 86 | public void loadPropertiesFile(@NotNull String filename) { |
| 87 | Properties prop = new Properties(); |
| 88 | try { |
| 89 | prop.load(IOUtils.getInputStream(filename)); |
| 90 | putProperties(prop); |
| 91 | } catch (FileNotFoundException ignored) { |
| 92 | if (LOG.isDebugEnabled()) |
| 93 | LOG.debug(Thread.currentThread().getName() + ": Properties file not found " + filename); |
| 94 | } catch (IOException e) { |
| 95 | throw new Error("Error loading " + filename + " exiting!", e); |
| 96 | } |
| 97 | } |
| 98 | } |