KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > cache > lib > BackgroundCleaner


1 /**
2  * Copyright (C) 2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.perseus.cache.lib;
19
20 import org.objectweb.perseus.cache.replacement.api.ReplacementManager;
21 import org.objectweb.perseus.cache.api.CacheException;
22 import org.objectweb.util.monolog.api.Logger;
23 import org.objectweb.util.monolog.api.BasicLevel;
24
25
26 /**
27  * This component is a thread managing the auto cleaning of a cache. The clean
28  * up is launched when a threshold of the cache size is reached.
29  *
30  * @author S.Chassande-Barrioz
31  */

32 public class BackgroundCleaner implements Runnable JavaDoc {
33
34     private Object JavaDoc wakeup;
35     private boolean runnning;
36     private ReplacementManager rm;
37     private Thread JavaDoc thread;
38     private int size;
39     private Logger logger;
40
41     public BackgroundCleaner(ReplacementManager rm, Logger logger) {
42         this.rm = rm;
43         wakeup = new Object JavaDoc();
44         runnning = true;
45         this.logger = logger;
46         thread = new Thread JavaDoc(this);
47         thread.setDaemon(true);
48         thread.start();
49     }
50
51     public Thread JavaDoc getThread() {
52         return thread;
53     }
54
55     public void restart() {
56         if (!runnning) {
57             synchronized(wakeup) {
58                 if (!runnning) {
59                     if (logger != null && logger.isLoggable(BasicLevel.INFO)) {
60                         logger.log(BasicLevel.INFO, "BackgroundCleaner restart");
61                     }
62                     runnning = true;
63                     wakeup.notify();
64                 }
65             }
66         }
67     }
68
69     public void stop() {
70         if (runnning) {
71             synchronized(wakeup) {
72                 if (runnning) {
73                     if (logger != null && logger.isLoggable(BasicLevel.INFO)) {
74                         logger.log(BasicLevel.INFO, "BackgroundCleaner stop");
75                     }
76                     runnning = false;
77                     wakeup.notify();
78                 }
79             }
80         }
81     }
82
83     public void backgroungCleanup(int size) {
84         synchronized(wakeup) {
85             this.size = size;
86             wakeup.notify();
87         }
88     }
89
90     public void run() {
91         while(runnning) {
92             synchronized(wakeup) {
93                 try {
94                     if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) {
95                         logger.log(BasicLevel.DEBUG, "Wait next wake up");
96                     }
97                     wakeup.wait();
98                 } catch (InterruptedException JavaDoc e) {
99                 }
100             }
101             if (runnning) {
102                 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) {
103                     logger.log(BasicLevel.DEBUG,
104                             "Cleanup the cache in background (" + size +")");
105                 }
106                 try {
107                     rm.forceFree(size);
108                 } catch (CacheException e) {
109                     if (logger != null) {
110                         logger.log(BasicLevel.ERROR,
111                                 "Impossible to cleanup the cache ("
112                                 + size + "): ", e);
113                     }
114                 }
115             }
116         }
117         if (logger != null && logger.isLoggable(BasicLevel.INFO)) {
118             logger.log(BasicLevel.INFO, "BackgroundCleaner end");
119         }
120     }
121 }
122
Popular Tags