Author: nbelaevski
Date: 2010-10-11 12:26:25 -0400 (Mon, 11 Oct 2010)
New Revision: 19522
Added:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/PushSession.java
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/PushSessionTracker.java
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/Request.java
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/RequestLifecycleListener.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/AtmospherePushHandler.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/DefaultMessageSerializer.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/MessagesContextImpl.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionImpl.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionTrackerImpl.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/RequestImpl.java
Removed:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicQueueSettings.java
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicQueuesContext.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/AtmospherePushHandler.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/DefaultMessageSerializer.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/MessagesContextImpl.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSession.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionImpl.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionTracker.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionTrackerImpl.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/Request.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/RequestImpl.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/RequestLifecycleListener.java
Modified:
branches/RF-7817/push-redesign/pom.xml
branches/RF-7817/push-redesign/src/main/java/org/richfaces/webapp/PushServlet.java
Log:
https://jira.jboss.org/browse/RF-7817 - refactoring
Copied: branches/RF-7817/core/api/src/main/java/org/richfaces/application/push (from rev
19499, branches/RF-7817/ui/core/api/src/main/java/org/richfaces/application/push)
Copied:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/PushSession.java
(from rev 19520,
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSession.java)
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/PushSession.java
(rev 0)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/PushSession.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.application.push;
+
+import java.io.IOException;
+
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public interface PushSession {
+
+ public String getId();
+
+ public void subscribe(TopicKey[] topics);
+
+ public void connect(Request request);
+
+ public void writeMessages() throws IOException;
+
+ public void disconnect();
+
+ public void destroy();
+
+}
Copied:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/PushSessionTracker.java
(from rev 19521,
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionTracker.java)
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/PushSessionTracker.java
(rev 0)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/PushSessionTracker.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -0,0 +1,39 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.application.push;
+
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public interface PushSessionTracker {
+
+ public PushSession createPushSession();
+
+ public PushSession getPushSession(String id);
+
+ public void removePushSession(String id);
+
+ public SubscriptionContext getSubscriptionContext();
+
+}
\ No newline at end of file
Copied:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/Request.java (from
rev 19520,
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/Request.java)
===================================================================
--- branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/Request.java
(rev 0)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/Request.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.application.push;
+
+import java.io.IOException;
+
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public interface Request {
+
+ public void write(Message message) throws IOException;
+
+ public void suspend() throws IOException;
+
+ public void resume() throws IOException;
+
+ public void addListener(RequestLifecycleListener listener);
+
+ public void removeListener(RequestLifecycleListener listener);
+
+ public boolean isSuspended();
+
+}
Copied:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/RequestLifecycleListener.java
(from rev 19520,
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/RequestLifecycleListener.java)
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/RequestLifecycleListener.java
(rev 0)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/RequestLifecycleListener.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.application.push;
+
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public interface RequestLifecycleListener {
+
+ public void onSuspend(Request request);
+
+ public void onDisconnect(Request request);
+
+ public void onResume(Request request);
+
+}
Deleted:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicQueueSettings.java
===================================================================
---
branches/RF-7817/ui/core/api/src/main/java/org/richfaces/application/push/TopicQueueSettings.java 2010-10-08
09:38:48 UTC (rev 19499)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicQueueSettings.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -1,61 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.application.push;
-
-
-/**
- * @author Nick Belaevski
- *
- */
-public interface TopicQueueSettings {
-
- public void addMessageTransformer(MessageTransformer transformer);
-
- public void removeMessageTransformer(MessageTransformer transformer);
-
- public void setMessageSerializer(MessageSerializer serializer);
-
- public MessageSerializer getMessageSerializer();
-
- public void addMessageFilter(MessageFilter filter);
-
- public void removeMessageFilter(MessageFilter filter);
-
- public boolean isSessionAware();
-
- public void setSessionAware(boolean sessionAware);
-
- public boolean isFacesContextAware();
-
- public void setFacesContextAware(boolean facesContextAware);
-
- public TopicMode getDeliveryMode();
-
- public void setDeliveryMode(TopicMode mode);
-
- public int getCapacity();
-
- public void setCapacity(int capacity);
-
- //TODO - transport settings?
-
-}
Deleted:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicQueuesContext.java
===================================================================
---
branches/RF-7817/ui/core/api/src/main/java/org/richfaces/application/push/TopicQueuesContext.java 2010-10-08
09:38:48 UTC (rev 19499)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicQueuesContext.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -1,35 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.application.push;
-
-/**
- * @author Nick Belaevski
- *
- */
-public interface TopicQueuesContext {
-
- public TopicQueueSettings getTopicQueueSettings(TopicKey topicKey);
-
- public TopicQueueSettings getDefaultTopicQueueSettings();
-
-}
-
Modified: branches/RF-7817/push-redesign/pom.xml
===================================================================
--- branches/RF-7817/push-redesign/pom.xml 2010-10-11 16:13:31 UTC (rev 19521)
+++ branches/RF-7817/push-redesign/pom.xml 2010-10-11 16:26:25 UTC (rev 19522)
@@ -45,8 +45,8 @@
<scope>provided</scope>
</dependency>
<dependency>
- <groupId>org.richfaces.ui.core</groupId>
- <artifactId>richfaces-ui-core-api</artifactId>
+ <groupId>org.richfaces.core</groupId>
+ <artifactId>richfaces-core-api</artifactId>
<version>4.0.0-SNAPSHOT</version>
</dependency>
<dependency>
Deleted:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/AtmospherePushHandler.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/AtmospherePushHandler.java 2010-10-11
16:13:31 UTC (rev 19521)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/AtmospherePushHandler.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -1,117 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.application.impl;
-
-import java.io.IOException;
-import java.util.concurrent.Executors;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.atmosphere.cpr.AtmosphereHandler;
-import org.atmosphere.cpr.AtmosphereResource;
-import org.atmosphere.cpr.AtmosphereResourceEvent;
-import org.richfaces.application.push.TopicKey;
-
-/**
- * @author Nick Belaevski
- *
- */
-public class AtmospherePushHandler implements AtmosphereHandler<HttpServletRequest,
HttpServletResponse> {
-
- private static final String PUSH_SESSION_ID_PARAM = "pushSessionId";
-
- private static final String PUSH_SESSION_ATTRIBUTE = "pushSession";
-
-// private PushSubscriberContext subscriberContext;
-//
-// private PushPublisherContext publisherContext;
-//
-// private Map<TopicKey, Broadcaster> broadcasters = new
MapMaker().makeComputingMap(new Function<TopicKey, Broadcaster>() {
-// public Broadcaster apply(TopicKey from) {
-// return new DefaultBroadcaster(from.getTopicName());
-// };
-// });
-//
-
- private PushSessionTracker pushTracker;
-
- protected PushSessionTracker getPushTracker() {
- return pushTracker;
- }
-
- public AtmospherePushHandler(MessagesContextImpl messagesContext) {
- super();
-
- pushTracker = new PushSessionTrackerImpl(Executors.newFixedThreadPool(1),
messagesContext);
- }
-
- /* (non-Javadoc)
- * @see
org.atmosphere.cpr.AtmosphereHandler#onRequest(org.atmosphere.cpr.AtmosphereResource)
- */
- public void onRequest(AtmosphereResource<HttpServletRequest,
HttpServletResponse> resource) throws IOException {
- // TODO Auto-generated method stub
-
- HttpServletRequest req = resource.getRequest();
- HttpServletResponse resp = resource.getResponse();
-
- String pushSessionId = req.getParameter(PUSH_SESSION_ID_PARAM);
-
- PushSessionImpl pushSession = null;
-
- if (pushSessionId != null) {
- pushSession = getPushTracker().getPushSession(pushSessionId);
- }
-
- if (pushSession == null) {
- //TODO - debug log
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
- return;
- }
-
- resp.setContentType("text/plain");
-
- req.setAttribute(PUSH_SESSION_ATTRIBUTE, pushSession);
- pushSession.connect(new RequestImpl(resource));
- pushSession.writeMessages();
- }
-
- public void onStateChange(AtmosphereResourceEvent<HttpServletRequest,
HttpServletResponse> event)
- throws IOException {
- //do nothing
- }
-
- public PushSessionImpl doConnect(String[] topicNames) {
- PushSessionImpl pushSession = getPushTracker().createPushSession();
-
- TopicKey[] topicKeys = new TopicKey[topicNames.length];
- for (int i = 0; i < topicNames.length; i++) {
- String topicName = topicNames[i];
-
- topicKeys[i] = new TopicKey(topicName);
- }
-
- //TODO - check permissions for channels
- pushSession.subscribe(topicKeys);
- return pushSession;
- }
-}
Deleted:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/DefaultMessageSerializer.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/DefaultMessageSerializer.java 2010-10-11
16:13:31 UTC (rev 19521)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/DefaultMessageSerializer.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -1,57 +0,0 @@
-///*
-// * JBoss, Home of Professional Open Source
-// * Copyright 2010, Red Hat, Inc. and individual contributors
-// * by the @authors tag. See the copyright.txt in the distribution for a
-// * full listing of individual contributors.
-// *
-// * This is free software; you can redistribute it and/or modify it
-// * under the terms of the GNU Lesser General Public License as
-// * published by the Free Software Foundation; either version 2.1 of
-// * the License, or (at your option) any later version.
-// *
-// * This software is distributed in the hope that it will be useful,
-// * but WITHOUT ANY WARRANTY; without even the implied warranty of
-// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// * Lesser General Public License for more details.
-// *
-// * You should have received a copy of the GNU Lesser General Public
-// * License along with this software; if not, write to the Free
-// * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
-// * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
-// */
-//package org.richfaces.application.impl;
-//
-//import java.util.HashMap;
-//import java.util.Map;
-//
-//import org.ajax4jsf.javascript.ScriptUtils;
-//import org.richfaces.Message;
-//import org.richfaces.application.MessageSerializer;
-//
-///**
-// * @author Nick Belaevski
-// *
-// */
-//public class DefaultMessageSerializer implements MessageSerializer {
-//
-// public static final String TOPIC_ATTRIBUTE = "topic";
-//
-// public static final String ATTRIBUTES_ATTRIBUTE = "attributes";
-//
-// public static final String DATA_ATTRIBUTE = "data";
-//
-// public String serialize(Message message) {
-// Map<String,Object> dataMap = new HashMap<String, Object>();
-//
-// dataMap.put(TOPIC_ATTRIBUTE, message.getTopicKey().getTopicName());
-//
-// if (message.hasAttributes()) {
-// dataMap.put(ATTRIBUTES_ATTRIBUTE, message.getAttributes());
-// }
-//
-// dataMap.put(DATA_ATTRIBUTE, message.getData());
-//
-// return ScriptUtils.toScript(dataMap);
-// }
-//
-//}
Deleted:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/MessagesContextImpl.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/MessagesContextImpl.java 2010-10-11
16:13:31 UTC (rev 19521)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/MessagesContextImpl.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -1,96 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.application.impl;
-
-import java.util.List;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.CopyOnWriteArrayList;
-
-import javax.servlet.ServletContext;
-
-import org.richfaces.application.push.Message;
-import org.richfaces.application.push.MessageListener;
-import org.richfaces.application.push.PublisherContext;
-import org.richfaces.application.push.SubscriptionContext;
-import org.richfaces.application.push.TopicKey;
-
-/**
- * @author Nick Belaevski
- *
- */
-public class MessagesContextImpl implements SubscriptionContext, PublisherContext {
-
- private ConcurrentMap<TopicKey, List<MessageListener>> listenersMap = new
ConcurrentHashMap<TopicKey, List<MessageListener>>();
-
- private MessagesContextImpl() {}
-
- public void addMessageListener(TopicKey topicKey, MessageListener listener) {
- List<MessageListener> listeners = listenersMap.get(topicKey);
- if (listeners == null) {
- List<MessageListener> newListenersList = new
CopyOnWriteArrayList<MessageListener>();
-
- listeners = listenersMap.putIfAbsent(topicKey, newListenersList);
- if (listeners == null) {
- listeners = newListenersList;
- }
- }
-
- listeners.add(listener);
- }
-
- public void removeMessageListener(TopicKey topicKey, MessageListener listener) {
- List<MessageListener> listeners = listenersMap.get(topicKey);
- if (listeners != null) {
- listeners.remove(listener);
- }
- }
-
- public void publish(TopicKey topicKey, Object data) {
- Message message = new Message(topicKey);
- message.setData(data);
-
- publish(message);
- }
-
- public void publish(Message message) {
- List<MessageListener> listeners = listenersMap.get(message.getTopicKey());
- if (listeners != null) {
- for (MessageListener listener : listeners) {
- listener.onMessage(message);
- }
- }
- }
-
- public static MessagesContextImpl create(ServletContext servletContext) {
- MessagesContextImpl result = new MessagesContextImpl();
-
- servletContext.setAttribute(PublisherContext.ATTRIBUTE_NAME, result);
-
- return result;
- }
-
- public static void destroy(ServletContext servletContext) {
- servletContext.removeAttribute(PublisherContext.ATTRIBUTE_NAME);
- }
-
-}
Deleted:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSession.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSession.java 2010-10-11
16:13:31 UTC (rev 19521)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSession.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -1,44 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.application.impl;
-
-import java.io.IOException;
-
-import org.richfaces.application.push.TopicKey;
-
-/**
- * @author Nick Belaevski
- *
- */
-public interface PushSession {
-
- public void subscribe(TopicKey[] topics);
-
- public void connect(Request request);
-
- public void writeMessages() throws IOException;
-
- public void disconnect();
-
- public void destroy();
-
-}
Deleted:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionImpl.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionImpl.java 2010-10-11
16:13:31 UTC (rev 19521)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionImpl.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -1,190 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.application.impl;
-
-import java.io.IOException;
-import java.util.Queue;
-import java.util.concurrent.Delayed;
-import java.util.concurrent.LinkedBlockingQueue;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicReference;
-
-import org.richfaces.application.push.Message;
-import org.richfaces.application.push.MessageListener;
-import org.richfaces.application.push.SubscriptionContext;
-import org.richfaces.application.push.TopicKey;
-
-/**
- * @author Nick Belaevski
- *
- */
-public class PushSessionImpl implements Delayed, PushSession {
-
- private static final long EXPIRATION_DELAY = 30 * 1000;
-
- private String id;
-
- private AtomicReference<Request> requestRef =
- new AtomicReference<Request>();
-
- private PushSessionTrackerImpl pushTracker;
-
- private volatile long expirationTime;
-
- private TopicKey[] topics;
-
- private Queue<Message> messagesQueue = new
LinkedBlockingQueue<Message>();
-
- private MessageListener queueMessageListener = new MessageListener() {
-
- public void onMessage(Message message) {
- messagesQueue.add(message);
- Request request = requestRef.get();
-
- if (request != null && request.isSuspended()) {
- try {
- writeMessages();
- request.resume();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- };
-
- private RequestLifecycleListener requestLifecycleListener = new
RequestLifecycleListener() {
-
- public void onSuspend(Request request) {
- try {
- writeMessages();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- public void onResume(Request request) {
- // TODO Auto-generated method stub
- }
-
- public void onDisconnect(Request request) {
- // TODO Auto-generated method stub
- PushSessionImpl.this.disconnect();
- }
- };
-
- public PushSessionImpl(PushSessionTrackerImpl pushTracker) {
- super();
- this.pushTracker = pushTracker;
- resetExpirationTime();
- }
-
- private void resetExpirationTime() {
- expirationTime = System.currentTimeMillis() + EXPIRATION_DELAY;
- }
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public int compareTo(Delayed o) {
- return
Long.valueOf(getDelay(TimeUnit.MILLISECONDS)).compareTo(o.getDelay(TimeUnit.MILLISECONDS));
- }
-
- public long getDelay(TimeUnit unit) {
- return unit.convert(expirationTime - System.currentTimeMillis(),
TimeUnit.MILLISECONDS);
- }
-
- public synchronized void subscribe(TopicKey[] topics) {
- SubscriptionContext subscriptionContext = pushTracker.getSubscriptionContext();
-
- if (this.topics != null) {
- for (TopicKey topicKey : topics) {
- subscriptionContext.removeMessageListener(topicKey,
queueMessageListener);
- }
- }
-
- this.topics = topics;
-
- for (TopicKey topicKey : topics) {
- subscriptionContext.addMessageListener(topicKey, queueMessageListener);
- }
-
- }
-
- public void connect(Request request) {
- if (this.requestRef.getAndSet(request) != null) {
- disconnect();
- }
-
- pushTracker.onRequestConnected(this);
- request.addListener(requestLifecycleListener);
- }
-
- public synchronized void writeMessages() throws IOException {
- Request request = requestRef.get();
-
- if (request == null) {
- return;
- }
-
- Message message = null;
-
- while (true) {
- message = messagesQueue.poll();
-
- if (message == null) {
- break;
- }
-
- request.write(message);
- }
-
- if (message == null) {
- request.suspend();
- } else {
- request.resume();
- }
- }
-
- public void disconnect() {
- Request request = this.requestRef.getAndSet(null);
- if (request == null) {
- return;
- }
-
- request.removeListener(requestLifecycleListener);
- pushTracker.onRequestDisconnected(this);
- }
-
- public synchronized void destroy() {
- SubscriptionContext subscriptionContext = pushTracker.getSubscriptionContext();
- for (TopicKey topicKey : topics) {
- subscriptionContext.removeMessageListener(topicKey, queueMessageListener);
- }
- }
-}
Deleted:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionTracker.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionTracker.java 2010-10-11
16:13:31 UTC (rev 19521)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionTracker.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -1,40 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.application.impl;
-
-import org.richfaces.application.push.SubscriptionContext;
-
-/**
- * @author Nick Belaevski
- *
- */
-public interface PushSessionTracker {
-
- public PushSessionImpl createPushSession();
-
- public PushSessionImpl getPushSession(String id);
-
- public void removePushSession(String id);
-
- public SubscriptionContext getSubscriptionContext();
-
-}
\ No newline at end of file
Deleted:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionTrackerImpl.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionTrackerImpl.java 2010-10-11
16:13:31 UTC (rev 19521)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionTrackerImpl.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -1,102 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.application.impl;
-
-import java.util.UUID;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.DelayQueue;
-import java.util.concurrent.ExecutorService;
-
-import org.richfaces.application.push.SubscriptionContext;
-
-import com.google.common.collect.MapMaker;
-
-/**
- * @author Nick Belaevski
- *
- */
-public class PushSessionTrackerImpl implements PushSessionTracker {
-
- private final class SessionsExpirationRunnable implements Runnable {
- public void run() {
- while (true) {
- try {
- PushSessionImpl pushSession = expirationQueue.take();
- pushSessionMap.remove(pushSession.getId());
- pushSession.destroy();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- }
- }
-
- private SubscriptionContext subscriptionContext;
-
- private ConcurrentMap<String, PushSessionImpl> pushSessionMap = new
MapMaker().makeMap();
-
- private DelayQueue<PushSessionImpl> expirationQueue = new
DelayQueue<PushSessionImpl>();
-
- public PushSessionTrackerImpl(ExecutorService executorService, SubscriptionContext
subscriptionContext) {
- executorService.submit(new SessionsExpirationRunnable());
-
- this.subscriptionContext = subscriptionContext;
- }
-
- public PushSessionImpl createPushSession() {
- PushSessionImpl pushSession = new PushSessionImpl(this);
- while (true) {
- String uuid = UUID.randomUUID().toString();
- pushSession.setId(uuid);
- if (pushSessionMap.putIfAbsent(uuid, pushSession) == null) {
- expirationQueue.put(pushSession);
-
- return pushSession;
- }
- }
- }
-
- public PushSessionImpl getPushSession(String id) {
- return pushSessionMap.get(id);
- }
-
- public void removePushSession(String id) {
- PushSessionImpl session = pushSessionMap.remove(id);
- if (session != null) {
- expirationQueue.remove(session);
- }
- }
-
- void onRequestConnected(PushSessionImpl pushSession) {
- expirationQueue.remove(pushSession);
- }
-
- void onRequestDisconnected(PushSessionImpl pushSession) {
- expirationQueue.add(pushSession);
- }
-
- public SubscriptionContext getSubscriptionContext() {
- return subscriptionContext;
- }
-}
Deleted:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/Request.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/Request.java 2010-10-11
16:13:31 UTC (rev 19521)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/Request.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -1,46 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.application.impl;
-
-import java.io.IOException;
-
-import org.richfaces.application.push.Message;
-
-/**
- * @author Nick Belaevski
- *
- */
-public interface Request {
-
- public void write(Message message) throws IOException;
-
- public void suspend() throws IOException;
-
- public void resume() throws IOException;
-
- public void addListener(RequestLifecycleListener listener);
-
- public void removeListener(RequestLifecycleListener listener);
-
- public boolean isSuspended();
-
-}
Deleted:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/RequestImpl.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/RequestImpl.java 2010-10-11
16:13:31 UTC (rev 19521)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/RequestImpl.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -1,171 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.application.impl;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.ajax4jsf.javascript.ScriptUtils;
-import org.atmosphere.cpr.AtmosphereEventLifecycle;
-import org.atmosphere.cpr.AtmosphereResource;
-import org.atmosphere.cpr.AtmosphereResourceEvent;
-import org.atmosphere.cpr.AtmosphereResourceEventListener;
-import org.richfaces.application.push.Message;
-
-/**
- * @author Nick Belaevski
- *
- */
-public class RequestImpl implements Request {
-
- private static final String DATA_WRAPPER_START = "[";
-
- private static final String DATA_WRAPPER_END = "]";
-
- private static final String DATA_BLANK = "";
-
- private AtmosphereResource<HttpServletRequest, HttpServletResponse>
atmosphereResource;
-
- private List<RequestLifecycleListener> listeners = new
ArrayList<RequestLifecycleListener>(2);
-
- private AtmosphereResourceEventListener atmosphereListener = new
AtmosphereResourceEventListener() {
-
- public void onSuspend(AtmosphereResourceEvent<HttpServletRequest,
HttpServletResponse> event) {
- for (RequestLifecycleListener listener : listeners) {
- listener.onSuspend(RequestImpl.this);
- }
- }
-
- public void onResume(AtmosphereResourceEvent<HttpServletRequest,
HttpServletResponse> event) {
- for (RequestLifecycleListener listener : listeners) {
- listener.onResume(RequestImpl.this);
- }
-
- try {
- encodeRequestEndElement();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- public void onDisconnect(AtmosphereResourceEvent<HttpServletRequest,
HttpServletResponse> event) {
- for (RequestLifecycleListener listener : listeners) {
- listener.onDisconnect(RequestImpl.this);
- }
- }
-
- public void onBroadcast(AtmosphereResourceEvent<HttpServletRequest,
HttpServletResponse> event) {
- // TODO Auto-generated method stub
-
- }
- };
-
- private boolean hasWrittenMessages = false;
-
-
- public RequestImpl(AtmosphereResource<HttpServletRequest, HttpServletResponse>
resource) {
- super();
-
- this.atmosphereResource = resource;
- ((AtmosphereEventLifecycle)
atmosphereResource).addEventListener(atmosphereListener);
- }
-
- public void addListener(RequestLifecycleListener listener) {
- listeners.add(listener);
- }
-
- public void removeListener(RequestLifecycleListener listener) {
- listeners.remove(listener);
- }
-
- public void write(Message message) throws IOException {
- HttpServletResponse response = atmosphereResource.getResponse();
-
- PrintWriter writer = response.getWriter();
-
- if (!hasWrittenMessages) {
- encodeRequestStartElement();
- }
-
- Map<String,Object> map = new HashMap<String, Object>();
-
- map.put("topic", message.getTopicKey().getTopicName());
- map.put("data", message.getData());
- map.put("attributes", message.getAttributes());
-
- //TODO use message serializer
- writer.write(ScriptUtils.toScript(map));
-
- if (hasWrittenMessages) {
- writer.write(", ");
- } else {
- hasWrittenMessages = true;
- }
-
- writer.flush();
-
- }
-
- public void suspend() throws IOException {
- if (!isSuspended()) {
- //TODO - customize interval
- atmosphereResource.suspend();
- }
- }
-
- public void resume() throws IOException {
- if (isSuspended()) {
- atmosphereResource.resume();
- }
- }
-
- public boolean isSuspended() {
- return atmosphereResource.getAtmosphereResourceEvent().isSuspended();
- }
-
- public void encodeRequestStartElement() throws IOException {
- write(DATA_WRAPPER_START);
- }
-
- public void encodeRequestEndElement() throws IOException {
- if (hasWrittenMessages) {
- write(DATA_WRAPPER_END);
- } else {
- write(DATA_BLANK);
- }
- }
-
- private void write(String s) throws IOException {
- HttpServletResponse response = atmosphereResource.getResponse();
-
- PrintWriter writer = response.getWriter();
- writer.write(s);
- }
-}
Deleted:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/RequestLifecycleListener.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/RequestLifecycleListener.java 2010-10-11
16:13:31 UTC (rev 19521)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/RequestLifecycleListener.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -1,37 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.application.impl;
-
-
-/**
- * @author Nick Belaevski
- *
- */
-public interface RequestLifecycleListener {
-
- public void onSuspend(Request request);
-
- public void onDisconnect(Request request);
-
- public void onResume(Request request);
-
-}
Copied:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/AtmospherePushHandler.java
(from rev 19521,
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/AtmospherePushHandler.java)
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/AtmospherePushHandler.java
(rev 0)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/AtmospherePushHandler.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -0,0 +1,119 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.application.push.impl;
+
+import java.io.IOException;
+import java.util.concurrent.Executors;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.atmosphere.cpr.AtmosphereHandler;
+import org.atmosphere.cpr.AtmosphereResource;
+import org.atmosphere.cpr.AtmosphereResourceEvent;
+import org.richfaces.application.push.PushSession;
+import org.richfaces.application.push.PushSessionTracker;
+import org.richfaces.application.push.TopicKey;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class AtmospherePushHandler implements AtmosphereHandler<HttpServletRequest,
HttpServletResponse> {
+
+ private static final String PUSH_SESSION_ID_PARAM = "pushSessionId";
+
+ private static final String PUSH_SESSION_ATTRIBUTE = "pushSession";
+
+// private PushSubscriberContext subscriberContext;
+//
+// private PushPublisherContext publisherContext;
+//
+// private Map<TopicKey, Broadcaster> broadcasters = new
MapMaker().makeComputingMap(new Function<TopicKey, Broadcaster>() {
+// public Broadcaster apply(TopicKey from) {
+// return new DefaultBroadcaster(from.getTopicName());
+// };
+// });
+//
+
+ private PushSessionTracker pushTracker;
+
+ protected PushSessionTracker getPushTracker() {
+ return pushTracker;
+ }
+
+ public AtmospherePushHandler(MessagesContextImpl messagesContext) {
+ super();
+
+ pushTracker = new PushSessionTrackerImpl(Executors.newFixedThreadPool(1),
messagesContext);
+ }
+
+ /* (non-Javadoc)
+ * @see
org.atmosphere.cpr.AtmosphereHandler#onRequest(org.atmosphere.cpr.AtmosphereResource)
+ */
+ public void onRequest(AtmosphereResource<HttpServletRequest,
HttpServletResponse> resource) throws IOException {
+ // TODO Auto-generated method stub
+
+ HttpServletRequest req = resource.getRequest();
+ HttpServletResponse resp = resource.getResponse();
+
+ String pushSessionId = req.getParameter(PUSH_SESSION_ID_PARAM);
+
+ PushSession pushSession = null;
+
+ if (pushSessionId != null) {
+ pushSession = getPushTracker().getPushSession(pushSessionId);
+ }
+
+ if (pushSession == null) {
+ //TODO - debug log
+ resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
+ return;
+ }
+
+ resp.setContentType("text/plain");
+
+ req.setAttribute(PUSH_SESSION_ATTRIBUTE, pushSession);
+ pushSession.connect(new RequestImpl(resource));
+ pushSession.writeMessages();
+ }
+
+ public void onStateChange(AtmosphereResourceEvent<HttpServletRequest,
HttpServletResponse> event)
+ throws IOException {
+ //do nothing
+ }
+
+ public PushSession doConnect(String[] topicNames) {
+ PushSession pushSession = getPushTracker().createPushSession();
+
+ TopicKey[] topicKeys = new TopicKey[topicNames.length];
+ for (int i = 0; i < topicNames.length; i++) {
+ String topicName = topicNames[i];
+
+ topicKeys[i] = new TopicKey(topicName);
+ }
+
+ //TODO - check permissions for channels
+ pushSession.subscribe(topicKeys);
+ return pushSession;
+ }
+}
Copied:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/DefaultMessageSerializer.java
(from rev 19520,
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/DefaultMessageSerializer.java)
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/DefaultMessageSerializer.java
(rev 0)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/DefaultMessageSerializer.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -0,0 +1,58 @@
+package org.richfaces.application.push.impl;
+///*
+// * JBoss, Home of Professional Open Source
+// * Copyright 2010, Red Hat, Inc. and individual contributors
+// * by the @authors tag. See the copyright.txt in the distribution for a
+// * full listing of individual contributors.
+// *
+// * This is free software; you can redistribute it and/or modify it
+// * under the terms of the GNU Lesser General Public License as
+// * published by the Free Software Foundation; either version 2.1 of
+// * the License, or (at your option) any later version.
+// *
+// * This software is distributed in the hope that it will be useful,
+// * but WITHOUT ANY WARRANTY; without even the implied warranty of
+// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// * Lesser General Public License for more details.
+// *
+// * You should have received a copy of the GNU Lesser General Public
+// * License along with this software; if not, write to the Free
+// * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+// * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+// */
+//package org.richfaces.application.impl;
+//
+//import java.util.HashMap;
+//import java.util.Map;
+//
+//import org.ajax4jsf.javascript.ScriptUtils;
+//import org.richfaces.Message;
+//import org.richfaces.application.MessageSerializer;
+//
+///**
+// * @author Nick Belaevski
+// *
+// */
+//public class DefaultMessageSerializer implements MessageSerializer {
+//
+// public static final String TOPIC_ATTRIBUTE = "topic";
+//
+// public static final String ATTRIBUTES_ATTRIBUTE = "attributes";
+//
+// public static final String DATA_ATTRIBUTE = "data";
+//
+// public String serialize(Message message) {
+// Map<String,Object> dataMap = new HashMap<String, Object>();
+//
+// dataMap.put(TOPIC_ATTRIBUTE, message.getTopicKey().getTopicName());
+//
+// if (message.hasAttributes()) {
+// dataMap.put(ATTRIBUTES_ATTRIBUTE, message.getAttributes());
+// }
+//
+// dataMap.put(DATA_ATTRIBUTE, message.getData());
+//
+// return ScriptUtils.toScript(dataMap);
+// }
+//
+//}
Copied:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/MessagesContextImpl.java
(from rev 19520,
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/MessagesContextImpl.java)
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/MessagesContextImpl.java
(rev 0)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/MessagesContextImpl.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -0,0 +1,96 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.application.push.impl;
+
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import javax.servlet.ServletContext;
+
+import org.richfaces.application.push.Message;
+import org.richfaces.application.push.MessageListener;
+import org.richfaces.application.push.PublisherContext;
+import org.richfaces.application.push.SubscriptionContext;
+import org.richfaces.application.push.TopicKey;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class MessagesContextImpl implements SubscriptionContext, PublisherContext {
+
+ private ConcurrentMap<TopicKey, List<MessageListener>> listenersMap = new
ConcurrentHashMap<TopicKey, List<MessageListener>>();
+
+ private MessagesContextImpl() {}
+
+ public void addMessageListener(TopicKey topicKey, MessageListener listener) {
+ List<MessageListener> listeners = listenersMap.get(topicKey);
+ if (listeners == null) {
+ List<MessageListener> newListenersList = new
CopyOnWriteArrayList<MessageListener>();
+
+ listeners = listenersMap.putIfAbsent(topicKey, newListenersList);
+ if (listeners == null) {
+ listeners = newListenersList;
+ }
+ }
+
+ listeners.add(listener);
+ }
+
+ public void removeMessageListener(TopicKey topicKey, MessageListener listener) {
+ List<MessageListener> listeners = listenersMap.get(topicKey);
+ if (listeners != null) {
+ listeners.remove(listener);
+ }
+ }
+
+ public void publish(TopicKey topicKey, Object data) {
+ Message message = new Message(topicKey);
+ message.setData(data);
+
+ publish(message);
+ }
+
+ public void publish(Message message) {
+ List<MessageListener> listeners = listenersMap.get(message.getTopicKey());
+ if (listeners != null) {
+ for (MessageListener listener : listeners) {
+ listener.onMessage(message);
+ }
+ }
+ }
+
+ public static MessagesContextImpl create(ServletContext servletContext) {
+ MessagesContextImpl result = new MessagesContextImpl();
+
+ servletContext.setAttribute(PublisherContext.ATTRIBUTE_NAME, result);
+
+ return result;
+ }
+
+ public static void destroy(ServletContext servletContext) {
+ servletContext.removeAttribute(PublisherContext.ATTRIBUTE_NAME);
+ }
+
+}
Copied:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionImpl.java
(from rev 19521,
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionImpl.java)
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionImpl.java
(rev 0)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionImpl.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -0,0 +1,193 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.application.push.impl;
+
+import java.io.IOException;
+import java.util.Queue;
+import java.util.concurrent.Delayed;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.richfaces.application.push.Message;
+import org.richfaces.application.push.MessageListener;
+import org.richfaces.application.push.PushSession;
+import org.richfaces.application.push.Request;
+import org.richfaces.application.push.RequestLifecycleListener;
+import org.richfaces.application.push.SubscriptionContext;
+import org.richfaces.application.push.TopicKey;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class PushSessionImpl implements Delayed, PushSession {
+
+ private static final long EXPIRATION_DELAY = 30 * 1000;
+
+ private String id;
+
+ private AtomicReference<Request> requestRef =
+ new AtomicReference<Request>();
+
+ private PushSessionTrackerImpl pushTracker;
+
+ private volatile long expirationTime;
+
+ private TopicKey[] topics;
+
+ private Queue<Message> messagesQueue = new
LinkedBlockingQueue<Message>();
+
+ private MessageListener queueMessageListener = new MessageListener() {
+
+ public void onMessage(Message message) {
+ messagesQueue.add(message);
+ Request request = requestRef.get();
+
+ if (request != null && request.isSuspended()) {
+ try {
+ writeMessages();
+ request.resume();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+ };
+
+ private RequestLifecycleListener requestLifecycleListener = new
RequestLifecycleListener() {
+
+ public void onSuspend(Request request) {
+ try {
+ writeMessages();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public void onResume(Request request) {
+ // TODO Auto-generated method stub
+ }
+
+ public void onDisconnect(Request request) {
+ // TODO Auto-generated method stub
+ PushSessionImpl.this.disconnect();
+ }
+ };
+
+ public PushSessionImpl(PushSessionTrackerImpl pushTracker) {
+ super();
+ this.pushTracker = pushTracker;
+ resetExpirationTime();
+ }
+
+ private void resetExpirationTime() {
+ expirationTime = System.currentTimeMillis() + EXPIRATION_DELAY;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public int compareTo(Delayed o) {
+ return
Long.valueOf(getDelay(TimeUnit.MILLISECONDS)).compareTo(o.getDelay(TimeUnit.MILLISECONDS));
+ }
+
+ public long getDelay(TimeUnit unit) {
+ return unit.convert(expirationTime - System.currentTimeMillis(),
TimeUnit.MILLISECONDS);
+ }
+
+ public synchronized void subscribe(TopicKey[] topics) {
+ SubscriptionContext subscriptionContext = pushTracker.getSubscriptionContext();
+
+ if (this.topics != null) {
+ for (TopicKey topicKey : topics) {
+ subscriptionContext.removeMessageListener(topicKey,
queueMessageListener);
+ }
+ }
+
+ this.topics = topics;
+
+ for (TopicKey topicKey : topics) {
+ subscriptionContext.addMessageListener(topicKey, queueMessageListener);
+ }
+
+ }
+
+ public void connect(Request request) {
+ if (this.requestRef.getAndSet(request) != null) {
+ disconnect();
+ }
+
+ pushTracker.onRequestConnected(this);
+ request.addListener(requestLifecycleListener);
+ }
+
+ public synchronized void writeMessages() throws IOException {
+ Request request = requestRef.get();
+
+ if (request == null) {
+ return;
+ }
+
+ Message message = null;
+
+ while (true) {
+ message = messagesQueue.poll();
+
+ if (message == null) {
+ break;
+ }
+
+ request.write(message);
+ }
+
+ if (message == null) {
+ request.suspend();
+ } else {
+ request.resume();
+ }
+ }
+
+ public void disconnect() {
+ Request request = this.requestRef.getAndSet(null);
+ if (request == null) {
+ return;
+ }
+
+ request.removeListener(requestLifecycleListener);
+ pushTracker.onRequestDisconnected(this);
+ }
+
+ public synchronized void destroy() {
+ SubscriptionContext subscriptionContext = pushTracker.getSubscriptionContext();
+ for (TopicKey topicKey : topics) {
+ subscriptionContext.removeMessageListener(topicKey, queueMessageListener);
+ }
+ }
+}
Copied:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionTrackerImpl.java
(from rev 19521,
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/PushSessionTrackerImpl.java)
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionTrackerImpl.java
(rev 0)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushSessionTrackerImpl.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -0,0 +1,103 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.application.push.impl;
+
+import java.util.UUID;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.DelayQueue;
+import java.util.concurrent.ExecutorService;
+
+import org.richfaces.application.push.PushSessionTracker;
+import org.richfaces.application.push.SubscriptionContext;
+
+import com.google.common.collect.MapMaker;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class PushSessionTrackerImpl implements PushSessionTracker {
+
+ private final class SessionsExpirationRunnable implements Runnable {
+ public void run() {
+ while (true) {
+ try {
+ PushSessionImpl pushSession = expirationQueue.take();
+ pushSessionMap.remove(pushSession.getId());
+ pushSession.destroy();
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ }
+ }
+
+ private SubscriptionContext subscriptionContext;
+
+ private ConcurrentMap<String, PushSessionImpl> pushSessionMap = new
MapMaker().makeMap();
+
+ private DelayQueue<PushSessionImpl> expirationQueue = new
DelayQueue<PushSessionImpl>();
+
+ public PushSessionTrackerImpl(ExecutorService executorService, SubscriptionContext
subscriptionContext) {
+ executorService.submit(new SessionsExpirationRunnable());
+
+ this.subscriptionContext = subscriptionContext;
+ }
+
+ public PushSessionImpl createPushSession() {
+ PushSessionImpl pushSession = new PushSessionImpl(this);
+ while (true) {
+ String uuid = UUID.randomUUID().toString();
+ pushSession.setId(uuid);
+ if (pushSessionMap.putIfAbsent(uuid, pushSession) == null) {
+ expirationQueue.put(pushSession);
+
+ return pushSession;
+ }
+ }
+ }
+
+ public PushSessionImpl getPushSession(String id) {
+ return pushSessionMap.get(id);
+ }
+
+ public void removePushSession(String id) {
+ PushSessionImpl session = pushSessionMap.remove(id);
+ if (session != null) {
+ expirationQueue.remove(session);
+ }
+ }
+
+ void onRequestConnected(PushSessionImpl pushSession) {
+ expirationQueue.remove(pushSession);
+ }
+
+ void onRequestDisconnected(PushSessionImpl pushSession) {
+ expirationQueue.add(pushSession);
+ }
+
+ public SubscriptionContext getSubscriptionContext() {
+ return subscriptionContext;
+ }
+}
Copied:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/RequestImpl.java
(from rev 19520,
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/impl/RequestImpl.java)
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/RequestImpl.java
(rev 0)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/RequestImpl.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -0,0 +1,173 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.application.push.impl;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.ajax4jsf.javascript.ScriptUtils;
+import org.atmosphere.cpr.AtmosphereEventLifecycle;
+import org.atmosphere.cpr.AtmosphereResource;
+import org.atmosphere.cpr.AtmosphereResourceEvent;
+import org.atmosphere.cpr.AtmosphereResourceEventListener;
+import org.richfaces.application.push.Message;
+import org.richfaces.application.push.Request;
+import org.richfaces.application.push.RequestLifecycleListener;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class RequestImpl implements Request {
+
+ private static final String DATA_WRAPPER_START = "[";
+
+ private static final String DATA_WRAPPER_END = "]";
+
+ private static final String DATA_BLANK = "";
+
+ private AtmosphereResource<HttpServletRequest, HttpServletResponse>
atmosphereResource;
+
+ private List<RequestLifecycleListener> listeners = new
ArrayList<RequestLifecycleListener>(2);
+
+ private AtmosphereResourceEventListener atmosphereListener = new
AtmosphereResourceEventListener() {
+
+ public void onSuspend(AtmosphereResourceEvent<HttpServletRequest,
HttpServletResponse> event) {
+ for (RequestLifecycleListener listener : listeners) {
+ listener.onSuspend(RequestImpl.this);
+ }
+ }
+
+ public void onResume(AtmosphereResourceEvent<HttpServletRequest,
HttpServletResponse> event) {
+ for (RequestLifecycleListener listener : listeners) {
+ listener.onResume(RequestImpl.this);
+ }
+
+ try {
+ encodeRequestEndElement();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public void onDisconnect(AtmosphereResourceEvent<HttpServletRequest,
HttpServletResponse> event) {
+ for (RequestLifecycleListener listener : listeners) {
+ listener.onDisconnect(RequestImpl.this);
+ }
+ }
+
+ public void onBroadcast(AtmosphereResourceEvent<HttpServletRequest,
HttpServletResponse> event) {
+ // TODO Auto-generated method stub
+
+ }
+ };
+
+ private boolean hasWrittenMessages = false;
+
+
+ public RequestImpl(AtmosphereResource<HttpServletRequest, HttpServletResponse>
resource) {
+ super();
+
+ this.atmosphereResource = resource;
+ ((AtmosphereEventLifecycle)
atmosphereResource).addEventListener(atmosphereListener);
+ }
+
+ public void addListener(RequestLifecycleListener listener) {
+ listeners.add(listener);
+ }
+
+ public void removeListener(RequestLifecycleListener listener) {
+ listeners.remove(listener);
+ }
+
+ public void write(Message message) throws IOException {
+ HttpServletResponse response = atmosphereResource.getResponse();
+
+ PrintWriter writer = response.getWriter();
+
+ if (!hasWrittenMessages) {
+ encodeRequestStartElement();
+ }
+
+ Map<String,Object> map = new HashMap<String, Object>();
+
+ map.put("topic", message.getTopicKey().getTopicName());
+ map.put("data", message.getData());
+ map.put("attributes", message.getAttributes());
+
+ //TODO use message serializer
+ writer.write(ScriptUtils.toScript(map));
+
+ if (hasWrittenMessages) {
+ writer.write(", ");
+ } else {
+ hasWrittenMessages = true;
+ }
+
+ writer.flush();
+
+ }
+
+ public void suspend() throws IOException {
+ if (!isSuspended()) {
+ //TODO - customize interval
+ atmosphereResource.suspend();
+ }
+ }
+
+ public void resume() throws IOException {
+ if (isSuspended()) {
+ atmosphereResource.resume();
+ }
+ }
+
+ public boolean isSuspended() {
+ return atmosphereResource.getAtmosphereResourceEvent().isSuspended();
+ }
+
+ public void encodeRequestStartElement() throws IOException {
+ write(DATA_WRAPPER_START);
+ }
+
+ public void encodeRequestEndElement() throws IOException {
+ if (hasWrittenMessages) {
+ write(DATA_WRAPPER_END);
+ } else {
+ write(DATA_BLANK);
+ }
+ }
+
+ private void write(String s) throws IOException {
+ HttpServletResponse response = atmosphereResource.getResponse();
+
+ PrintWriter writer = response.getWriter();
+ writer.write(s);
+ }
+}
Modified:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/webapp/PushServlet.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/webapp/PushServlet.java 2010-10-11
16:13:31 UTC (rev 19521)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/webapp/PushServlet.java 2010-10-11
16:26:25 UTC (rev 19522)
@@ -31,9 +31,9 @@
import javax.servlet.http.HttpServletResponse;
import org.atmosphere.cpr.AtmosphereServlet;
-import org.richfaces.application.impl.AtmospherePushHandler;
-import org.richfaces.application.impl.MessagesContextImpl;
-import org.richfaces.application.impl.PushSessionImpl;
+import org.richfaces.application.push.PushSession;
+import org.richfaces.application.push.impl.AtmospherePushHandler;
+import org.richfaces.application.push.impl.MessagesContextImpl;
/**
* @author Nick Belaevski
@@ -79,10 +79,10 @@
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
String[] topicNames = req.getParameterValues(PUSH_TOPIC_PARAM);
- PushSessionImpl pushData = pushHandler.doConnect(topicNames);
+ PushSession pushSession = pushHandler.doConnect(topicNames);
resp.setStatus(HttpServletResponse.SC_OK);
- resp.getWriter().write(pushData.getId());
+ resp.getWriter().write(pushSession.getId());
}
@Override