KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > repository > vfs > VfsResource


1 /**
2  * This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
3  * Copyright Jayasoft 2005 - All rights reserved
4  *
5  * VFS implementation of the Resource interface
6  *
7  * @author glen
8  * @author Matt Inger
9  * @author Stephen Nesbitt
10  *
11  */

12 package fr.jayasoft.ivy.repository.vfs;
13
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.apache.commons.vfs.FileContent;
20 import org.apache.commons.vfs.FileObject;
21 import org.apache.commons.vfs.FileSystemException;
22 import org.apache.commons.vfs.FileSystemManager;
23 import org.apache.commons.vfs.FileType;
24
25 import fr.jayasoft.ivy.repository.Resource;
26 import fr.jayasoft.ivy.util.Message;
27 import fr.jayasoft.ivy.resolver.VfsResolver;
28
29 public class VfsResource implements Resource {
30     private String JavaDoc _vfsURI;
31     private FileSystemManager _fsManager;
32     
33     private transient boolean _init = false;
34     private transient boolean _exists;
35     private transient long _lastModified;
36     private transient long _contentLength;
37
38     private transient FileContent _content = null;
39     private transient FileObject _resourceImpl;
40     
41     // Constructor
42
public VfsResource(String JavaDoc vfsURI, FileSystemManager fsManager) {
43         this._vfsURI = vfsURI;
44         this._fsManager = fsManager;
45         this._init = false;
46     }
47     
48     private void init() {
49         if (!_init) {
50             try {
51                 _resourceImpl = _fsManager.resolveFile(_vfsURI);
52                 _content = _resourceImpl.getContent();
53                 
54                 _exists = _resourceImpl.exists();
55                 _lastModified = _content.getLastModifiedTime();
56                 _contentLength = _content.getSize();
57             } catch (FileSystemException e) {
58                 Message.verbose(e.getLocalizedMessage());
59                 _exists = false;
60                 _lastModified = 0;
61                 _contentLength = 0;
62             }
63             
64             _init = true;
65         }
66     }
67     
68     /**
69      * Get a list of direct descendents of the given resource.
70      * Note that attempts to get a list of children does <emphasize>not</emphasize>
71      * result in an error. Instead an error message is logged and an empty ArrayList returned.
72      *
73      * @return A <code>ArrayList</code> of VFSResources
74      *
75      */

76     public List JavaDoc getChildren() {
77         init();
78         ArrayList JavaDoc list = new ArrayList JavaDoc();
79         try {
80             if ((_resourceImpl != null) && _resourceImpl.exists() && (_resourceImpl.getType() == FileType.FOLDER)) {
81                 FileObject[] children = _resourceImpl.getChildren();
82                 for (int i = 0; i < children.length; i++) {
83                     FileObject child = children[i];
84                     list.add(normalize(child.getName().getURI()));
85                 }
86             }
87         } catch (IOException JavaDoc e) {
88             Message.verbose(e.getLocalizedMessage());
89         }
90         return list;
91     }
92     
93     public FileContent getContent() {
94         init();
95         return _content;
96     }
97     
98      /**
99      * Get the name of the resource.
100      *
101      * @return a <code>String</code> representing the Resource URL.
102      */

103     public String JavaDoc getName() {
104         return normalize(_vfsURI);
105     }
106     
107     public Resource clone(String JavaDoc cloneName) {
108         return new VfsResource(cloneName, _fsManager);
109     }
110     
111     /**
112      * The VFS FileName getURI method seems to have a bug in it where
113      * file: URIs will have 4 forward slashes instead of 3.
114      *
115      * @param vfsURI
116      * @return a normalized <class>String</class> representing the VFS URI
117      */

118     public static String JavaDoc normalize(String JavaDoc vfsURI) {
119         if (vfsURI == null) {
120             return "";
121         }
122         
123         if (vfsURI.startsWith("file:////")) {
124             vfsURI = vfsURI.replaceFirst("////", "///");
125         }
126         return vfsURI;
127     }
128
129     /**
130      * Get the last modification time of the resource.
131      *
132      * @return a <code>long</code> indicating last modified time.
133      */

134      public long getLastModified() {
135          init();
136          return _lastModified;
137     }
138
139      /**
140       * Get the size of the resource
141       *
142       * @return a <code>long</code> representing the size of the resource (in bytes).
143       */

144     public long getContentLength() {
145         init();
146         return _contentLength;
147     }
148
149     /**
150      * Flag indicating whether a resource is available for querying
151      *
152      * @return <code>true</code> if the resource is available for querying,
153      * <code>false</code> otherwise.
154      */

155     public boolean exists() {
156         init();
157         return _exists;
158     }
159     
160     /**
161      * Return a flag indicating whether a provided VFS resource physically exists
162      *
163      * @return <code>true</code> if the resource physically exists, <code>false</code>
164      * otherwise.
165      */

166      public boolean physicallyExists() {
167          // TODO: there is no need for this method anymore, replace it by calling exists();
168
init();
169         
170         try {
171             return _resourceImpl.exists();
172             // originally I only checked for a FileSystemException. I expanded it to
173
// include all exceptions when I found it would throw a NPE exception when the query was
174
// run on non-wellformed VFS URI.
175
} catch (Exception JavaDoc e) {
176             Message.verbose(e.getLocalizedMessage());
177             return false;
178         }
179     }
180
181     public String JavaDoc toString() {
182         return VfsResolver.prepareForDisplay(getName());
183     }
184
185     public boolean isLocal() {
186         return getName().startsWith("file:");
187     }
188
189     public InputStream JavaDoc openStream() throws IOException JavaDoc {
190         return getContent().getInputStream();
191     }
192 }
193
Popular Tags