KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > target > dynamic > AbstractRefreshableTargetSource


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.aop.target.dynamic;
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  * Abstract TargetSource implementation that wraps a refreshable target object.
26  * Subclasses can determine whether a refresh is required, and need to provide
27  * fresh target objects.
28  *
29  * <p>Implements the Refreshable interface for explicit control.
30  *
31  * @author Rod Johnson
32  * @author Rob Harrop
33  * @author Juergen Hoeller
34  * @since 2.0
35  */

36 public abstract class AbstractRefreshableTargetSource implements TargetSource, Refreshable {
37
38     /** Logger available to subclasses */
39     protected Log logger = LogFactory.getLog(getClass());
40
41     protected Object JavaDoc targetObject;
42
43     private long refreshCheckDelay = -1;
44
45     private long lastRefreshCheck = -1;
46
47     private long lastRefreshTime = -1;
48
49     private long refreshCount = 0;
50
51
52     /**
53      * Set the delay between refresh checks, in milliseconds.
54      * Default is -1, indicating no refresh checks at all.
55      * <p>Note that an actual refresh will only happen when
56      * <code>requiresRefresh()</code> returns <code>true</code>.
57      * @see #requiresRefresh()
58      */

59     public void setRefreshCheckDelay(long refreshCheckDelay) {
60         this.refreshCheckDelay = refreshCheckDelay;
61     }
62
63
64     public Class JavaDoc getTargetClass() {
65         if (this.targetObject == null) {
66             refresh();
67         }
68         return this.targetObject.getClass();
69     }
70
71     /**
72      * Not static.
73      */

74     public boolean isStatic() {
75         return false;
76     }
77
78     public final synchronized Object JavaDoc getTarget() {
79         if ((refreshCheckDelayElapsed() && requiresRefresh()) || this.targetObject == null) {
80             refresh();
81         }
82         return this.targetObject;
83     }
84
85     /**
86      * No need to release target.
87      */

88     public void releaseTarget(Object JavaDoc object) {
89     }
90
91
92     public final synchronized void refresh() {
93         logger.debug("Attempting to refresh target");
94
95         this.targetObject = freshTarget();
96         this.refreshCount++;
97         this.lastRefreshTime = System.currentTimeMillis();
98
99         logger.debug("Target refreshed successfully");
100     }
101
102     public long getRefreshCount() {
103         return this.refreshCount;
104     }
105
106     public long getLastRefreshTime() {
107         return this.lastRefreshTime;
108     }
109
110
111     private boolean refreshCheckDelayElapsed() {
112         if (this.refreshCheckDelay < 0) {
113             return false;
114         }
115
116         long currentTimeMillis = System.currentTimeMillis();
117
118         if (this.lastRefreshCheck < 0 || currentTimeMillis - this.lastRefreshCheck > this.refreshCheckDelay) {
119             // Going to perform a refresh check - update the time.
120
this.lastRefreshCheck = currentTimeMillis;
121             logger.debug("Refresh check delay elapsed - checking whether refresh is required");
122             return true;
123         }
124
125         return false;
126     }
127
128
129     /**
130      * Determine whether a refresh is required.
131      * Invoked for each refresh check, after the refresh check delay has elapsed.
132      * <p>Default implementation always returns <code>true</code>, trigger
133      * a refresh every time the delay has elapsed. To be overridden by subclasses
134      * with an appropriate check of the underlying target resource.
135      */

136     protected boolean requiresRefresh() {
137         return true;
138     }
139
140     /**
141      * Obtain a fresh target object.
142      * Only invoked if a refresh check has found that a refresh is required
143      * (that is, <code>requiresRefresh</code> has returned <code>true</code>).
144      * @see #requiresRefresh()
145      */

146     protected abstract Object JavaDoc freshTarget();
147
148 }
149
Popular Tags