View Javadoc
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.commons.vfs2.provider.hdfs;
18  
19  import java.io.FileNotFoundException;
20  import java.io.InputStream;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.vfs2.FileNotFolderException;
25  import org.apache.commons.vfs2.FileObject;
26  import org.apache.commons.vfs2.FileSystemException;
27  import org.apache.commons.vfs2.FileType;
28  import org.apache.commons.vfs2.RandomAccessContent;
29  import org.apache.commons.vfs2.provider.AbstractFileName;
30  import org.apache.commons.vfs2.provider.AbstractFileObject;
31  import org.apache.commons.vfs2.util.RandomAccessMode;
32  import org.apache.hadoop.fs.FileStatus;
33  import org.apache.hadoop.fs.FileSystem;
34  import org.apache.hadoop.fs.Path;
35  
36  /**
37   * A VFS representation of an HDFS file.
38   *
39   * @since 2.1
40   */
41  public class HdfsFileObject extends AbstractFileObject<HdfsFileSystem>
42  {
43      private final HdfsFileSystem fs;
44      private final FileSystem hdfs;
45      private final Path path;
46      private FileStatus stat;
47  
48      /**
49       * Constructs a new HDFS FileObject
50       *
51       * @param name
52       *            FileName
53       * @param fs
54       *            HdfsFileSystem instance
55       * @param hdfs
56       *            Hadoop FileSystem instance
57       * @param p
58       *            Path to the file in HDFS
59       */
60      protected HdfsFileObject(final AbstractFileName name, final HdfsFileSystem fs, final FileSystem hdfs, final Path p)
61      {
62          super(name, fs);
63          this.fs = fs;
64          this.hdfs = hdfs;
65          this.path = p;
66      }
67  
68      /**
69       * @see org.apache.commons.vfs2.provider.AbstractFileObject#canRenameTo(org.apache.commons.vfs2.FileObject)
70       */
71      @Override
72      public boolean canRenameTo(final FileObject newfile)
73      {
74          throw new UnsupportedOperationException();
75      }
76  
77      /**
78       * @see org.apache.commons.vfs2.provider.AbstractFileObject#doAttach()
79       */
80      @Override
81      protected void doAttach() throws Exception
82      {
83          try
84          {
85              this.stat = this.hdfs.getFileStatus(this.path);
86          }
87          catch (final FileNotFoundException e)
88          {
89              this.stat = null;
90              return;
91          }
92      }
93  
94      /**
95       * @see org.apache.commons.vfs2.provider.AbstractFileObject#doGetAttributes()
96       */
97      @Override
98      protected Map<String, Object> doGetAttributes() throws Exception
99      {
100         if (null == this.stat)
101         {
102             return super.doGetAttributes();
103         }
104         else
105         {
106             final Map<String, Object> attrs = new HashMap<String, Object>();
107             attrs.put(HdfsFileAttributes.LAST_ACCESS_TIME.toString(), this.stat.getAccessTime());
108             attrs.put(HdfsFileAttributes.BLOCK_SIZE.toString(), this.stat.getBlockSize());
109             attrs.put(HdfsFileAttributes.GROUP.toString(), this.stat.getGroup());
110             attrs.put(HdfsFileAttributes.OWNER.toString(), this.stat.getOwner());
111             attrs.put(HdfsFileAttributes.PERMISSIONS.toString(), this.stat.getPermission().toString());
112             attrs.put(HdfsFileAttributes.LENGTH.toString(), this.stat.getLen());
113             attrs.put(HdfsFileAttributes.MODIFICATION_TIME.toString(), this.stat.getModificationTime());
114             return attrs;
115         }
116     }
117 
118     /**
119      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doGetContentSize()
120      */
121     @Override
122     protected long doGetContentSize() throws Exception
123     {
124         return stat.getLen();
125     }
126 
127     /**
128      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doGetInputStream()
129      */
130     @Override
131     protected InputStream doGetInputStream() throws Exception
132     {
133         return this.hdfs.open(this.path);
134     }
135 
136     /**
137      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doGetLastModifiedTime()
138      */
139     @Override
140     protected long doGetLastModifiedTime() throws Exception
141     {
142         if (null != this.stat)
143         {
144             return this.stat.getModificationTime();
145         }
146         else
147         {
148             return -1;
149         }
150     }
151 
152     /**
153      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doGetRandomAccessContent
154      *      (org.apache.commons.vfs2.util.RandomAccessMode)
155      */
156     @Override
157     protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception
158     {
159         if (mode.equals(RandomAccessMode.READWRITE))
160         {
161             throw new UnsupportedOperationException();
162         }
163         return new HdfsRandomAccessContent(this.path, this.hdfs);
164     }
165 
166     /**
167      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doGetType()
168      */
169     @Override
170     protected FileType doGetType() throws Exception
171     {
172         try
173         {
174             doAttach();
175             if (null == stat)
176             {
177                 return FileType.IMAGINARY;
178             }
179             if (stat.isDir())
180             {
181                 return FileType.FOLDER;
182             }
183             else
184             {
185                 return FileType.FILE;
186             }
187         }
188         catch (final FileNotFoundException fnfe)
189         {
190             return FileType.IMAGINARY;
191         }
192     }
193 
194     /**
195      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doIsHidden()
196      */
197     @Override
198     protected boolean doIsHidden() throws Exception
199     {
200         return false;
201     }
202 
203     /**
204      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doIsReadable()
205      */
206     @Override
207     protected boolean doIsReadable() throws Exception
208     {
209         return true;
210     }
211 
212     /**
213      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doIsWriteable()
214      */
215     @Override
216     protected boolean doIsWriteable() throws Exception
217     {
218         return false;
219     }
220 
221     /**
222      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doListChildren()
223      */
224     @Override
225     protected String[] doListChildren() throws Exception
226     {
227         if (this.doGetType() != FileType.FOLDER)
228         {
229             throw new FileNotFolderException(this);
230         }
231 
232         final FileStatus[] files = this.hdfs.listStatus(this.path);
233         final String[] children = new String[files.length];
234         int i = 0;
235         for (final FileStatus status : files)
236         {
237             children[i++] = status.getPath().getName();
238         }
239         return children;
240     }
241 
242     /**
243      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doListChildrenResolved()
244      */
245     @Override
246     protected FileObject[] doListChildrenResolved() throws Exception
247     {
248         if (this.doGetType() != FileType.FOLDER)
249         {
250             return null;
251         }
252         final String[] children = doListChildren();
253         final FileObject[] fo = new FileObject[children.length];
254         for (int i = 0; i < children.length; i++)
255         {
256             final Path p = new Path(this.path, children[i]);
257             fo[i] = this.fs.resolveFile(p.toUri().toString());
258         }
259         return fo;
260     }
261 
262     /**
263      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doRemoveAttribute(java.lang.String)
264      */
265     @Override
266     protected void doRemoveAttribute(final String attrName) throws Exception
267     {
268         throw new UnsupportedOperationException();
269     }
270 
271     /**
272      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doSetAttribute(java.lang.String, java.lang.Object)
273      */
274     @Override
275     protected void doSetAttribute(final String attrName, final Object value) throws Exception
276     {
277         throw new UnsupportedOperationException();
278     }
279 
280     /**
281      * @see org.apache.commons.vfs2.provider.AbstractFileObject#doSetLastModifiedTime(long)
282      */
283     @Override
284     protected boolean doSetLastModifiedTime(final long modtime) throws Exception
285     {
286         throw new UnsupportedOperationException();
287     }
288 
289     /**
290      * @see org.apache.commons.vfs2.provider.AbstractFileObject#exists()
291      * @return boolean true if file exists, false if not
292      */
293     @Override
294     public boolean exists() throws FileSystemException
295     {
296         try
297         {
298             doAttach();
299             return this.stat != null;
300         }
301         catch (final FileNotFoundException fne)
302         {
303             return false;
304         }
305         catch (final Exception e)
306         {
307             throw new FileSystemException("Unable to check existance ", e);
308         }
309     }
310 
311 }