View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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          // consume it
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          // consume it
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 }