package net.sf.dobo.guice; import com.google.inject.Provider; import net.sf.dobo.Context; import net.sf.dobo.Dobo; import java.lang.annotation.Annotation; /** * Google guice provider * * @author arif * @version 1.0 * * @param Type of Provider */ public class DoboProvider implements Provider { /** * 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 T instance = null; /** * The Factory Constructor * @param contextImplementationObject * @param context */ public DoboProvider(final Object contextImplementationObject, final Class context) { this.contextImplementationObject = contextImplementationObject; this.context = context; Dobo.check(contextImplementationObject.getClass()); } /** * @see org.springframework.beans.factory.FactoryBean#getObject() */ @SuppressWarnings("unchecked") public T get() { synchronized (this) { if (singleton && (instance == null)) { instance = (T) Dobo.instantiate(contextImplementationObject, context); return instance; } else { return (T) 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 DoboProvider setSingleton(final boolean singleton) { this.singleton = singleton; return this; } }