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