001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.spring.bean;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.lang.reflect.Field;
021    
022    import java.util.ArrayList;
023    import java.util.IdentityHashMap;
024    import java.util.List;
025    import java.util.Map;
026    
027    import org.springframework.beans.factory.BeanFactory;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class BeanReferenceRefreshUtil {
033    
034            public static void refresh(BeanFactory beanFactory) throws Exception {
035                    for (Map.Entry<Object, List<RefreshPoint>> entry :
036                                    _registeredRefreshPoints.entrySet()) {
037    
038                            _refresh(beanFactory, entry.getKey(), entry.getValue());
039                    }
040    
041                    _registeredRefreshPoints.clear();
042            }
043    
044            public static void registerRefreshPoint(
045                    Object targetBean, Field field, String referencedBeanName) {
046    
047                    List<RefreshPoint> refreshPoints = _registeredRefreshPoints.get(
048                            targetBean);
049    
050                    if (refreshPoints == null) {
051                            refreshPoints = new ArrayList<RefreshPoint>();
052    
053                            _registeredRefreshPoints.put(targetBean, refreshPoints);
054                    }
055    
056                    refreshPoints.add(new RefreshPoint(field, referencedBeanName));
057            }
058    
059            public static interface PACL {
060    
061                    public Object getNewReferencedBean(
062                            String referencedBeanName, BeanFactory beanFactory);
063    
064            }
065    
066            private static void _refresh(
067                            BeanFactory beanFactory, Object targetBean,
068                            List<RefreshPoint> refreshPoints)
069                    throws Exception {
070    
071                    for (RefreshPoint refreshPoint : refreshPoints) {
072                            _refresh(beanFactory, targetBean, refreshPoint);
073                    }
074            }
075    
076            private static void _refresh(
077                            BeanFactory beanFactory, Object targetBean,
078                            RefreshPoint refreshPoint)
079                    throws Exception {
080    
081                    Field field = refreshPoint._field;
082    
083                    Object oldReferenceBean = field.get(targetBean);
084    
085                    String referencedBeanName = refreshPoint._referencedBeanName;
086    
087                    Object newReferencedBean = _pacl.getNewReferencedBean(
088                            referencedBeanName, beanFactory);
089    
090                    if (oldReferenceBean == newReferencedBean) {
091                            return;
092                    }
093    
094                    field.set(targetBean, newReferencedBean);
095    
096                    if (_log.isDebugEnabled()) {
097                            _log.debug(
098                                    "Refreshed field " + field + " with old value " +
099                                            oldReferenceBean + " with new value " + newReferencedBean +
100                                                    " on bean " + targetBean);
101                    }
102            }
103    
104            private static Log _log = LogFactoryUtil.getLog(
105                    BeanReferenceRefreshUtil.class);
106    
107            private static PACL _pacl = new NoPACL();
108            private static Map<Object, List<RefreshPoint>> _registeredRefreshPoints =
109                    new IdentityHashMap<Object, List<RefreshPoint>>();
110    
111            private static class NoPACL implements PACL {
112    
113                    @Override
114                    public Object getNewReferencedBean(
115                            String referencedBeanName, BeanFactory beanFactory) {
116    
117                            return beanFactory.getBean(referencedBeanName);
118                    }
119    
120            }
121    
122            private static class RefreshPoint {
123    
124                    public RefreshPoint(Field field, String referencedBeanName) {
125                            _field = field;
126                            _referencedBeanName = referencedBeanName;
127                    }
128    
129                    private Field _field;
130                    private String _referencedBeanName;
131    
132            }
133    
134    }