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 _topicIdCache = new ConcurrentHashMap<String, TopicId>();
052
053 if (Boolean.TRUE.toString().equals(destinationProperties.get("no-local")))
054 noLocal = true;
055 }
056
057
058 public Topic getTopic(TopicId id) {
059 return rootTopic.getChild(id);
060 }
061
062 public Topic getTopic(String id) {
063 TopicId cid = getTopicId(id);
064 if (cid.depth() == 0)
065 return null;
066 return rootTopic.getChild(cid);
067 }
068
069 public Topic getTopic(String id, boolean create) {
070 synchronized (this) {
071 Topic topic = getTopic(id);
072
073 if (topic == null && create) {
074 topic = new Topic(id, this);
075 rootTopic.addChild(topic);
076 log.debug("New Topic: %s", topic);
077 }
078 return topic;
079 }
080 }
081
082 public TopicId getTopicId(String id) {
083 TopicId tid = _topicIdCache.get(id);
084 if (tid == null) {
085 tid = new TopicId(id);
086 _topicIdCache.put(id, tid);
087 }
088 return tid;
089 }
090
091 public boolean hasTopic(String id) {
092 TopicId cid = getTopicId(id);
093 return rootTopic.getChild(cid) != null;
094 }
095
096 @Override
097 public Object invoke(Channel fromChannel, AsyncMessage message) {
098 String topicId = TopicId.normalize(((String)message.getHeader(AsyncMessage.SUBTOPIC_HEADER)));
099
100 AsyncMessage reply = null;
101
102 if (getSecurityPolicy().canPublish(fromChannel, topicId, message)) {
103 TopicId tid = getTopicId(topicId);
104
105 try {
106 fromChannel.publish(new AsyncPublishedMessage(rootTopic, tid, message));
107 reply = new AcknowledgeMessage(message);
108 reply.setMessageId(message.getMessageId());
109 }
110 catch (MessagePublishingException e) {
111 log.error(e, "Error while publishing message: %s from channel %s to topic: %s", message, fromChannel, tid);
112 reply = new ErrorMessage(message, null);
113 ((ErrorMessage)reply).setFaultString("Server.Publish.Error");
114 }
115 }
116 else {
117 log.warn("Channel %s tried to publish a message to topic %s", fromChannel, topicId);
118 reply = new ErrorMessage(message, null);
119 ((ErrorMessage)reply).setFaultString("Server.Access.Denied");
120 }
121
122 return reply;
123 }
124
125 @Override
126 public Object manage(Channel fromChannel, CommandMessage message) {
127 AsyncMessage reply = null;
128
129 if (message.getOperation() == CommandMessage.SUBSCRIBE_OPERATION) {
130 String subscribeTopicId = TopicId.normalize(((String)message.getHeader(AsyncMessage.SUBTOPIC_HEADER)));
131
132 if (getSecurityPolicy().canSubscribe(fromChannel, subscribeTopicId, message)) {
133 Topic topic = getTopic(subscribeTopicId);
134 if (topic == null && getSecurityPolicy().canCreate(fromChannel, subscribeTopicId, message))
135 topic = getTopic(subscribeTopicId, true);
136
137 if (topic != null) {
138 String subscriptionId = (String)message.getHeader(AsyncMessage.DESTINATION_CLIENT_ID_HEADER);
139 String selector = (String)message.getHeader(CommandMessage.SELECTOR_HEADER);
140 if (subscriptionId == null)
141 log.warn("No subscriptionId for subscription message");
142 else
143 topic.subscribe(fromChannel, message.getDestination(), subscriptionId, selector, noLocal);
144
145 reply = new AcknowledgeMessage(message);
146 }
147 else {
148 reply = new ErrorMessage(message, null);
149 ((ErrorMessage)reply).setFaultString("cannot create");
150 }
151 }
152 else {
153 reply = new ErrorMessage(message, null);
154 ((ErrorMessage)reply).setFaultString("cannot subscribe");
155 }
156 }
157 else if (message.getOperation() == CommandMessage.UNSUBSCRIBE_OPERATION) {
158 String unsubscribeTopicId = TopicId.normalize(((String)message.getHeader(AsyncMessage.SUBTOPIC_HEADER)));
159
160 Topic topic = getTopic(unsubscribeTopicId);
161 String subscriptionId = null;
162 if (topic != null) {
163 subscriptionId = (String)message.getHeader(AsyncMessage.DESTINATION_CLIENT_ID_HEADER);
164 if (subscriptionId == null)
165 log.warn("No subscriptionId for unsubscription message");
166 else
167 topic.unsubscribe(fromChannel, subscriptionId);
168 }
169
170 reply = new AcknowledgeMessage(message);
171 reply.setHeader(AsyncMessage.DESTINATION_CLIENT_ID_HEADER, subscriptionId);
172 }
173 else {
174 reply = new ErrorMessage(message, null);
175 ((ErrorMessage)reply).setFaultString("unknown operation");
176
177 }
178
179 return reply;
180 }
181 }