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.aop.framework.AopConfigException;
20: import org.springframework.core.Ordered;
21: import org.springframework.util.Assert;
22:
23: /**
24: * Implementation of {@link AspectInstanceFactory} that creates a new instance
25: * of the specified aspect class for every {@link #getAspectInstance()} call.
26: *
27: * @author Juergen Hoeller
28: * @since 2.0.4
29: */
30: public class SimpleAspectInstanceFactory implements
31: AspectInstanceFactory {
32:
33: private final Class aspectClass;
34:
35: /**
36: * Create a new SimpleAspectInstanceFactory for the given aspect class.
37: * @param aspectClass the aspect class
38: */
39: public SimpleAspectInstanceFactory(Class aspectClass) {
40: Assert.notNull(aspectClass, "Aspect class must not be null");
41: this .aspectClass = aspectClass;
42: }
43:
44: /**
45: * Return the specified aspect class (never <code>null</code>).
46: */
47: public final Class getAspectClass() {
48: return this .aspectClass;
49: }
50:
51: public final Object getAspectInstance() {
52: try {
53: return this .aspectClass.newInstance();
54: } catch (InstantiationException ex) {
55: throw new AopConfigException(
56: "Unable to instantiate aspect class ["
57: + this .aspectClass.getName() + "]", ex);
58: } catch (IllegalAccessException ex) {
59: throw new AopConfigException(
60: "Cannot access element class ["
61: + this .aspectClass.getName() + "]", ex);
62: }
63: }
64:
65: public ClassLoader getAspectClassLoader() {
66: return this .aspectClass.getClassLoader();
67: }
68:
69: /**
70: * Determine the order for this factory's aspect instance,
71: * either an instance-specific order expressed through implementing
72: * the {@link org.springframework.core.Ordered} interface,
73: * or a fallback order.
74: * @see org.springframework.core.Ordered
75: * @see #getOrderForAspectClass
76: */
77: public int getOrder() {
78: return getOrderForAspectClass(this .aspectClass);
79: }
80:
81: /**
82: * Determine a fallback order for the case that the aspect instance
83: * does not express an instance-specific order through implementing
84: * the {@link org.springframework.core.Ordered} interface.
85: * <p>The default implementation simply returns <code>Ordered.LOWEST_PRECEDENCE</code>.
86: * @param aspectClass the aspect class
87: */
88: protected int getOrderForAspectClass(Class aspectClass) {
89: return Ordered.LOWEST_PRECEDENCE;
90: }
91:
92: }
|