1 /* 2 * #%L 3 * settings4j 4 * =============================================================== 5 * Copyright (C) 2008 - 2015 Brabenetz Harald, Austria 6 * =============================================================== 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * #L% 19 */ 20 package org.settings4j.helper.spring; 21 22 import org.settings4j.Settings4j; 23 import org.springframework.beans.factory.FactoryBean; 24 import org.springframework.beans.factory.InitializingBean; 25 26 27 /** 28 * BeanFactory which gets the Object from {@link Settings4j#getObject(String)}.<br> 29 * This can also be used for DataSource-Objects with the JNDI-Name as Key. 30 * <h2>Simple SpringBean Example:</h2> 31 * <p> 32 * The following Example can be configured with the settings4j-Key "env/MyVariable".<br> 33 * e.g.: System.setProperty("env/MyVariable", "Hallo World");<br> 34 * OR with JNDI-Context OR in a Classpath-File under "classpath:env/MyVariable" Or with a custom {@link org.settings4j.Connector}-Implementation. 35 * </p> 36 * 37 * <pre> 38 * <bean id="MyConfigurableValue" class="org.settings4j.helper.spring.Settings4jFactoryBean"> 39 * <property name="key"><value><b>env/MyVariable</b></value></property> 40 * </bean> 41 * </pre> 42 * 43 * <h2>More complex SpringBean Example:</h2> 44 * <p> 45 * This Example shows how a Hibernate SessionFactory can optional customized with additional HibernateProperties. 46 * </p> 47 * 48 * <pre> 49 * <bean id="hibernateProperties" 50 * class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 51 * <property name="locations"> 52 * <list> 53 * <value>classpath:org/settings4j/helper/spring/DefaultHibernate.properties</value> 54 * <bean class="org.settings4j.helper.spring.Settings4jFactoryBean"> 55 * <property name="key"> 56 * <value><b>env/MyCustomProprtiesLocationKey</b></value> 57 * </property> 58 * <property name="defaultObject"> 59 * <!-- This could also be an empty Property-File But it must Exist. --> 60 * <!-- PropertiesFactoryBean cannot handle invalid Paths. --> 61 * <value>classpath:org/settings4j/helper/spring/DefaultHibernate.properties</value> 62 * </property> 63 * </bean> 64 * </list> 65 * </property> 66 * </bean> 67 * 68 * <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 69 * <property name="dataSource" ref="db2Datasource2" /> 70 * <property name="hibernateProperties" ref="hibernateProperties" /> 71 * .... 72 * </bean> 73 * </pre> 74 * 75 * @author Harald.Brabenetz 76 */ 77 public class Settings4jFactoryBean implements FactoryBean<Object>, InitializingBean { 78 79 private String key; 80 private boolean singleton = true; 81 private Object singletonObject; 82 private Object defaultObject; 83 private Class<?> expectedType; 84 85 public String getKey() { 86 return this.key; 87 } 88 89 /** 90 * @param key - the Setting4j-Key for Your Configuration-Value. 91 */ 92 public void setKey(final String key) { 93 this.key = key; 94 } 95 96 /** 97 * @param singleton - If <code>true</code>, the Value will be read during SpringBean-Initialization. Default is 98 * <code>true</code> 99 */ 100 public void setSingleton(final boolean singleton) { 101 this.singleton = singleton; 102 } 103 104 /** 105 * Specify the type that the configured object is supposed to be assignable to, if any. 106 * 107 * @param expectedType the expected {@link Class}. 108 */ 109 public void setExpectedType(final Class<?> expectedType) { 110 this.expectedType = expectedType; 111 } 112 113 /** 114 * Return the type that the configured object is supposed to be assignable to, if any. 115 * 116 * @return the type that the configured object is supposed to be assignable to. 117 */ 118 public Class<?> getExpectedType() { 119 return this.expectedType; 120 } 121 122 123 public Object getDefaultObject() { 124 return this.defaultObject; 125 } 126 127 128 public void setDefaultObject(final Object defaultObject) { 129 this.defaultObject = defaultObject; 130 } 131 132 @Override 133 public Object getObject() { 134 if (this.singleton) { 135 return this.singletonObject; 136 } 137 return getSettings4jObject(); 138 } 139 140 @Override 141 public Class<?> getObjectType() { 142 final Object obj = getObject(); 143 if (obj != null) { 144 return obj.getClass(); 145 } 146 return getExpectedType(); 147 } 148 149 @Override 150 public boolean isSingleton() { 151 return this.singleton; 152 } 153 154 @Override 155 public void afterPropertiesSet() { 156 if (this.singleton) { 157 this.singletonObject = getSettings4jObject(); 158 } 159 } 160 161 private Object getSettings4jObject() { 162 Object result = Settings4j.getObject(this.key); 163 if (result == null) { 164 result = Settings4j.getString(this.key); 165 } 166 167 if (result == null) { 168 result = this.defaultObject; 169 } 170 return result; 171 } 172 173 }