001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.aop.target;
018:
019: import java.io.Serializable;
020:
021: import org.springframework.aop.TargetSource;
022: import org.springframework.util.ObjectUtils;
023:
024: /**
025: * Canonical <code>TargetSource</code> when there is no target
026: * (or just the target class known), and behavior is supplied
027: * by interfaces and advisors only.
028: *
029: * @author Rod Johnson
030: * @author Juergen Hoeller
031: */
032: public class EmptyTargetSource implements TargetSource, Serializable {
033:
034: /** use serialVersionUID from Spring 1.2 for interoperability */
035: private static final long serialVersionUID = 3680494563553489691L;
036:
037: //---------------------------------------------------------------------
038: // Static factory methods
039: //---------------------------------------------------------------------
040:
041: /**
042: * The canonical (Singleton) instance of this {@link EmptyTargetSource}.
043: */
044: public static final EmptyTargetSource INSTANCE = new EmptyTargetSource(
045: null, true);
046:
047: /**
048: * Return an EmptyTargetSource for the given target Class.
049: * @param targetClass the target Class (may be <code>null</code>)
050: * @see #getTargetClass()
051: */
052: public static EmptyTargetSource forClass(Class targetClass) {
053: return forClass(targetClass, true);
054: }
055:
056: /**
057: * Return an EmptyTargetSource for the given target Class.
058: * @param targetClass the target Class (may be <code>null</code>)
059: * @param isStatic whether the TargetSource should be marked as static
060: * @see #getTargetClass()
061: */
062: public static EmptyTargetSource forClass(Class targetClass,
063: boolean isStatic) {
064: return (targetClass == null && isStatic ? INSTANCE
065: : new EmptyTargetSource(targetClass, isStatic));
066: }
067:
068: //---------------------------------------------------------------------
069: // Instance implementation
070: //---------------------------------------------------------------------
071:
072: private final Class targetClass;
073:
074: private final boolean isStatic;
075:
076: /**
077: * Create a new instance of the {@link EmptyTargetSource} class.
078: * <p>This constructor is <code>private</code> to enforce the
079: * Singleton pattern / factory method pattern.
080: * @param targetClass the target class to expose (may be <code>null</code>)
081: * @param isStatic whether the TargetSource is marked as static
082: */
083: private EmptyTargetSource(Class targetClass, boolean isStatic) {
084: this .targetClass = targetClass;
085: this .isStatic = isStatic;
086: }
087:
088: /**
089: * Always returns the specified target Class, or <code>null</code> if none.
090: */
091: public Class getTargetClass() {
092: return this .targetClass;
093: }
094:
095: /**
096: * Always returns <code>true</code>.
097: */
098: public boolean isStatic() {
099: return this .isStatic;
100: }
101:
102: /**
103: * Always returns <code>null</code>.
104: */
105: public Object getTarget() {
106: return null;
107: }
108:
109: /**
110: * Nothing to release.
111: */
112: public void releaseTarget(Object target) {
113: }
114:
115: /**
116: * Returns the canonical instance on deserialization in case
117: * of no target class, thus protecting the Singleton pattern.
118: */
119: private Object readResolve() {
120: return (this .targetClass == null && this .isStatic ? INSTANCE
121: : this );
122: }
123:
124: public boolean equals(Object other) {
125: if (this == other) {
126: return true;
127: }
128: if (!(other instanceof EmptyTargetSource)) {
129: return false;
130: }
131: EmptyTargetSource otherTs = (EmptyTargetSource) other;
132: return (ObjectUtils.nullSafeEquals(this .targetClass,
133: otherTs.targetClass) && this .isStatic == otherTs.isStatic);
134: }
135:
136: public int hashCode() {
137: return EmptyTargetSource.class.hashCode() * 13
138: + ObjectUtils.nullSafeHashCode(this .targetClass);
139: }
140:
141: public String toString() {
142: return "EmptyTargetSource: "
143: + (this .targetClass != null ? "target class ["
144: + this .targetClass.getName() + "]"
145: : "no target class") + ", "
146: + (this .isStatic ? "static" : "dynamic");
147: }
148:
149: }
|