KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > support > ApplicationObjectSupport


1 /*
2  * Copyright 2002-2006 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.springframework.context.support;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import org.springframework.beans.BeansException;
23 import org.springframework.context.ApplicationContext;
24 import org.springframework.context.ApplicationContextAware;
25 import org.springframework.context.ApplicationContextException;
26
27 /**
28  * Convenient superclass for application objects that want to be aware of
29  * the application context, e.g. for custom lookup of collaborating beans
30  * or for context-specific resource access. It saves the application
31  * context reference and provides an initialization callback method.
32  * Furthermore, it offers numerous convenience methods for message lookup.
33  *
34  * <p>There is no requirement to subclass this class: It just makes things
35  * a little easier if you need access to the context, e.g. for access to
36  * file resources or to the message source. Note that many application
37  * objects do not need to be aware of the application context at all,
38  * as they can receive collaborating beans via bean references.
39  *
40  * <p>Many framework classes are derived from this class, particularly
41  * within the web support.
42  *
43  * @author Rod Johnson
44  * @author Juergen Hoeller
45  * @see org.springframework.web.context.support.WebApplicationObjectSupport
46  */

47 public abstract class ApplicationObjectSupport implements ApplicationContextAware {
48     
49     /** Logger that is available to subclasses */
50     protected final Log logger = LogFactory.getLog(getClass());
51     
52     /** ApplicationContext this object runs in */
53     private ApplicationContext applicationContext;
54
55     /** MessageSourceAccessor for easy message access */
56     private MessageSourceAccessor messageSourceAccessor;
57
58
59     public final void setApplicationContext(ApplicationContext context) throws BeansException {
60         if (context == null && !isContextRequired()) {
61             // Reset internal context state.
62
this.applicationContext = null;
63             this.messageSourceAccessor = null;
64         }
65         else if (this.applicationContext == null) {
66             // Initialize with passed-in context.
67
if (!requiredContextClass().isInstance(context)) {
68                 throw new ApplicationContextException(
69                         "Invalid application context: needs to be of type [" + requiredContextClass().getName() + "]");
70             }
71             this.applicationContext = context;
72             this.messageSourceAccessor = new MessageSourceAccessor(context);
73             initApplicationContext();
74         }
75         else {
76             // Ignore reinitialization if same context passed in.
77
if (this.applicationContext != context) {
78                 throw new ApplicationContextException(
79                         "Cannot reinitialize with different application context: current one is [" +
80                         this.applicationContext + "], passed-in one is [" + context + "]");
81             }
82         }
83     }
84
85     /**
86      * Determine whether this application object needs to run in an ApplicationContext.
87      * <p>Default is "false". Can be overridden to enforce running in a context
88      * (i.e. to throw IllegalStateException on accessors if outside a context).
89      * @see #getApplicationContext
90      * @see #getMessageSourceAccessor
91      */

92     protected boolean isContextRequired() {
93         return false;
94     }
95
96     /**
97      * Determine the context class that any context passed to
98      * <code>setApplicationContext</code> must be an instance of.
99      * Can be overridden in subclasses.
100      * @see #setApplicationContext
101      */

102     protected Class JavaDoc requiredContextClass() {
103         return ApplicationContext.class;
104     }
105
106     /**
107      * Subclasses can override this for custom initialization behavior.
108      * Gets called by <code>setApplicationContext</code> after setting the context instance.
109      * <p>Note: Does </i>not</i> get called on reinitialization of the context
110      * but rather just on first initialization of this object's context reference.
111      * @throws ApplicationContextException in case of initialization errors
112      * @throws BeansException if thrown by ApplicationContext methods
113      * @see #setApplicationContext
114      */

115     protected void initApplicationContext() throws BeansException {
116     }
117
118
119     /**
120      * Return the ApplicationContext instance used by this object.
121      */

122     public final ApplicationContext getApplicationContext() throws IllegalStateException JavaDoc {
123         if (this.applicationContext == null && isContextRequired()) {
124             throw new IllegalStateException JavaDoc(
125                     "ApplicationObjectSupport instance [" + this + "] does not run in an ApplicationContext");
126         }
127         return applicationContext;
128     }
129
130     /**
131      * Return a MessageSourceAccessor for the application context
132      * used by this object, for easy message access.
133      * @throws IllegalStateException if not running in an ApplicationContext
134      */

135     protected final MessageSourceAccessor getMessageSourceAccessor() throws IllegalStateException JavaDoc {
136         if (this.messageSourceAccessor == null && isContextRequired()) {
137             throw new IllegalStateException JavaDoc(
138                     "ApplicationObjectSupport instance [" + this + "] does not run in an ApplicationContext");
139         }
140         return this.messageSourceAccessor;
141     }
142
143 }
144
Popular Tags