1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.cache.impl;
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
25 import net.sf.ehcache.Ehcache;
26
27 import org.apache.jetspeed.cache.CacheElement;
28 import org.apache.jetspeed.cache.PortletWindowCache;
29 import org.apache.pluto.om.entity.PortletEntity;
30 import org.apache.pluto.om.window.PortletWindow;
31
32 /***
33 * <p>
34 * EhPortletWindowCache
35 * </p>
36 * <p>
37 * Implementation of {@link PortletWindowCache} that is backed
38 * <a href="http://ehcache.sourceforge.net/">Ehcache</a>.
39 * </p>
40 * @author <a href="mailto:scott.t.weaver@gmail.com">Scott T. Weaver</a>
41 *
42 */
43 public class EhPortletWindowCache extends EhCacheImpl implements PortletWindowCache {
44
45 /*** Allows us to track {@link PortletWindow}s in cache by {@link PortletEntity#getId()}*/
46 private Map portletEntityIdToEntityid;
47
48
49 public EhPortletWindowCache(Ehcache ehcache)
50 {
51 super(ehcache);
52 portletEntityIdToEntityid = new HashMap();
53 }
54
55
56
57
58 public PortletWindow getPortletWindow(String windowId)
59 {
60 assert windowId != null;
61 CacheElement cacheElement = get(windowId);
62 if(cacheElement != null)
63 {
64 return (PortletWindow) cacheElement.getContent();
65 }
66 else
67 {
68 return null;
69 }
70 }
71
72
73
74
75 public PortletWindow getPortletWindowByEntityId(String portletEntityId)
76 {
77 assert portletEntityId != null;
78 if(portletEntityIdToEntityid.containsKey(portletEntityId))
79 {
80 return (PortletWindow) getPortletWindow((String) portletEntityIdToEntityid.get(portletEntityId));
81 }
82 else
83 {
84 return null;
85 }
86 }
87
88
89
90
91 public void putPortletWindow(PortletWindow window)
92 {
93 assert window != null;
94 String windowId = window.getId().toString();
95 portletEntityIdToEntityid.put(window.getPortletEntity().getId().toString(), windowId);
96 put(createElement(windowId, window));
97 }
98
99
100
101
102 public void removePortletWindow(String portletWindowId)
103 {
104 assert portletWindowId != null;
105 PortletWindow window = getPortletWindow(portletWindowId);
106 if(window != null)
107 {
108 portletEntityIdToEntityid.remove(window.getPortletEntity().getId().toString());
109 removeQuiet(portletWindowId);
110 }
111 }
112
113 public void removePortletWindowByPortletEntityId(String portletEntityId)
114 {
115 assert portletEntityId != null;
116 PortletWindow portletWindow = getPortletWindowByEntityId(portletEntityId);
117 if(portletWindow != null)
118 {
119 portletEntityIdToEntityid.remove(portletEntityId);
120 removeQuiet(portletWindow.getId().toString());
121 }
122 }
123
124 public Set getAllPortletWindows()
125 {
126 Iterator keys = ehcache.getKeys().iterator();
127 Set windows = new HashSet();
128 while(keys.hasNext())
129 {
130 String key = (String) keys.next();
131 PortletWindow window = getPortletWindow(key);
132 if(window != null)
133 {
134 windows.add(window);
135 }
136 }
137 return windows;
138 }
139
140 }