01: /*
02: * Copyright 2002-2006 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.annotation;
18:
19: import org.springframework.core.Ordered;
20: import org.springframework.util.Assert;
21:
22: /**
23: * Decorator to cause a MetadataAwareAspectInstanceFactory to instantiate only once.
24: *
25: * @author Rod Johnson
26: * @author Juergen Hoeller
27: * @since 2.0
28: */
29: public class LazySingletonAspectInstanceFactoryDecorator implements
30: MetadataAwareAspectInstanceFactory {
31:
32: private final MetadataAwareAspectInstanceFactory maaif;
33:
34: private Object materialized;
35:
36: /**
37: * Create a new lazily initializing decorator for the given AspectInstanceFactory.
38: * @param maaif the MetadataAwareAspectInstanceFactory to decorate
39: */
40: public LazySingletonAspectInstanceFactoryDecorator(
41: MetadataAwareAspectInstanceFactory maaif) {
42: Assert.notNull(maaif, "AspectInstanceFactory must not be null");
43: this .maaif = maaif;
44: }
45:
46: public synchronized Object getAspectInstance() {
47: if (this .materialized == null) {
48: this .materialized = this .maaif.getAspectInstance();
49: }
50: return this .materialized;
51: }
52:
53: public boolean isMaterialized() {
54: return (this .materialized != null);
55: }
56:
57: public AspectMetadata getAspectMetadata() {
58: return this .maaif.getAspectMetadata();
59: }
60:
61: public int getOrder() {
62: if (this .maaif instanceof Ordered) {
63: return ((Ordered) this .maaif).getOrder();
64: }
65: return Ordered.LOWEST_PRECEDENCE;
66: }
67:
68: @Override
69: public String toString() {
70: return "LazySingletonAspectInstanceFactoryDecorator: decorating "
71: + this.maaif;
72: }
73:
74: }
|