001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.spring.bean;
016    
017    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    
021    import java.lang.reflect.Field;
022    
023    import java.util.ArrayList;
024    import java.util.IdentityHashMap;
025    import java.util.List;
026    import java.util.Map;
027    
028    import org.springframework.beans.factory.BeanFactory;
029    import org.springframework.beans.factory.NoSuchBeanDefinitionException;
030    
031    /**
032     * @author Shuyang Zhou
033     */
034    public class BeanReferenceRefreshUtil {
035    
036            public static void refresh(BeanFactory beanFactory) throws Exception {
037                    for (Map.Entry<Object, List<RefreshPoint>> entry :
038                                    _registeredRefreshPoints.entrySet()) {
039    
040                            _refresh(beanFactory, entry.getKey(), entry.getValue());
041                    }
042    
043                    _registeredRefreshPoints.clear();
044            }
045    
046            public static void registerRefreshPoint(
047                    Object targetBean, Field field, String referencedBeanName) {
048    
049                    List<RefreshPoint> refreshPoints = _registeredRefreshPoints.get(
050                            targetBean);
051    
052                    if (refreshPoints == null) {
053                            refreshPoints = new ArrayList<RefreshPoint>();
054    
055                            _registeredRefreshPoints.put(targetBean, refreshPoints);
056                    }
057    
058                    refreshPoints.add(new RefreshPoint(field, referencedBeanName));
059            }
060    
061            public static interface PACL {
062    
063                    public Object getNewReferencedBean(
064                            String referencedBeanName, BeanFactory beanFactory);
065    
066            }
067    
068            private static void _refresh(
069                            BeanFactory beanFactory, Object targetBean,
070                            List<RefreshPoint> refreshPoints)
071                    throws Exception {
072    
073                    for (RefreshPoint refreshPoint : refreshPoints) {
074                            _refresh(beanFactory, targetBean, refreshPoint);
075                    }
076            }
077    
078            private static void _refresh(
079                            BeanFactory beanFactory, Object targetBean,
080                            RefreshPoint refreshPoint)
081                    throws Exception {
082    
083                    Field field = refreshPoint._field;
084    
085                    Object oldReferenceBean = field.get(targetBean);
086    
087                    String referencedBeanName = refreshPoint._referencedBeanName;
088    
089                    Object newReferencedBean = _pacl.getNewReferencedBean(
090                            referencedBeanName, beanFactory);
091    
092                    if (oldReferenceBean == newReferencedBean) {
093                            return;
094                    }
095    
096                    field.set(targetBean, newReferencedBean);
097    
098                    if (_log.isDebugEnabled()) {
099                            _log.debug(
100                                    "Refreshed field " + field + " with old value " +
101                                            oldReferenceBean + " with new value " + newReferencedBean +
102                                                    " on bean " + targetBean);
103                    }
104            }
105    
106            private static Log _log = LogFactoryUtil.getLog(
107                    BeanReferenceRefreshUtil.class);
108    
109            private static PACL _pacl = new NoPACL();
110            private static Map<Object, List<RefreshPoint>> _registeredRefreshPoints =
111                    new IdentityHashMap<Object, List<RefreshPoint>>();
112    
113            private static class NoPACL implements PACL {
114    
115                    @Override
116                    public Object getNewReferencedBean(
117                            String referencedBeanName, BeanFactory beanFactory) {
118    
119                            try {
120                                    return beanFactory.getBean(referencedBeanName);
121                            }
122                            catch (NoSuchBeanDefinitionException nsbde) {
123                                    if (_log.isInfoEnabled()) {
124                                            _log.info(
125                                                    "Bean " + referencedBeanName + " may be defined in " +
126                                                            "the portal");
127                                    }
128    
129                                    return PortalBeanLocatorUtil.locate(referencedBeanName);
130                            }
131                    }
132    
133            }
134    
135            private static class RefreshPoint {
136    
137                    public RefreshPoint(Field field, String referencedBeanName) {
138                            _field = field;
139                            _referencedBeanName = referencedBeanName;
140                    }
141    
142                    private Field _field;
143                    private String _referencedBeanName;
144    
145            }
146    
147    }