001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): Benoit PELLETIER
022: * --------------------------------------------------------------------------
023: * $Id: AbsCleanTask.java 6673 2005-04-28 16:53:00Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas_lib.deployment.work;
026:
027: import java.io.File;
028:
029: import java.util.Enumeration;
030: import java.util.Vector;
031:
032: import org.objectweb.util.monolog.api.Logger;
033: import org.objectweb.util.monolog.api.BasicLevel;
034:
035: import org.objectweb.jonas.common.Log;
036:
037: /**
038: * Abstract Clean Task class which define a generic job executed by the cleaner
039: * thread
040: * @author Benoit PELLETIER
041: */
042: public abstract class AbsCleanTask {
043:
044: /**
045: * The logger used in JOnAS
046: */
047: private static Logger logger = Log
048: .getLogger(Log.JONAS_DEPLOY_WORK_PREFIX);
049:
050: /**
051: * Default constructor : Construct a new cleaner.
052: */
053: protected AbsCleanTask() {
054: }
055:
056: /**
057: * @return the logger
058: */
059: protected static Logger getLogger() {
060: return logger;
061: }
062:
063: /**
064: * Abstract method defined in the derived classes return true if the work
065: * copy exist and is up to date
066: * @param logEntry entry in a deploy log
067: * @return true if the work copy exists and is up to date
068: * @throws CleanerException if it fails
069: */
070: protected abstract boolean isValidLogEntry(LogEntry logEntry)
071: throws CleanerException;
072:
073: /**
074: * Abstract method defined in the derived classes remove the work copy
075: * specified in the log entry and the log entry
076: * @param logEntry entry in a deploy log
077: * @throws CleanerException if it fails
078: */
079: protected abstract void removeLogEntry(LogEntry logEntry)
080: throws CleanerException;
081:
082: /**
083: * Abstract method defined in the derived classes get the log entries
084: * @return the log entries
085: */
086: protected abstract Vector getLogEntries();
087:
088: /**
089: * Check if the package pointed by the log entry is currently deploy
090: * @param logEntry entry in a deploy log
091: * @return true if the package pointed by the log entry is currently deployed
092: * @throws CleanerException if it fails
093: */
094: protected abstract boolean isDeployLogEntry(LogEntry logEntry)
095: throws CleanerException;
096:
097: /**
098: * Run the clean task
099: * @throws CleanerException if it failed.
100: */
101: public void execute() throws CleanerException {
102:
103: if (getLogger().isLoggable(BasicLevel.DEBUG)) {
104: getLogger().log(BasicLevel.DEBUG, "execute : called");
105: }
106:
107: // Get the entries from the logger
108: Vector logEntries = getLogEntries();
109:
110: // Check if the files in this vector exists
111: LogEntry logEntry = null;
112:
113: for (Enumeration e = logEntries.elements(); e.hasMoreElements();) {
114:
115: logEntry = (LogEntry) e.nextElement();
116: if (getLogger().isLoggable(BasicLevel.DEBUG)) {
117: getLogger().log(
118: BasicLevel.DEBUG,
119: "LogEntry <" + logEntry.getOriginal().getName()
120: + "," + logEntry.getCopy().getName()
121: + ">");
122: }
123:
124: // if the package is deployed, do nothing
125: if (isDeployLogEntry(logEntry)) {
126: if (getLogger().isLoggable(BasicLevel.DEBUG)) {
127: getLogger()
128: .log(BasicLevel.DEBUG,
129: "LogEntry currently deployed - > do nothing");
130: }
131:
132: continue;
133:
134: }
135:
136: // if the source package file doesn't exists anymore
137: // or if the file is present but don't care the right timestamp
138: if (!isValidLogEntry(logEntry)) {
139:
140: // we remove the entry
141: removeLogEntry(logEntry);
142:
143: // enumeration is now inconsistent, so we 're restarting the
144: // loop
145: e = logEntries.elements();
146:
147: }
148: //else time stamp always the same
149: }
150: }
151:
152: /**
153: * Remove a directory with all its child (recursive)
154: * @param file the file or directory which must be deleted
155: */
156: protected void removeRecursiveDirectory(File file) {
157:
158: //File or directory doesn't exists, exit.
159: if (!file.exists()) {
160: return;
161: }
162:
163: //Remove the child before the current file(directory)
164: if (file.isDirectory()) {
165: //remove all the child
166: File[] childFiles = file.listFiles();
167: for (int i = 0; i < childFiles.length; i++) {
168: removeRecursiveDirectory(childFiles[i]);
169: }
170: }
171: //Since all childs are removed , remove us
172: file.delete();
173: }
174: }
|