001/*
002 * Copyright 2002-2012 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.autoproxy;
018
019import org.springframework.core.NamedThreadLocal;
020
021/**
022 * Holder for the current proxy creation context, as exposed by auto-proxy creators
023 * such as {@link AbstractAdvisorAutoProxyCreator}.
024 *
025 * @author Juergen Hoeller
026 * @author Ramnivas Laddad
027 * @since 2.5
028 */
029public class ProxyCreationContext {
030
031        /** ThreadLocal holding the current proxied bean name during Advisor matching */
032        private static final ThreadLocal<String> currentProxiedBeanName =
033                        new NamedThreadLocal<String>("Name of currently proxied bean");
034
035
036        /**
037         * Return the name of the currently proxied bean instance.
038         * @return the name of the bean, or {@code null} if none available
039         */
040        public static String getCurrentProxiedBeanName() {
041                return currentProxiedBeanName.get();
042        }
043
044        /**
045         * Set the name of the currently proxied bean instance.
046         * @param beanName the name of the bean, or {@code null} to reset it
047         */
048        static void setCurrentProxiedBeanName(String beanName) {
049                if (beanName != null) {
050                        currentProxiedBeanName.set(beanName);
051                }
052                else {
053                        currentProxiedBeanName.remove();
054                }
055        }
056
057}