]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/vfs/VfsResource.java
Download process log
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / vfs / VfsResource.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.vfs;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.net.URL;
25
26 import org.apache.commons.io.IOExceptionWithCause;
27 import org.apache.commons.vfs.FileObject;
28 import org.apache.commons.vfs.FileSystemException;
29 import org.apache.commons.vfs.NameScope;
30 import org.argeo.slc.SlcException;
31 import org.springframework.core.io.Resource;
32
33 public class VfsResource implements Resource {
34 private FileObject fileObject;
35
36 public VfsResource(FileObject fileObject) {
37 this.fileObject = fileObject;
38 }
39
40 public Resource createRelative(String relativePath) throws IOException {
41 return new VfsResource(fileObject.resolveFile(relativePath,
42 NameScope.DESCENDENT_OR_SELF));
43 }
44
45 public boolean exists() {
46 try {
47 return fileObject.exists();
48 } catch (FileSystemException e) {
49 throw new SlcException("Cannot find out whether " + fileObject
50 + " exists", e);
51 }
52 }
53
54 public String getDescription() {
55 return "VFS resource " + fileObject;
56 }
57
58 public File getFile() throws IOException {
59 throw new IOException("Cannot access " + getDescription()
60 + " as a local file");
61 // TODO: access local files
62 // if(fileObject instanceof LocalFile){
63 // ((LocalFile)fileObject).
64 // }
65 // return null;
66 }
67
68 public String getFilename() {
69 return fileObject.getName().getBaseName();
70 }
71
72 public URI getURI() throws IOException {
73 try {
74 return new URI(fileObject.getName().getURI());
75 } catch (URISyntaxException e) {
76 throw new IOExceptionWithCause(e);
77 }
78 }
79
80 public URL getURL() throws IOException {
81 return fileObject.getURL();
82 }
83
84 public boolean isOpen() {
85 return fileObject.isContentOpen();
86 }
87
88 public boolean isReadable() {
89 try {
90 return fileObject.isReadable();
91 } catch (FileSystemException e) {
92 throw new SlcException("Cannot find out whether " + fileObject
93 + " is readable", e);
94 }
95 }
96
97 public long lastModified() throws IOException {
98 return fileObject.getContent().getLastModifiedTime();
99 }
100
101 public InputStream getInputStream() throws IOException {
102 return fileObject.getContent().getInputStream();
103 }
104
105 public FileObject getFileObject() {
106 return fileObject;
107 }
108
109 }