Coverage report

  %line %branch
org.apache.jetspeed.cache.impl.EhDecorationContentCacheImpl
0% 
0% 

 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  
 
 18  
 package org.apache.jetspeed.cache.impl;
 19  
 
 20  
 import java.util.Collections;
 21  
 import java.util.HashMap;
 22  
 import java.util.Iterator;
 23  
 import java.util.Map;
 24  
 import java.util.StringTokenizer;
 25  
 
 26  
 import net.sf.ehcache.Cache;
 27  
 import net.sf.ehcache.Ehcache;
 28  
 import net.sf.ehcache.Element;
 29  
 
 30  
 import org.apache.jetspeed.cache.CacheElement;
 31  
 import org.apache.jetspeed.cache.ContentCacheElement;
 32  
 import org.apache.jetspeed.cache.ContentCacheKey;
 33  
 import org.apache.jetspeed.cache.ContentCacheKeyGenerator;
 34  
 import org.apache.jetspeed.cache.JetspeedCache;
 35  
 import org.apache.jetspeed.cache.JetspeedCacheEventListener;
 36  
 import org.apache.jetspeed.cache.JetspeedContentCache;
 37  
 import org.apache.jetspeed.decoration.Theme;
 38  
 import org.apache.jetspeed.om.page.ContentPage;
 39  
 import org.apache.jetspeed.request.RequestContext;
 40  
 
 41  
 /**
 42  
  * Wrapper around actual cache implementation
 43  
  * 
 44  
  * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
 45  
  * @version $Id: $
 46  
  */
 47  
 public class EhDecorationContentCacheImpl extends EhCacheImpl implements JetspeedContentCache, JetspeedCacheEventListener
 48  
 {
 49  
 
 50  0
 	JetspeedCache preferenceCache = null;
 51  0
     ContentCacheKeyGenerator keyGenerator = null;    
 52  
 
 53  
     public EhDecorationContentCacheImpl(Ehcache ehcache, JetspeedCache preferenceCache, ContentCacheKeyGenerator keyGenerator)
 54  
     {
 55  0
         this(ehcache);
 56  0
         this.preferenceCache = preferenceCache;
 57  0
         this.keyGenerator = keyGenerator;
 58  0
         preferenceCache.addEventListener(this,false); //only listen to remote events
 59  0
     }
 60  
     
 61  
     public EhDecorationContentCacheImpl(Ehcache ehcache, JetspeedCache preferenceCache)
 62  
     {
 63  0
         this(ehcache);
 64  0
         this.preferenceCache = preferenceCache;
 65  0
         preferenceCache.addEventListener(this,false); //only listen to remote events
 66  0
     }
 67  
         
 68  
     public EhDecorationContentCacheImpl(Ehcache ehcache)
 69  
     {
 70  0
         super(ehcache);
 71  0
     }
 72  
 
 73  
     public EhDecorationContentCacheImpl(Cache ehcache, ContentCacheKeyGenerator keyGenerator)
 74  
     {
 75  0
         this(ehcache);
 76  0
         this.keyGenerator = keyGenerator;
 77  0
     }
 78  
     
 79  
 
 80  
 	public void notifyElementAdded(JetspeedCache cache, boolean local, Object key, Object element)
 81  
 	{
 82  0
 	}
 83  
 
 84  
 	public void notifyElementChanged(JetspeedCache cache, boolean local, Object key, Object element)
 85  
 	{
 86  0
 	}
 87  
 
 88  
 	public void notifyElementEvicted(JetspeedCache cache, boolean local, Object key, Object element)
 89  
 	{
 90  0
 	}
 91  
 
 92  
 	public void notifyElementExpired(JetspeedCache cache, boolean local, Object key, Object element)
 93  
 	{
 94  0
 		notifyElementRemoved(cache,local,key,element);
 95  0
 	}
 96  
 
 97  
 
 98  
     public static final String KEY_THEME_KEY = 
 99  
         EhDecorationContentCacheElementImpl.KEY_SEPARATOR + "theme" + EhDecorationContentCacheElementImpl.KEY_SEPARATOR ;
 100  0
 	public static final int KEY_THEME_KEY_LENGTH = KEY_THEME_KEY.length();
 101  
     
 102  
 	public void notifyElementRemoved(JetspeedCache cache, boolean local,
 103  
             Object key, Object element)
 104  
     {
 105  0
         if (local) return; // not interested in local changes
 106  
 
 107  
         // System.out.println("notifying DecorationContent that element " +
 108  
         // key.toString() + " has been removed");
 109  0
         if (!(key instanceof String)) return;
 110  0
         String s = (String) key;
 111  0
         if (!(s.startsWith(KEY_THEME_KEY))) return;
 112  0
         StringTokenizer st = new StringTokenizer(s,
 113  
                 EhDecorationContentCacheElementImpl.KEY_SEPARATOR);
 114  0
         int count = 0;
 115  0
         String te = null;
 116  0
         String user = null;
 117  0
         while (st.hasMoreTokens())
 118  
         {
 119  0
             String temp = st.nextToken();
 120  0
             switch (count)
 121  
             {
 122  
             case 0:
 123  0
                 break;
 124  
             case 1:
 125  0
                 te = temp; 
 126  0
                 break;
 127  
             case 2:
 128  0
                 user = temp;
 129  
                 break;
 130  
             }
 131  0
             count++;
 132  0
             if (count > 2) break;
 133  0
         }
 134  0
         if (te != null)
 135  
         {
 136  0
             removeUserEntry(user, "theme", te);     
 137  
         }
 138  0
     }
 139  
 
 140  
     void removeUserEntry(String username, String pipeline, String windowId)
 141  
     {        
 142  0
         ContentCacheKey key = keyGenerator.createUserCacheKey(username, pipeline, windowId);
 143  0
         if (ehcache.remove(key.getKey()))
 144  
         {
 145  0
             Element userElement = ehcache.get(username);
 146  
                 
 147  0
             if (userElement != null)
 148  
             {
 149  0
                 Map map = (Map)userElement.getObjectValue();
 150  0
                 if (map != null)
 151  
                 {
 152  0
                     map.remove(windowId);
 153  
                 }
 154  
             }
 155  
         }
 156  0
     }
 157  
     
 158  
     public CacheElement get(Object key)
 159  
     {
 160  0
         ContentCacheKey cckey = (ContentCacheKey)key;
 161  0
         Element element = ehcache.get(cckey.getKey());
 162  0
         if (element == null)
 163  0
             return null;
 164  0
         return new EhDecorationContentCacheElementImpl(element, cckey);
 165  
     }
 166  
 
 167  
     public int getTimeToIdleSeconds()
 168  
     {
 169  0
         return (int)ehcache.getTimeToIdleSeconds();
 170  
     }
 171  
 
 172  
     public int getTimeToLiveSeconds()
 173  
     {
 174  0
         return (int)ehcache.getTimeToLiveSeconds();
 175  
     }
 176  
 
 177  
     public boolean isKeyInCache(Object key)
 178  
     {
 179  0
         ContentCacheKey cckey = (ContentCacheKey)key;        
 180  0
         return ehcache.isKeyInCache(cckey.getKey());
 181  
     }
 182  
 
 183  
     public void put(CacheElement element)
 184  
     {
 185  0
         ContentCacheElement ccElement = (ContentCacheElement)element;
 186  0
         EhDecorationContentCacheElementImpl impl = (EhDecorationContentCacheElementImpl)element;
 187  0
         Element ehl = impl.getImplElement();        
 188  0
         String userKey = ccElement.getContentCacheKey().getSessionId();
 189  0
         if (userKey == null)
 190  
         {
 191  0
             userKey = ccElement.getContentCacheKey().getUsername();
 192  
         }
 193  0
         String windowId = ccElement.getContentCacheKey().getWindowId();
 194  
         try
 195  
         {
 196  0
             ehcache.put(ehl);
 197  
         }
 198  0
         catch (Exception e)
 199  
         {
 200  0
             e.printStackTrace();
 201  0
         }
 202  0
         Element userElement = ehcache.get(userKey);
 203  0
         if (userElement == null)
 204  
         {
 205  0
             Map map = Collections.synchronizedMap(new HashMap());
 206  0
             map.put(windowId, ccElement.getContentCacheKey());
 207  0
             userElement = new Element(userKey, map);
 208  0
             ehcache.put(userElement);           
 209  0
         }
 210  
         else
 211  
         {
 212  0
             Map map = (Map)userElement.getObjectValue();
 213  0
             map.put(windowId, ccElement.getContentCacheKey());
 214  
         }        
 215  0
     }
 216  
     
 217  
     public CacheElement createElement(Object key, Object content)
 218  
     {
 219  0
         ContentCacheKey cckey = (ContentCacheKey)key;
 220  0
         Element cachedElement = new Element(cckey.getKey(), content);        
 221  0
         return new EhDecorationContentCacheElementImpl(cachedElement, cckey);
 222  
     }
 223  
 
 224  
     public boolean remove(Object key)
 225  
     {
 226  0
         CacheElement element = this.get(key);
 227  0
         boolean removed = false;
 228  0
         if (element == null)
 229  0
             return false;
 230  
         
 231  0
         ContentCacheElement ccElement = (ContentCacheElement)element;
 232  0
         EhDecorationContentCacheElementImpl impl = (EhDecorationContentCacheElementImpl)element;
 233  0
         Element ehl = impl.getImplElement();        
 234  0
         String userKey = ccElement.getContentCacheKey().getSessionId();
 235  0
         if (userKey == null)
 236  
         {
 237  0
             userKey = ccElement.getContentCacheKey().getUsername();
 238  
         }        
 239  0
         String windowId = ccElement.getContentCacheKey().getWindowId();        
 240  0
         removed = ehcache.remove(ccElement.getContentCacheKey().getKey());
 241  0
         Element userElement = ehcache.get(userKey);
 242  0
         if (userElement != null)
 243  
         {
 244  0
             Map map = (Map)userElement.getObjectValue();
 245  0
             if (map != null)
 246  
             {
 247  0
                 map.remove(windowId);
 248  
             }
 249  
         }
 250  0
         return removed;
 251  
     }
 252  
         
 253  
     public void evictContentForUser(String username)
 254  
     {
 255  0
         Element userElement = saveGet(username);
 256  0
         if (userElement != null)
 257  
         {
 258  0
             Map map = (Map)userElement.getObjectValue();
 259  0
             if (map != null)
 260  
             {
 261  0
                 Iterator entities = map.values().iterator();
 262  0
                 while (entities.hasNext())
 263  
                 {
 264  0
                     ContentCacheKey ccKey = (ContentCacheKey)entities.next();
 265  0
                     ehcache.remove(ccKey.getKey());
 266  0
                 }
 267  
             }
 268  0
             ehcache.remove(username);
 269  
         }
 270  0
     }
 271  
 
 272  
     public void evictContentForSession(String session)
 273  
     {
 274  0
         Element userElement = saveGet(session);
 275  0
         if (userElement != null)
 276  
         {
 277  0
             Map map = (Map)userElement.getObjectValue();
 278  0
             if (map != null)
 279  
             {
 280  0
                 Iterator entities = map.values().iterator();
 281  0
                 while (entities.hasNext())
 282  
                 {
 283  0
                     ContentCacheKey ccKey = (ContentCacheKey)entities.next();
 284  0
                     ehcache.remove(ccKey.getKey());
 285  0
                 }
 286  
             }
 287  0
             ehcache.remove(session);
 288  
         }
 289  0
     }
 290  
     
 291  
     public void clear()
 292  
     {
 293  0
         ehcache.removeAll();
 294  0
     }
 295  
         
 296  
     public ContentCacheKey createCacheKey(RequestContext context, String windowId)
 297  
     {
 298  0
         return this.keyGenerator.createCacheKey(context, windowId);        
 299  
     }
 300  
     
 301  
     protected Element saveGet(Object key)
 302  
     {
 303  
         try
 304  
         {
 305  0
             return ehcache.get(key);
 306  
         }
 307  0
         catch (IllegalStateException ise)
 308  
         {
 309  
             // can be thrown during shutdown for instance
 310  0
             return null;
 311  
         }
 312  
     }
 313  
     
 314  
     public String createSessionKey(RequestContext context)
 315  
     {
 316  0
         boolean isAjaxRequest = (context == null);
 317  0
         String mode = isAjaxRequest ? "-d-" : "-p-";
 318  0
         String user = context.getRequest().getRemoteUser();
 319  0
         if (user == null)
 320  0
             user = "guest";        
 321  0
         return user + mode + context.getPage().getId();        
 322  
     }
 323  
     
 324  
     public void invalidate(RequestContext context)
 325  
     {
 326  0
         ContentPage page = context.getPage();
 327  0
         ContentCacheKey themeContentCacheKey = createCacheKey(context, page.getId());
 328  0
         CacheElement themeCacheElem = get(themeContentCacheKey);
 329  
         
 330  0
         if (themeCacheElem != null)
 331  
         {
 332  0
             Theme theme = (Theme) themeCacheElem.getContent();
 333  0
             theme.setInvalidated(true);
 334  
         }
 335  0
     }
 336  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.