1 /* *************************************************************************** 2 * Copyright (c) 2008 Brabenetz Harald, Austria. 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 "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 *****************************************************************************/ 17 package org.settings4j.connector; 18 19 import java.io.ByteArrayInputStream; 20 import java.io.IOException; 21 import java.util.Properties; 22 23 import org.apache.commons.lang3.StringUtils; 24 import org.settings4j.contentresolver.ClasspathContentResolver; 25 import org.settings4j.contentresolver.FSContentResolver; 26 27 28 /** 29 * The {@link Properties}-File implementation of an {@link org.settings4j.Connector}. 30 * <p> 31 * Example usage read property File from Classpath with {@link ClasspathConnector} (<param name="propertyFromContent" 32 * ... />): 33 * 34 * <pre> 35 * <settings4j:configuration xmlns:settings4j='http://settings4j.org/'> 36 * 37 * <connector name="PropertyFileConnector" class="org.settings4j.connector.PropertyFileConnector"> 38 * <param name="propertyFromContent" 39 * value="${connectors.content['org/settings4j/config/propertyFile.properties']}" /> 40 * <connector-ref ref="ClasspathConnector" /> 41 * </connector> 42 * 43 * <connector name="ClasspathConnector" 44 * class="org.settings4j.connector.ClasspathConnector" /> 45 * 46 * </settings4j:configuration> 47 * </pre> 48 * 49 * You can also use other connectors which have a nativ implementation for 50 * {@link org.settings4j.Connector#getContent(String)} (doesn't use contentResolver). <br /> 51 * e.g.: {@link FSConnector} for FileSystem paths. 52 * <p> 53 * Example usage read property File from Classpath with {@link ClasspathConnector}: 54 * <p> 55 * If you don't like ExpressionLanguage, then you can set the path to your PropertyFile directly. But in this case a 56 * prefix "file:" or "classpath" is required. And only "file:" and "classpath" are supported.<br /> 57 * Example usage read property File from Classpath with classpathString (<param name="propertyFromPath" ... />): 58 * 59 * <pre> 60 * <settings4j:configuration xmlns:settings4j='http://settings4j.org/'> 61 * 62 * <connector name="PropertyFileConnector" class="org.settings4j.connector.PropertyFileConnector"> 63 * <param name="propertyFromPath" 64 * value="classpath:org/settings4j/config/propertyFile.properties" /> 65 * </connector> 66 * 67 * </settings4j:configuration> 68 * </pre> 69 * 70 * @author Harald.Brabenetz 71 */ 72 public class PropertyFileConnector extends AbstractPropertyConnector { 73 74 /** General Logger for this Class. */ 75 private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(PropertyFileConnector.class); 76 77 private Properties property = new Properties(); 78 79 80 /** {@inheritDoc} */ 81 @Override 82 protected String getProperty(final String key, final String def) { 83 return property.getProperty(key, def); 84 } 85 86 public void setProperty(final Properties property) { 87 this.property = property; 88 } 89 90 /** 91 * @param content The byte[] Content of a Property-File. 92 */ 93 public void setPropertyFromContent(final byte[] content) { 94 final Properties tmpProperty = new Properties(); 95 try { 96 tmpProperty.load(new ByteArrayInputStream(content)); 97 property = tmpProperty; 98 } catch (final IOException e) { 99 LOG.error(e.getMessage(), e); 100 } 101 } 102 103 /** 104 * @param propertyPath The filepath to a Property-File. Supported prefixes: "file:" and "classpath:". 105 */ 106 public void setPropertyFromPath(final String propertyPath) { 107 if (StringUtils.isEmpty(propertyPath)) { 108 throw new IllegalArgumentException("The Property Path cannot be empty"); 109 } 110 111 if (propertyPath.startsWith(FSContentResolver.FILE_URL_PREFIX)) { 112 setPropertyFromContent(new FSContentResolver().getContent(propertyPath)); 113 } else if (propertyPath.startsWith(ClasspathContentResolver.CLASSPATH_URL_PREFIX)) { 114 setPropertyFromContent(new ClasspathContentResolver().getContent(propertyPath)); 115 } else { 116 throw new IllegalArgumentException("The Property Path must start with 'file:' or 'classpath:'. " // 117 + "But the File Property Path was: '" + propertyPath + "'."); 118 } 119 } 120 }