org.mmbase.util
Class ResourceWatcher

java.lang.Object
  extended by org.mmbase.util.ResourceWatcher
Direct Known Subclasses:
RegexpReplacer.PatternWatcher

public abstract class ResourceWatcher
extends Object

Like FileWatcher but for Resources. If (one of the) file(s) to which the resource resolves to is added or changed, it's onChange will be triggered, if not a 'more important' one was existing already. If a file is removed, and was the most important one, it will be removed from the filewatcher.

Since:
MMBase-1.8
Version:
$Id: ResourceWatcher.java 41790 2010-04-08 10:50:53Z michiel $
Author:
Michiel Meeuwissen
See Also:
FileWatcher, ResourceLoader

Nested Class Summary
protected  class ResourceWatcher.ResourceFileWatcher
          A FileWatcher associated with a certain resource of this ResourceWatcher.
 
Field Summary
protected  Map<String,FileWatcher> fileWatchers
          Wrapped FileWatcher for watching the file-resources.
protected  Map<Integer,String> nodeNumberToResourceName
          When a resource is loaded from a Node, we must know which Nodes correspond to which resource.
(package private) static String resourceBuilder
           
protected  ResourceLoader resourceLoader
          The resource-loader associated with this ResourceWatcher.
protected  SortedSet<String> resources
          All resources watched by this ResourceWatcher.
(package private) static Map<ResourceWatcher,Object> resourceWatchers
          All instantiated ResourceWatchers.
 
Constructor Summary
protected ResourceWatcher()
          Constructor, defaulting to the Root ResourceLoader (see ResourceLoader.getConfigurationRoot()).
protected ResourceWatcher(ResourceLoader rl)
          Constructor.
protected ResourceWatcher(ResourceLoader rl, boolean administrate)
           
 
Method Summary
 void add(String resourceName)
           
 void add(URL url)
          If you resolved a resource already to an URL, you can still add it for watching.
 void clear()
          Removes all resources.
protected  void createFileWatcher(String resource)
          When a resource is added to this ResourceWatcher, this method is called to create a ResourceWatcher.ResourceFileWatcher, and add all files associated with the resource to it.
 void exit()
          Stops watching.
 long getDelay()
           
 Map<String,FileWatcher> getFileWatchers()
           
 ResourceLoader getResourceLoader()
          The associated ResourceLoader
 Set<String> getResources()
           
static Set<ResourceWatcher> getResourceWatchers()
           
 boolean isRunning()
           
protected  boolean mapNodeNumber(String resource)
          When a resource is added to this ResourceWatcher, this method is called to check wether a ResourceBuilder node is associated with this resource.
 void onChange()
          Calls onChange(String) for every added resource.
abstract  void onChange(String resourceName)
          Put here the stuff that has to be executed, when a file has been changed.
protected  void readdResources()
           
static void reinitWatchers()
           
 void remove(String resourceName)
           
 void setDelay(long delay)
          Set the delay to observe between each check of the file changes.
static void setResourceBuilder(String builder)
          Sets the MMBase builder which must be used for resource.
 void start()
          If a node (of the type 'resourceBuilder') changes, checks if it is a node belonging to one of the resource of this resource-watcher.
 String toString()
          Shows the 'contents' of the filewatcher.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

resourceBuilder

static String resourceBuilder

resourceWatchers

static final Map<ResourceWatcher,Object> resourceWatchers
All instantiated ResourceWatchers.


resources

protected final SortedSet<String> resources
All resources watched by this ResourceWatcher. A Set of Strings. Often, a ResourceWatcher would watch only one resource.


nodeNumberToResourceName

protected final Map<Integer,String> nodeNumberToResourceName
When a resource is loaded from a Node, we must know which Nodes correspond to which resource. You could ask the node itself, but if the node happens to be deleted, then you can't know that any more. Used in #notify(NodeEvent)


fileWatchers

protected final Map<String,FileWatcher> fileWatchers
Wrapped FileWatcher for watching the file-resources. ResourceName -> FileWatcher.


resourceLoader

protected final ResourceLoader resourceLoader
The resource-loader associated with this ResourceWatcher.

Constructor Detail

ResourceWatcher

protected ResourceWatcher(ResourceLoader rl)
Constructor.


ResourceWatcher

protected ResourceWatcher(ResourceLoader rl,
                          boolean administrate)

ResourceWatcher

protected ResourceWatcher()
Constructor, defaulting to the Root ResourceLoader (see ResourceLoader.getConfigurationRoot()).

Method Detail

setResourceBuilder

public static void setResourceBuilder(String builder)
Sets the MMBase builder which must be used for resource. The builder must have an URL and a HANDLE field. This method can be called only once. Considers all resource-watchers. Perhaps onChange must be called, because there is a node for this resource available now.

Parameters:
b - An String (this may be null if no such builder available)
Throws:
RuntimeException - if builder was set already.

reinitWatchers

public static void reinitWatchers()
Since:
MMBase-1.9.2

getResources

public Set<String> getResources()
Returns:
Unmodifiable set of String of watched resources

getResourceLoader

public ResourceLoader getResourceLoader()
The associated ResourceLoader


add

public void add(String resourceName)
Parameters:
resourceName - The resource to be monitored.

add

public void add(URL url)
If you resolved a resource already to an URL, you can still add it for watching.


createFileWatcher

protected void createFileWatcher(String resource)
When a resource is added to this ResourceWatcher, this method is called to create a ResourceWatcher.ResourceFileWatcher, and add all files associated with the resource to it.


mapNodeNumber

protected boolean mapNodeNumber(String resource)
When a resource is added to this ResourceWatcher, this method is called to check wether a ResourceBuilder node is associated with this resource. If so, this methods maps the number of the node to the resource name. This is needed in #notify(NodeEvent) in case of a node-deletion.

Returns:
Whether a Node as found to map.

start

public void start()
If a node (of the type 'resourceBuilder') changes, checks if it is a node belonging to one of the resource of this resource-watcher. If so, onChange(java.lang.String) is called.


onChange

public abstract void onChange(String resourceName)
Put here the stuff that has to be executed, when a file has been changed.

Parameters:
resourceName - The resource that was changed.

onChange

public final void onChange()
Calls onChange(String) for every added resource.


setDelay

public void setDelay(long delay)
Set the delay to observe between each check of the file changes.

Parameters:
delay - The delay in milliseconds

remove

public void remove(String resourceName)

clear

public void clear()
Removes all resources.


readdResources

protected void readdResources()
Since:
MMBase-1.9.2

exit

public void exit()
Stops watching. Stops all filewatchers, removes observers.


toString

public String toString()
Shows the 'contents' of the filewatcher. It shows a list of files/last modified timestamps.

Overrides:
toString in class Object

getResourceWatchers

public static Set<ResourceWatcher> getResourceWatchers()
Since:
MMBase-1.9.2

getDelay

public long getDelay()
Since:
MMBase-1.9.2

getFileWatchers

public Map<String,FileWatcher> getFileWatchers()
Since:
MMBase-1.9.2

isRunning

public boolean isRunning()
Since:
MMBase-1.9.2


MMBase 2.0-SNAPSHOT - null