001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.aop.aspectj.annotation;
018
019import java.io.Serializable;
020
021import org.springframework.lang.Nullable;
022import org.springframework.util.Assert;
023
024/**
025 * Decorator to cause a {@link MetadataAwareAspectInstanceFactory} to instantiate only once.
026 *
027 * @author Rod Johnson
028 * @author Juergen Hoeller
029 * @since 2.0
030 */
031@SuppressWarnings("serial")
032public class LazySingletonAspectInstanceFactoryDecorator implements MetadataAwareAspectInstanceFactory, Serializable {
033
034        private final MetadataAwareAspectInstanceFactory maaif;
035
036        @Nullable
037        private volatile Object materialized;
038
039
040        /**
041         * Create a new lazily initializing decorator for the given AspectInstanceFactory.
042         * @param maaif the MetadataAwareAspectInstanceFactory to decorate
043         */
044        public LazySingletonAspectInstanceFactoryDecorator(MetadataAwareAspectInstanceFactory maaif) {
045                Assert.notNull(maaif, "AspectInstanceFactory must not be null");
046                this.maaif = maaif;
047        }
048
049
050        @Override
051        public Object getAspectInstance() {
052                Object aspectInstance = this.materialized;
053                if (aspectInstance == null) {
054                        Object mutex = this.maaif.getAspectCreationMutex();
055                        if (mutex == null) {
056                                aspectInstance = this.maaif.getAspectInstance();
057                                this.materialized = aspectInstance;
058                        }
059                        else {
060                                synchronized (mutex) {
061                                        aspectInstance = this.materialized;
062                                        if (aspectInstance == null) {
063                                                aspectInstance = this.maaif.getAspectInstance();
064                                                this.materialized = aspectInstance;
065                                        }
066                                }
067                        }
068                }
069                return aspectInstance;
070        }
071
072        public boolean isMaterialized() {
073                return (this.materialized != null);
074        }
075
076        @Override
077        @Nullable
078        public ClassLoader getAspectClassLoader() {
079                return this.maaif.getAspectClassLoader();
080        }
081
082        @Override
083        public AspectMetadata getAspectMetadata() {
084                return this.maaif.getAspectMetadata();
085        }
086
087        @Override
088        @Nullable
089        public Object getAspectCreationMutex() {
090                return this.maaif.getAspectCreationMutex();
091        }
092
093        @Override
094        public int getOrder() {
095                return this.maaif.getOrder();
096        }
097
098
099        @Override
100        public String toString() {
101                return "LazySingletonAspectInstanceFactoryDecorator: decorating " + this.maaif;
102        }
103
104}