001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.tide.spring;
022
023 import org.aopalliance.intercept.MethodInterceptor;
024 import org.aopalliance.intercept.MethodInvocation;
025 import org.granite.gravity.Gravity;
026 import org.granite.logging.Logger;
027 import org.granite.tide.data.DataContext;
028 import org.granite.tide.data.DataEnabled;
029 import org.granite.tide.data.DataUpdatePostprocessor;
030 import org.granite.tide.data.DataEnabled.PublishMode;
031 import org.springframework.beans.factory.annotation.Autowired;
032 import org.springframework.transaction.support.TransactionSynchronizationAdapter;
033 import org.springframework.transaction.support.TransactionSynchronizationManager;
034
035 /**
036 * Spring AOP interceptor to handle publishing of data changes instead of relying on the default behaviour
037 * This can be used outside of a HTTP Granite context and inside the security/transaction context
038 *
039 * @author William DRAI
040 */
041 public class TideDataPublishingInterceptor implements MethodInterceptor {
042
043 private static final Logger log = Logger.getLogger(TideDataPublishingInterceptor.class);
044
045 private Gravity gravity;
046 private DataUpdatePostprocessor dataUpdatePostprocessor;
047
048 public void setGravity(Gravity gravity) {
049 this.gravity = gravity;
050 }
051
052 @Autowired(required=false)
053 public void setDataUpdatePostprocessor(DataUpdatePostprocessor dataUpdatePostprocessor) {
054 this.dataUpdatePostprocessor = dataUpdatePostprocessor;
055 }
056
057 public Object invoke(final MethodInvocation invocation) throws Throwable {
058 DataEnabled dataEnabled = invocation.getThis().getClass().getAnnotation(DataEnabled.class);
059 if (dataEnabled == null || !dataEnabled.useInterceptor())
060 return invocation.proceed();
061
062 boolean shouldRemoveContextAtEnd = DataContext.get() == null;
063 boolean shouldInitContext = shouldRemoveContextAtEnd || DataContext.isNull();
064 boolean onCommit = false;
065
066 if (shouldInitContext) {
067 DataContext.init(gravity, dataEnabled.topic(), dataEnabled.params(), dataEnabled.publish());
068 if (dataUpdatePostprocessor != null)
069 DataContext.get().setDataUpdatePostprocessor(dataUpdatePostprocessor);
070 }
071
072 DataContext.observe();
073 try {
074 if (dataEnabled.publish().equals(PublishMode.ON_COMMIT) && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
075 if (TransactionSynchronizationManager.isSynchronizationActive()) {
076 TransactionSynchronizationManager.registerSynchronization(new DataPublishingTransactionSynchronization(shouldRemoveContextAtEnd));
077 onCommit = true;
078 }
079 else
080 log.warn("Could not register synchronization for ON_COMMIT publish mode, check that the Spring PlatformTransactionManager supports it");
081 }
082
083 Object ret = invocation.proceed();
084
085 DataContext.publish(PublishMode.ON_SUCCESS);
086 return ret;
087 }
088 finally {
089 if (shouldRemoveContextAtEnd && !onCommit)
090 DataContext.remove();
091 }
092 }
093
094 private static class DataPublishingTransactionSynchronization extends TransactionSynchronizationAdapter {
095
096 private boolean removeContext = false;
097
098 public DataPublishingTransactionSynchronization(boolean removeContext) {
099 this.removeContext = removeContext;
100 }
101
102 @Override
103 public void beforeCommit(boolean readOnly) {
104 if (!readOnly)
105 DataContext.publish(PublishMode.ON_COMMIT);
106 }
107
108 @Override
109 public void beforeCompletion() {
110 if (removeContext)
111 DataContext.remove();
112 }
113
114
115 }
116 }