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.provider;
18  
19  import java.util.ArrayList;
20  import java.util.stream.Stream;
21  
22  import org.apache.commons.vfs2.FileSystemException;
23  
24  /**
25   * A {@link VfsComponent} that contains a set of sub-components.
26   */
27  public abstract class AbstractVfsContainer extends AbstractVfsComponent {
28  
29      /**
30       * The components contained by this component.
31       */
32      private final ArrayList<Object> components = new ArrayList<>(); // @GuardedBy("self")
33  
34      /**
35       * Adds a sub-component to this component.
36       * <p>
37       * If the sub-component implements {@link VfsComponent}, it is initialized. All sub-components are closed when this
38       * component is closed.
39       * </p>
40       *
41       * @param component the component to add.
42       * @throws FileSystemException if any error occurs.
43       */
44      protected void addComponent(final Object component) throws FileSystemException {
45          synchronized (components) {
46              if (!components.contains(component)) {
47                  // Initialize
48                  if (component instanceof VfsComponent) {
49                      final VfsComponent../org/apache/commons/vfs2/provider/VfsComponent.html#VfsComponent">VfsComponent vfsComponent = (VfsComponent) component;
50                      vfsComponent.setLogger(getLogger());
51                      vfsComponent.setContext(getContext());
52                      vfsComponent.init();
53                  }
54  
55                  // Keep track of component, to close it later
56                  components.add(component);
57              }
58          } // synchronized
59      }
60  
61      /**
62       * Removes a sub-component from this component.
63       *
64       * @param component the component to remove.
65       */
66      protected void removeComponent(final Object component) {
67          synchronized (components) {
68              // multiple instances should not happen
69              components.remove(component);
70          }
71      }
72  
73      /**
74       * Closes the sub-components of this component.
75       */
76      @Override
77      public void close() {
78          final Object[] toclose;
79          synchronized (components) {
80              toclose = components.toArray();
81              components.clear();
82          }
83  
84          // Close all components
85          Stream.of(toclose).filter(component -> component instanceof VfsComponent)
86                            .forEach(component -> ((VfsComponent) component).close());
87      }
88  }