KickJava   Java API By Example, From Geeks To Geeks.

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


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): Benoit PELLETIER
22  * --------------------------------------------------------------------------
23  * $Id: AbsCleanTask.java,v 1.3 2005/04/28 16:53:00 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_lib.deployment.work;
28
29 import java.io.File JavaDoc;
30
31 import java.util.Enumeration JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import org.objectweb.util.monolog.api.Logger;
35 import org.objectweb.util.monolog.api.BasicLevel;
36
37 import org.objectweb.jonas.common.Log;
38
39 /**
40  * Abstract Clean Task class which define a generic job executed by the cleaner
41  * thread
42  * @author Benoit PELLETIER
43  */

44 public abstract class AbsCleanTask {
45
46     /**
47      * The logger used in JOnAS
48      */

49     private static Logger logger = Log.getLogger(Log.JONAS_DEPLOY_WORK_PREFIX);
50
51     /**
52      * Default constructor : Construct a new cleaner.
53      */

54     protected AbsCleanTask() {
55     }
56
57     /**
58      * @return the logger
59      */

60     protected static Logger getLogger() {
61         return logger;
62     }
63
64     /**
65      * Abstract method defined in the derived classes return true if the work
66      * copy exist and is up to date
67      * @param logEntry entry in a deploy log
68      * @return true if the work copy exists and is up to date
69      * @throws CleanerException if it fails
70      */

71     protected abstract boolean isValidLogEntry(LogEntry logEntry) throws CleanerException;
72
73     /**
74      * Abstract method defined in the derived classes remove the work copy
75      * specified in the log entry and the log entry
76      * @param logEntry entry in a deploy log
77      * @throws CleanerException if it fails
78      */

79     protected abstract void removeLogEntry(LogEntry logEntry) throws CleanerException;
80
81     /**
82      * Abstract method defined in the derived classes get the log entries
83      * @return the log entries
84      */

85     protected abstract Vector JavaDoc getLogEntries();
86
87     /**
88      * Check if the package pointed by the log entry is currently deploy
89      * @param logEntry entry in a deploy log
90      * @return true if the package pointed by the log entry is currently deployed
91      * @throws CleanerException if it fails
92      */

93     protected abstract boolean isDeployLogEntry(LogEntry logEntry) throws CleanerException;
94
95     /**
96      * Run the clean task
97      * @throws CleanerException if it failed.
98      */

99     public void execute() throws CleanerException {
100
101         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
102             getLogger().log(BasicLevel.DEBUG, "execute : called");
103         }
104
105         // Get the entries from the logger
106
Vector JavaDoc logEntries = getLogEntries();
107
108         // Check if the files in this vector exists
109
LogEntry logEntry = null;
110
111         for (Enumeration JavaDoc e = logEntries.elements(); e.hasMoreElements();) {
112
113             logEntry = (LogEntry) e.nextElement();
114             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
115                 getLogger().log(BasicLevel.DEBUG,
116                         "LogEntry <" + logEntry.getOriginal().getName() + "," + logEntry.getCopy().getName() + ">");
117             }
118
119             // if the package is deployed, do nothing
120
if (isDeployLogEntry(logEntry)) {
121                 if (getLogger().isLoggable(BasicLevel.DEBUG)) {
122                     getLogger().log(BasicLevel.DEBUG, "LogEntry currently deployed - > do nothing");
123                 }
124
125                 continue;
126
127             }
128
129             // if the source package file doesn't exists anymore
130
// or if the file is present but don't care the right timestamp
131
if (!isValidLogEntry(logEntry)) {
132
133                 // we remove the entry
134
removeLogEntry(logEntry);
135
136                 // enumeration is now inconsistent, so we 're restarting the
137
// loop
138
e = logEntries.elements();
139
140             }
141             //else time stamp always the same
142
}
143     }
144
145     /**
146      * Remove a directory with all its child (recursive)
147      * @param file the file or directory which must be deleted
148      */

149     protected void removeRecursiveDirectory(File JavaDoc file) {
150
151         //File or directory doesn't exists, exit.
152
if (!file.exists()) {
153             return;
154         }
155
156         //Remove the child before the current file(directory)
157
if (file.isDirectory()) {
158             //remove all the child
159
File JavaDoc[] childFiles = file.listFiles();
160             for (int i = 0; i < childFiles.length; i++) {
161                 removeRecursiveDirectory(childFiles[i]);
162             }
163         }
164         //Since all childs are removed , remove us
165
file.delete();
166     }
167 }
Popular Tags