KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > filemonitor > FileMonitorService


1 /*
2  The contents of this file are subject to the Mozilla Public License Version 1.1
3  (the "License"); you may not use this file except in compliance with the
4  License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5  
6  Software distributed under the License is distributed on an "AS IS" basis,
7  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8  for the specific language governing rights and
9  limitations under the License.
10
11  The Original Code is "The Columba Project"
12  
13  The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
14  Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
15  
16  All Rights Reserved.
17  */

18 package org.columba.core.filemonitor;
19
20 import java.io.File JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.columba.core.scripting.service.api.IColumbaService;
26
27 /**
28  * @author Celso Pinto <cpinto@yimports.com>
29  */

30 public class FileMonitorService implements IColumbaService {
31
32     private List JavaDoc<FileObserver> observerList;
33
34     private ObserverThread observerThread;
35
36     public FileMonitorService() {
37         observerList = new ArrayList JavaDoc();
38         /* TODO must get polling interval from config file */
39         observerThread = new ObserverThread(
40                 ObserverThread.DEFAULT_POLLING_INTERVAL);
41     }
42
43     public void startService() {
44         observerThread.start();
45     }
46
47     public void disposeService() {
48     }
49
50     public boolean initService() {
51         return true;
52     }
53
54     public void stopService() {
55
56         for (FileObserver obs : observerList)
57             obs.stoppingService();
58
59         observerList.clear();
60
61     }
62
63     public void monitorFile(File JavaDoc file, FileObserver observer)
64             throws FileNotFoundException JavaDoc, FileMonitorException {
65
66         if (!file.exists())
67             throw new FileNotFoundException JavaDoc(file.getName());
68
69         if (file.isDirectory())
70             throw new FileMonitorException(
71                     "Monitoring directories not yet supported");
72
73         if (!observerList.contains(observer))
74             observerList.add(observer);
75
76         observerThread.monitorFile(file, observer);
77
78     }
79
80 }
81
Popular Tags