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 */ 017package org.apache.commons.vfs2.provider.temp; 018 019import java.io.File; 020import java.util.Collection; 021 022import org.apache.commons.vfs2.Capability; 023import org.apache.commons.vfs2.FileName; 024import org.apache.commons.vfs2.FileObject; 025import org.apache.commons.vfs2.FileSystem; 026import org.apache.commons.vfs2.FileSystemException; 027import org.apache.commons.vfs2.FileSystemOptions; 028import org.apache.commons.vfs2.provider.AbstractFileProvider; 029import org.apache.commons.vfs2.provider.UriParser; 030import org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider; 031import org.apache.commons.vfs2.provider.local.LocalFileSystem; 032 033/** 034 * A provider for temporary files. 035 */ 036public class TemporaryFileProvider extends AbstractFileProvider implements Comparable<Object> { 037 038 private File rootFile; 039 040 /* 041 * private final static FileName tmpFileName = new AbstractFileName("tmp", "/") { protected FileName 042 * createName(String absPath) { return null; } 043 * 044 * protected void appendRootUri(StringBuffer buffer) { } }; 045 */ 046 047 /** 048 * Constructs a new instance. 049 */ 050 public TemporaryFileProvider() { 051 } 052 053 /** 054 * Constructs a new instance. 055 * 056 * @param rootFile The root file. 057 */ 058 public TemporaryFileProvider(final File rootFile) { 059 this(); 060 this.rootFile = rootFile; 061 } 062 063 @Override 064 public int compareTo(final Object o) { 065 final int h1 = hashCode(); 066 final int h2 = o.hashCode(); 067 return Integer.compare(h1, h2); 068 } 069 070 /** 071 * Locates a file object, by absolute URI. 072 * 073 * @param baseFile The base FileObject. 074 * @param uri The URI of the file to be located. 075 * @param fileSystemOptions FileSystemOptions to use to locate or create the file. 076 * @return The FileObject. 077 * @throws FileSystemException if an error occurs. 078 */ 079 @Override 080 public synchronized FileObject findFile(final FileObject baseFile, final String uri, 081 final FileSystemOptions fileSystemOptions) throws FileSystemException { 082 // Parse the name 083 final StringBuilder buffer = new StringBuilder(uri); 084 final String scheme = UriParser.extractScheme(getContext().getFileSystemManager().getSchemes(), uri, buffer); 085 UriParser.fixSeparators(buffer); 086 UriParser.normalisePath(buffer); 087 final String path = buffer.toString(); 088 089 // Create the temp file system if it does not exist 090 // FileSystem filesystem = findFileSystem( this, (Properties) null); 091 FileSystem filesystem = findFileSystem(this, fileSystemOptions); 092 if (filesystem == null) { 093 if (rootFile == null) { 094 rootFile = getContext().getTemporaryFileStore().allocateFile("tempfs"); 095 } 096 final FileName rootName = getContext().parseURI(scheme + ":" + FileName.ROOT_PATH); 097 // final FileName rootName = 098 // new LocalFileName(scheme, scheme + ":", FileName.ROOT_PATH); 099 filesystem = new LocalFileSystem(rootName, rootFile.getAbsolutePath(), fileSystemOptions); 100 addFileSystem(this, filesystem); 101 } 102 103 // Find the file 104 return filesystem.resolveFile(path); 105 } 106 107 @Override 108 public Collection<Capability> getCapabilities() { 109 return DefaultLocalFileProvider.capabilities; 110 } 111}