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     */
022    package org.granite.gravity;
023    
024    import javax.servlet.ServletContext;
025    
026    import flex.messaging.messages.ErrorMessage;
027    import org.granite.config.GraniteConfig;
028    import org.granite.config.flex.ServicesConfig;
029    import org.granite.context.GraniteContext;
030    import org.granite.gravity.adapters.ServiceAdapter;
031    import org.granite.gravity.udp.UdpReceiverFactory;
032    
033    import flex.messaging.messages.AsyncMessage;
034    import flex.messaging.messages.Message;
035    import org.granite.messaging.jmf.SharedContext;
036    
037    /**
038     * @author William DRAI
039     */
040    public class GravityProxy implements Gravity {
041    
042            private ServletContext servletContext;
043    
044        public GravityProxy() {
045        }
046    
047            public GravityProxy(ServletContext servletContext) {
048                    this.servletContext = servletContext;
049            }
050            
051            protected Gravity getGravity() {
052                    return GravityManager.getGravity(servletContext);
053            }
054    
055        ///////////////////////////////////////////////////////////////////////////
056        // Granite/Services configs access.
057    
058            public GravityConfig getGravityConfig() {
059            return getGravity().getGravityConfig();
060        }
061        public ServicesConfig getServicesConfig() {
062            return getGravity().getServicesConfig();
063        }
064        public GraniteConfig getGraniteConfig() {
065            return getGravity().getGraniteConfig();
066        }
067        public SharedContext getSharedContext() {
068            return getGravity().getSharedContext();
069        }
070    
071        ///////////////////////////////////////////////////////////////////////////
072        // Properties.
073    
074            public boolean isStarted() {
075                    return getGravity().isStarted();
076            }
077    
078        ///////////////////////////////////////////////////////////////////////////
079        // Operations.
080    
081        public GraniteContext initThread(String sessionId, String clientType) {
082            return getGravity().initThread(sessionId, clientType);
083        }
084        public void releaseThread() {
085            getGravity().releaseThread();
086        }
087            
088            public ServiceAdapter getServiceAdapter(String messageType, String destinationId) {
089                    return getGravity().getServiceAdapter(messageType, destinationId);
090            }
091    
092            public boolean hasUdpReceiverFactory() {
093                    return getGravity().hasUdpReceiverFactory();
094            }
095        public UdpReceiverFactory getUdpReceiverFactory() {
096                    return getGravity().getUdpReceiverFactory();
097            }
098            
099        public void start() throws Exception {
100            getGravity().start();
101        }
102        public void reconfigure(GravityConfig gravityConfig, GraniteConfig graniteConfig) {
103            getGravity().reconfigure(gravityConfig, graniteConfig);
104        }
105        public void stop() throws Exception {
106            getGravity().stop();
107        }
108        public void stop(boolean now) throws Exception {
109            getGravity().stop(now);
110        }
111    
112        public <C extends Channel> C getChannel(ChannelFactory<C> channelFactory, String channelId) {
113            Gravity gravity = getGravity();
114            if (gravity == null)
115                return null;
116            return gravity.getChannel(channelFactory, channelId);
117        }
118        public Channel removeChannel(String channelId, boolean timeout) {
119            Gravity gravity = getGravity();
120            if (gravity == null)
121                return null;
122            return gravity.removeChannel(channelId, timeout);
123        }
124        public boolean access(String channelId) {
125            Gravity gravity = getGravity();
126            if (gravity == null)
127                return false;
128            return gravity.access(channelId);
129        }
130        public void execute(AsyncChannelRunner runnable) {
131            Gravity gravity = getGravity();
132            if (gravity == null)
133                return;
134            gravity.execute(runnable);
135        }
136        public boolean cancel(AsyncChannelRunner runnable) {
137            Gravity gravity = getGravity();
138            if (gravity == null)
139                return false;
140            return gravity.cancel(runnable);
141        }
142    
143        public Message handleMessage(ChannelFactory<?> channelFactory, Message message) {
144            // Should probably throw an exception, not intended to be used through the proxy
145            Gravity gravity = getGravity();
146            if (gravity == null)
147                return new ErrorMessage(message, new IllegalStateException("Gravity not initialized"));
148            return gravity.handleMessage(channelFactory, message);
149        }
150        public Message handleMessage(ChannelFactory<?> channelFactory, Message message, boolean skipInterceptor) {
151            // Should probably throw an exception, not intended to be used through the proxy
152            Gravity gravity = getGravity();
153            if (gravity == null)
154                return new ErrorMessage(message, new IllegalStateException("Gravity not initialized"));
155            return gravity.handleMessage(channelFactory, message, skipInterceptor);
156        }
157        public Message publishMessage(AsyncMessage message) {
158            return publishMessage(null, message);
159        }
160        public Message publishMessage(Channel fromChannel, AsyncMessage message) {
161            // Should probably throw an exception, not intended to be used through the proxy
162            Gravity gravity = getGravity();
163            if (gravity == null)
164                return new ErrorMessage(message, new IllegalStateException("Gravity not initialized"));
165    
166            return getGravity().publishMessage(fromChannel, message);
167        }
168    }