1 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 ; 28 import java.util.Hashtable ; 29 import java.util.Observer ; 30 import java.util.Iterator ; 31 import org.jahia.settings.SettingsBean; 32 import org.jahia.exceptions.JahiaInitializationException; 33 34 35 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 49 private static JahiaFileWatcherBaseService m_Instance = null; 50 51 56 private Hashtable m_Registry; 57 58 59 62 protected JahiaFileWatcherBaseService () { 63 64 logger.info ("***** Starting the Jahia File Watcher Base Service *****"); 65 m_Registry = new Hashtable (); 66 67 } 68 69 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 88 public synchronized void addFileWatcher (String threadName, 89 String 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 105 } catch (IOException 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 120 public void startFileWatcher (String threadName) throws JahiaException { 121 122 try { 123 124 synchronized (this) { 125 126 getFileWatcher (threadName).start (); 127 } 128 129 } catch (IOException 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 143 public void stopFileWatcher (String threadName) 144 throws JahiaException { 145 146 synchronized (this) { 147 getFileWatcher(threadName).stop(); 148 } 149 } 150 151 152 158 public void registerObserver (String threadName, 159 Observer 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 178 public synchronized void shutdown () 179 throws JahiaException 180 { 181 mIsServiceInitialized = false; 182 Iterator fileWatcherIter = m_Registry.values().iterator(); 183 while (fileWatcherIter.hasNext()) { 184 FileWatcher curFileWatcher = (FileWatcher) fileWatcherIter.next(); 185 curFileWatcher.stop(); 186 } 187 } 188 189 197 protected synchronized FileWatcher getFileWatcher (String threadName) { 198 199 return (FileWatcher) m_Registry.get (threadName); 200 201 } 202 203 } 204 | Popular Tags |