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.temp;
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.FileSystemException;
023: import org.apache.commons.vfs.FileSystemOptions;
024: import org.apache.commons.vfs.FileType;
025: import org.apache.commons.vfs.provider.AbstractFileProvider;
026: import org.apache.commons.vfs.provider.FileProvider;
027: import org.apache.commons.vfs.provider.UriParser;
028: import org.apache.commons.vfs.provider.local.DefaultLocalFileProvider;
029: import org.apache.commons.vfs.provider.local.LocalFileSystem;
030:
031: import java.io.File;
032: import java.util.Collection;
033:
034: /**
035: * A provider for temporary files.
036: *
037: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
038: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
039: */
040: public class TemporaryFileProvider extends AbstractFileProvider
041: implements FileProvider, Comparable {
042: private File rootFile;
043:
044: /*
045: private final static FileName tmpFileName = new AbstractFileName("tmp", "/")
046: {
047: protected FileName createName(String absPath)
048: {
049: return null;
050: }
051:
052: protected void appendRootUri(StringBuffer buffer)
053: {
054: }
055: };
056: */
057:
058: public TemporaryFileProvider(final File rootFile) {
059: this ();
060:
061: this .rootFile = rootFile;
062: }
063:
064: public TemporaryFileProvider() {
065: super ();
066: }
067:
068: public int compareTo(Object o) {
069: int h1 = hashCode();
070: int h2 = o.hashCode();
071: if (h1 < h2) {
072: return -1;
073: }
074: if (h1 > h2) {
075: return 1;
076: }
077:
078: return 0;
079: }
080:
081: /**
082: * Locates a file object, by absolute URI.
083: */
084: public synchronized FileObject findFile(final FileObject baseFile,
085: final String uri, final FileSystemOptions properties)
086: throws FileSystemException {
087: // Parse the name
088: final StringBuffer buffer = new StringBuffer(uri);
089: final String scheme = UriParser.extractScheme(uri, buffer);
090:
091: UriParser.fixSeparators(buffer);
092:
093: FileType fileType = UriParser.normalisePath(buffer);
094: final String path = buffer.toString();
095:
096: // Create the temp file system if it does not exist
097: // FileSystem filesystem = findFileSystem( this, (Properties) null);
098: FileSystem filesystem = findFileSystem(this , properties);
099: if (filesystem == null) {
100: if (rootFile == null) {
101: rootFile = getContext().getTemporaryFileStore()
102: .allocateFile("tempfs");
103: }
104: final FileName rootName = getContext().parseURI(
105: scheme + ":" + FileName.ROOT_PATH);
106: // final FileName rootName =
107: // new LocalFileName(scheme, scheme + ":", FileName.ROOT_PATH);
108: filesystem = new LocalFileSystem(rootName, rootFile
109: .getAbsolutePath(), properties);
110: addFileSystem(this , filesystem);
111: }
112:
113: // Find the file
114: return filesystem.resolveFile(path);
115: }
116:
117: public Collection getCapabilities() {
118: return DefaultLocalFileProvider.capabilities;
119: }
120: }
|