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: /**
52: * Determine the order for this factory's aspect instance,
53: * either an instance-specific order expressed through implementing
54: * the {@link org.springframework.core.Ordered} interface,
55: * or a fallback order.
56: * @see org.springframework.core.Ordered
57: * @see #getOrderForAspectClass
58: */
59: public int getOrder() {
60: if (this .aspectInstance instanceof Ordered) {
61: return ((Ordered) this .aspectInstance).getOrder();
62: }
63: return getOrderForAspectClass(this .aspectInstance.getClass());
64: }
65:
66: /**
67: * Determine a fallback order for the case that the aspect instance
68: * does not express an instance-specific order through implementing
69: * the {@link org.springframework.core.Ordered} interface.
70: * <p>The default implementation simply returns <code>Ordered.LOWEST_PRECEDENCE</code>.
71: * @param aspectClass the aspect class
72: */
73: protected int getOrderForAspectClass(Class aspectClass) {
74: return Ordered.LOWEST_PRECEDENCE;
75: }
76:
77: }
|