001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.cache.impl;
018:
019: import java.util.HashMap;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import net.sf.ehcache.Ehcache;
026:
027: import org.apache.jetspeed.cache.CacheElement;
028: import org.apache.jetspeed.cache.PortletWindowCache;
029: import org.apache.pluto.om.entity.PortletEntity;
030: import org.apache.pluto.om.window.PortletWindow;
031:
032: /**
033: * <p>
034: * EhPortletWindowCache
035: * </p>
036: * <p>
037: * Implementation of {@link PortletWindowCache} that is backed
038: * <a href="http://ehcache.sourceforge.net/">Ehcache</a>.
039: * </p>
040: * @author <a href="mailto:scott.t.weaver@gmail.com">Scott T. Weaver</a>
041: *
042: */
043: public class EhPortletWindowCache extends EhCacheImpl implements
044: PortletWindowCache {
045:
046: /** Allows us to track {@link PortletWindow}s in cache by {@link PortletEntity#getId()}*/
047: private Map portletEntityIdToEntityid;
048:
049: public EhPortletWindowCache(Ehcache ehcache) {
050: super (ehcache);
051: portletEntityIdToEntityid = new HashMap();
052: }
053:
054: /* (non-Javadoc)
055: * @see org.apache.jetspeed.cache.impl.PortletWindowCache#getPortletWindow(java.lang.String)
056: */
057: public PortletWindow getPortletWindow(String windowId) {
058: assert windowId != null;
059: CacheElement cacheElement = get(windowId);
060: if (cacheElement != null) {
061: return (PortletWindow) cacheElement.getContent();
062: } else {
063: return null;
064: }
065: }
066:
067: /* (non-Javadoc)
068: * @see org.apache.jetspeed.cache.impl.PortletWindowCache#getPortletWindowByEntityId(java.lang.String)
069: */
070: public PortletWindow getPortletWindowByEntityId(
071: String portletEntityId) {
072: assert portletEntityId != null;
073: if (portletEntityIdToEntityid.containsKey(portletEntityId)) {
074: return (PortletWindow) getPortletWindow((String) portletEntityIdToEntityid
075: .get(portletEntityId));
076: } else {
077: return null;
078: }
079: }
080:
081: /* (non-Javadoc)
082: * @see org.apache.jetspeed.cache.impl.PortletWindowCache#putPortletWindow(org.apache.pluto.om.window.PortletWindow)
083: */
084: public void putPortletWindow(PortletWindow window) {
085: assert window != null;
086: String windowId = window.getId().toString();
087: portletEntityIdToEntityid.put(window.getPortletEntity().getId()
088: .toString(), windowId);
089: put(createElement(windowId, window));
090: }
091:
092: /* (non-Javadoc)
093: * @see org.apache.jetspeed.cache.impl.PortletWindowCache#removePortletWindow(java.lang.String)
094: */
095: public void removePortletWindow(String portletWindowId) {
096: assert portletWindowId != null;
097: PortletWindow window = getPortletWindow(portletWindowId);
098: if (window != null) {
099: portletEntityIdToEntityid.remove(window.getPortletEntity()
100: .getId().toString());
101: removeQuiet(portletWindowId);
102: }
103: }
104:
105: public void removePortletWindowByPortletEntityId(
106: String portletEntityId) {
107: assert portletEntityId != null;
108: PortletWindow portletWindow = getPortletWindowByEntityId(portletEntityId);
109: if (portletWindow != null) {
110: portletEntityIdToEntityid.remove(portletEntityId);
111: removeQuiet(portletWindow.getId().toString());
112: }
113: }
114:
115: public Set getAllPortletWindows() {
116: Iterator keys = ehcache.getKeys().iterator();
117: Set windows = new HashSet();
118: while (keys.hasNext()) {
119: String key = (String) keys.next();
120: PortletWindow window = getPortletWindow(key);
121: if (window != null) {
122: windows.add(window);
123: }
124: }
125: return windows;
126: }
127:
128: }
|