org.apache.commons.vfs2.FileListener Java Examples

The following examples show how to use org.apache.commons.vfs2.FileListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractFileSystem.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Fires an event.
 */
private void fireEvent(final AbstractFileChangeEvent event) {
    FileListener[] fileListeners = null;
    final FileObject fileObject = event.getFileObject();

    synchronized (listenerMap) {
        final ArrayList<?> listeners = listenerMap.get(fileObject.getName());
        if (listeners != null) {
            fileListeners = listeners.toArray(new FileListener[listeners.size()]);
        }
    }

    if (fileListeners != null) {
        for (final FileListener fileListener : fileListeners) {
            try {
                event.notify(fileListener);
            } catch (final Exception e) {
                final String message = Messages.getString("vfs.provider/notify-listener.warn", fileObject);
                // getLogger().warn(message, e);
                VfsLog.warn(getLogger(), LOG, message, e);
            }
        }
    }
}
 
Example #2
Source File: AbstractFileSystem.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a listener on a file in this file system.
 *
 * @param file The FileObject to be monitored.
 * @param listener The FileListener
 */
@Override
public void addListener(final FileObject file, final FileListener listener) {
    synchronized (listenerMap) {
        ArrayList<FileListener> listeners = listenerMap.get(file.getName());
        if (listeners == null) {
            listeners = new ArrayList<>();
            listenerMap.put(file.getName(), listeners);
        }
        listeners.add(listener);
    }
}
 
Example #3
Source File: AbstractFileSystem.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Removes a listener from a file in this file system.
 *
 * @param file The FileObject to be monitored.
 * @param listener The FileListener
 */
@Override
public void removeListener(final FileObject file, final FileListener listener) {
    synchronized (listenerMap) {
        final ArrayList<?> listeners = listenerMap.get(file.getName());
        if (listeners != null) {
            listeners.remove(listener);
            if (listeners.isEmpty()) {
                listenerMap.remove(file.getName());
            }
        }
    }
}
 
Example #4
Source File: WeakRefFileListener.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the wrapped listener. If it is gone, the WeakRefFileListener wrapper will remove itself from the list of
 * listeners.
 *
 * @return The FileListener.
 * @throws Exception if an error occurs.
 */
protected FileListener getListener() throws Exception {
    final FileListener listener = this.listener.get();
    if (listener == null) {
        try (final FileObject fileObject = fs.resolveFile(name)) {
            fileObject.getFileSystem().removeListener(fileObject, this);
        }
    }
    return listener;
}
 
Example #5
Source File: WeakRefFileListener.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Called when a file is created.
 *
 * @param event The FileChangeEvent.
 * @throws Exception if an error occurs.
 */
@Override
public void fileCreated(final FileChangeEvent event) throws Exception {
    final FileListener listener = getListener();
    if (listener == null) {
        return;
    }
    listener.fileCreated(event);
}
 
Example #6
Source File: WeakRefFileListener.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Called when a file is deleted.
 *
 * @param event The FileChangeEvent.
 * @throws Exception if an error occurs.
 */
@Override
public void fileDeleted(final FileChangeEvent event) throws Exception {
    final FileListener listener = getListener();
    if (listener == null) {
        return;
    }
    listener.fileDeleted(event);
}
 
Example #7
Source File: CreateEvent.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
public void notify(final FileListener listener) throws Exception {
    listener.fileCreated(this);
}
 
Example #8
Source File: DeleteEvent.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
public void notify(final FileListener listener) throws Exception {
    listener.fileDeleted(this);
}
 
Example #9
Source File: ChangedEvent.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
public void notify(final FileListener listener) throws Exception {
    listener.fileChanged(this);
}
 
Example #10
Source File: WeakRefFileListener.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
protected WeakRefFileListener(final FileObject file, final FileListener listener) {
    this.fs = file.getFileSystem();
    this.name = file.getName();
    this.listener = new WeakReference<>(listener);
}
 
Example #11
Source File: DefaultFileMonitor.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
public DefaultFileMonitor(final FileListener listener) {
    this.listener = listener;
}
 
Example #12
Source File: WeakRefFileListener.java    From commons-vfs with Apache License 2.0 3 votes vote down vote up
/**
 * Called when a file is changed.
 * <p>
 * This will only happen if you monitor the file using {@link org.apache.commons.vfs2.FileMonitor}.
 * </p>
 *
 * @param event The FileChangeEvent.
 * @throws Exception if an error occurs.
 */
@Override
public void fileChanged(final FileChangeEvent event) throws Exception {
    final FileListener listener = getListener();
    if (listener == null) {
        return;
    }
    listener.fileChanged(event);
}
 
Example #13
Source File: WeakRefFileListener.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Installs the {@code listener} at the given {@code file}.
 *
 * @param file The FileObject to listen on.
 * @param listener The FileListener
 */
public static void installListener(final FileObject file, final FileListener listener) {
    final WeakRefFileListener weakListener = new WeakRefFileListener(file, listener);

    file.getFileSystem().addListener(file, new WeakRefFileListener(file, weakListener));
}
 
Example #14
Source File: DefaultFileMonitor.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Access method to get the current FileListener object notified when there are changes with the files added.
 *
 * @return The FileListener.
 */
FileListener getFileListener() {
    return this.listener;
}
 
Example #15
Source File: AbstractFileChangeEvent.java    From commons-vfs with Apache License 2.0 votes vote down vote up
public abstract void notify(final FileListener listener) throws Exception;