KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > cluster > deploy > WarWatcher


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.catalina.cluster.deploy;
18
19 import java.io.File JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 /**
25  * <p>
26  * The <b>WarWatcher </b> watches the deployDir for changes made to the
27  * directory (adding new WAR files->deploy or remove WAR files->undeploy) And
28  * notifies a listener of the changes made
29  * </p>
30  *
31  * @author Filip Hanik
32  * @author Peter Rossbach
33  * @version 1.1
34  */

35
36 public class WarWatcher {
37
38     /*--Static Variables----------------------------------------*/
39     public static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
40             .getLog(WarWatcher.class);
41
42     /*--Instance Variables--------------------------------------*/
43     /**
44      * Directory to watch for war files
45      */

46     protected File JavaDoc watchDir = null;
47
48     /**
49      * Parent to be notified of changes
50      */

51     protected FileChangeListener listener = null;
52
53     /**
54      * Currently deployed files
55      */

56     protected Map JavaDoc currentStatus = new HashMap JavaDoc();
57
58     /*--Constructor---------------------------------------------*/
59
60     public WarWatcher() {
61     }
62
63     public WarWatcher(FileChangeListener listener, File JavaDoc watchDir) {
64         this.listener = listener;
65         this.watchDir = watchDir;
66     }
67
68     /*--Logic---------------------------------------------------*/
69
70     /**
71      * check for modification and send notifcation to listener
72      */

73     public void check() {
74         if (log.isInfoEnabled())
75             log.info("check cluster wars at " + watchDir);
76         File JavaDoc[] list = watchDir.listFiles(new WarFilter());
77         if (list == null)
78             list = new File JavaDoc[0];
79         //first make sure all the files are listed in our current status
80
for (int i = 0; i < list.length; i++) {
81             addWarInfo(list[i]);
82         }//for
83

84         //check all the status codes and update the FarmDeployer
85
for (Iterator JavaDoc i = currentStatus.entrySet().iterator(); i.hasNext();) {
86             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
87             WarInfo info = (WarInfo) entry.getValue();
88             int check = info.check();
89             if (check == 1) {
90                 listener.fileModified(info.getWar());
91             } else if (check == -1) {
92                 listener.fileRemoved(info.getWar());
93                 //no need to keep in memory
94
currentStatus.remove(info.getWar());
95             }//end if
96
}//for
97

98     }
99
100     /**
101      * add cluster war to the watcher state
102      * @param warfile
103      */

104     protected void addWarInfo(File JavaDoc warfile) {
105         WarInfo info = (WarInfo) currentStatus.get(warfile.getAbsolutePath());
106         if (info == null) {
107             info = new WarInfo(warfile);
108             info.setLastState(-1); //assume file is non existent
109
currentStatus.put(warfile.getAbsolutePath(), info);
110         }
111     }
112
113     /**
114      * clear watcher state
115      */

116     public void clear() {
117         currentStatus.clear();
118     }
119
120     /**
121      * @return Returns the watchDir.
122      */

123     public File JavaDoc getWatchDir() {
124         return watchDir;
125     }
126
127     /**
128      * @param watchDir
129      * The watchDir to set.
130      */

131     public void setWatchDir(File JavaDoc watchDir) {
132         this.watchDir = watchDir;
133     }
134
135     /**
136      * @return Returns the listener.
137      */

138     public FileChangeListener getListener() {
139         return listener;
140     }
141
142     /**
143      * @param listener
144      * The listener to set.
145      */

146     public void setListener(FileChangeListener listener) {
147         this.listener = listener;
148     }
149
150     /*--Inner classes-------------------------------------------*/
151
152     /**
153      * File name filter for war files
154      */

155     protected class WarFilter implements java.io.FilenameFilter JavaDoc {
156         public boolean accept(File JavaDoc path, String JavaDoc name) {
157             if (name == null)
158                 return false;
159             return name.endsWith(".war");
160         }
161     }
162
163     /**
164      * File information on existing WAR files
165      */

166     protected class WarInfo {
167         protected File JavaDoc war = null;
168
169         protected long lastChecked = 0;
170
171         protected long lastState = 0;
172
173         public WarInfo(File JavaDoc war) {
174             this.war = war;
175             this.lastChecked = war.lastModified();
176             if (!war.exists())
177                 lastState = -1;
178         }
179
180         public boolean modified() {
181             return war.exists() && war.lastModified() > lastChecked;
182         }
183
184         public boolean exists() {
185             return war.exists();
186         }
187
188         /**
189          * Returns 1 if the file has been added/modified, 0 if the file is
190          * unchanged and -1 if the file has been removed
191          *
192          * @return int 1=file added; 0=unchanged; -1=file removed
193          */

194         public int check() {
195             //file unchanged by default
196
int result = 0;
197
198             if (modified()) {
199                 //file has changed - timestamp
200
result = 1;
201                 lastState = result;
202             } else if ((!exists()) && (!(lastState == -1))) {
203                 //file was removed
204
result = -1;
205                 lastState = result;
206             } else if ((lastState == -1) && exists()) {
207                 //file was added
208
result = 1;
209                 lastState = result;
210             }
211             this.lastChecked = System.currentTimeMillis();
212             return result;
213         }
214
215         public File JavaDoc getWar() {
216             return war;
217         }
218
219         public int hashCode() {
220             return war.getAbsolutePath().hashCode();
221         }
222
223         public boolean equals(Object JavaDoc other) {
224             if (other instanceof WarInfo) {
225                 WarInfo wo = (WarInfo) other;
226                 return wo.getWar().equals(getWar());
227             } else {
228                 return false;
229             }
230         }
231
232         protected void setLastState(int lastState) {
233             this.lastState = lastState;
234         }
235
236     }
237
238 }
Popular Tags