/* ***************************************************************************
 * Copyright (c) 2010 BearingPoint INFONOVA GmbH, Austria.
 *
 * This software is the confidential and proprietary information of
 * BearingPoint INFONOVA GmbH, Austria. You shall not disclose such
 * Confidential Information and shall use it only in accordance with the
 * terms of the license agreement you entered into with INFONOVA.
 *
 * BEARINGPOINT INFONOVA MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
 * A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. BEARINGPOINT INFONOVA SHALL
 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *****************************************************************************/

package org.settings4j.helper.spring;

import org.settings4j.Settings4j;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;


/**
 * BeanFactory which gets the Object from {@link Settings4j#getObject(String)}.
 * <p>
 * TODO brabenetz Apr 11, 2011 : create Description, Examples and UnitTests.
 * 
 * 
 * @author <a href="mailto:harald.brabenetz@infonova.com">Harald Brabenetz (hbrabenetz)</a>
 *
 */
public class Settings4jFactoryBean implements FactoryBean, InitializingBean {

    private String key;
    private boolean singleton = true;
    private Object singletonObject;
    private Class expectedType;

    public String getKey() {
        return key;
    }
    
    public void setKey(String key) {
        this.key = key;
    }

    public void setSingleton(boolean singleton) {
        this.singleton = singleton;
    }
    
    /**
     * Specify the type that the located JNDI object is supposed
     * to be assignable to, if any.
     * 
     * @param expectedType
     */
    public void setExpectedType(Class expectedType) {
        this.expectedType = expectedType;
    }

    /**
     * Return the type that the located JNDI object is supposed
     * to be assignable to, if any.
     * 
     * @return the type that the located JNDI object is supposed
     * to be assignable to.
     */
    public Class getExpectedType() {
        return this.expectedType;
    }

    /** {@inheritDoc} */
    public Object getObject() {
        if (singleton) {
            return singletonObject;
        }
        return Settings4j.getObject(key);
    }

    /** {@inheritDoc} */
    public Class getObjectType() {
        Object obj = getObject();
        if (obj != null) {
            return obj.getClass();
        }
        return getExpectedType();
    }

    /** {@inheritDoc} */
    public boolean isSingleton() {
        return singleton;
    }

    /** {@inheritDoc} */
    public void afterPropertiesSet() throws Exception {
        if (singleton) {
            singletonObject = Settings4j.getObject(key);
        }
    }

}

@@ -1212,85 +1212,628 @@
 %0A * 
-TODO brabenetz Apr 11, 2011 : create Description, Examples and UnitTests.%0A * 
+%3Ch2%3ESimple SpringBean Example:%3C/h2%3E%0A * The following Example can be configured with the settings4j-Key %22env/MyVariable%22.%3Cbr /%3E%0A * e.g.: System.setProperty(%22env/MyVariable%22, %22Hallo World%22);%3Cbr /%3E%0A * OR with JNDI-Context OR in a Classpath-File under %22classpath:env/MyVariable%22%0A * Or with a custom %7B@link org.settings4j.Connector%7D-Implementation.%0A * %3Cpre%3E%0A *  &lt;bean id=%22MyConfigurableValue%22 class=%22org.settings4j.helper.spring.Settings4jFactoryBean%22&gt;%0A *      &lt;property name=%22key%22&gt;&lt;value&gt;env/MyVariable&lt;/value&gt;&lt;/property&gt;%0A *  &lt;/bean&gt;%0A * %3C/pre%3E%0A * %3Ch2%3EMore complex SpringBean Example:%3C/h2%3E
 %0A * 
@@ -2210,71 +2210,305 @@
     
-public void setKey(String key) %7B%0A        this.key = key;%0A    %7D%0A
+/**%0A     * @param key - the Setting4j-Key for Your Configuration-Value.%0A     */%0A    public void setKey(String key) %7B%0A        this.key = key;%0A    %7D%0A%0A    /**%0A     * @param singleton - If %3Ccode%3Etrue%3C/code%3E, the Value will be read during SpringBean-Initialization. Default is %3Ccode%3Etrue%3C/code%3E%0A     */
 %0A   
@@ -2638,36 +2638,34 @@
 pe that the 
-located JNDI
+configured
  object is s
@@ -2890,36 +2890,34 @@
 pe that the 
-located JNDI
+configured
  object is s
@@ -3005,20 +3005,18 @@
 the 
-located JNDI
+configured
  obj
@@ -3288,16 +3288,19 @@
  return 
+get
 Settings
@@ -3297,38 +3297,31 @@
 etSettings4j
-.get
 Object(
-key
 );%0A    %7D%0A%0A  
@@ -3758,44 +3758,261 @@
 t = 
-Settings4j.getObject(key);%0A        %7D
+getSettings4jObject();%0A        %7D%0A    %7D%0A%0A    private Object getSettings4jObject() %7B%0A        Object result = Settings4j.getObject(key);%0A        if (result == null) %7B%0A            result = Settings4j.getString(key);%0A        %7D%0A        %0A        return result;
 %0A   

add Javadoc