01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20:
21: import org.springframework.aop.TargetSource;
22: import org.springframework.util.Assert;
23: import org.springframework.util.ObjectUtils;
24:
25: /**
26: * Implementation of the {@link org.springframework.aop.TargetSource} interface
27: * that holds a given object. This is the default implementation of the TargetSource
28: * interface, as used by the Spring AOP framework. There is usually no need to
29: * create objects of this class in application code.
30: *
31: * <p>This class is serializable. However, the actual serializability of a
32: * SingletonTargetSource will depend on whether the target is serializable.
33: *
34: * @author Rod Johnson
35: * @author Juergen Hoeller
36: * @see org.springframework.aop.framework.AdvisedSupport#setTarget(Object)
37: */
38: public class SingletonTargetSource implements TargetSource,
39: Serializable {
40:
41: /** use serialVersionUID from Spring 1.2 for interoperability */
42: private static final long serialVersionUID = 9031246629662423738L;
43:
44: /** Target cached and invoked using reflection */
45: private final Object target;
46:
47: /**
48: * Create a new SingletonTargetSource for the given target.
49: * @param target the target object
50: */
51: public SingletonTargetSource(Object target) {
52: Assert.notNull(target, "Target object must not be null");
53: this .target = target;
54: }
55:
56: public Class getTargetClass() {
57: return this .target.getClass();
58: }
59:
60: public Object getTarget() {
61: return this .target;
62: }
63:
64: public void releaseTarget(Object target) {
65: // nothing to do
66: }
67:
68: public boolean isStatic() {
69: return true;
70: }
71:
72: /**
73: * Two invoker interceptors are equal if they have the same target or if the
74: * targets or the targets are equal.
75: */
76: public boolean equals(Object other) {
77: if (this == other) {
78: return true;
79: }
80: if (!(other instanceof SingletonTargetSource)) {
81: return false;
82: }
83: SingletonTargetSource otherTargetSource = (SingletonTargetSource) other;
84: return this .target.equals(otherTargetSource.target);
85: }
86:
87: /**
88: * SingletonTargetSource uses the hash code of the target object.
89: */
90: public int hashCode() {
91: return this .target.hashCode();
92: }
93:
94: public String toString() {
95: return "SingletonTargetSource for target object ["
96: + ObjectUtils.identityToString(this .target) + "]";
97: }
98:
99: }
|