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 */
022package org.granite.gravity;
023
024import javax.servlet.ServletContext;
025
026import flex.messaging.messages.ErrorMessage;
027import org.granite.config.GraniteConfig;
028import org.granite.config.flex.ServicesConfig;
029import org.granite.context.GraniteContext;
030import org.granite.gravity.adapters.ServiceAdapter;
031import org.granite.gravity.udp.UdpReceiverFactory;
032
033import flex.messaging.messages.AsyncMessage;
034import flex.messaging.messages.Message;
035import org.granite.messaging.jmf.SharedContext;
036
037/**
038 * @author William DRAI
039 */
040public 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        return getGravity().getChannel(channelFactory, channelId);
114    }
115    public Channel removeChannel(String channelId, boolean timeout) {
116        return getGravity().removeChannel(channelId, timeout);
117    }
118    public boolean access(String channelId) {
119        return getGravity().access(channelId);
120    }
121    public void execute(AsyncChannelRunner runnable) {
122        getGravity().execute(runnable);
123    }
124    public boolean cancel(AsyncChannelRunner runnable) {
125        return getGravity().cancel(runnable);
126    }
127
128    public Message handleMessage(ChannelFactory<?> channelFactory, Message message) {
129        return getGravity().handleMessage(channelFactory, message);
130    }
131    public Message handleMessage(ChannelFactory<?> channelFactory, Message message, boolean skipInterceptor) {
132        return getGravity().handleMessage(channelFactory, message, skipInterceptor);
133    }
134    public Message publishMessage(AsyncMessage message) {
135        return publishMessage(null, message);
136    }
137    public Message publishMessage(Channel fromChannel, AsyncMessage message) {
138        if (getGravity() == null)
139            return new ErrorMessage(message, new IllegalStateException("Gravity Proxy not yet ready"));
140
141        return getGravity().publishMessage(fromChannel, message);
142    }
143}