View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.portals.messaging;
17  
18  import java.io.NotSerializableException;
19  import java.io.Serializable;
20  
21  import javax.portlet.PortletRequest;
22  import javax.portlet.PortletSession;
23  
24  
25  /***
26   * PortletMessageComponent
27   * Throwaway Naive implementation of Porlet Messages as an abstraction and a place holder for when the next 
28   * spec covers inter-portlet communication
29   * 
30   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
31   * @version $Id: PortletMessaging.java 188512 2005-04-22 07:48:18 +0200 (Fri, 22 Apr 2005) taylor $
32   */
33  public class PortletMessaging
34  {
35      public static final void publish(PortletRequest request, String portletTopic, String messageName, Object message)
36      throws NotSerializableException
37      {
38          String key = portletTopic + ":" + messageName;
39          if (message instanceof Serializable)
40          {
41              request.getPortletSession().setAttribute(key, message, PortletSession.APPLICATION_SCOPE);
42          }
43          else
44          {
45              throw new NotSerializableException("Message not serializable for " + key);
46          }
47      }
48  
49      public static final Object consume(PortletRequest request, String portletTopic, String messageName)
50      {
51          String key = portletTopic + ":" + messageName;
52          Object object = request.getPortletSession().getAttribute(key, PortletSession.APPLICATION_SCOPE);
53          // consume it
54          request.getPortletSession().removeAttribute(key, PortletSession.APPLICATION_SCOPE);        
55          return object;
56      }
57  
58      public static final Object receive(PortletRequest request, String portletTopic, String messageName)
59      {
60          String key = portletTopic + ":" + messageName;
61          Object object = request.getPortletSession().getAttribute(key, PortletSession.APPLICATION_SCOPE);
62          return object;
63      }
64      
65      public static final void cancel(PortletRequest request, String portletTopic, String messageName)
66      {
67          String key = portletTopic + ":" + messageName;
68          request.getPortletSession().removeAttribute(key, PortletSession.APPLICATION_SCOPE);
69      }
70  
71      public static final void publish(PortletRequest request, String messageName, Object message)
72      throws NotSerializableException
73      {
74          String key = messageName;
75          if (message instanceof Serializable)
76          {
77              request.getPortletSession().setAttribute(key, message, PortletSession.PORTLET_SCOPE);
78          }
79          else
80          {
81              throw new NotSerializableException("Message not serializable for " + key);
82          }
83      }
84  
85      public static final Object consume(PortletRequest request, String messageName)
86      {
87          String key = messageName;
88          Object object = request.getPortletSession().getAttribute(key, PortletSession.PORTLET_SCOPE);
89          // consume it
90          request.getPortletSession().removeAttribute(key, PortletSession.PORTLET_SCOPE);        
91          return object;
92      }
93  
94      public static final Object receive(PortletRequest request, String messageName)
95      {
96          String key = messageName;
97          Object object = request.getPortletSession().getAttribute(key, PortletSession.PORTLET_SCOPE);
98          return object;
99      }
100     
101     public static final void cancel(PortletRequest request, String messageName)
102     {
103         String key = messageName;
104         request.getPortletSession().removeAttribute(key, PortletSession.PORTLET_SCOPE);
105     }
106     
107 }