KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > containers > util > cache > NRUSessionCache


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.ejb.containers.util.cache;
25
26 import java.util.Properties JavaDoc;
27
28 import com.sun.ejb.spi.container.SFSBContainerCallback;
29
30 public class NRUSessionCache
31     extends LruSessionCache
32 {
33
34     protected boolean doOrdering = false;
35     protected int orderingThreshold = 0;
36
37     public NRUSessionCache(String JavaDoc cacheName,
38         SFSBContainerCallback container, int cacheIdleTime, int removalTime)
39     {
40         super("NRU-" + cacheName, container, cacheIdleTime, removalTime);
41     }
42
43     public void init(int maxEntries, float loadFactor, Properties JavaDoc props) {
44         super.init(maxEntries, loadFactor, props);
45         orderingThreshold = (int) (0.75 * threshold);
46     }
47     
48     protected CacheItem itemAdded(CacheItem item) {
49         CacheItem addedItem = super.itemAdded(item);
50         doOrdering = (entryCount >= orderingThreshold);
51         return addedItem;
52     }
53     
54     protected void itemAccessed(CacheItem item) {
55         LruCacheItem lc = (LruCacheItem) item;
56         synchronized (this) {
57             if (lc.isTrimmed) {
58                 lc.isTrimmed = false;
59                 CacheItem overflow = super.itemAdded(item);
60                 if (overflow != null) {
61                     trimItem(overflow);
62                 }
63             } else if (doOrdering) {
64                 super.itemAccessed(item);
65             }
66         }
67     }
68
69     protected void itemRefreshed(CacheItem item, int oldSize) {
70     }
71     
72     protected void itemRemoved(CacheItem item) {
73         super.itemRemoved(item);
74         doOrdering = (entryCount >= orderingThreshold);
75     }
76
77     public void trimTimedoutItems(int maxCount) {
78         // If we are maintaining an ordered list use
79
// the superclass method for trimming
80
if (doOrdering) {
81             super.trimTimedoutItems(maxCount);
82         } else {
83             // we don't have an ordered list,
84
// so go through the whole cache and pick victims
85
trimUnSortedTimedoutItems(maxCount);
86         }
87     }
88
89 }
90
Popular Tags