KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > portlet > util > PortletMessaging


1 /*
2  * Copyright (c) 2005 Opensymphony. All Rights Reserved.
3  */

4 package com.opensymphony.webwork.portlet.util;
5
6 import javax.portlet.PortletRequest;
7 import javax.portlet.PortletSession;
8 import java.io.NotSerializableException JavaDoc;
9 import java.io.Serializable JavaDoc;
10
11
12 /**
13  * @author <a HREF="mailto:hu_pengfei@yahoo.com.cn">Henry Hu </a>
14  * @since 2005-7-18
15  */

16 public class PortletMessaging {
17
18     public static final void publish(PortletRequest request, String JavaDoc messageName, Object JavaDoc message)
19             throws NotSerializableException JavaDoc {
20         String JavaDoc key = messageName;
21         if (message instanceof Serializable JavaDoc) {
22             request.getPortletSession().setAttribute(key, message, PortletSession.PORTLET_SCOPE);
23         } else {
24             throw new NotSerializableException JavaDoc("Message not serializable for " + key);
25         }
26     }
27
28     public static final Object JavaDoc consume(PortletRequest request, String JavaDoc messageName) {
29         String JavaDoc key = messageName;
30         Object JavaDoc object = request.getPortletSession().getAttribute(key, PortletSession.PORTLET_SCOPE);
31         // consume it
32
request.getPortletSession().removeAttribute(key, PortletSession.PORTLET_SCOPE);
33         return object;
34     }
35
36     public static final Object JavaDoc receive(PortletRequest request, String JavaDoc messageName) {
37         String JavaDoc key = messageName;
38         Object JavaDoc object = request.getPortletSession().getAttribute(key, PortletSession.PORTLET_SCOPE);
39         return object;
40     }
41
42     public static final void cancel(PortletRequest request, String JavaDoc messageName) {
43         String JavaDoc key = messageName;
44         request.getPortletSession().removeAttribute(key, PortletSession.PORTLET_SCOPE);
45     }
46
47 }
48
Popular Tags