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 */ 022package org.granite.tide.spring; 023 024import org.aopalliance.intercept.MethodInterceptor; 025import org.aopalliance.intercept.MethodInvocation; 026import org.granite.gravity.Gravity; 027import org.granite.tide.data.DataEnabled; 028import org.granite.tide.data.DataUpdatePostprocessor; 029import org.granite.util.ThrowableCallable; 030import org.springframework.beans.factory.InitializingBean; 031import org.springframework.beans.factory.annotation.Autowired; 032 033/** 034 * Spring AOP interceptor to handle publishing of data changes instead of relying on the default behaviour 035 * This can be used outside of a HTTP Granite context and inside the security/transaction context 036 * 037 * Ensure that the order of the interceptor is correctly setup to use ON_COMMIT publish mode: this interceptor must be executed inside a transaction 038 * 039 * @author William DRAI 040 */ 041public class TideDataPublishingInterceptor implements MethodInterceptor, InitializingBean { 042 043 //private static final Logger log = Logger.getLogger(TideDataPublishingInterceptor.class); 044 045 private Gravity gravity; 046 private DataUpdatePostprocessor dataUpdatePostprocessor; 047 048 private TideDataPublishingWrapper tideDataPublishingWrapper = null; 049 050 051 public void setGravity(Gravity gravity) { 052 this.gravity = gravity; 053 } 054 055 public void setTideDataPublishingWrapper(TideDataPublishingWrapper tideDataPublishingWrapper) { 056 this.tideDataPublishingWrapper = tideDataPublishingWrapper; 057 } 058 059 @Autowired(required=false) 060 public void setDataUpdatePostprocessor(DataUpdatePostprocessor dataUpdatePostprocessor) { 061 this.dataUpdatePostprocessor = dataUpdatePostprocessor; 062 } 063 064 @Override 065 public void afterPropertiesSet() throws Exception { 066 if (tideDataPublishingWrapper == null) 067 tideDataPublishingWrapper = new TideDataPublishingWrapper(gravity, dataUpdatePostprocessor); 068 } 069 070 public Object invoke(final MethodInvocation invocation) throws Throwable { 071 DataEnabled dataEnabled = invocation.getThis().getClass().getAnnotation(DataEnabled.class); 072 if (dataEnabled == null || !dataEnabled.useInterceptor()) 073 return invocation.proceed(); 074 075 return tideDataPublishingWrapper.execute(dataEnabled, new ThrowableCallable<Object>() { 076 @Override 077 public Object call() throws Throwable { 078 return invocation.proceed(); 079 } 080 }); 081 } 082}