1   /*
2    * Copyright 2000-2001,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  
17  package org.apache.wsrp4j.consumer.driver;
18  
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import oasis.names.tc.wsrp.v1.types.SessionContext;
24  
25  import org.apache.wsrp4j.consumer.PortletSession;
26  import org.apache.wsrp4j.consumer.PortletWindowSession;
27  import org.apache.wsrp4j.log.LogManager;
28  import org.apache.wsrp4j.log.Logger;
29  
30  public abstract class GenericPortletSessionImpl implements PortletSession {
31      // the session context passed from the producer to store
32      private SessionContext sessionCtx = null;
33  
34      // the portlet handle identifying the where the session belogns to
35      private String handle = null;
36  
37      // holds the varios window sessions for this portlet instance
38      protected final Map windowSessions;
39  
40      // logger
41      private static final Logger logger = LogManager.getLogManager().getLogger(
42              GenericPortletSessionImpl.class);
43  
44      public GenericPortletSessionImpl(String handle) {
45          final String MN = "constructor";
46          if (logger.isLogging(Logger.TRACE_HIGH)) {
47              logger.entry(Logger.TRACE_HIGH, MN);
48          }
49  
50          this.windowSessions = new HashMap();
51          this.handle = handle;
52  
53          if (logger.isLogging(Logger.TRACE_HIGH)) {
54              logger.exit(Logger.TRACE_HIGH, MN);
55          }
56      }
57  
58      public String getPortletHandle() {
59          return handle;
60      }
61  
62      public void setPortletHandle(String handle) {
63          if (handle != null) {
64              this.handle = handle;
65          }
66      }
67  
68      public SessionContext getSessionContext() {
69          return sessionCtx;
70      }
71  
72      public void setSessionContext(SessionContext ctx) {
73          this.sessionCtx = ctx;
74      }
75  
76      /**
77       * Get the <code>PortletWindowSession</code> of the portlet window with the given ID.
78       * 
79       * @param windowID The ID of the portlet window
80       * @return The <code>PorletWindowSession</code> with the given ID.    
81       **/
82      public abstract PortletWindowSession getPortletWindowSession(String windowID);
83  
84      /**
85       * Get all window session which belong to the portlet session
86       * 
87       * @return An Iterator of <code>SimplePortletWindowSession</code> objects.     
88       **/
89      public Iterator getAllPorletWindowSessions() {
90  
91          return this.windowSessions.entrySet().iterator();
92      }
93  
94      /**
95       * Remove the porlet window session with the given window id.
96       * 
97       * @param windowID The ID of the portlet window whichs session shoul dbe removed
98       * @return The portlet window session which has been removed or null if the session did not exist.    
99       **/
100     public PortletWindowSession removePortletWindowSession(String windowID) {
101         final String MN = "getPortletWindowSession";
102 
103         PortletWindowSession winSession = (PortletWindowSession) this.windowSessions
104                 .remove(windowID);
105 
106         if (logger.isLogging(Logger.TRACE_HIGH) && winSession != null) {
107             logger.text(Logger.TRACE_HIGH, MN,
108                     "removed PortletWindowSession with ID: " + windowID);
109         }
110 
111         return winSession;
112     }
113 
114     /**
115      * Remove all portlet window sessions which  belong to this portlet session.
116      **/
117     public void removeAllPortletWindowSessions() {
118         this.windowSessions.clear();
119     }
120 }