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 java.util.concurrent.ConcurrentHashMap;
024    
025    import org.granite.gravity.Channel;
026    import org.granite.gravity.adapters.ServiceAdapter;
027    import org.granite.logging.Logger;
028    import org.granite.messaging.service.ServiceException;
029    import org.granite.util.XMap;
030    
031    import flex.messaging.messages.AcknowledgeMessage;
032    import flex.messaging.messages.AsyncMessage;
033    import flex.messaging.messages.CommandMessage;
034    import flex.messaging.messages.ErrorMessage;
035    
036    /**
037     * @author William DRAI
038     */
039    public class GAEServiceAdapter extends ServiceAdapter {
040    
041        private static final Logger log = Logger.getLogger(GAEServiceAdapter.class);
042    
043        private final GAETopic rootTopic = new GAETopic("/", this);
044        private transient ConcurrentHashMap<String, GAETopicId> _topicIdCache;
045        
046        private boolean noLocal = false;
047    
048    
049        @Override
050        public void configure(XMap adapterProperties, XMap destinationProperties) throws ServiceException {
051            _topicIdCache = new ConcurrentHashMap<String, GAETopicId>();
052            
053            if (Boolean.TRUE.toString().equals(destinationProperties.get("no-local")))
054                    noLocal = true;
055        }
056    
057    
058        public GAETopic getTopic(GAETopicId id) {
059            return rootTopic.getChild(id);
060        }
061    
062        public GAETopic getTopic(String id) {
063            GAETopicId cid = getTopicId(id);
064            if (cid.depth() == 0)
065                return null;
066            return rootTopic.getChild(cid);
067        }
068    
069        public GAETopic getTopic(String id, boolean create)  {
070            synchronized (this) {
071                GAETopic topic = getTopic(id);
072    
073                if (topic == null && create) {
074                    topic = new GAETopic(id, this);
075                    rootTopic.addChild(topic);
076                    log.debug("New Topic: %s", topic);
077                }
078                return topic;
079            }
080        }
081    
082        public GAETopicId getTopicId(String id) {
083            GAETopicId tid = _topicIdCache.get(id);
084            if (tid == null) {
085                tid = new GAETopicId(id);
086                _topicIdCache.put(id, tid);
087            }
088            return tid;
089        }
090    
091        public boolean hasTopic(String id) {
092            GAETopicId cid = getTopicId(id);
093            return rootTopic.getChild(cid) != null;
094        }
095    
096        @Override
097        public Object invoke(Channel fromChannel, AsyncMessage message) {
098            String topicId = GAETopicId.normalize(((String)message.getHeader(AsyncMessage.SUBTOPIC_HEADER)));
099    
100            AsyncMessage reply = null;
101    
102            if (message.getBody() != null && getSecurityPolicy().canPublish(fromChannel, topicId, message)) {
103                GAETopicId tid = getTopicId(topicId);
104    
105                rootTopic.publish(tid, fromChannel, message);
106    
107                reply = new AcknowledgeMessage(message);
108                reply.setMessageId(message.getMessageId());
109            }
110            else {
111                reply = new ErrorMessage(message, null);
112                ((ErrorMessage)reply).setFaultString("unknown channel");
113            }
114    
115            return reply;
116        }
117    
118        @Override
119        public Object manage(Channel fromChannel, CommandMessage message) {
120            AsyncMessage reply = null;
121    
122            if (message.getOperation() == CommandMessage.SUBSCRIBE_OPERATION) {
123                String subscribeTopicId = GAETopicId.normalize(((String)message.getHeader(AsyncMessage.SUBTOPIC_HEADER)));
124    
125                if (getSecurityPolicy().canSubscribe(fromChannel, subscribeTopicId, message)) {
126                    GAETopic topic = getTopic(subscribeTopicId);
127                    if (topic == null && getSecurityPolicy().canCreate(fromChannel, subscribeTopicId, message))
128                        topic = getTopic(subscribeTopicId, true);
129    
130                    if (topic != null) {
131                        String subscriptionId = (String)message.getHeader(AsyncMessage.DESTINATION_CLIENT_ID_HEADER);
132                        String selector = (String)message.getHeader(CommandMessage.SELECTOR_HEADER);
133                        topic.subscribe(fromChannel, message.getDestination(), subscriptionId, selector, noLocal);
134    
135                        reply = new AcknowledgeMessage(message);
136                    }
137                    else {
138                        reply = new ErrorMessage(message, null);
139                        ((ErrorMessage)reply).setFaultString("cannot create");
140                    }
141                }
142                else {
143                    reply = new ErrorMessage(message, null);
144                    ((ErrorMessage)reply).setFaultString("cannot subscribe");
145                }
146            }
147            else if (message.getOperation() == CommandMessage.UNSUBSCRIBE_OPERATION) {
148                String unsubscribeTopicId = GAETopicId.normalize(((String)message.getHeader(AsyncMessage.SUBTOPIC_HEADER)));
149    
150                GAETopic topic = getTopic(unsubscribeTopicId);
151                String subscriptionId = null;
152                if (topic != null) {
153                    subscriptionId = (String)message.getHeader(AsyncMessage.DESTINATION_CLIENT_ID_HEADER);
154                    topic.unsubscribe(fromChannel, subscriptionId);
155                }
156    
157                reply = new AcknowledgeMessage(message);
158                reply.setHeader(AsyncMessage.DESTINATION_CLIENT_ID_HEADER, subscriptionId);
159            }
160            else {
161                reply = new ErrorMessage(message, null);
162                ((ErrorMessage)reply).setFaultString("unknown operation");
163    
164            }
165    
166            return reply;
167        }
168    }