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