KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > AbstractVfsContainer


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.provider;
17
18 import org.apache.commons.vfs.FileSystemException;
19
20 import java.util.ArrayList JavaDoc;
21
22 /**
23  * A {@link VfsComponent} that contains a set of sub-components.
24  *
25  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
26  */

27 public abstract class AbstractVfsContainer
28     extends AbstractVfsComponent
29 {
30     /**
31      * The components contained by this component.
32      */

33     private final ArrayList JavaDoc components = new ArrayList JavaDoc();
34
35     /**
36      * Adds a sub-component to this component. If the sub-component implements
37      * {@link VfsComponent}, it is initialised. All sub-components are closed
38      * when this component is closed.
39      */

40     protected void addComponent(final Object JavaDoc component)
41         throws FileSystemException
42     {
43         if (!components.contains(component))
44         {
45             // Initialise
46
if (component instanceof VfsComponent)
47             {
48                 VfsComponent vfsComponent = (VfsComponent) component;
49                 vfsComponent.setLogger(getLogger());
50                 vfsComponent.setContext(getContext());
51                 vfsComponent.init();
52             }
53
54             // Keep track of component, to close it later
55
components.add(component);
56         }
57     }
58
59     /**
60      * Removes a sub-component from this component.
61      */

62     protected void removeComponent(final Object JavaDoc component)
63     {
64         components.remove(component);
65     }
66
67     /**
68      * Closes the sub-components of this component.
69      */

70     public void close()
71     {
72         // Close all components
73
final int count = components.size();
74         for (int i = 0; i < count; i++)
75         {
76             final Object JavaDoc component = components.get(i);
77             if (component instanceof VfsComponent)
78             {
79                 final VfsComponent vfsComponent = (VfsComponent) component;
80                 vfsComponent.close();
81             }
82         }
83         components.clear();
84     }
85 }
86
Popular Tags