KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.Serializable JavaDoc;
20
21 import org.springframework.aop.TargetSource;
22 import org.springframework.util.Assert;
23
24 /**
25  * {@link org.springframework.aop.TargetSource} implementation that
26  * caches a local target object, but allows the target to be swapped
27  * while the application is running.
28  *
29  * <p>If configuring an object of this class in a Spring IoC container,
30  * use constructor injection.
31  *
32  * <p>This TargetSource is serializable if the target is at the time
33  * of serialization.
34  *
35  * @author Rod Johnson
36  * @author Juergen Hoeller
37  */

38 public class HotSwappableTargetSource implements TargetSource, Serializable JavaDoc {
39
40     /** use serialVersionUID from Spring 1.2 for interoperability */
41     private static final long serialVersionUID = 7497929212653839187L;
42
43
44     /** The current target object */
45     private Object JavaDoc target;
46
47
48     /**
49      * Create a new HotSwappableTargetSource with the given initial target object.
50      * @param initialTarget the initial target object
51      */

52     public HotSwappableTargetSource(Object JavaDoc initialTarget) {
53         Assert.notNull(initialTarget, "Target object must not be null");
54         this.target = initialTarget;
55     }
56
57
58     /**
59      * Return the type of the current target object.
60      * <p>The returned type should usually be constant across all target objects.
61      */

62     public synchronized Class JavaDoc getTargetClass() {
63         return this.target.getClass();
64     }
65
66     public final boolean isStatic() {
67         return false;
68     }
69
70     public synchronized Object JavaDoc getTarget() {
71         return this.target;
72     }
73
74     public void releaseTarget(Object JavaDoc target) {
75         // nothing to do
76
}
77
78
79     /**
80      * Swap the target, returning the old target object.
81      * @param newTarget the new target object
82      * @return the old target object
83      * @throws IllegalArgumentException if the new target is invalid
84      */

85     public synchronized Object JavaDoc swap(Object JavaDoc newTarget) throws IllegalArgumentException JavaDoc {
86         Assert.notNull(newTarget, "Target object must not be null");
87         Object JavaDoc old = this.target;
88         this.target = newTarget;
89         return old;
90     }
91
92
93     /**
94      * Two HotSwappableTargetSources are equal if the current target
95      * objects are equal.
96      */

97     public boolean equals(Object JavaDoc other) {
98         return (this == other || (other instanceof HotSwappableTargetSource &&
99                 this.target.equals(((HotSwappableTargetSource) other).target)));
100     }
101
102     public int hashCode() {
103         return HotSwappableTargetSource.class.hashCode();
104     }
105
106     public String JavaDoc toString() {
107         return "HotSwappableTargetSource for target: " + this.target;
108     }
109
110 }
111
Popular Tags