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.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  	/* (non-Javadoc)
56  	 * @see org.apache.jetspeed.cache.impl.PortletWindowCache#getPortletWindow(java.lang.String)
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  	/* (non-Javadoc)
73  	 * @see org.apache.jetspeed.cache.impl.PortletWindowCache#getPortletWindowByEntityId(java.lang.String)
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  	/* (non-Javadoc)
89  	 * @see org.apache.jetspeed.cache.impl.PortletWindowCache#putPortletWindow(org.apache.pluto.om.window.PortletWindow)
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  	/* (non-Javadoc)
100 	 * @see org.apache.jetspeed.cache.impl.PortletWindowCache#removePortletWindow(java.lang.String)
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 }