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.cdi; 023 024import javax.enterprise.event.Event; 025import javax.enterprise.inject.Instance; 026import javax.inject.Inject; 027import javax.interceptor.AroundInvoke; 028import javax.interceptor.Interceptor; 029import javax.interceptor.InvocationContext; 030 031import org.granite.gravity.Gravity; 032import org.granite.tide.data.DataContext; 033import org.granite.tide.data.DataEnabled; 034import org.granite.tide.data.DataEnabled.PublishMode; 035import org.granite.tide.data.DataTopicParams; 036import org.granite.tide.data.DataUpdatePostprocessor; 037 038 039/** 040 * CDI interceptor to handle publishing of data changes instead of relying on the default behaviour 041 * This can be used outside of a HTTP Granite context and should be applied inside the security/transaction context 042 * 043 * @author William DRAI 044 */ 045@DataEnabled(topic="", params=DataTopicParams.class, publish=PublishMode.MANUAL, useInterceptor=true) 046@Interceptor 047public class TideDataPublishingInterceptor { 048 049 @Inject 050 private Gravity gravity; 051 052 @Inject 053 private Instance<DataUpdatePostprocessor> dataUpdatePostprocessor; 054 055 @Inject 056 private Event<TideDataPublishingEvent> dataPublishingEvent; 057 058 @AroundInvoke 059 public Object processPublishData(InvocationContext invocationContext) throws Throwable { 060 DataEnabled dataEnabled = invocationContext.getMethod().getDeclaringClass().getAnnotation(DataEnabled.class); 061 if (dataEnabled == null || !dataEnabled.useInterceptor()) 062 return invocationContext.proceed(); 063 064 boolean shouldRemoveContextAtEnd = DataContext.get() == null; 065 boolean shouldInitContext = shouldRemoveContextAtEnd || DataContext.isNull(); 066 boolean onCommit = false; 067 068 if (shouldInitContext) { 069 DataContext.init(gravity, dataEnabled.topic(), dataEnabled.params(), dataEnabled.publish()); 070 071 if (!dataUpdatePostprocessor.isUnsatisfied()) 072 DataContext.get().setDataUpdatePostprocessor(dataUpdatePostprocessor.get()); 073 } 074 075 DataContext.observe(); 076 try { 077 if (dataEnabled.publish().equals(PublishMode.ON_COMMIT)) { 078 dataPublishingEvent.fire(new TideDataPublishingEvent(shouldRemoveContextAtEnd)); 079 onCommit = true; 080 } 081 082 Object ret = invocationContext.proceed(); 083 084 DataContext.publish(PublishMode.ON_SUCCESS); 085 return ret; 086 } 087 finally { 088 if (shouldRemoveContextAtEnd && !onCommit) 089 DataContext.remove(); 090 } 091 } 092}