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: * --------------------------------------------------------------------------
022: * $Id: EarCleanTask.java 6673 2005-04-28 16:53:00Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_lib.deployment.work;
025:
026: import java.io.File;
027: import java.net.MalformedURLException;
028: import java.net.URL;
029: import java.util.Vector;
030:
031: import org.objectweb.jonas.service.ServiceManager;
032: import org.objectweb.jonas.ear.EarService;
033:
034: import org.objectweb.util.monolog.api.BasicLevel;
035:
036: /**
037: * JOnAS Ear unused directory clean task class. This class provides a way for
038: * removing directories wich are unconsistent directories for ear files.
039: * @author Florent BENOIT
040: * @author Benoit PELLETIER
041: */
042: public class EarCleanTask extends AbsCleanTask {
043:
044: /**
045: * reference to the deployerLog
046: */
047: private static DeployerLog earDeployerLog = null;
048:
049: /**
050: * Url of the jonas_ROOT/apps directory
051: */
052: private static URL earAppsUrl = null;
053:
054: /**
055: * Default constructor : Construct a new cleaner.
056: * @param earApps the jonasroot apps directory
057: * @param earDeployerLog the deployer logger
058: */
059: public EarCleanTask(URL earApps, DeployerLog earDeployerLog) {
060: super ();
061: EarCleanTask.earAppsUrl = earApps;
062: EarCleanTask.earDeployerLog = earDeployerLog;
063: }
064:
065: /**
066: * Return true if the work copy exists and is up to date
067: * @param logEntry entry in a deploy log
068: * @return true if the work copy exists and is up to date
069: * @throws CleanerException if it fails
070: */
071: protected boolean isValidLogEntry(LogEntry logEntry)
072: throws CleanerException {
073: String fTimeStamp = null;
074: File earLogEntryFile = logEntry.getOriginal();
075: String earLogEntryUnpackedDir = logEntry.getCopy().getName();
076: //File dirEar = new File(earAppsUrl.getFile() + File.separator + earLogEntryUnpackedDir);
077: if (getLogger().isLoggable(BasicLevel.DEBUG)) {
078: getLogger().log(
079: BasicLevel.DEBUG,
080: "LogEntry <" + earLogEntryFile.getName()
081: + "> exist :" + earLogEntryFile.exists());
082: }
083:
084: // if the file doesn't exist, return
085: if (!earLogEntryFile.exists()) {
086: return false;
087: }
088:
089: //get the timestamp
090: try {
091: fTimeStamp = FileManager.fileToTimeStampDir(earLogEntryFile
092: .toURL());
093: } catch (FileManagerException efme) {
094: throw new CleanerException(
095: "Can't get the timestamp of the file "
096: + earLogEntryFile + " : "
097: + efme.getMessage());
098: } catch (MalformedURLException mue) {
099: throw new CleanerException(
100: "Can't get the timestamp of the file "
101: + earLogEntryFile + " : "
102: + mue.getMessage());
103: }
104:
105: if (getLogger().isLoggable(BasicLevel.DEBUG)) {
106: getLogger().log(BasicLevel.DEBUG,
107: "LogEntry fTimeStamp :" + fTimeStamp);
108: getLogger()
109: .log(
110: BasicLevel.DEBUG,
111: "LogEntry isValid :"
112: + fTimeStamp
113: .equalsIgnoreCase(earLogEntryUnpackedDir));
114: }
115:
116: //compare
117: return (fTimeStamp.equalsIgnoreCase(earLogEntryUnpackedDir));
118:
119: }
120:
121: /**
122: * Remove the work copy specified in the log entry and the log entry
123: * @param logEntry entry in a deploy log
124: * @throws CleanerException if it fails
125: */
126: protected void removeLogEntry(LogEntry logEntry)
127: throws CleanerException {
128: String earLogEntryUnpackedDir = logEntry.getCopy().getName();
129:
130: File dirEar = new File(earAppsUrl.getFile() + File.separator
131: + earLogEntryUnpackedDir);
132:
133: removeRecursiveDirectory(dirEar);
134:
135: try {
136: earDeployerLog.removeEntry(logEntry);
137: } catch (DeployerLogException edle) {
138: throw new CleanerException("Can't remove an entry"
139: + edle.getMessage());
140: }
141:
142: }
143:
144: /**
145: * Gets the log entries
146: * @return the log entries
147: */
148: protected Vector getLogEntries() {
149: return earDeployerLog.getEntries();
150: }
151:
152: /**
153: * Check if the package pointed by the log entry is currently deploy
154: * @param logEntry entry in a deploy log
155: * @return true if the package pointed by the log entry is currently deployed
156: * @throws CleanerException if it fails
157: */
158: protected boolean isDeployLogEntry(LogEntry logEntry)
159: throws CleanerException {
160:
161: // get the ear service
162: ServiceManager sm = null;
163:
164: try {
165: sm = ServiceManager.getInstance();
166: } catch (Exception e) {
167: throw new CleanerException(
168: "Cannot get ServiceManager instance");
169: }
170: EarService earService = (EarService) sm.getEarService();
171:
172: // check if the ear file is deployed
173: return earService.isEarDeployedByUnpackName(logEntry.getCopy()
174: .getName());
175: }
176: }
|