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.seam;
022
023 import org.granite.gravity.Gravity;
024 import org.granite.tide.data.DataContext;
025 import org.granite.tide.data.DataEnabled;
026 import org.granite.tide.data.DataUpdatePostprocessor;
027 import org.granite.tide.data.DataEnabled.PublishMode;
028 import org.jboss.seam.Component;
029 import org.jboss.seam.annotations.intercept.AroundInvoke;
030 import org.jboss.seam.annotations.intercept.Interceptor;
031 import org.jboss.seam.core.Events;
032 import org.jboss.seam.intercept.AbstractInterceptor;
033 import org.jboss.seam.intercept.InvocationContext;
034 import org.jboss.seam.transaction.TransactionInterceptor;
035
036
037 /**
038 * Seam interceptor to handle publishing of data changes instead of relying on the default behaviour
039 * This can be used outside of a HTTP Granite context and inside the security/transaction context
040 * @author William DRAI
041 *
042 */
043 @Interceptor(stateless=true, within={TransactionInterceptor.class})
044 public class TideDataPublishingInterceptor extends AbstractInterceptor {
045
046 private static final long serialVersionUID = 1L;
047
048 @AroundInvoke
049 public Object aroundInvoke(InvocationContext invocationContext) throws Exception {
050 DataEnabled dataEnabled = getComponent().getBeanClass().getAnnotation(DataEnabled.class);
051
052 if (dataEnabled == null || !dataEnabled.useInterceptor())
053 return invocationContext.proceed();
054
055 if (SeamUtils.isLifecycleMethod(getComponent(), invocationContext.getMethod()))
056 return invocationContext.proceed();
057
058 boolean shouldRemoveContextAtEnd = DataContext.get() == null;
059 boolean shouldInitContext = shouldRemoveContextAtEnd || DataContext.isNull();
060 boolean onCommit = false;
061
062 if (shouldInitContext) {
063 Gravity gravity = (Gravity)Component.getInstance("org.granite.seam.gravity");
064 DataContext.init(gravity, dataEnabled.topic(), dataEnabled.params(), dataEnabled.publish());
065
066 DataUpdatePostprocessor dataUpdatePostprocessor = (DataUpdatePostprocessor)Component.getInstance("org.granite.tide.seam.data.dataUpdatePreprocessor", true);
067 if (dataUpdatePostprocessor != null)
068 DataContext.get().setDataUpdatePostprocessor(dataUpdatePostprocessor);
069 }
070
071 DataContext.observe();
072 try {
073 if (dataEnabled.publish().equals(PublishMode.ON_COMMIT)) {
074 Events.instance().raiseTransactionSuccessEvent("org.granite.tide.seam.data.transactionSuccess", shouldRemoveContextAtEnd);
075 Events.instance().raiseTransactionCompletionEvent("org.granite.tide.seam.data.transactionCompletion", shouldRemoveContextAtEnd);
076 onCommit = true;
077 }
078
079 Object ret = invocationContext.proceed();
080
081 DataContext.publish(PublishMode.ON_SUCCESS);
082 return ret;
083 }
084 finally {
085 if (shouldRemoveContextAtEnd && !onCommit)
086 DataContext.remove();
087 }
088 }
089
090 // Needed for Seam 2.1
091 public boolean isInterceptorEnabled() {
092 return getComponent().beanClassHasAnnotation(DataEnabled.class) && getComponent().getBeanClass().getAnnotation(DataEnabled.class).useInterceptor();
093 }
094 }