View Javadoc

1   /*
2    * Copyright 2004-2009 the original author or authors.
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  
17  package org.springmodules.validation.util.context;
18  
19  import org.springframework.beans.BeansException;
20  import org.springframework.beans.factory.BeanFactory;
21  import org.springframework.beans.factory.BeanFactoryAware;
22  import org.springframework.context.ApplicationContext;
23  import org.springframework.context.ApplicationContextAware;
24  import org.springframework.context.ApplicationEventPublisher;
25  import org.springframework.context.ApplicationEventPublisherAware;
26  import org.springframework.context.MessageSource;
27  import org.springframework.context.MessageSourceAware;
28  import org.springframework.context.ResourceLoaderAware;
29  import org.springframework.core.io.ResourceLoader;
30  
31  /**
32   * @author Uri Boness
33   */
34  public class BasicContextAware implements ContextAware {
35  
36      protected ApplicationContext applicationContext = null;
37      protected BeanFactory beanFactory = null;
38      protected ResourceLoader resourceLoader = null;
39      protected MessageSource messageSource = null;
40      protected ApplicationEventPublisher applicationEventPublisher = null;
41  
42      /**
43       * Implementation of <code>BeanFactoryAware</code>.
44       */
45      public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
46          this.beanFactory = beanFactory;
47      }
48  
49      /**
50       * Implementation of <code>ApplicationContextAware</code>.
51       */
52      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
53          this.applicationContext = applicationContext;
54      }
55  
56      /**
57       * Implementation of <code>ResourceLoaderAware</code>.
58       */
59      public void setResourceLoader(ResourceLoader resourceLoader) {
60          this.resourceLoader = resourceLoader;
61      }
62  
63      /**
64       * Implementation of <code>MessageSourceAware</code>.
65       */
66      public void setMessageSource(MessageSource messageSource) {
67          this.messageSource = messageSource;
68      }
69  
70      /**
71       * Implementation of <code>ApplicationEventPublisherAware</code>.
72       */
73      public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
74          this.applicationEventPublisher = applicationEventPublisher;
75      }
76  
77  
78      protected void initLifecycle(BasicContextAware object) {
79          object.setBeanFactory(beanFactory);
80          object.setApplicationContext(applicationContext);
81          object.setResourceLoader(resourceLoader);
82          object.setMessageSource(messageSource);
83          object.setApplicationEventPublisher(applicationEventPublisher);
84      }
85  
86      protected void initLifecycle(Object object) {
87          if (object == null) {
88              return;
89          }
90          if (object instanceof BeanFactoryAware) {
91              ((BeanFactoryAware) object).setBeanFactory(beanFactory);
92          }
93          if (object instanceof ApplicationContextAware) {
94              ((ApplicationContextAware) object).setApplicationContext(applicationContext);
95          }
96          if (object instanceof ResourceLoaderAware) {
97              ((ResourceLoaderAware) object).setResourceLoader(resourceLoader);
98          }
99          if (object instanceof MessageSourceAware) {
100             ((MessageSourceAware) object).setMessageSource(messageSource);
101         }
102         if (object instanceof ApplicationEventPublisherAware) {
103             ((ApplicationEventPublisherAware) object).setApplicationEventPublisher(applicationEventPublisher);
104         }
105     }
106     
107 }