001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.util;
018:
019: import org.apache.commons.vfs.FileChangeEvent;
020: import org.apache.commons.vfs.FileListener;
021: import org.apache.commons.vfs.FileName;
022: import org.apache.commons.vfs.FileObject;
023: import org.apache.commons.vfs.FileSystem;
024:
025: import java.lang.ref.WeakReference;
026:
027: /**
028: * Wrap a listener with a WeakReference.
029: *
030: * @author <a href="mailto:doogie@brainfood.com">Adam Heath</a>
031: * @version $Revision: 262 $ $Date: 2006-12-20T09:14:53.055649Z $
032: */
033: public class WeakRefFileListener implements FileListener {
034: private final FileSystem fs;
035: private final FileName name;
036: private final WeakReference listener;
037:
038: protected WeakRefFileListener(final FileObject file,
039: final FileListener listener) {
040: this .fs = file.getFileSystem();
041: this .name = file.getName();
042: this .listener = new WeakReference(listener);
043: }
044:
045: /**
046: * This will install the <code>listener<code> at the given <code>file</code>
047: */
048: public static void installListener(final FileObject file,
049: final FileListener listener) {
050: WeakRefFileListener weakListener = new WeakRefFileListener(
051: file, listener);
052:
053: file.getFileSystem().addListener(file,
054: new WeakRefFileListener(file, weakListener));
055: }
056:
057: /**
058: * returns the wrapped listener. If it is gone, the WeakRefFileListener wrapper will
059: * remove itself from the list of listeners.
060: */
061: protected FileListener getListener() throws Exception {
062: FileListener listener = (FileListener) this .listener.get();
063: if (listener == null) {
064: FileObject file = fs.resolveFile(name);
065: file.getFileSystem().removeListener(file, this );
066: }
067: return listener;
068: }
069:
070: /**
071: * Called when a file is created.
072: */
073: public void fileCreated(final FileChangeEvent event)
074: throws Exception {
075: FileListener listener = getListener();
076: if (listener == null) {
077: return;
078: }
079: listener.fileCreated(event);
080: }
081:
082: /**
083: * Called when a file is deleted.
084: */
085: public void fileDeleted(final FileChangeEvent event)
086: throws Exception {
087: FileListener listener = getListener();
088: if (listener == null) {
089: return;
090: }
091: listener.fileDeleted(event);
092: }
093:
094: /**
095: * Called when a file is changed.
096: * <p/>
097: * This will only happen if you monitor the file using {@link org.apache.commons.vfs.FileMonitor}.
098: */
099: public void fileChanged(FileChangeEvent event) throws Exception {
100: FileListener listener = getListener();
101: if (listener == null) {
102: return;
103: }
104: listener.fileChanged(event);
105: }
106: }
|