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.aspectj;
18:
19: import org.springframework.core.Ordered;
20: import org.springframework.util.Assert;
21:
22: /**
23: * Implementation of {@link AspectInstanceFactory} that is backed by a
24: * specified singleton object, returning the same instance for every
25: * {@link #getAspectInstance()} call.
26: *
27: * @author Rod Johnson
28: * @author Juergen Hoeller
29: * @since 2.0
30: * @see SimpleAspectInstanceFactory
31: */
32: public class SingletonAspectInstanceFactory implements
33: AspectInstanceFactory {
34:
35: private final Object aspectInstance;
36:
37: /**
38: * Create a new SingletonAspectInstanceFactory for the given aspect instance.
39: * @param aspectInstance the singleton aspect instance
40: */
41: public SingletonAspectInstanceFactory(Object aspectInstance) {
42: Assert.notNull(aspectInstance,
43: "Aspect instance must not be null");
44: this .aspectInstance = aspectInstance;
45: }
46:
47: public final Object getAspectInstance() {
48: return this .aspectInstance;
49: }
50:
51: public ClassLoader getAspectClassLoader() {
52: return this .aspectInstance.getClass().getClassLoader();
53: }
54:
55: /**
56: * Determine the order for this factory's aspect instance,
57: * either an instance-specific order expressed through implementing
58: * the {@link org.springframework.core.Ordered} interface,
59: * or a fallback order.
60: * @see org.springframework.core.Ordered
61: * @see #getOrderForAspectClass
62: */
63: public int getOrder() {
64: if (this .aspectInstance instanceof Ordered) {
65: return ((Ordered) this .aspectInstance).getOrder();
66: }
67: return getOrderForAspectClass(this .aspectInstance.getClass());
68: }
69:
70: /**
71: * Determine a fallback order for the case that the aspect instance
72: * does not express an instance-specific order through implementing
73: * the {@link org.springframework.core.Ordered} interface.
74: * <p>The default implementation simply returns <code>Ordered.LOWEST_PRECEDENCE</code>.
75: * @param aspectClass the aspect class
76: */
77: protected int getOrderForAspectClass(Class aspectClass) {
78: return Ordered.LOWEST_PRECEDENCE;
79: }
80:
81: }
|