1 /*
2  * Copyright 2002-2009 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.util.Assert;
20 
21 /**
22  * Decorator to cause a {@link MetadataAwareAspectInstanceFactory} to instantiate only once.
23  *
24  * @author Rod Johnson
25  * @author Juergen Hoeller
26  * @since 2.0
27  */
28 public class LazySingletonAspectInstanceFactoryDecorator implements MetadataAwareAspectInstanceFactory {
29 
30 	private final MetadataAwareAspectInstanceFactory maaif;
31 
32 	private volatile Object materialized;
33 
34 
35 	/**
36 	 * Create a new lazily initializing decorator for the given AspectInstanceFactory.
37 	 * @param maaif the MetadataAwareAspectInstanceFactory to decorate
38 	 */
LazySingletonAspectInstanceFactoryDecorator(MetadataAwareAspectInstanceFactory maaif)39 	public LazySingletonAspectInstanceFactoryDecorator(MetadataAwareAspectInstanceFactory maaif) {
40 		Assert.notNull(maaif, "AspectInstanceFactory must not be null");
41 		this.maaif = maaif;
42 	}
43 
44 
getAspectInstance()45 	public synchronized Object getAspectInstance() {
46 		if (this.materialized == null) {
47 			synchronized (this) {
48 				if (this.materialized == null) {
49 					this.materialized = this.maaif.getAspectInstance();
50 				}
51 			}
52 		}
53 		return this.materialized;
54 	}
55 
isMaterialized()56 	public boolean isMaterialized() {
57 		return (this.materialized != null);
58 	}
59 
getAspectClassLoader()60 	public ClassLoader getAspectClassLoader() {
61 		return this.maaif.getAspectClassLoader();
62 	}
63 
getAspectMetadata()64 	public AspectMetadata getAspectMetadata() {
65 		return this.maaif.getAspectMetadata();
66 	}
67 
getOrder()68 	public int getOrder() {
69 		return this.maaif.getOrder();
70 	}
71 
72 
73 	@Override
toString()74 	public String toString() {
75 		return "LazySingletonAspectInstanceFactoryDecorator: decorating " + this.maaif;
76 	}
77 
78 }
79