001/* 002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.platform.ui.web.invalidations; 021 022import java.lang.reflect.Method; 023 024import org.jboss.seam.Component; 025import org.jboss.seam.ScopeType; 026import org.jboss.seam.annotations.intercept.Interceptor; 027import org.jboss.seam.core.BijectionInterceptor; 028import org.jboss.seam.intercept.AbstractInterceptor; 029import org.jboss.seam.intercept.InvocationContext; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 032 033/** 034 * Interceptor used for automatic injection/invalidation tied to currentDocumentModel 035 * 036 * @author tiry 037 */ 038@Interceptor(stateless = true, within = BijectionInterceptor.class) 039public class DocumentContextInvalidatorInterceptor extends AbstractInterceptor { 040 041 private static final long serialVersionUID = 1L; 042 043 @Override 044 public Object aroundInvoke(InvocationContext invocationContext) throws Exception { // stupid Seam API 045 beforeInvocation(invocationContext); 046 return invocationContext.proceed(); 047 } 048 049 private void beforeInvocation(InvocationContext invocationContext) { 050 Object target = invocationContext.getTarget(); 051 for (Method meth : target.getClass().getMethods()) { 052 if (meth.isAnnotationPresent(DocumentContextInvalidation.class)) { 053 doInvalidationCall(target, meth); 054 } 055 } 056 } 057 058 private void doInvalidationCall(Object target, Method meth) { 059 try { 060 if (meth.getParameterTypes().length == 0) { 061 meth.invoke(target); 062 } else { 063 DocumentModel currentDoc = getCurrentDocumentModel(); 064 // currentDoc may be null, it's ok 065 meth.invoke(target, currentDoc); 066 } 067 } catch (ReflectiveOperationException e) { 068 throw new RuntimeException(e); 069 } 070 } 071 072 private DocumentModel getCurrentDocumentModel() { 073 NavigationContext navigationContext = (NavigationContext) Component.getInstance("navigationContext", 074 ScopeType.CONVERSATION); 075 return navigationContext.getCurrentDocument(); 076 } 077 078 @Override 079 public boolean isInterceptorEnabled() { 080 return true; 081 } 082 083}