001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.provider;
018:
019: import org.apache.commons.vfs.FileName;
020: import org.apache.commons.vfs.FileObject;
021: import org.apache.commons.vfs.FileSystem;
022: import org.apache.commons.vfs.FileSystemConfigBuilder;
023: import org.apache.commons.vfs.FileSystemException;
024: import org.apache.commons.vfs.FileSystemOptions;
025: import org.apache.commons.vfs.provider.local.GenericFileNameParser;
026:
027: import java.util.Map;
028: import java.util.TreeMap;
029:
030: /**
031: * A partial {@link FileProvider} implementation. Takes care of managing the
032: * file systems created by the provider.
033: *
034: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
035: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
036: */
037: public abstract class AbstractFileProvider extends AbstractVfsContainer
038: implements FileProvider {
039: private FileNameParser parser;
040:
041: public AbstractFileProvider() {
042: parser = GenericFileNameParser.getInstance();
043: }
044:
045: protected FileNameParser getFileNameParser() {
046: return parser;
047: }
048:
049: protected void setFileNameParser(FileNameParser parser) {
050: this .parser = parser;
051: }
052:
053: /**
054: * The cached file systems. This is a mapping from root URI to
055: * FileSystem object.
056: */
057: // private final Map fileSystems = new HashMap();
058: private final Map fileSystems = new TreeMap();
059:
060: /**
061: * Closes the file systems created by this provider.
062: */
063: public void close() {
064: synchronized (this ) {
065: fileSystems.clear();
066: }
067:
068: super .close();
069: }
070:
071: /**
072: * Creates a layered file system. This method throws a 'not supported' exception.
073: */
074: public FileObject createFileSystem(final String scheme,
075: final FileObject file, final FileSystemOptions properties)
076: throws FileSystemException {
077: // Can't create a layered file system
078: throw new FileSystemException(
079: "vfs.provider/not-layered-fs.error", scheme);
080: }
081:
082: /**
083: * Adds a file system to those cached by this provider. The file system
084: * may implement {@link VfsComponent}, in which case it is initialised.
085: */
086: protected void addFileSystem(final Comparable key,
087: final FileSystem fs) throws FileSystemException {
088: // Add to the cache
089: addComponent(fs);
090:
091: FileSystemKey treeKey = new FileSystemKey(key, fs
092: .getFileSystemOptions());
093: ((AbstractFileSystem) fs).setCacheKey(treeKey);
094:
095: synchronized (this ) {
096: fileSystems.put(treeKey, fs);
097: }
098: }
099:
100: /**
101: * Locates a cached file system
102: *
103: * @return The provider, or null if it is not cached.
104: */
105: protected FileSystem findFileSystem(final Comparable key,
106: final FileSystemOptions fileSystemProps) {
107: FileSystemKey treeKey = new FileSystemKey(key, fileSystemProps);
108:
109: synchronized (this ) {
110: return (FileSystem) fileSystems.get(treeKey);
111: }
112: }
113:
114: public FileSystemConfigBuilder getConfigBuilder() {
115: return null;
116: }
117:
118: public void freeUnusedResources() {
119: Object[] item;
120: synchronized (this ) {
121: item = fileSystems.values().toArray();
122: }
123: for (int i = 0; i < item.length; ++i) {
124: AbstractFileSystem fs = (AbstractFileSystem) item[i];
125: if (fs.isReleaseable()) {
126: fs.closeCommunicationLink();
127: }
128: }
129: }
130:
131: public void closeFileSystem(final FileSystem filesystem) {
132: AbstractFileSystem fs = (AbstractFileSystem) filesystem;
133:
134: synchronized (this ) {
135: if (fs.getCacheKey() != null) {
136: fileSystems.remove(fs.getCacheKey());
137: }
138: }
139:
140: removeComponent(fs);
141: fs.close();
142: }
143:
144: /**
145: * Parses an absolute URI.
146: *
147: * @param base The base file - if null the <code>uri</code> needs to be absolute
148: * @param uri The URI to parse.
149: */
150: public FileName parseUri(FileName base, String uri)
151: throws FileSystemException {
152: if (getFileNameParser() != null) {
153: return getFileNameParser()
154: .parseUri(getContext(), base, uri);
155: }
156:
157: throw new FileSystemException(
158: "vfs.provider/filename-parser-missing.error");
159: // return GenericFileName.parseUri(getFileNameParser(), uri, 0);
160: }
161: }
|