View Javadoc
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   *  &lt;bean id="MyConfigurableValue" class="org.settings4j.helper.spring.Settings4jFactoryBean"&gt;
39   *      &lt;property name="key"&gt;&lt;value&gt;<b>env/MyVariable</b>&lt;/value&gt;&lt;/property&gt;
40   *  &lt;/bean&gt;
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   *  &lt;bean id="hibernateProperties"
50   *      class="org.springframework.beans.factory.config.PropertiesFactoryBean"&gt;
51   *      &lt;property name="locations"&gt;
52   *          &lt;list&gt;
53   *              &lt;value&gt;classpath:org/settings4j/helper/spring/DefaultHibernate.properties&lt;/value&gt;
54   *              &lt;bean class="org.settings4j.helper.spring.Settings4jFactoryBean"&gt;
55   *                  &lt;property name="key"&gt;
56   *                      &lt;value&gt;<b>env/MyCustomProprtiesLocationKey</b>&lt;/value&gt;
57   *                  &lt;/property&gt;
58   *                  &lt;property name="defaultObject"&gt;
59   *                      &lt;!-- This could also be an empty Property-File But it must Exist. --&gt;
60   *                      &lt;!-- PropertiesFactoryBean cannot handle invalid Paths. --&gt;
61   *                      &lt;value&gt;classpath:org/settings4j/helper/spring/DefaultHibernate.properties&lt;/value&gt;
62   *                  &lt;/property&gt;
63   *              &lt;/bean&gt;
64   *          &lt;/list&gt;
65   *      &lt;/property&gt;
66   *  &lt;/bean&gt;
67   *
68   *  &lt;bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
69   *      &lt;property name="dataSource" ref="db2Datasource2" /&gt;
70   *      &lt;property name="hibernateProperties" ref="hibernateProperties" /&gt;
71   *      ....
72   *  &lt;/bean&gt;
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 }