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.seam21;
022    
023    import static org.jboss.seam.annotations.Install.FRAMEWORK;
024    
025    import java.lang.reflect.Method;
026    
027    import org.granite.tide.seam.AbstractSeamServiceContext;
028    import org.jboss.seam.Component;
029    import org.jboss.seam.ScopeType;
030    import org.jboss.seam.annotations.AutoCreate;
031    import org.jboss.seam.annotations.Install;
032    import org.jboss.seam.annotations.Logger;
033    import org.jboss.seam.annotations.Name;
034    import org.jboss.seam.annotations.Scope;
035    import org.jboss.seam.annotations.intercept.BypassInterceptors;
036    import org.jboss.seam.async.AbstractDispatcher;
037    import org.jboss.seam.async.Dispatcher;
038    import org.jboss.seam.async.Schedule;
039    import org.jboss.seam.log.Log;
040    
041    
042    /**
043     * TideEvents override to intercept Seam events handling
044     * 
045     * @author William DRAI
046     */
047    @Name("org.jboss.seam.core.events")
048    @Install(precedence=FRAMEWORK+1)
049    @Scope(ScopeType.STATELESS)
050    @BypassInterceptors
051    @AutoCreate
052    public class TideEvents extends org.granite.tide.seam.TideEvents {
053        
054        protected @Logger Log log;
055        
056            
057        // Seam 2.1
058        private static final Class<?>[] SEAM21_TIMED_EVENT_ARGS = new Class<?>[] { String.class, Schedule.class, Object[].class };
059        
060        @SuppressWarnings("all")
061        public void raiseTimedEvent(String type, Schedule schedule, Object... parameters) {
062            AbstractSeamServiceContext serviceContext = (AbstractSeamServiceContext)Component.getInstance(AbstractSeamServiceContext.COMPONENT_NAME, false);
063            
064            String sessionId = serviceContext != null ? serviceContext.getSessionId() : null;
065            Dispatcher dispatcher = AbstractDispatcher.instance();
066            if (dispatcher != null) {
067                    try {
068                            Method m = dispatcher.getClass().getMethod("scheduleTimedEvent", SEAM21_TIMED_EVENT_ARGS);
069                            if (serviceContext != null && sessionId != null)
070                                    m.invoke(dispatcher, ASYNC_EVENT, schedule, new Object[] { new WrappedEvent(serviceContext.getAsyncContext(), type, parameters) });
071                            else
072                                    m.invoke(dispatcher, type, schedule, parameters);
073                    }
074                    catch (Exception e) {
075                            log.error("Could not raise timed event", e);
076                    }
077            }
078        }
079    }