package net.sf.dobo.spring; import net.sf.dobo.Context; import net.sf.dobo.Dobo; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.FactoryBean; import java.lang.annotation.Annotation; /** * FactoryBean of Dobo Object * * @author arif * @version 1.0 */ public class DoboFactoryBean implements FactoryBean, BeanFactoryAware, BeanNameAware { /** * Context Implementation Object */ private Object contextImplementationObject; /** * The context */ private Class context; /** * Flag indicating bean is singleton */ private boolean singleton = false; /** * The instance of proxy */ private Object instance = null; /** * Name of the dobo object */ private String name; /** * Bean factory */ private BeanFactory beanFactory; /** * @see org.springframework.beans.factory.FactoryBean#getObject() */ public Object getObject() throws Exception { synchronized (this) { if (singleton && (instance == null)) { instance = Dobo.instantiate(contextImplementationObject, context); return instance; } else { return Dobo.instantiate(contextImplementationObject, context); } } } /** * @see org.springframework.beans.factory.FactoryBean#getObjectType() */ public Class getObjectType() { return context.getAnnotation(Context.class).value(); } /** * @see org.springframework.beans.factory.FactoryBean#isSingleton() */ public boolean isSingleton() { return singleton; } /** * Set to indicate object is singleton * * @param singleton */ public void setSingleton(final boolean singleton) { this.singleton = singleton; } /** * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory) */ public void setBeanFactory(final BeanFactory beanFactory) throws BeansException { if (contextImplementationObject == null) { throw new NullPointerException( "Please set \"contextImplementationObject\" property of bean " + name); } if (context == null) { throw new NullPointerException("Please set \"context\" property of bean " + name); } Dobo.check(contextImplementationObject.getClass()); this.beanFactory = beanFactory; } /** * @param context */ public void setContext(final Class context) { this.context = context; } /** * @param contextImplementationObject */ public void setContextImplementationObject(final Object contextImplementationObject) { this.contextImplementationObject = contextImplementationObject; } /** * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String) */ public void setBeanName(final String name) { this.name = name; } }