KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > source > impl > CommonsVFSSource


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.source.impl;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.avalon.framework.logger.LogEnabled;
25 import org.apache.avalon.framework.logger.Logger;
26 import org.apache.commons.vfs.FileContent;
27 import org.apache.commons.vfs.FileObject;
28 import org.apache.commons.vfs.FileSystemException;
29 import org.apache.commons.vfs.FileSystemManager;
30 import org.apache.commons.vfs.VFS;
31 import org.apache.excalibur.source.ModifiableSource;
32 import org.apache.excalibur.source.SourceException;
33 import org.apache.excalibur.source.SourceUtil;
34
35 /**
36  * Source implementation that provides resolver access to all protocols
37  * supported by <a HREF="http://jakarta.apache.org/commons/sandbox/vfs">Commons VFS</a>.
38  *
39  * @author <a HREF="mailto:crafterm@apache.org">Marcus Crafter</a>
40  * @version $Revision:$
41  * @since Nov 19, 2004 10:54:02 AM
42  */

43 public class CommonsVFSSource extends AbstractSource
44     implements LogEnabled, ModifiableSource {
45     
46     /**
47      * Constructor, creates instance of class.
48      *
49      * @param location location to resolve
50      * @param parameters protocol specific parameters
51      * @throws FileSystemException if an error occurs
52      */

53     public CommonsVFSSource(final String JavaDoc location, final Map JavaDoc parameters)
54         throws FileSystemException {
55         m_location = location;
56         m_manager = VFS.getManager();
57         m_fileObject = m_manager.resolveFile(location); // REVISIT: parameters
58
m_fileContent = m_fileObject.getContent();
59     }
60     
61     /**
62      * Sets content information for this source.
63      *
64      * @see org.apache.excalibur.source.impl.AbstractSource#getInfos()
65      */

66     protected void getInfos() {
67         try {
68             setContentLength(m_fileContent.getSize());
69         } catch (final FileSystemException e) {
70             
71             if (getLogger().isWarnEnabled()) {
72                 getLogger().warn(
73                     "Unable to determine content length for " + m_location, e
74                 );
75             }
76             setContentLength(-1); // Source API says return -1 if unknown
77
}
78         
79         try {
80             setLastModified(m_fileContent.getLastModifiedTime());
81         } catch (final FileSystemException e) {
82
83             if (getLogger().isWarnEnabled()) {
84                 getLogger().warn(
85                     "Unable to determine last modified date for " + m_location, e
86                 );
87             }
88             setLastModified(0); // Source API says return 0 if unknown
89
}
90
91         setSystemId(m_location);
92         setScheme(SourceUtil.getScheme(m_location));
93     }
94     
95     /**
96      * Obtain an {@link InputStream} for this source.
97      *
98      * @throws IOException if an IO error occurs
99      * @throws SourceException if a source exception occurs
100      * @return {@link InputStream}
101      * @see org.apache.excalibur.source.Source#getInputStream()
102      */

103     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, SourceException {
104         return m_fileContent.getInputStream();
105     }
106
107     /**
108      * Whether this resource exists or not
109      *
110      * @return true if it does exist, false otherwise, false on error
111      * @see org.apache.excalibur.source.Source#exists()
112      */

113     public boolean exists() {
114         try {
115             return m_fileObject.exists();
116         } catch (final FileSystemException e) {
117
118             if (getLogger().isWarnEnabled()) {
119                 getLogger().warn("Unable to determine existence for " + m_location, e);
120             }
121             return false;
122         }
123     }
124     
125     // ModifiableSource methods
126

127     /**
128      * Whether we can cancel writing to the output stream
129      *
130      * @param stream stream to cancel
131      * @return true if we can cancel, false otherwise
132      * @see org.apache.excalibur.source.ModifiableSource#canCancel(java.io.OutputStream)
133      */

134     public boolean canCancel(final OutputStream JavaDoc stream) {
135         // VFS API doesn't support buffering of write streams till close() directly
136
return false;
137     }
138     
139     /**
140      * Cancels writing to the specified output stream.
141      *
142      * @param stream stream to cancel writing to
143      * @throws IOException if an error occurs
144      * @see org.apache.excalibur.source.ModifiableSource#cancel(java.io.OutputStream)
145      */

146     public void cancel(final OutputStream JavaDoc stream) throws IOException JavaDoc {
147         throw new IOException JavaDoc("Cancel() not implemented");
148     }
149     
150     /**
151      * Deletes the source.
152      *
153      * @throws SourceException if an error occurs
154      * @see org.apache.excalibur.source.ModifiableSource#delete()
155      */

156     public void delete() throws SourceException {
157         try {
158             m_fileObject.delete();
159         } catch (final FileSystemException e) {
160             throw new SourceException("Unable to delete resource: " + m_location, e);
161         }
162     }
163     
164     /**
165      * Obtain an {@link OutputStream} to the source
166      *
167      * @return an {@link OutputStream}
168      * @see org.apache.excalibur.source.ModifiableSource#getOutputStream()
169      */

170     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
171         return m_fileContent.getOutputStream();
172     }
173     
174     /**
175      * Enables logging for this source.
176      *
177      * @param logger {@link Logger} instance to use
178      * @see org.apache.avalon.framework.logger.LogEnabled
179      * #enableLogging(org.apache.avalon.framework.logger.Logger)
180      */

181     public void enableLogging(final Logger logger) {
182         m_logger = logger;
183     }
184     
185     /**
186      * Obtain access to this components logger.
187      *
188      * @return the logger
189      */

190     private Logger getLogger() {
191         return m_logger;
192     }
193
194     /** Resource location */
195     private final String JavaDoc m_location;
196     
197     /** {@link FileSystemManager} reference */
198     private final FileSystemManager m_manager;
199     
200     /** The resource itself */
201     private final FileObject m_fileObject;
202     
203     /** The content of the resource */
204     private final FileContent m_fileContent;
205     
206     /** Our logging target */
207     private Logger m_logger;
208 }
209
Popular Tags