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.aspectj.lang.ProceedingJoinPoint;
027 import org.aspectj.lang.annotation.Around;
028 import org.aspectj.lang.annotation.Aspect;
029 import org.granite.gravity.Gravity;
030 import org.granite.tide.data.DataEnabled;
031 import org.granite.tide.data.DataUpdatePostprocessor;
032 import org.springframework.beans.factory.InitializingBean;
033 import org.springframework.beans.factory.annotation.Autowired;
034 import org.springframework.core.Ordered;
035
036 /**
037 * Spring AOP AspectJ aspect to handle publishing of data changes instead of relying on the default behaviour
038 * This can be used outside of a HTTP Granite context and inside the security/transaction context
039 *
040 * @author William DRAI
041 */
042 @Aspect
043 public class TideDataPublishingAspect implements Ordered, InitializingBean {
044
045 //private static final Logger log = Logger.getLogger(TideDataPublishingAspect.class);
046
047 private int order = 0;
048 private Gravity gravity;
049 private DataUpdatePostprocessor dataUpdatePostprocessor;
050
051 private TideDataPublishingWrapper tideDataPublishingWrapper = null;
052
053 public void setGravity(Gravity gravity) {
054 this.gravity = gravity;
055 }
056
057 public void setTideDataPublishingWrapper(TideDataPublishingWrapper tideDataPublishingWrapper) {
058 this.tideDataPublishingWrapper = tideDataPublishingWrapper;
059 }
060
061 @Autowired(required=false)
062 public void setDataUpdatePostprocessor(DataUpdatePostprocessor dataUpdatePostprocessor) {
063 this.dataUpdatePostprocessor = dataUpdatePostprocessor;
064 }
065
066 public int getOrder() {
067 return order;
068 }
069 public void setOrder(int order) {
070 this.order = order;
071 }
072
073 @Override
074 public void afterPropertiesSet() throws Exception {
075 if (tideDataPublishingWrapper == null)
076 tideDataPublishingWrapper = new TideDataPublishingWrapper(gravity, dataUpdatePostprocessor);
077 }
078
079 @Around("@within(dataEnabled)")
080 public Object invoke(final ProceedingJoinPoint pjp, DataEnabled dataEnabled) throws Throwable {
081 if (dataEnabled == null || !dataEnabled.useInterceptor())
082 return pjp.proceed();
083
084 return tideDataPublishingWrapper.execute(dataEnabled, new Callable<Object>() {
085 @Override
086 public Object call() throws Exception {
087 try {
088 return pjp.proceed();
089 }
090 catch (Exception e) {
091 throw e;
092 }
093 catch (Throwable t) {
094 // Not sure what to do in case of a throwable
095 throw new RuntimeException("Data publishing error", t);
096 }
097 }
098 });
099 }
100 }