View Javadoc
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.helper.spring;
18  
19  import org.settings4j.Settings4j;
20  import org.springframework.beans.factory.FactoryBean;
21  import org.springframework.beans.factory.InitializingBean;
22  
23  
24  /**
25   * BeanFactory which gets the Object from {@link Settings4j#getObject(String)}.<br />
26   * This can also be used for DataSource-Objects with the JNDI-Name as Key.
27   * 
28   * <p>
29   * <h2>Simple SpringBean Example:</h2> The following Example can be configured with the settings4j-Key "env/MyVariable".
30   * <br />
31   * e.g.: System.setProperty("env/MyVariable", "Hallo World");<br />
32   * OR with JNDI-Context OR in a Classpath-File under "classpath:env/MyVariable" Or with a custom
33   * {@link org.settings4j.Connector}-Implementation.
34   * 
35   * <pre>
36   *  &lt;bean id="MyConfigurableValue" class="org.settings4j.helper.spring.Settings4jFactoryBean"&gt;
37   *      &lt;property name="key"&gt;&lt;value&gt;<b>env/MyVariable</b>&lt;/value&gt;&lt;/property&gt;
38   *  &lt;/bean&gt;
39   * </pre>
40   * 
41   * <h2>More complex SpringBean Example:</h2> This Example shows how a Hibernate SessionFactory can optional customized
42   * with additional HibernateProperties.
43   * 
44   * <pre>
45   *  &lt;bean id="hibernateProperties"
46   *      class="org.springframework.beans.factory.config.PropertiesFactoryBean"&gt;
47   *      &lt;property name="locations"&gt;
48   *          &lt;list&gt;
49   *              &lt;value&gt;classpath:org/settings4j/helper/spring/DefaultHibernate.properties&lt;/value&gt;
50   *              &lt;bean class="org.settings4j.helper.spring.Settings4jFactoryBean"&gt;
51   *                  &lt;property name="key"&gt;
52   *                      &lt;value&gt;<b>env/MyCustomProprtiesLocationKey</b>&lt;/value&gt;
53   *                  &lt;/property&gt;
54   *                  &lt;property name="defaultObject"&gt;
55   *                      &lt;!-- This could also be an empty Property-File But it must Exist. --&gt;
56   *                      &lt;!-- PropertiesFactoryBean cannot handle invalid Paths. --&gt;
57   *                      &lt;value&gt;classpath:org/settings4j/helper/spring/DefaultHibernate.properties&lt;/value&gt;
58   *                  &lt;/property&gt;
59   *              &lt;/bean&gt;
60   *          &lt;/list&gt;
61   *      &lt;/property&gt;
62   *  &lt;/bean&gt;
63   *  
64   *  &lt;bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
65   *      &lt;property name="dataSource" ref="db2Datasource2" /&gt;
66   *      &lt;property name="hibernateProperties" ref="hibernateProperties" /&gt;
67   *      ....
68   *  &lt;/bean&gt;
69   * </pre>
70   * 
71   * @author Harald.Brabenetz
72   */
73  public class Settings4jFactoryBean implements FactoryBean, InitializingBean {
74  
75      private String key;
76      private boolean singleton = true;
77      private Object singletonObject;
78      private Object defaultObject;
79      private Class expectedType;
80  
81      public String getKey() {
82          return this.key;
83      }
84  
85      /**
86       * @param key - the Setting4j-Key for Your Configuration-Value.
87       */
88      public void setKey(final String key) {
89          this.key = key;
90      }
91  
92      /**
93       * @param singleton - If <code>true</code>, the Value will be read during SpringBean-Initialization. Default is
94       *            <code>true</code>
95       */
96      public void setSingleton(final boolean singleton) {
97          this.singleton = singleton;
98      }
99  
100     /**
101      * Specify the type that the configured object is supposed to be assignable to, if any.
102      * 
103      * @param expectedType the expected {@link Class}.
104      */
105     public void setExpectedType(final Class expectedType) {
106         this.expectedType = expectedType;
107     }
108 
109     /**
110      * Return the type that the configured object is supposed to be assignable to, if any.
111      * 
112      * @return the type that the configured object is supposed to be assignable to.
113      */
114     public Class getExpectedType() {
115         return this.expectedType;
116     }
117 
118 
119     public Object getDefaultObject() {
120         return this.defaultObject;
121     }
122 
123 
124     public void setDefaultObject(final Object defaultObject) {
125         this.defaultObject = defaultObject;
126     }
127 
128     /** {@inheritDoc} */
129     public Object getObject() {
130         if (this.singleton) {
131             return this.singletonObject;
132         }
133         return getSettings4jObject();
134     }
135 
136     /** {@inheritDoc} */
137     public Class getObjectType() {
138         final Object obj = getObject();
139         if (obj != null) {
140             return obj.getClass();
141         }
142         return getExpectedType();
143     }
144 
145     /** {@inheritDoc} */
146     public boolean isSingleton() {
147         return this.singleton;
148     }
149 
150     /** {@inheritDoc} */
151     public void afterPropertiesSet() {
152         if (this.singleton) {
153             this.singletonObject = getSettings4jObject();
154         }
155     }
156 
157     private Object getSettings4jObject() {
158         Object result = Settings4j.getObject(this.key);
159         if (result == null) {
160             result = Settings4j.getString(this.key);
161         }
162 
163         if (result == null) {
164             result = this.defaultObject;
165         }
166         return result;
167     }
168 
169 }