KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > deamons > filewatcher > JahiaFileWatcherBaseService


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
// JahiaFileWatcherBaseService
15
//
16
// NK 12.01.2001
17
//
18
//
19

20
21 package org.jahia.services.deamons.filewatcher;
22
23
24 import org.jahia.exceptions.JahiaException;
25 import org.jahia.tools.files.FileWatcher;
26
27 import java.io.IOException JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Observer JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import org.jahia.settings.SettingsBean;
32 import org.jahia.exceptions.JahiaInitializationException;
33
34
35 /**
36  * This Service hold a pool of instance of jahia.tools.FileWatcher Class.
37  * Each Thread are identified by a name and accessible through this name.
38  * Threads are added in an Hashtable registry.
39  *
40  * @author Khue ng
41  * @version 1.0
42  */

43 public class JahiaFileWatcherBaseService extends JahiaFileWatcherService {
44
45     private static org.apache.log4j.Logger logger =
46             org.apache.log4j.Logger.getLogger (JahiaFileWatcherBaseService.class);
47
48     /** The singelton instance of this class * */
49     private static JahiaFileWatcherBaseService m_Instance = null;
50
51     /**
52      * The Pool of Threads *
53      *
54      * @associates FileWatcher
55      */

56     private Hashtable JavaDoc m_Registry;
57
58
59     /**
60      * Protected Constructor
61      */

62     protected JahiaFileWatcherBaseService () {
63
64         logger.info ("***** Starting the Jahia File Watcher Base Service *****");
65         m_Registry = new Hashtable JavaDoc ();
66
67     }
68
69     /**
70      * Use this method to get an instance of this class
71      */

72     public static synchronized JahiaFileWatcherBaseService getInstance () {
73
74         if (m_Instance == null) {
75             m_Instance = new JahiaFileWatcherBaseService ();
76         }
77         return m_Instance;
78     }
79
80     /**
81      * addFileWatcher
82      *
83      * @param (String) threadName, the Name to identify this thread
84      * @param (String) fullFolderPath, the real path to the folder to watch
85      * @param (boolean) checkDate, check new file by last modif date or not
86      * @param (long) interval, the interval in millis
87      */

88     public synchronized void addFileWatcher (String JavaDoc threadName,
89                                              String JavaDoc fullFolderPath,
90                                              boolean checkDate,
91                                              long interval,
92                                              boolean fileOnly
93                                              ) throws JahiaException {
94         try {
95
96             FileWatcher fw = new FileWatcher (fullFolderPath,
97                     checkDate,
98                     interval,
99                     fileOnly);
100
101             m_Registry.put (threadName, fw);
102
103             //fw.start();
104

105         } catch (IOException JavaDoc e) {
106
107             logger.error ("addFileWatcher:: " + e.getMessage ());
108
109             throw new JahiaException ("JahiaFileWatcherBaseService", "failed adding File Watcher",
110                     JahiaException.SERVICE_ERROR, JahiaException.WARNING_SEVERITY);
111         }
112
113     }
114
115     /**
116      * Call the start method of the thread
117      *
118      * @param (String) threadName, the Name to identify this thread
119      */

120     public void startFileWatcher (String JavaDoc threadName) throws JahiaException {
121
122         try {
123
124             synchronized (this) {
125
126                 getFileWatcher (threadName).start ();
127             }
128
129         } catch (IOException JavaDoc e) {
130             logger.error ("startFileWatcher:: " + e.getMessage ());
131
132             throw new JahiaException ("JahiaFileWatcherBaseService", "failed starting File Watcher",
133                     JahiaException.SERVICE_ERROR, JahiaException.WARNING_SEVERITY);
134         }
135     }
136
137     /**
138      * Stops the file watcher thread. Should be called when shutting down the
139     * application.
140      * @param threadName String the name of the thread to shutdown.
141      * @throws JahiaException
142      */

143     public void stopFileWatcher (String JavaDoc threadName)
144         throws JahiaException {
145
146         synchronized (this) {
147             getFileWatcher(threadName).stop();
148         }
149     }
150
151
152     /**
153      * Register an Observer Thread with an Observable Thread
154      *
155      * @param threadName the Name of Observable object
156      * @param obs the observer object
157      */

158     public void registerObserver (String JavaDoc threadName,
159                                   Observer JavaDoc obs
160                                   ) {
161         synchronized (this) {
162             getFileWatcher (threadName).addObserver (obs);
163         }
164     }
165
166     public void init (SettingsBean jSettings)
167         throws JahiaInitializationException {
168
169     }
170
171     /**
172      * Code to clean up the services ressource here. Override this method
173      * with specific services shutdown codes.
174      *
175      * @exception JahiaException
176      * Raise an JahiaException exception on any failure.
177      */

178     public synchronized void shutdown ()
179         throws JahiaException
180     {
181         mIsServiceInitialized = false;
182         Iterator JavaDoc fileWatcherIter = m_Registry.values().iterator();
183         while (fileWatcherIter.hasNext()) {
184             FileWatcher curFileWatcher = (FileWatcher) fileWatcherIter.next();
185             curFileWatcher.stop();
186         }
187     }
188
189     /**
190      * getFileWatcher
191      *
192      * @param (String) threadName, the Name to identify this thread
193      *
194      * @return (FileWatcher) return the FileWatcher Thread or null if
195      * not in registry
196      */

197     protected synchronized FileWatcher getFileWatcher (String JavaDoc threadName) {
198
199         return (FileWatcher) m_Registry.get (threadName);
200
201     }
202
203 }
204
Popular Tags