KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileName;
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.commons.vfs.FileSystem;
21 import org.apache.commons.vfs.FileSystemConfigBuilder;
22 import org.apache.commons.vfs.FileSystemException;
23 import org.apache.commons.vfs.FileSystemOptions;
24 import org.apache.commons.vfs.provider.local.GenericFileNameParser;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.TreeMap JavaDoc;
29
30 /**
31  * A partial {@link FileProvider} implementation. Takes care of managing the
32  * file systems created by the provider.
33  *
34  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
35  */

36 public abstract class AbstractFileProvider
37     extends AbstractVfsContainer
38     implements FileProvider
39 {
40     private FileNameParser parser;
41
42     public AbstractFileProvider()
43     {
44         parser = GenericFileNameParser.getInstance();
45     }
46
47     protected FileNameParser getFileNameParser()
48     {
49         return parser;
50     }
51
52     protected void setFileNameParser(FileNameParser parser)
53     {
54         this.parser = parser;
55     }
56
57     /**
58      * The cached file systems. This is a mapping from root URI to
59      * FileSystem object.
60      */

61     // private final Map fileSystems = new HashMap();
62
private final Map JavaDoc fileSystems = new TreeMap JavaDoc();
63
64     /**
65      * Closes the file systems created by this provider.
66      */

67     public void close()
68     {
69         synchronized (fileSystems)
70         {
71             fileSystems.clear();
72         }
73
74         super.close();
75     }
76
77     /**
78      * Creates a layered file system. This method throws a 'not supported' exception.
79      */

80     public FileObject createFileSystem(final String JavaDoc scheme, final FileObject file, final FileSystemOptions properties)
81         throws FileSystemException
82     {
83         // Can't create a layered file system
84
throw new FileSystemException("vfs.provider/not-layered-fs.error", scheme);
85     }
86
87     /**
88      * Adds a file system to those cached by this provider. The file system
89      * may implement {@link VfsComponent}, in which case it is initialised.
90      */

91     protected void addFileSystem(final Comparable JavaDoc key, final FileSystem fs)
92         throws FileSystemException
93     {
94         // Add to the cache
95
addComponent(fs);
96
97         FileSystemKey treeKey = new FileSystemKey(key, fs.getFileSystemOptions());
98         ((AbstractFileSystem) fs).setCacheKey(treeKey);
99
100         synchronized (fileSystems)
101         {
102             fileSystems.put(treeKey, fs);
103         }
104     }
105
106     /**
107      * Locates a cached file system
108      *
109      * @return The provider, or null if it is not cached.
110      */

111     protected FileSystem findFileSystem(final Comparable JavaDoc key, final FileSystemOptions fileSystemProps)
112     {
113         FileSystemKey treeKey = new FileSystemKey(key, fileSystemProps);
114
115         synchronized (fileSystems)
116         {
117             return (FileSystem) fileSystems.get(treeKey);
118         }
119     }
120
121     public FileSystemConfigBuilder getConfigBuilder()
122     {
123         return null;
124     }
125
126     public void freeUnusedResources()
127     {
128         Object JavaDoc[] item;
129         synchronized (fileSystems)
130         {
131             item = fileSystems.values().toArray();
132         }
133         for (int i = 0; i < item.length; ++i)
134         {
135             AbstractFileSystem fs = (AbstractFileSystem) item[i];
136             if (fs.isReleaseable())
137             {
138                 fs.closeCommunicationLink();
139             }
140         }
141     }
142
143     public void closeFileSystem(final FileSystem filesystem)
144     {
145         AbstractFileSystem fs = (AbstractFileSystem) filesystem;
146
147         synchronized (fileSystems)
148         {
149             fileSystems.remove(fs.getCacheKey());
150         }
151
152         removeComponent(fs);
153         fs.close();
154     }
155
156     /**
157      * Parses an absolute URI.
158      *
159      * @param base The base file - if null the <code>uri</code> needs to be absolute
160      * @param uri The URI to parse.
161      */

162     public FileName parseUri(FileName base, String JavaDoc uri) throws FileSystemException
163     {
164         if (getFileNameParser() != null)
165         {
166             return getFileNameParser().parseUri(getContext(), base, uri);
167         }
168
169         throw new FileSystemException("vfs.provider/filename-parser-missing.error");
170         // return GenericFileName.parseUri(getFileNameParser(), uri, 0);
171
}
172 }
173
Popular Tags