1 package net.sf.dobo.guice;
2
3 import com.google.inject.Provider;
4
5 import net.sf.dobo.Context;
6 import net.sf.dobo.Dobo;
7
8 import java.lang.annotation.Annotation;
9
10
11 /***
12 * Google guice provider
13 *
14 * @author arif
15 * @version 1.0
16 *
17 * @param <T> Type of Provider
18 */
19 public class DoboProvider<T> implements Provider<T> {
20 /***
21 * Context Implementation Object
22 */
23 private Object contextImplementationObject;
24
25 /***
26 * The context
27 */
28 private Class<?extends Annotation> context;
29
30 /***
31 * Flag indicating bean is singleton
32 */
33 private boolean singleton = false;
34
35 /***
36 * The instance of proxy
37 */
38 private T instance = null;
39
40 /***
41 * The Factory Constructor
42 * @param contextImplementationObject
43 * @param context
44 */
45 public DoboProvider(final Object contextImplementationObject,
46 final Class<?extends Annotation> context) {
47 this.contextImplementationObject = contextImplementationObject;
48 this.context = context;
49 Dobo.check(contextImplementationObject.getClass());
50 }
51
52 /***
53 * @see org.springframework.beans.factory.FactoryBean#getObject()
54 */
55 @SuppressWarnings("unchecked")
56 public T get() {
57 synchronized (this) {
58 if (singleton && (instance == null)) {
59 instance = (T) Dobo.instantiate(contextImplementationObject, context);
60
61 return instance;
62 } else {
63 return (T) Dobo.instantiate(contextImplementationObject, context);
64 }
65 }
66 }
67
68 /***
69 * @see org.springframework.beans.factory.FactoryBean#getObjectType()
70 */
71 public Class<?> getObjectType() {
72 return context.getAnnotation(Context.class).value();
73 }
74
75 /***
76 * @see org.springframework.beans.factory.FactoryBean#isSingleton()
77 */
78 public boolean isSingleton() {
79 return singleton;
80 }
81
82 /***
83 * Set to indicate object is singleton
84 *
85 * @param singleton
86 */
87 public DoboProvider<T> setSingleton(final boolean singleton) {
88 this.singleton = singleton;
89
90 return this;
91 }
92 }