View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jci.monitor;
19  
20  import java.io.File;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * It's a runnable that spawns of a monitoring thread triggering the
31   * the observers and managing the their listeners.
32   * 
33   * @author tcurdt
34   */
35  public final class FilesystemAlterationMonitor implements Runnable {
36  
37      private final Log log = LogFactory.getLog(FilesystemAlterationMonitor.class);
38  
39      private final Object observersLock = new Object();
40      private Map observers = Collections.unmodifiableMap(new HashMap());    
41      private long delay = 3000;
42      private Thread thread;
43  
44      private volatile boolean running = true;
45          
46      public FilesystemAlterationMonitor() {
47      }
48  
49  
50      public void start() {
51          thread = new Thread(this);
52          thread.start();
53      }
54  
55  
56      public void stop() {
57          running = false;
58  
59          try {
60              thread.join(delay);
61          } catch (InterruptedException e) {
62          }
63      }
64  
65  
66      public void setInterval( final long pDelay ) {
67          delay = pDelay;
68      }
69  
70  
71      public void addListener( final File pRoot, final FilesystemAlterationListener pListener ) {
72  
73          FilesystemAlterationObserver observer;
74  
75          synchronized (observersLock) {
76              observer = (FilesystemAlterationObserver)observers.get(pRoot);
77  
78              if (observer == null) {
79                  final Map newObservers = new HashMap(observers);
80                  observer = new FilesystemAlterationObserverImpl(pRoot);
81                  newObservers.put(pRoot, observer);
82                  observers = Collections.unmodifiableMap(newObservers);
83              }
84          }
85  
86          observer.addListener(pListener);
87      }
88     
89      public void removeListener( final FilesystemAlterationListener pListener ) {
90          synchronized (observersLock) {
91              for (Iterator it = observers.values().iterator(); it.hasNext();) {
92                  final FilesystemAlterationObserver observer = (FilesystemAlterationObserver) it.next();
93                  observer.removeListener(pListener);
94                  // FIXME: remove observer if there are no listeners?
95              }
96          }
97      }
98  
99      public FilesystemAlterationListener[] getListenersFor( final File pRoot  ) {
100         final FilesystemAlterationObserver observer = (FilesystemAlterationObserver)observers.get(pRoot);
101 
102         if (observer == null) {
103             return new FilesystemAlterationListener[0];
104         }
105 
106         return observer.getListeners();
107     }
108 
109 
110     public void run() {
111         log.debug("fam running");
112         
113         while (true) {
114             if (!running) {
115                 break;
116             }
117 
118             final Map currentObservers = observers;
119             
120             for (Iterator it = currentObservers.values().iterator(); it.hasNext();) {
121                 final FilesystemAlterationObserver observer = (FilesystemAlterationObserver) it.next();
122                 observer.checkAndNotify();
123             }
124 
125             try {
126                 Thread.sleep(delay);
127             } catch (final InterruptedException e) {
128             }
129         }
130         
131         log.debug("fam exiting");
132     }
133 
134 }