KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > deployment > work > WorkCleaner


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Florent BENOIT & Ludovic BERT
22  * --------------------------------------------------------------------------
23  * $Id: WorkCleaner.java,v 1.5 2005/04/28 16:53:00 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_lib.deployment.work;
28
29 import java.util.Enumeration JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import org.objectweb.util.monolog.api.Logger;
33 import org.objectweb.util.monolog.api.BasicLevel;
34
35 import org.objectweb.jonas.common.Log;
36
37 /**
38  * JOnAS work deployment files and directories cleaner class. This class
39  * provides a way for removing entries wich are unconsistent.
40  * @author Florent Benoit
41  * @author Ludovic Bert
42  * @author Benoit PELLETIER
43  */

44 public class WorkCleaner extends Thread JavaDoc {
45
46     /**
47      * The logger used in JOnAS
48      */

49     private static Logger logger = Log.getLogger(Log.JONAS_DEPLOY_WORK_PREFIX);
50
51     /**
52      * Sleep time for the thread of the cleaner
53      */

54     private static final int SLEEP_TIME = 300000;
55
56     /**
57      * the instance of this VM. There is only one instance of the cleaner
58      * running in the same VM.
59      */

60     private static WorkCleaner unique = null;
61
62     /**
63      * The list of tasks to execute at each period
64      */

65     private Vector JavaDoc taskList = null;
66
67     /**
68      * Default constructor : Construct a new cleaner.
69      */

70     private WorkCleaner() {
71         super("WorkCleaner");
72         // set a low priority for this thread in order to reduce
73
// the impacts on 'functionnal' threads.
74
this.setPriority(Thread.MIN_PRIORITY);
75         taskList = new Vector JavaDoc();
76     }
77
78     /**
79      * @return the logger
80      */

81     protected static Logger getLogger() {
82         return logger;
83     }
84
85     /**
86      * Get an instance of the Work Cleaner.
87      * @return the instance of the Work Cleaner.
88      */

89     public static synchronized WorkCleaner getInstance() {
90         if (unique == null) {
91             unique = new WorkCleaner();
92         }
93         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
94             getLogger().log(BasicLevel.DEBUG, "getInstance : ->" + unique);
95         }
96         return unique;
97     }
98
99     /**
100      * Register a new clean task
101      * @param absCleanTask the task to add
102      * @throws CleanerException if the task cannot be registred
103      */

104     public synchronized void registerTask(AbsCleanTask absCleanTask) throws CleanerException {
105         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
106             getLogger().log(BasicLevel.DEBUG, "registerTask : called");
107         }
108         // checks
109
if (taskList == null) {
110             throw new CleanerException("Can not add an entry, the vector is null");
111         }
112         if (absCleanTask == null) {
113             throw new CleanerException("Can not add a null entry");
114         }
115         // Add if don't already exist
116
if (!taskList.contains(absCleanTask)) {
117             taskList.add(absCleanTask);
118         }
119     }
120
121     /**
122      * Execute the registered tasks
123      */

124     public synchronized void executeTasks() {
125         try {
126             AbsCleanTask absCleanTask = null;
127
128             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
129                 getLogger().log(BasicLevel.DEBUG, "executeTasks : wake up");
130             }
131             for (Enumeration JavaDoc e = taskList.elements(); e.hasMoreElements();) {
132
133                 absCleanTask = (AbsCleanTask) e.nextElement();
134                 absCleanTask.execute();
135             }
136
137         } catch (CleanerException e) {
138             e.printStackTrace();
139         }
140     }
141
142     /**
143      * Start the thread of this class It will clean all the work entries
144      */

145     public void run() {
146
147         for (;;) {
148             executeTasks();
149             try {
150                 Thread.sleep(SLEEP_TIME);
151             } catch (InterruptedException JavaDoc e) {
152                 throw new RuntimeException JavaDoc("Thread fail to sleep");
153             }
154         }
155     }
156
157 }
158
Popular Tags