001/*
002 * Copyright 2002-2018 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.framework;
018
019import java.util.LinkedList;
020import java.util.List;
021
022import org.springframework.util.Assert;
023
024/**
025 * Base class for proxy factories.
026 * Provides convenient access to a configurable AopProxyFactory.
027 *
028 * @author Juergen Hoeller
029 * @since 2.0.3
030 * @see #createAopProxy()
031 */
032@SuppressWarnings("serial")
033public class ProxyCreatorSupport extends AdvisedSupport {
034
035        private AopProxyFactory aopProxyFactory;
036
037        private final List<AdvisedSupportListener> listeners = new LinkedList<>();
038
039        /** Set to true when the first AOP proxy has been created. */
040        private boolean active = false;
041
042
043        /**
044         * Create a new ProxyCreatorSupport instance.
045         */
046        public ProxyCreatorSupport() {
047                this.aopProxyFactory = new DefaultAopProxyFactory();
048        }
049
050        /**
051         * Create a new ProxyCreatorSupport instance.
052         * @param aopProxyFactory the AopProxyFactory to use
053         */
054        public ProxyCreatorSupport(AopProxyFactory aopProxyFactory) {
055                Assert.notNull(aopProxyFactory, "AopProxyFactory must not be null");
056                this.aopProxyFactory = aopProxyFactory;
057        }
058
059
060        /**
061         * Customize the AopProxyFactory, allowing different strategies
062         * to be dropped in without changing the core framework.
063         * <p>Default is {@link DefaultAopProxyFactory}, using dynamic JDK
064         * proxies or CGLIB proxies based on the requirements.
065         */
066        public void setAopProxyFactory(AopProxyFactory aopProxyFactory) {
067                Assert.notNull(aopProxyFactory, "AopProxyFactory must not be null");
068                this.aopProxyFactory = aopProxyFactory;
069        }
070
071        /**
072         * Return the AopProxyFactory that this ProxyConfig uses.
073         */
074        public AopProxyFactory getAopProxyFactory() {
075                return this.aopProxyFactory;
076        }
077
078        /**
079         * Add the given AdvisedSupportListener to this proxy configuration.
080         * @param listener the listener to register
081         */
082        public void addListener(AdvisedSupportListener listener) {
083                Assert.notNull(listener, "AdvisedSupportListener must not be null");
084                this.listeners.add(listener);
085        }
086
087        /**
088         * Remove the given AdvisedSupportListener from this proxy configuration.
089         * @param listener the listener to deregister
090         */
091        public void removeListener(AdvisedSupportListener listener) {
092                Assert.notNull(listener, "AdvisedSupportListener must not be null");
093                this.listeners.remove(listener);
094        }
095
096
097        /**
098         * Subclasses should call this to get a new AOP proxy. They should <b>not</b>
099         * create an AOP proxy with {@code this} as an argument.
100         */
101        protected final synchronized AopProxy createAopProxy() {
102                if (!this.active) {
103                        activate();
104                }
105                return getAopProxyFactory().createAopProxy(this);
106        }
107
108        /**
109         * Activate this proxy configuration.
110         * @see AdvisedSupportListener#activated
111         */
112        private void activate() {
113                this.active = true;
114                for (AdvisedSupportListener listener : this.listeners) {
115                        listener.activated(this);
116                }
117        }
118
119        /**
120         * Propagate advice change event to all AdvisedSupportListeners.
121         * @see AdvisedSupportListener#adviceChanged
122         */
123        @Override
124        protected void adviceChanged() {
125                super.adviceChanged();
126                synchronized (this) {
127                        if (this.active) {
128                                for (AdvisedSupportListener listener : this.listeners) {
129                                        listener.adviceChanged(this);
130                                }
131                        }
132                }
133        }
134
135        /**
136         * Subclasses can call this to check whether any AOP proxies have been created yet.
137         */
138        protected final synchronized boolean isActive() {
139                return this.active;
140        }
141
142}