001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2007-2010 ADEQUATE SYSTEMS SARL
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.seam;
022    
023    import static org.jboss.seam.annotations.Install.FRAMEWORK;
024    
025    import java.io.Serializable;
026    
027    import org.granite.tide.seam.async.AsyncContext;
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.Name;
033    import org.jboss.seam.annotations.Scope;
034    import org.jboss.seam.annotations.intercept.BypassInterceptors;
035    import org.jboss.seam.async.Schedule;
036    import org.jboss.seam.core.Events;
037    
038    
039    /**
040     * TideEvents override to intercept Seam events handling
041     * 
042     * @author William DRAI
043     */
044    @Name("org.jboss.seam.core.events")
045    @Install(precedence=FRAMEWORK)
046    @Scope(ScopeType.STATELESS)
047    @BypassInterceptors
048    @AutoCreate
049    public class TideEvents extends Events {
050    
051        private static final long serialVersionUID = -5395975397632138270L;
052        
053        
054        @Override
055        public void raiseEvent(String type, Object... parameters) {
056            if (ASYNC_EVENT.equals(type)) {
057                    TideInvocation.init();
058                AbstractSeamServiceContext serviceContext = (AbstractSeamServiceContext)Component.getInstance(AbstractSeamServiceContext.COMPONENT_NAME, true);
059                
060                WrappedEvent event = (WrappedEvent)parameters[0];
061                serviceContext.setAsyncContext(event.getAsyncContext());    // Reset context
062                
063                raiseEvent(event.getType(), event.getParams());
064                
065                // Send event through Gravity only 
066                serviceContext.sendEvent(null, null);
067            }
068            else {
069                super.raiseEvent(type, parameters);
070                
071                // Ignore built-in Seam events to avoid stack overflow in component initialization
072                if (!type.startsWith("org.jboss.seam.pre") && !type.startsWith("org.jboss.seam.post")) {
073                    // Event should be always handled if we want to allow to send them through Gravity
074                    AbstractSeamServiceContext serviceContext = (AbstractSeamServiceContext)Component.getInstance(AbstractSeamServiceContext.COMPONENT_NAME, false);
075                    if (serviceContext != null)     // ServiceContext is null during Seam initialization
076                        serviceContext.raiseEvent(type, parameters);
077                }
078            }
079        }
080        
081        
082        protected static final String ASYNC_EVENT = "org.granite.tide.seam.AsyncEvent";
083        
084        protected static class WrappedEvent implements Serializable {
085            
086            private static final long serialVersionUID = 1L;
087            
088            private AsyncContext asyncContext;
089            private String type;
090            private Object[] params;
091            
092            public WrappedEvent(AsyncContext asyncContext, String type, Object[] params) {
093                this.asyncContext = asyncContext;
094                this.type = type;
095                this.params = params;
096            }
097            
098            public AsyncContext getAsyncContext() {
099                return asyncContext;
100            }
101            
102            public String getType() {
103                return type;
104            }
105            
106            public Object[] getParams() {
107                return params;
108            }
109        }
110    
111        
112        @Override
113        public void raiseAsynchronousEvent(String type, Object... parameters) {
114            AbstractSeamServiceContext serviceContext = (AbstractSeamServiceContext)Component.getInstance(AbstractSeamServiceContext.COMPONENT_NAME, false);
115            String sessionId = serviceContext != null ? serviceContext.getSessionId() : null;
116            if (serviceContext != null && sessionId != null)
117                super.raiseAsynchronousEvent(ASYNC_EVENT, new WrappedEvent(serviceContext.getAsyncContext(), type, parameters));
118            else
119                super.raiseAsynchronousEvent(type, parameters);
120        }
121        
122        
123        // Seam 2.0
124        @SuppressWarnings("all")
125        public void raiseTimedEvent(String type, Object schedule, Object... parameters) {
126            AbstractSeamServiceContext serviceContext = (AbstractSeamServiceContext)Component.getInstance(AbstractSeamServiceContext.COMPONENT_NAME, false);
127            String sessionId = serviceContext != null ? serviceContext.getSessionId() : null;
128            if (serviceContext != null && sessionId != null)
129                super.raiseTimedEvent(ASYNC_EVENT, (Schedule)schedule, new WrappedEvent(serviceContext.getAsyncContext(), type, parameters));
130            else
131                super.raiseTimedEvent(type, (Schedule)schedule, parameters);
132        }
133    }