KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > target > AbstractLazyCreationTargetSource


1 /*
2  * Copyright 2002-2007 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.aop.target;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import org.springframework.aop.TargetSource;
23
24 /**
25  * {@link org.springframework.aop.TargetSource} implementation that will
26  * lazily create a user-managed object.
27  *
28  * <p>Creation of the lazy target object is controlled by the user by implementing
29  * the {@link #createObject()} method. This <code>TargetSource</code> will invoke
30  * this method the first time the proxy is accessed.
31  *
32  * <p>Useful when you need to pass a reference to some dependency to an object
33  * but you don't actually want the dependency to be created until it is first used.
34  * A typical scenario for this is a connection to a remote resource.
35  *
36  * @author Rob Harrop
37  * @author Juergen Hoeller
38  * @since 1.2.4
39  * @see #isInitialized()
40  * @see #createObject()
41  */

42 public abstract class AbstractLazyCreationTargetSource implements TargetSource {
43
44     /** Logger available to subclasses */
45     protected final Log logger = LogFactory.getLog(getClass());
46
47     /** The lazily initialized target object */
48     private Object JavaDoc lazyTarget;
49
50
51     /**
52      * Return whether the lazy target object of this TargetSource
53      * has already been fetched.
54      */

55     public synchronized boolean isInitialized() {
56         return (this.lazyTarget != null);
57     }
58
59     /**
60      * This default implementation returns <code>null</code> if the
61      * target is <code>null</code> (it is hasn't yet been initialized),
62      * or the target class if the target has already been initialized.
63      * <p>Subclasses may wish to override this method in order to provide
64      * a meaningful value when the target is still <code>null</code>.
65      * @see #isInitialized()
66      */

67     public synchronized Class JavaDoc getTargetClass() {
68         return (this.lazyTarget != null ? this.lazyTarget.getClass() : null);
69     }
70
71     public boolean isStatic() {
72         return false;
73     }
74
75     /**
76      * Returns the lazy-initialized target object,
77      * creating it on-the-fly if it doesn't exist already.
78      * @see #createObject()
79      */

80     public synchronized Object JavaDoc getTarget() throws Exception JavaDoc {
81         if (this.lazyTarget == null) {
82             logger.debug("Initializing lazy target object");
83             this.lazyTarget = createObject();
84         }
85         return this.lazyTarget;
86     }
87
88     public void releaseTarget(Object JavaDoc target) throws Exception JavaDoc {
89         // nothing to do
90
}
91
92
93     /**
94      * Subclasses should implement this method to return the lazy initialized object.
95      * Called the first time the proxy is invoked.
96      * @return the created object
97      * @throws Exception if creation failed
98      */

99     protected abstract Object JavaDoc createObject() throws Exception JavaDoc;
100
101 }
102
Popular Tags