KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > spring > BeanCreator


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.spring;
17
18 import org.directwebremoting.create.AbstractCreator;
19 import org.springframework.aop.support.AopUtils;
20 import org.springframework.context.ApplicationContextAware;
21 import org.springframework.context.ApplicationContext;
22 import org.springframework.beans.FatalBeanException;
23 import org.springframework.beans.factory.InitializingBean;
24 import org.springframework.util.Assert;
25
26 /**
27  * A creator that proxies to the specified bean. <br>
28  * Note that it can be configured with additional include rules,
29  * exclude rules, filters and authentication rules using the
30  * specified creator configuration.
31  * @see CreatorConfig
32  * @author Bram Smeets
33  */

34 public class BeanCreator extends AbstractCreator implements ApplicationContextAware, InitializingBean {
35
36     /**
37      * Is called by the Spring container after all properties have been set. <br>
38      * It is implemented in order to initialize the beanClass field correctly and to make sure
39      * that either the bean id or the bean itself have been set on this creator.
40      * @see org.springframework.beans.factory.InitializingBean
41      */

42     public void afterPropertiesSet()
43     {
44         // make sure that either the bean or the beanId have been set correctly
45
if (bean != null) {
46             this.beanClass = bean.getClass();
47         } else if (beanId != null) {
48             this.beanClass = applicationContext.getType(beanId);
49         } else {
50             throw new FatalBeanException(
51                     "You should either set the bean property directly or set the beanId property");
52         }
53
54         // make sure to handle cglib proxies correctly
55
if(AopUtils.isCglibProxyClass(this.beanClass)) {
56             this.beanClass = this.beanClass.getSuperclass();
57         }
58     }
59
60     /**
61      * Accessor for the class that this creator allows access to. <br>
62      * It returns the class specified by the <code>beanClass</code>
63      * property. In case no class name has been set, it returns the
64      * class of the specified bean.
65      * @return the type of this allowed class
66      */

67     public Class JavaDoc getType()
68     {
69         return beanClass;
70     }
71
72     /**
73      * Accessor for the instance of this creator. <br>
74      * It returns the specified bean property.
75      * @return the bean instance of this creator
76      */

77     public Object JavaDoc getInstance()
78     {
79         synchronized(monitor) {
80             if (bean == null) {
81                 Assert.notNull(beanId, "The bean id needs to be specified");
82                 bean = applicationContext.getBean(beanId);
83             }
84         }
85
86         return bean;
87     }
88
89     /**
90      * Sets the bean for this bean creator.
91      * @param bean the bean for this creator
92      */

93     public void setBean(Object JavaDoc bean)
94     {
95         this.bean = bean;
96     }
97
98     /**
99      * Sets the bean class for this creator. <br>
100      * Use this property to specify a different class or interface for
101      * instance in case the specified bean is a proxy or implementation
102      * and we want to expose the interface.
103      * @param beanClass the class of the bean to remote
104      */

105     public void setBeanClass(Class JavaDoc beanClass)
106     {
107         this.beanClass = beanClass;
108     }
109
110     /**
111      * Sets the application context. <br>
112      * This method is called by the Spring bean container to set a reference to the
113      * application context.
114      * @param applicationContext the application context
115      */

116     public void setApplicationContext(ApplicationContext applicationContext)
117     {
118         this.applicationContext = applicationContext;
119     }
120
121     /**
122      * Sets the id of the bean to remote using DWR. <br>
123      * Either set this property on the creator, or set the bean to be
124      * remoted directly on this creator.
125      * @param beanId the id of the bean to remote
126      */

127     public void setBeanId(String JavaDoc beanId)
128     {
129         this.beanId = beanId;
130     }
131
132     /**
133      * Sets the configuration for this creator. <br>
134      * Use the configuration to specify include and exclude rules, filters
135      * and/or authentication rules.
136      * @see org.directwebremoting.spring.CreatorConfig
137      * @param config the configuration for this creator
138      */

139     public void setConfig(CreatorConfig config)
140     {
141         this.config = config;
142     }
143
144     /**
145      * Gets the configuration for this creator.
146      * @return the configuration for this creator
147      */

148     public CreatorConfig getConfig()
149     {
150         return config;
151     }
152
153     /**
154      * The bean for this creator.
155      */

156     private Object JavaDoc bean;
157
158     /**
159      * The optional bean class for this creator.
160      */

161     private Class JavaDoc beanClass;
162
163     /**
164      * The optional bean name.
165      */

166     private String JavaDoc beanId;
167
168     /**
169      * The application context that creates this creator.
170      */

171     private ApplicationContext applicationContext;
172
173     /**
174      * The optional creator configuration for this creator.
175      */

176     private CreatorConfig config;
177
178     /** Monitor object to synchronize on during inititalization. */
179     private final Object JavaDoc monitor = new Object JavaDoc();
180 }
181
Popular Tags