KickJava   Java API By Example, From Geeks To Geeks.

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


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  * --------------------------------------------------------------------------
22  * $Id: EarCleanTask.java,v 1.5 2005/04/28 16:53:00 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_lib.deployment.work;
27
28 import java.io.File JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import org.objectweb.jonas.service.ServiceManager;
34 import org.objectweb.jonas.ear.EarService;
35
36 import org.objectweb.util.monolog.api.BasicLevel;
37
38 /**
39  * JOnAS Ear unused directory clean task class. This class provides a way for
40  * removing directories wich are unconsistent directories for ear files.
41  * @author Florent BENOIT
42  * @author Benoit PELLETIER
43  */

44 public class EarCleanTask extends AbsCleanTask {
45
46     /**
47      * reference to the deployerLog
48      */

49     private static DeployerLog earDeployerLog = null;
50
51     /**
52      * Url of the jonas_ROOT/apps directory
53      */

54     private static URL JavaDoc earAppsUrl = null;
55
56     /**
57      * Default constructor : Construct a new cleaner.
58      * @param earApps the jonasroot apps directory
59      * @param earDeployerLog the deployer logger
60      */

61     public EarCleanTask(URL JavaDoc earApps, DeployerLog earDeployerLog) {
62         super();
63         EarCleanTask.earAppsUrl = earApps;
64         EarCleanTask.earDeployerLog = earDeployerLog;
65     }
66
67     /**
68      * Return true if the work copy exists and is up to date
69      * @param logEntry entry in a deploy log
70      * @return true if the work copy exists and is up to date
71      * @throws CleanerException if it fails
72      */

73     protected boolean isValidLogEntry(LogEntry logEntry) throws CleanerException {
74         String JavaDoc fTimeStamp = null;
75         File JavaDoc earLogEntryFile = logEntry.getOriginal();
76         String JavaDoc earLogEntryUnpackedDir = logEntry.getCopy().getName();
77         //File dirEar = new File(earAppsUrl.getFile() + File.separator + earLogEntryUnpackedDir);
78
if (getLogger().isLoggable(BasicLevel.DEBUG)) {
79             getLogger().log(BasicLevel.DEBUG,
80                     "LogEntry <" + earLogEntryFile.getName() + "> exist :" + earLogEntryFile.exists());
81         }
82
83         // if the file doesn't exist, return
84
if (!earLogEntryFile.exists()) {
85             return false;
86         }
87
88         //get the timestamp
89
try {
90             fTimeStamp = FileManager.fileToTimeStampDir(earLogEntryFile.toURL());
91         } catch (FileManagerException efme) {
92             throw new CleanerException("Can't get the timestamp of the file " + earLogEntryFile + " : "
93                     + efme.getMessage());
94         } catch (MalformedURLException JavaDoc mue) {
95             throw new CleanerException("Can't get the timestamp of the file " + earLogEntryFile + " : "
96                     + mue.getMessage());
97         }
98
99         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
100             getLogger().log(BasicLevel.DEBUG, "LogEntry fTimeStamp :" + fTimeStamp);
101             getLogger().log(BasicLevel.DEBUG, "LogEntry isValid :" + fTimeStamp.equalsIgnoreCase(earLogEntryUnpackedDir));
102         }
103
104         //compare
105
return (fTimeStamp.equalsIgnoreCase(earLogEntryUnpackedDir));
106
107     }
108
109     /**
110      * Remove the work copy specified in the log entry and the log entry
111      * @param logEntry entry in a deploy log
112      * @throws CleanerException if it fails
113      */

114     protected void removeLogEntry(LogEntry logEntry) throws CleanerException {
115         String JavaDoc earLogEntryUnpackedDir = logEntry.getCopy().getName();
116
117         File JavaDoc dirEar = new File JavaDoc(earAppsUrl.getFile() + File.separator + earLogEntryUnpackedDir);
118
119         removeRecursiveDirectory(dirEar);
120
121         try {
122             earDeployerLog.removeEntry(logEntry);
123         } catch (DeployerLogException edle) {
124             throw new CleanerException("Can't remove an entry" + edle.getMessage());
125         }
126
127     }
128
129     /**
130      * Gets the log entries
131      * @return the log entries
132      */

133     protected Vector JavaDoc getLogEntries() {
134         return earDeployerLog.getEntries();
135     }
136
137     /**
138      * Check if the package pointed by the log entry is currently deploy
139      * @param logEntry entry in a deploy log
140      * @return true if the package pointed by the log entry is currently deployed
141      * @throws CleanerException if it fails
142      */

143     protected boolean isDeployLogEntry(LogEntry logEntry) throws CleanerException {
144
145         // get the ear service
146
ServiceManager sm = null;
147
148         try {
149             sm = ServiceManager.getInstance();
150         } catch (Exception JavaDoc e) {
151             throw new CleanerException("Cannot get ServiceManager instance");
152         }
153         EarService earService = (EarService) sm.getEarService();
154
155         // check if the ear file is deployed
156
return earService.isEarDeployedByUnpackName(logEntry.getCopy().getName());
157     }
158 }
Popular Tags