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.gravity.gae;
022    
023    import org.granite.config.GraniteConfig;
024    import org.granite.config.flex.ServicesConfig;
025    import org.granite.gravity.Channel;
026    import org.granite.gravity.DefaultGravity;
027    import org.granite.gravity.GravityConfig;
028    import org.granite.gravity.Subscription;
029    import org.granite.util.UUIDUtil;
030    
031    import com.google.appengine.api.memcache.Expiration;
032    import com.google.appengine.api.memcache.MemcacheService;
033    import com.google.appengine.api.memcache.MemcacheServiceFactory;
034    
035    import flex.messaging.messages.Message;
036    
037    /**
038     * @author William DRAI
039     * @author Franck WOLFF
040     */
041    public class GAEGravity extends DefaultGravity {
042    
043        ///////////////////////////////////////////////////////////////////////////
044        // Fields.
045        
046        static final String CHANNEL_PREFIX = "org.granite.gravity.gae.channel.";
047        
048        private static MemcacheService gaeCache = MemcacheServiceFactory.getMemcacheService();
049        
050        ///////////////////////////////////////////////////////////////////////////
051        // Constructor.
052    
053        public GAEGravity(GravityConfig gravityConfig, ServicesConfig servicesConfig, GraniteConfig graniteConfig) {
054            super(gravityConfig, servicesConfig, graniteConfig);
055        }
056        
057    
058        ///////////////////////////////////////////////////////////////////////////
059        // Channel's operations.
060    
061        @Override
062        protected Channel createChannel() {
063            Channel channel = getGravityConfig().getChannelFactory().newChannel(UUIDUtil.randomUUID());
064            Expiration expiration = Expiration.byDeltaMillis((int)getGravityConfig().getChannelIdleTimeoutMillis());
065            gaeCache.put(CHANNEL_PREFIX + channel.getId(), channel, expiration);
066            gaeCache.put(GAEChannel.MSG_COUNT_PREFIX + channel.getId(), 0L, expiration);
067            return channel;
068        }
069    
070        @Override
071        public Channel getChannel(String channelId) {
072            if (channelId == null)
073                return null;
074    
075            return (Channel)gaeCache.get(CHANNEL_PREFIX + channelId);
076        }
077    
078    
079        @Override
080        public Channel removeChannel(String channelId) {
081            if (channelId == null)
082                return null;
083    
084            Channel channel = (Channel)gaeCache.get(CHANNEL_PREFIX + channelId);
085            if (channel != null) {
086                for (Subscription subscription : channel.getSubscriptions()) {
087                    Message message = subscription.getUnsubscribeMessage();
088                    handleMessage(message, true);
089                }
090    
091                channel.destroy();
092                gaeCache.delete(CHANNEL_PREFIX + channelId);
093                    gaeCache.delete(GAEChannel.MSG_COUNT_PREFIX + channelId);
094            }
095    
096            return channel;
097        }
098    
099        @Override
100        public boolean access(String channelId) {
101            return true;
102        }
103        
104        
105        @Override
106        public void internalStart() {
107            // Avoid starting thread pool
108        }
109    
110        
111        @Override
112        protected void postManage(Channel channel) {
113            Expiration expiration = Expiration.byDeltaMillis((int)getGravityConfig().getChannelIdleTimeoutMillis());
114            gaeCache.put(CHANNEL_PREFIX + channel.getId(), channel, expiration);
115        }
116    }