KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > url > UrlFileProvider


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
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 package org.apache.commons.vfs.provider.url;
17
18 import org.apache.commons.vfs.Capability;
19 import org.apache.commons.vfs.FileName;
20 import org.apache.commons.vfs.FileObject;
21 import org.apache.commons.vfs.FileSystem;
22 import org.apache.commons.vfs.FileSystemConfigBuilder;
23 import org.apache.commons.vfs.FileSystemException;
24 import org.apache.commons.vfs.FileSystemOptions;
25 import org.apache.commons.vfs.provider.AbstractFileProvider;
26
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Collections JavaDoc;
32
33 /**
34  * A file provider backed by Java's URL API.
35  *
36  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
37  */

38 public class UrlFileProvider
39     extends AbstractFileProvider
40 {
41     protected final static Collection JavaDoc capabilities = Collections.unmodifiableCollection(Arrays.asList(new Capability[]
42     {
43         Capability.READ_CONTENT,
44         Capability.URI,
45         Capability.GET_LAST_MODIFIED
46     }));
47
48     public UrlFileProvider()
49     {
50         super();
51         setFileNameParser(new UrlFileNameParser());
52     }
53
54     /**
55      * Locates a file object, by absolute URI.
56      */

57     public synchronized FileObject findFile(final FileObject baseFile,
58                                             final String JavaDoc uri,
59                                             final FileSystemOptions fileSystemOptions)
60         throws FileSystemException
61     {
62         try
63         {
64             final URL JavaDoc url = new URL JavaDoc(uri);
65
66             URL JavaDoc rootUrl = new URL JavaDoc(url, "/");
67             final String JavaDoc key = this.getClass().getName() + rootUrl.toString();
68             FileSystem fs = findFileSystem(key, fileSystemOptions);
69             if (fs == null)
70             {
71                 String JavaDoc extForm = rootUrl.toExternalForm();
72                 final FileName rootName =
73                     getContext().parseURI(extForm);
74                 // final FileName rootName =
75
// new BasicFileName(rootUrl, FileName.ROOT_PATH);
76
fs = new UrlFileSystem(rootName, fileSystemOptions);
77                 addFileSystem(key, fs);
78             }
79             return fs.resolveFile(url.getPath());
80         }
81         catch (final MalformedURLException JavaDoc e)
82         {
83             throw new FileSystemException("vfs.provider.url/badly-formed-uri.error", uri, e);
84         }
85     }
86
87     public FileSystemConfigBuilder getConfigBuilder()
88     {
89         return org.apache.commons.vfs.provider.res.ResourceFileSystemConfigBuilder.getInstance();
90     }
91
92     public Collection JavaDoc getCapabilities()
93     {
94         return capabilities;
95     }
96 }
97
Popular Tags