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.vfs.provider;
18  
19  import org.apache.commons.vfs.FileSystemException;
20  
21  import java.util.ArrayList;
22  
23  /***
24   * A {@link VfsComponent} that contains a set of sub-components.
25   *
26   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
27   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
28   */
29  public abstract class AbstractVfsContainer
30      extends AbstractVfsComponent
31  {
32      /***
33       * The components contained by this component.
34       */
35      private final ArrayList components = new ArrayList();
36  
37      /***
38       * Adds a sub-component to this component.  If the sub-component implements
39       * {@link VfsComponent}, it is initialised.  All sub-components are closed
40       * when this component is closed.
41       */
42      protected void addComponent(final Object component)
43          throws FileSystemException
44      {
45          if (!components.contains(component))
46          {
47              // Initialise
48              if (component instanceof VfsComponent)
49              {
50                  VfsComponent vfsComponent = (VfsComponent) component;
51                  vfsComponent.setLogger(getLogger());
52                  vfsComponent.setContext(getContext());
53                  vfsComponent.init();
54              }
55  
56              // Keep track of component, to close it later
57              components.add(component);
58          }
59      }
60  
61      /***
62       * Removes a sub-component from this component.
63       */
64      protected void removeComponent(final Object component)
65      {
66          components.remove(component);
67      }
68  
69      /***
70       * Closes the sub-components of this component.
71       */
72      public void close()
73      {
74          // Close all components
75          final int count = components.size();
76          for (int i = 0; i < count; i++)
77          {
78              final Object component = components.get(i);
79              if (component instanceof VfsComponent)
80              {
81                  final VfsComponent vfsComponent = (VfsComponent) component;
82                  vfsComponent.close();
83              }
84          }
85          components.clear();
86      }
87  }