1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.portals.messaging;
18
19 import java.io.NotSerializableException;
20 import java.io.Serializable;
21
22 import javax.portlet.PortletRequest;
23 import javax.portlet.PortletSession;
24
25
26 /***
27 * PortletMessageComponent
28 * Throwaway Naive implementation of Porlet Messages as an abstraction and a place holder for when the next
29 * spec covers inter-portlet communication
30 *
31 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
32 * @version $Id: PortletMessaging.java 517068 2007-03-12 01:44:37Z ate $
33 */
34 public class PortletMessaging
35 {
36 public static final void publish(PortletRequest request, String portletTopic, String messageName, Object message)
37 throws NotSerializableException
38 {
39 String key = portletTopic + ":" + messageName;
40 if (message instanceof Serializable)
41 {
42 request.getPortletSession().setAttribute(key, message, PortletSession.APPLICATION_SCOPE);
43 }
44 else
45 {
46 throw new NotSerializableException("Message not serializable for " + key);
47 }
48 }
49
50 public static final Object consume(PortletRequest request, String portletTopic, String messageName)
51 {
52 String key = portletTopic + ":" + messageName;
53 Object object = request.getPortletSession().getAttribute(key, PortletSession.APPLICATION_SCOPE);
54
55 request.getPortletSession().removeAttribute(key, PortletSession.APPLICATION_SCOPE);
56 return object;
57 }
58
59 public static final Object receive(PortletRequest request, String portletTopic, String messageName)
60 {
61 String key = portletTopic + ":" + messageName;
62 Object object = request.getPortletSession().getAttribute(key, PortletSession.APPLICATION_SCOPE);
63 return object;
64 }
65
66 public static final void cancel(PortletRequest request, String portletTopic, String messageName)
67 {
68 String key = portletTopic + ":" + messageName;
69 request.getPortletSession().removeAttribute(key, PortletSession.APPLICATION_SCOPE);
70 }
71
72 public static final void publish(PortletRequest request, String messageName, Object message)
73 throws NotSerializableException
74 {
75 String key = messageName;
76 if (message instanceof Serializable)
77 {
78 request.getPortletSession().setAttribute(key, message, PortletSession.PORTLET_SCOPE);
79 }
80 else
81 {
82 throw new NotSerializableException("Message not serializable for " + key);
83 }
84 }
85
86 public static final Object consume(PortletRequest request, String messageName)
87 {
88 String key = messageName;
89 Object object = request.getPortletSession().getAttribute(key, PortletSession.PORTLET_SCOPE);
90
91 request.getPortletSession().removeAttribute(key, PortletSession.PORTLET_SCOPE);
92 return object;
93 }
94
95 public static final Object receive(PortletRequest request, String messageName)
96 {
97 String key = messageName;
98 Object object = request.getPortletSession().getAttribute(key, PortletSession.PORTLET_SCOPE);
99 return object;
100 }
101
102 public static final void cancel(PortletRequest request, String messageName)
103 {
104 String key = messageName;
105 request.getPortletSession().removeAttribute(key, PortletSession.PORTLET_SCOPE);
106 }
107
108 }