001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.tide.spring;
023    
024    import java.util.concurrent.Callable;
025    
026    import org.aopalliance.intercept.MethodInterceptor;
027    import org.aopalliance.intercept.MethodInvocation;
028    import org.granite.gravity.Gravity;
029    import org.granite.tide.data.DataEnabled;
030    import org.granite.tide.data.DataUpdatePostprocessor;
031    import org.springframework.beans.factory.InitializingBean;
032    import org.springframework.beans.factory.annotation.Autowired;
033    
034    /**
035     * Spring AOP interceptor to handle publishing of data changes instead of relying on the default behaviour
036     * This can be used outside of a HTTP Granite context and inside the security/transaction context
037     * 
038     * Ensure that the order of the interceptor is correctly setup to use ON_COMMIT publish mode: this interceptor must be executed inside a transaction
039     *  
040     * @author William DRAI
041     */
042    public class TideDataPublishingInterceptor implements MethodInterceptor, InitializingBean {
043            
044            //private static final Logger log = Logger.getLogger(TideDataPublishingInterceptor.class);
045            
046            private Gravity gravity;
047            private DataUpdatePostprocessor dataUpdatePostprocessor;
048            
049            private TideDataPublishingWrapper tideDataPublishingWrapper = null;
050    
051            
052            public void setGravity(Gravity gravity) {
053                    this.gravity = gravity;
054            }
055    
056        public void setTideDataPublishingWrapper(TideDataPublishingWrapper tideDataPublishingWrapper) {
057            this.tideDataPublishingWrapper = tideDataPublishingWrapper;
058        }
059            
060            @Autowired(required=false)
061            public void setDataUpdatePostprocessor(DataUpdatePostprocessor dataUpdatePostprocessor) {
062                    this.dataUpdatePostprocessor = dataUpdatePostprocessor;
063            }
064    
065        @Override
066        public void afterPropertiesSet() throws Exception {
067            if (tideDataPublishingWrapper == null)
068                tideDataPublishingWrapper = new TideDataPublishingWrapper(gravity, dataUpdatePostprocessor);
069        }
070    
071        public Object invoke(final MethodInvocation invocation) throws Throwable {
072            DataEnabled dataEnabled = invocation.getThis().getClass().getAnnotation(DataEnabled.class);
073            if (dataEnabled == null || !dataEnabled.useInterceptor())
074                    return invocation.proceed();
075    
076            return tideDataPublishingWrapper.execute(dataEnabled, new Callable<Object>() {
077                @Override
078                public Object call() throws Exception {
079                    try {
080                        return invocation.proceed();
081                    }
082                    catch (Exception e) {
083                        throw e;
084                    }
085                    catch (Throwable t) {
086                        // Not sure what to do in case of a throwable
087                        throw new RuntimeException("Data publishing error", t);
088                    }
089                }
090            });
091        }
092    }