KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > aspectj > annotation > LazySingletonAspectInstanceFactoryDecorator


1 /*
2  * Copyright 2002-2006 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.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 MetadataAwareAspectInstanceFactory {
30
31     private final MetadataAwareAspectInstanceFactory maaif;
32
33     private Object JavaDoc materialized;
34
35
36     /**
37      * Create a new lazily initializing decorator for the given AspectInstanceFactory.
38      * @param maaif the MetadataAwareAspectInstanceFactory to decorate
39      */

40     public LazySingletonAspectInstanceFactoryDecorator(MetadataAwareAspectInstanceFactory maaif) {
41         Assert.notNull(maaif, "AspectInstanceFactory must not be null");
42         this.maaif = maaif;
43     }
44
45     public synchronized Object JavaDoc getAspectInstance() {
46         if (this.materialized == null) {
47             this.materialized = this.maaif.getAspectInstance();
48         }
49         return this.materialized;
50     }
51
52     public boolean isMaterialized() {
53         return (this.materialized != null);
54     }
55
56     public AspectMetadata getAspectMetadata() {
57         return this.maaif.getAspectMetadata();
58     }
59
60     public int getOrder() {
61         if (this.maaif instanceof Ordered) {
62             return ((Ordered) this.maaif).getOrder();
63         }
64         return Ordered.LOWEST_PRECEDENCE;
65     }
66
67
68     @Override JavaDoc
69     public String JavaDoc toString() {
70         return "LazySingletonAspectInstanceFactoryDecorator: decorating " + this.maaif;
71     }
72
73 }
74
Popular Tags