1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to You under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * 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, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package org.apache.jetspeed;
18  
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  import java.util.Map.Entry;
25  
26  import org.apache.jetspeed.cache.PortletWindowCache;
27  import org.apache.pluto.om.window.PortletWindow;
28  
29  /***
30   * This implementation of {@link PortletWindowCache} is to be used ONLY for testing purposes.
31   * 
32   * @author <a href="mailto:scott.t.weaver@gmail.com">Scott T. Weaver</a>
33   *
34   */
35  public class HashMapWindowCache implements PortletWindowCache
36  {
37  
38  
39      private Map portletEntityToWindow = new HashMap();
40      private Map windows = new HashMap();
41      
42      public Set getAllPortletWindows()
43      {
44          Set windowSet = new HashSet();
45          
46          Iterator itr = windows.entrySet().iterator();
47          while(itr.hasNext())
48          {
49              Map.Entry entry = (Entry) itr.next();                                
50              windowSet.add((PortletWindow)entry.getValue());
51          }
52          
53          return windowSet;
54      }
55  
56      public PortletWindow getPortletWindow(String windowId)
57      {
58          return (PortletWindow) windows.get(windowId);
59      }
60  
61      public PortletWindow getPortletWindowByEntityId(String portletEntityId)
62      {
63          if(portletEntityToWindow.containsKey(portletEntityId))
64          {
65              return (PortletWindow) windows.get((String) portletEntityToWindow.get(portletEntityId));
66          }
67          else
68          {
69              return null;
70          }
71      }
72  
73      public void putPortletWindow(PortletWindow window)
74      {
75          windows.put(window.getId().toString(), window);
76          portletEntityToWindow.put(window.getPortletEntity().getId().toString(), window.getId().toString());
77  
78      }
79  
80      public void removePortletWindow(String windowId)
81      {
82          PortletWindow window = (PortletWindow) windows.get(windowId);
83          if(window != null)
84          {
85              windows.remove(windowId);
86              portletEntityToWindow.remove(window.getPortletEntity().getId().toString());
87          }
88  
89      }
90  
91      public void removePortletWindowByPortletEntityId(String portletEntityId)
92      {
93          PortletWindow window = getPortletWindowByEntityId(portletEntityId);
94          if(window != null)
95          {   
96              removePortletWindow(window.getId().toString());
97          }
98  
99      }
100 }