1 package net.sf.dobo.spring;
2
3 import net.sf.dobo.Context;
4 import net.sf.dobo.Dobo;
5
6 import org.springframework.beans.BeansException;
7 import org.springframework.beans.factory.BeanFactory;
8 import org.springframework.beans.factory.BeanFactoryAware;
9 import org.springframework.beans.factory.BeanNameAware;
10 import org.springframework.beans.factory.FactoryBean;
11
12 import java.lang.annotation.Annotation;
13
14
15 /***
16 * FactoryBean of Dobo Object
17 *
18 * @author arif
19 * @version 1.0
20 */
21 public class DoboFactoryBean implements FactoryBean, BeanFactoryAware, BeanNameAware {
22 /***
23 * Context Implementation Object
24 */
25 private Object contextImplementationObject;
26
27 /***
28 * The context
29 */
30 private Class<?extends Annotation> context;
31
32 /***
33 * Flag indicating bean is singleton
34 */
35 private boolean singleton = false;
36
37 /***
38 * The instance of proxy
39 */
40 private Object instance = null;
41
42 /***
43 * Name of the dobo object
44 */
45 private String name;
46
47 /***
48 * Bean factory
49 */
50 private BeanFactory beanFactory;
51
52 /***
53 * @see org.springframework.beans.factory.FactoryBean#getObject()
54 */
55 public Object getObject() throws Exception {
56 synchronized (this) {
57 if (singleton && (instance == null)) {
58 instance = Dobo.instantiate(contextImplementationObject, context);
59
60 return instance;
61 } else {
62 return Dobo.instantiate(contextImplementationObject, context);
63 }
64 }
65 }
66
67 /***
68 * @see org.springframework.beans.factory.FactoryBean#getObjectType()
69 */
70 public Class getObjectType() {
71 return context.getAnnotation(Context.class).value();
72 }
73
74 /***
75 * @see org.springframework.beans.factory.FactoryBean#isSingleton()
76 */
77 public boolean isSingleton() {
78 return singleton;
79 }
80
81 /***
82 * Set to indicate object is singleton
83 *
84 * @param singleton
85 */
86 public void setSingleton(final boolean singleton) {
87 this.singleton = singleton;
88 }
89
90 /***
91 * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
92 */
93 public void setBeanFactory(final BeanFactory beanFactory)
94 throws BeansException {
95 if (contextImplementationObject == null) {
96 throw new NullPointerException(
97 "Please set \"contextImplementationObject\" property of bean " + name);
98 }
99
100 if (context == null) {
101 throw new NullPointerException("Please set \"context\" property of bean " + name);
102 }
103
104 Dobo.check(contextImplementationObject.getClass());
105 this.beanFactory = beanFactory;
106 }
107
108 /***
109 * @param context
110 */
111 public void setContext(final Class<?extends Annotation> context) {
112 this.context = context;
113 }
114
115 /***
116 * @param contextImplementationObject
117 */
118 public void setContextImplementationObject(final Object contextImplementationObject) {
119 this.contextImplementationObject = contextImplementationObject;
120 }
121
122 /***
123 * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String)
124 */
125 public void setBeanName(final String name) {
126 this.name = name;
127 }
128 }