Author: nbelaevski
Date: 2010-10-21 17:25:54 -0400 (Thu, 21 Oct 2010)
New Revision: 19647
Added:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/EventAbortedException.java
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionPreSubscriptionEvent.java
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionSubscriptionEvent.java
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionTopicEvent.java
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionTopicListener.java
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionUnsubscriptionEvent.java
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SubscriptionAbortedException.java
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicEvent.java
Modified:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/Session.java
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/Topic.java
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicListener.java
branches/RF-7817/push-redesign-app/src/main/java/demo/ChatBean.java
branches/RF-7817/push-redesign-app/src/main/java/demo/TopicsInitializer.java
branches/RF-7817/push-redesign-app/src/main/webapp/index.xhtml
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/AbstractTopic.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushResource.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/jms/SessionImpl.java
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/jms/TopicImpl.java
Log:
https://jira.jboss.org/browse/RF-7817
- started adding listeners for connections to topics
Added:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/EventAbortedException.java
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/EventAbortedException.java
(rev 0)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/EventAbortedException.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -0,0 +1,48 @@
+/*
+ * 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 class EventAbortedException extends Exception {
+
+ private static final long serialVersionUID = -1546282468438542993L;
+
+ public EventAbortedException() {
+ super();
+ }
+
+ public EventAbortedException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public EventAbortedException(String message) {
+ super(message);
+ }
+
+ public EventAbortedException(Throwable cause) {
+ super(cause);
+ }
+
+}
Modified:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/Session.java
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/Session.java 2010-10-21
17:44:09 UTC (rev 19646)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/Session.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -35,7 +35,7 @@
public String getId();
- public void subscribe(TopicKey topic) throws Exception;
+ public void subscribe(TopicKey topic) throws Exception,
SubscriptionAbortedException;
public void connect(Request request) throws Exception;
Added:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionPreSubscriptionEvent.java
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionPreSubscriptionEvent.java
(rev 0)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionPreSubscriptionEvent.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -0,0 +1,41 @@
+/*
+ * 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 class SessionPreSubscriptionEvent extends SessionTopicEvent {
+
+ private static final long serialVersionUID = 2741390800212036457L;
+
+ public SessionPreSubscriptionEvent(Topic topic, Session session) {
+ super(topic, session);
+ }
+
+ @Override
+ public void invokeListener(TopicListener listener) throws EventAbortedException {
+ ((SessionTopicListener) listener).processPreSubscriptionEvent(this);
+ }
+}
Added:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionSubscriptionEvent.java
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionSubscriptionEvent.java
(rev 0)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionSubscriptionEvent.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -0,0 +1,41 @@
+/*
+ * 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 class SessionSubscriptionEvent extends SessionTopicEvent {
+
+ private static final long serialVersionUID = -463481692840464586L;
+
+ public SessionSubscriptionEvent(Topic topic, Session session) {
+ super(topic, session);
+ }
+
+ @Override
+ public void invokeListener(TopicListener listener) throws EventAbortedException {
+ ((SessionTopicListener) listener).processSubscriptionEvent(this);
+ }
+}
Added:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionTopicEvent.java
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionTopicEvent.java
(rev 0)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionTopicEvent.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -0,0 +1,47 @@
+/*
+ * 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 abstract class SessionTopicEvent extends TopicEvent {
+
+ private static final long serialVersionUID = 6339351737472180503L;
+
+ private Session session;
+
+ public SessionTopicEvent(Topic topic, Session session) {
+ super(topic);
+ this.session = session;
+ }
+
+ public Session getSession() {
+ return session;
+ }
+
+ @Override
+ public boolean isAppropriateListener(TopicListener listener) {
+ return (listener instanceof SessionTopicListener);
+ }
+}
Added:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionTopicListener.java
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionTopicListener.java
(rev 0)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionTopicListener.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -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 SessionTopicListener extends TopicListener {
+
+ public void processPreSubscriptionEvent(SessionPreSubscriptionEvent event) throws
EventAbortedException;
+
+ public void processSubscriptionEvent(SessionSubscriptionEvent event) throws
EventAbortedException;
+
+ public void processUnsubscriptionEvent(SessionUnsubscriptionEvent event) throws
EventAbortedException;
+
+}
Added:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionUnsubscriptionEvent.java
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionUnsubscriptionEvent.java
(rev 0)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SessionUnsubscriptionEvent.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -0,0 +1,41 @@
+/*
+ * 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 class SessionUnsubscriptionEvent extends SessionTopicEvent {
+
+ private static final long serialVersionUID = -2286664647234464678L;
+
+ public SessionUnsubscriptionEvent(Topic topic, Session session) {
+ super(topic, session);
+ }
+
+ @Override
+ public void invokeListener(TopicListener listener) throws EventAbortedException {
+ ((SessionTopicListener) listener).processUnsubscriptionEvent(this);
+ }
+}
Added:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SubscriptionAbortedException.java
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SubscriptionAbortedException.java
(rev 0)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/SubscriptionAbortedException.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -0,0 +1,40 @@
+/*
+ * 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 class SubscriptionAbortedException extends Exception {
+
+ private static final long serialVersionUID = -2070837902276133333L;
+
+ public SubscriptionAbortedException() {
+ super();
+ }
+
+ public SubscriptionAbortedException(String message) {
+ super(message);
+ }
+
+}
Modified:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/Topic.java
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/Topic.java 2010-10-21
17:44:09 UTC (rev 19646)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/Topic.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -22,6 +22,7 @@
package org.richfaces.application.push;
+
/**
* @author Nick Belaevski
*
@@ -38,10 +39,8 @@
public void removeTopicListener(TopicListener topicListener);
- public void notifySubscription(Session session, TopicKey topicKey) throws
AbortSubscriptionException;
-
- public void notifyUnsubscription(Session session, TopicKey topicKey);
-
+ public void publishEvent(TopicEvent event) throws EventAbortedException;
+
public void publish(Object messageData) throws MessageException;
}
\ No newline at end of file
Added:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicEvent.java
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicEvent.java
(rev 0)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicEvent.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -0,0 +1,49 @@
+/*
+ * 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.util.EventObject;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class TopicEvent extends EventObject {
+
+ private static final long serialVersionUID = 1986841627148973279L;
+
+ public TopicEvent(Topic topic) {
+ super(topic);
+ }
+
+ public Topic getTopic() {
+ return (Topic) source;
+ }
+
+ public boolean isAppropriateListener(TopicListener listener) {
+ return false;
+ }
+
+ public void invokeListener(TopicListener listener) throws EventAbortedException {
+ throw new IllegalArgumentException(listener.getClass().getName());
+ }
+}
Modified:
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicListener.java
===================================================================
---
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicListener.java 2010-10-21
17:44:09 UTC (rev 19646)
+++
branches/RF-7817/core/api/src/main/java/org/richfaces/application/push/TopicListener.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -21,14 +21,12 @@
*/
package org.richfaces.application.push;
+import java.util.EventListener;
+
/**
* @author Nick Belaevski
*
*/
-public interface TopicListener {
+public interface TopicListener extends EventListener {
- public void onSessionSubscribed(Session session, TopicKey topicKey) throws
AbortSubscriptionException;
-
- public void onSessionUnsubscribed(Session session, TopicKey topicKey);
-
}
Modified:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/AbstractTopic.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/AbstractTopic.java 2010-10-21
17:44:09 UTC (rev 19646)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/AbstractTopic.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -21,15 +21,15 @@
*/
package org.richfaces.application.push.impl;
+import java.text.MessageFormat;
import java.util.List;
-import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;
-import org.richfaces.application.push.AbortSubscriptionException;
+import org.richfaces.application.push.EventAbortedException;
import org.richfaces.application.push.MessageDataSerializer;
import org.richfaces.application.push.MessageException;
-import org.richfaces.application.push.Session;
import org.richfaces.application.push.Topic;
+import org.richfaces.application.push.TopicEvent;
import org.richfaces.application.push.TopicKey;
import org.richfaces.application.push.TopicListener;
import org.richfaces.log.Logger;
@@ -78,39 +78,20 @@
listeners.remove(topicListener);
}
-
- public void notifySubscription(Session session, TopicKey topicKey) throws
AbortSubscriptionException {
- ListIterator<TopicListener> itr = listeners.listIterator();
- while (itr.hasNext()) {
- TopicListener listener = itr.next();
- try {
- listener.onSessionSubscribed(session, topicKey);
- } catch (AbortSubscriptionException e) {
- while (itr.hasPrevious()) {
- try {
- itr.previous().onSessionUnsubscribed(session, topicKey);
- } catch (Exception e1) {
- LOGGER.error(e1.getMessage(), e1);
- }
+ public void publishEvent(TopicEvent event) throws EventAbortedException {
+ for (TopicListener listener: listeners) {
+ if (event.isAppropriateListener(listener)) {
+ try {
+ event.invokeListener(listener);
+ } catch (EventAbortedException e) {
+ throw e;
+ } catch (Exception e) {
+ LOGGER.error(MessageFormat.format("Exception invoking listener:
{0}", e.getMessage()), e);
}
-
- throw e;
- } catch (Exception e) {
- LOGGER.error(e.getMessage(), e);
}
}
}
-
- public void notifyUnsubscription(Session session, TopicKey topicKey) {
- for (TopicListener listener : listeners) {
- try {
- listener.onSessionUnsubscribed(session, topicKey);
- } catch (Exception e) {
- LOGGER.error(e.getMessage(), e);
- }
- }
- }
-
+
public abstract void publish(Object messageData) throws MessageException;
}
Modified:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushResource.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushResource.java 2010-10-21
17:44:09 UTC (rev 19646)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/PushResource.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -34,6 +34,7 @@
import org.richfaces.application.push.PushContext;
import org.richfaces.application.push.PushContextFactory;
import org.richfaces.application.push.Session;
+import org.richfaces.application.push.SubscriptionAbortedException;
import org.richfaces.application.push.TopicKey;
import org.richfaces.resource.DynamicResource;
import org.richfaces.resource.UserResource;
@@ -55,6 +56,7 @@
return null;
}
+ //TODO implement "session forget" param
public InputStream getInputStream() throws IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
PushContextFactory pushContextFactory =
ServiceTracker.getService(PushContextFactory.class);
@@ -73,6 +75,9 @@
try {
session.subscribe(topicKey);
+ } catch (SubscriptionAbortedException e) {
+ System.out.println("Aborted: '" + e.getMessage() +
"'");
+ // TODO: handle exception
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Modified:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/jms/SessionImpl.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/jms/SessionImpl.java 2010-10-21
17:44:09 UTC (rev 19646)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/jms/SessionImpl.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -33,9 +33,14 @@
import javax.jms.Topic;
import javax.jms.TopicSubscriber;
-import org.richfaces.application.push.AbortSubscriptionException;
+import org.richfaces.application.push.EventAbortedException;
+import org.richfaces.application.push.MessageDataSerializer;
import org.richfaces.application.push.Request;
import org.richfaces.application.push.RequestLifecycleListener;
+import org.richfaces.application.push.SessionPreSubscriptionEvent;
+import org.richfaces.application.push.SessionSubscriptionEvent;
+import org.richfaces.application.push.SessionUnsubscriptionEvent;
+import org.richfaces.application.push.SubscriptionAbortedException;
import org.richfaces.application.push.TopicKey;
import org.richfaces.application.push.TopicsContext;
import org.richfaces.application.push.impl.AbstractSession;
@@ -77,15 +82,22 @@
org.richfaces.application.push.Topic topic =
topicsContext.getTopic(topicKey);
try {
- topic.notifySubscription(this, topicKey);
+ SessionPreSubscriptionEvent preSubscriptionEvent = new
SessionPreSubscriptionEvent(topic, this);
+ topic.publishEvent(preSubscriptionEvent);
+ } catch (EventAbortedException e) {
+ throw new SubscriptionAbortedException(e.getMessage());
+ }
+
+ session.createDurableSubscriber(messagingContext.lookup(topicKey),
getSubscriptionClientId(topicKey));
+ subscribedTopics.add(topicKey);
+
+ try {
+ SessionSubscriptionEvent subscriptionEvent = new
SessionSubscriptionEvent(topic, this);
+ topic.publishEvent(subscriptionEvent);
- //TODO send event to check subscription permissions
- session.createDurableSubscriber(messagingContext.lookup(topicKey),
getSubscriptionClientId(topicKey));
- subscribedTopics.add(topicKey);
- } catch (AbortSubscriptionException e) {
+ } catch (EventAbortedException e) {
// TODO: handle exception
}
-
} finally {
if (session != null) {
try {
@@ -101,7 +113,12 @@
public void destroy() {
for (TopicKey topicKey : subscribedTopics) {
org.richfaces.application.push.Topic topic =
topicsContext.getTopic(topicKey);
- topic.notifyUnsubscription(this, topicKey);
+
+ try {
+ topic.publishEvent(new SessionUnsubscriptionEvent(topic, this));
+ } catch (EventAbortedException e) {
+ // TODO: handle exception
+ }
}
// TODO Auto-generated method stub
@@ -181,13 +198,47 @@
e.printStackTrace();
}
}
+
+ private String serializeMessage(org.richfaces.application.push.Topic topic, Message
message) {
+ String serializedMessageData = null;
+ Object messageData = null;
+
+ try {
+ if (message instanceof ObjectMessage) {
+ messageData = ((ObjectMessage) message).getObject();
+ } else if (message instanceof TextMessage) {
+ TextMessage textMessage = (TextMessage) message;
+
+ if (message.getBooleanProperty(TopicImpl.SERIALIZED_DATA_INDICATOR)) {
+ serializedMessageData = textMessage.getText();
+ } else {
+ messageData = textMessage.getText();
+ }
+ }
+ } catch (JMSException e) {
+ LOGGER.error(e.getMessage(), e);
+ }
+
+ if (serializedMessageData != null) {
+ return serializedMessageData;
+ }
+
+ if (messageData != null) {
+ MessageDataSerializer messageDataSerializer =
topic.getMessageDataSerializer();
+ try {
+ return messageDataSerializer.serialize(messageData);
+ } catch (Exception e) {
+ LOGGER.error(e.getMessage(), e);
+ }
+ }
+
+ return null;
+ }
/* (non-Javadoc)
* @see javax.jms.MessageListener#onMessage(javax.jms.Message)
*/
public void onMessage(Message message) {
- String messageData = null;
-
try {
String topicName = ((Topic) message.getJMSDestination()).getTopicName();
@@ -197,23 +248,13 @@
return;
}
- if (message.getBooleanProperty(TopicImpl.SERIALIZED_DATA_INDICATOR)) {
- messageData = ((TextMessage) message).getText();
- } else if (message instanceof ObjectMessage) {
- ObjectMessage objectMessage = (ObjectMessage) message;
- messageData =
topic.getMessageDataSerializer().serialize(objectMessage.getObject());
- } else if (message instanceof TextMessage) {
- TextMessage textMessage = (TextMessage) message;
- messageData =
topic.getMessageDataSerializer().serialize(textMessage.getText());
- }
-
-
- if (messageData == null) {
+ String serializedMessageData = serializeMessage(topic, message);
+ if (serializedMessageData == null) {
//TODO log
return;
}
- getRequest().postMessage(new TopicKey(topicName), messageData);
+ getRequest().postMessage(new TopicKey(topicName), serializedMessageData);
message.acknowledge();
} catch (JMSException e) {
Modified:
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/jms/TopicImpl.java
===================================================================
---
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/jms/TopicImpl.java 2010-10-21
17:44:09 UTC (rev 19646)
+++
branches/RF-7817/push-redesign/src/main/java/org/richfaces/application/push/impl/jms/TopicImpl.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -79,4 +79,5 @@
}
}
}
+
}
Modified: branches/RF-7817/push-redesign-app/src/main/java/demo/ChatBean.java
===================================================================
--- branches/RF-7817/push-redesign-app/src/main/java/demo/ChatBean.java 2010-10-21
17:44:09 UTC (rev 19646)
+++ branches/RF-7817/push-redesign-app/src/main/java/demo/ChatBean.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -27,7 +27,7 @@
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
-import javax.faces.bean.ViewScoped;
+import javax.faces.bean.SessionScoped;
import org.richfaces.application.push.MessageException;
import org.richfaces.application.push.TopicKey;
@@ -40,7 +40,7 @@
*
*/
@ManagedBean
-@ViewScoped
+@SessionScoped
public class ChatBean implements Serializable {
private static final long serialVersionUID = -6377543444437645751L;
Modified: branches/RF-7817/push-redesign-app/src/main/java/demo/TopicsInitializer.java
===================================================================
---
branches/RF-7817/push-redesign-app/src/main/java/demo/TopicsInitializer.java 2010-10-21
17:44:09 UTC (rev 19646)
+++
branches/RF-7817/push-redesign-app/src/main/java/demo/TopicsInitializer.java 2010-10-21
21:25:54 UTC (rev 19647)
@@ -23,16 +23,21 @@
import java.text.MessageFormat;
+import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;
import javax.servlet.http.HttpServletRequest;
+import org.richfaces.application.push.EventAbortedException;
import org.richfaces.application.push.Session;
+import org.richfaces.application.push.SessionPreSubscriptionEvent;
+import org.richfaces.application.push.SessionSubscriptionEvent;
+import org.richfaces.application.push.SessionTopicListener;
+import org.richfaces.application.push.SessionUnsubscriptionEvent;
import org.richfaces.application.push.Topic;
import org.richfaces.application.push.TopicKey;
-import org.richfaces.application.push.TopicListener;
import org.richfaces.application.push.TopicsContext;
import org.richfaces.application.push.impl.DefaultMessageDataSerializer;
@@ -50,21 +55,35 @@
topic.setMessageDataSerializer(DefaultMessageDataSerializer.instance());
- topic.addTopicListener(new TopicListener() {
+ topic.addTopicListener(new SessionTopicListener() {
@Override
- public void onSessionUnsubscribed(Session session, TopicKey topicKey) {
+ public void processUnsubscriptionEvent(SessionUnsubscriptionEvent event)
throws EventAbortedException {
+ TopicKey topicKey = event.getTopic().getKey();
+ Session session = event.getSession();
System.out.println(MessageFormat.format("Session {0} disconnected
from {1}", session.getId(), topicKey.getTopicName()));
}
@Override
- public void onSessionSubscribed(Session session, TopicKey topicKey) {
+ public void processSubscriptionEvent(SessionSubscriptionEvent event) throws
EventAbortedException {
+ TopicKey topicKey = event.getTopic().getKey();
+ Session session = event.getSession();
+
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletRequest hsr = (HttpServletRequest)
facesContext.getExternalContext().getRequest();
System.out.println(MessageFormat.format("Session {0} connected to
{1} from {2}", session.getId(),
topicKey.getTopicName(), hsr.getRemoteAddr()));
}
+
+ @Override
+ public void processPreSubscriptionEvent(SessionPreSubscriptionEvent event)
throws EventAbortedException {
+ ExternalContext externalContext =
FacesContext.getCurrentInstance().getExternalContext();
+ ChatBean chatBean = (ChatBean)
externalContext.getSessionMap().get("chatBean");
+ if (chatBean == null ||
"badname".equals(chatBean.getUserName())) {
+ throw new EventAbortedException("User name has not passed
validation");
+ }
+ }
});
}
Modified: branches/RF-7817/push-redesign-app/src/main/webapp/index.xhtml
===================================================================
--- branches/RF-7817/push-redesign-app/src/main/webapp/index.xhtml 2010-10-21 17:44:09 UTC
(rev 19646)
+++ branches/RF-7817/push-redesign-app/src/main/webapp/index.xhtml 2010-10-21 21:25:54 UTC
(rev 19647)
@@ -14,6 +14,9 @@
Enter username: <h:inputText value="#{username}"
required="true" />
<h:commandLink action="chat" value="Enter chat" />
</h:form>
+ <h:link value="Test with bad user name" outcome="chat">
+ <f:param name="username" value="badname" />
+ </h:link>
</h:body>
</f:view>
</html>
\ No newline at end of file