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  package org.apache.commons.vfs2.util;
18  
19  import java.lang.ref.WeakReference;
20  
21  import org.apache.commons.vfs2.FileChangeEvent;
22  import org.apache.commons.vfs2.FileListener;
23  import org.apache.commons.vfs2.FileName;
24  import org.apache.commons.vfs2.FileObject;
25  import org.apache.commons.vfs2.FileSystem;
26  
27  /**
28   * Wraps a listener with a WeakReference.
29   *
30   * @since 2.0
31   */
32  public class WeakRefFileListener implements FileListener {
33  
34      private final FileSystem fs;
35      private final FileName name;
36      private final WeakReference<FileListener> listener;
37  
38      protected WeakRefFileListener(final FileObject file, final FileListener listener) {
39          this.fs = file.getFileSystem();
40          this.name = file.getName();
41          this.listener = new WeakReference<>(listener);
42      }
43  
44      /**
45       * Installs the {@code listener} at the given {@code file}.
46       *
47       * @param file The FileObject to listen on.
48       * @param listener The FileListener
49       */
50      public static void installListener(final FileObject file, final FileListener listener) {
51          final WeakRefFileListenerener.html#WeakRefFileListener">WeakRefFileListener weakListener = new WeakRefFileListener(file, listener);
52  
53          file.getFileSystem().addListener(file, new WeakRefFileListener(file, weakListener));
54      }
55  
56      /**
57       * Gets the wrapped listener. If it is gone, the WeakRefFileListener wrapper will remove itself from the list of
58       * listeners.
59       *
60       * @return The FileListener.
61       * @throws Exception if an error occurs.
62       */
63      protected FileListener getListener() throws Exception {
64          final FileListener listener = this.listener.get();
65          if (listener == null) {
66              try (final FileObject fileObject = fs.resolveFile(name)) {
67                  fileObject.getFileSystem().removeListener(fileObject, this);
68              }
69          }
70          return listener;
71      }
72  
73      /**
74       * Called when a file is created.
75       *
76       * @param event The FileChangeEvent.
77       * @throws Exception if an error occurs.
78       */
79      @Override
80      public void fileCreated(final FileChangeEvent event) throws Exception {
81          final FileListener listener = getListener();
82          if (listener == null) {
83              return;
84          }
85          listener.fileCreated(event);
86      }
87  
88      /**
89       * Called when a file is deleted.
90       *
91       * @param event The FileChangeEvent.
92       * @throws Exception if an error occurs.
93       */
94      @Override
95      public void fileDeleted(final FileChangeEvent event) throws Exception {
96          final FileListener listener = getListener();
97          if (listener == null) {
98              return;
99          }
100         listener.fileDeleted(event);
101     }
102 
103     /**
104      * Called when a file is changed.
105      * <p>
106      * This will only happen if you monitor the file using {@link org.apache.commons.vfs2.FileMonitor}.
107      * </p>
108      *
109      * @param event The FileChangeEvent.
110      * @throws Exception if an error occurs.
111      */
112     @Override
113     public void fileChanged(final FileChangeEvent event) throws Exception {
114         final FileListener listener = getListener();
115         if (listener == null) {
116             return;
117         }
118         listener.fileChanged(event);
119     }
120 }