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        private static final long serialVersionUID = -5395975397632138270L;
055        
056        protected @Logger Log log;
057        
058            
059        // Seam 2.1
060        private static final Class<?>[] SEAM21_TIMED_EVENT_ARGS = new Class<?>[] { String.class, Schedule.class, Object[].class };
061        
062        @SuppressWarnings("all")
063        public void raiseTimedEvent(String type, Schedule schedule, Object... parameters) {
064            AbstractSeamServiceContext serviceContext = (AbstractSeamServiceContext)Component.getInstance(AbstractSeamServiceContext.COMPONENT_NAME, false);
065            
066            String sessionId = serviceContext != null ? serviceContext.getSessionId() : null;
067            Dispatcher dispatcher = AbstractDispatcher.instance();
068            if (dispatcher != null) {
069                    try {
070                            Method m = dispatcher.getClass().getMethod("scheduleTimedEvent", SEAM21_TIMED_EVENT_ARGS);
071                            if (serviceContext != null && sessionId != null)
072                                    m.invoke(dispatcher, ASYNC_EVENT, schedule, new Object[] { new WrappedEvent(serviceContext.getAsyncContext(), type, parameters) });
073                            else
074                                    m.invoke(dispatcher, type, schedule, parameters);
075                    }
076                    catch (Exception e) {
077                            log.error("Could not raise timed event", e);
078                    }
079            }
080        }
081    }