PageRenderTime 50ms CodeModel.GetById 2ms app.highlight 44ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/shims/src/0.20/java/org/apache/hadoop/fs/ProxyFileSystem.java

#
Java | 268 lines | 184 code | 55 blank | 29 comment | 1 complexity | 4c5624b015265fccc9e391aa76b42634 MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19package org.apache.hadoop.fs;
 20
 21import java.io.*;
 22import java.net.URI;
 23import java.net.URISyntaxException;
 24
 25import org.apache.hadoop.conf.Configuration;
 26import org.apache.hadoop.fs.permission.FsPermission;
 27import org.apache.hadoop.util.Progressable;
 28
 29/****************************************************************
 30 * A FileSystem that can serve a given scheme/authority using some
 31 * other file system. In that sense, it serves as a proxy for the
 32 * real/underlying file system
 33 *****************************************************************/
 34
 35public class ProxyFileSystem extends FilterFileSystem {
 36
 37  protected String myScheme;
 38  protected String myAuthority;
 39  protected URI myUri;
 40
 41  protected String realScheme;
 42  protected String realAuthority;
 43  protected URI realUri;
 44
 45  
 46
 47  private Path swizzleParamPath(Path p) {
 48    return new Path (realScheme, realAuthority, p.toUri().getPath());
 49  }
 50
 51  private Path swizzleReturnPath(Path p) {
 52    return new Path (myScheme, myAuthority, p.toUri().getPath());
 53  }
 54
 55  private FileStatus swizzleFileStatus(FileStatus orig, boolean isParam) {
 56    FileStatus ret =
 57      new FileStatus(orig.getLen(), orig.isDir(), orig.getReplication(),
 58                     orig.getBlockSize(), orig.getModificationTime(),
 59                     orig.getAccessTime(), orig.getPermission(),
 60                     orig.getOwner(), orig.getGroup(),
 61                     isParam ? swizzleParamPath(orig.getPath()) :
 62                     swizzleReturnPath(orig.getPath()));
 63    return ret;
 64  }
 65
 66  public ProxyFileSystem() {
 67    throw new RuntimeException ("Unsupported constructor");
 68  }
 69  
 70  public ProxyFileSystem(FileSystem fs) {
 71    throw new RuntimeException ("Unsupported constructor");
 72  }
 73
 74  /**
 75   * Create a proxy file system for fs.
 76   * 
 77   * @param fs FileSystem to create proxy for
 78   * @param myUri URI to use as proxy. Only the scheme and authority from
 79   *              this are used right now
 80   */
 81  public ProxyFileSystem(FileSystem fs, URI myUri) {
 82    super(fs);
 83
 84    URI realUri = fs.getUri();
 85    this.realScheme = realUri.getScheme();
 86    this.realAuthority=realUri.getAuthority();
 87    this.realUri = realUri;
 88
 89    this.myScheme = myUri.getScheme();
 90    this.myAuthority=myUri.getAuthority();
 91    this.myUri = myUri;
 92  }
 93
 94  public void initialize(URI name, Configuration conf) throws IOException {
 95    try {
 96      URI realUri = new URI (realScheme, realAuthority,
 97                            name.getPath(), name.getQuery(), name.getFragment());
 98      super.initialize(realUri, conf);
 99    } catch (URISyntaxException e) {
100      throw new RuntimeException(e);
101    }
102  }
103
104  public URI getUri() {
105    return myUri;
106  }
107
108  public String getName() {
109    return getUri().toString();
110  }
111
112  public Path makeQualified(Path path) {
113    return swizzleReturnPath(super.makeQualified(swizzleParamPath(path)));
114  }
115
116
117  protected void checkPath(Path path) {
118    super.checkPath(swizzleParamPath(path));
119  }
120
121  public BlockLocation[] getFileBlockLocations(FileStatus file, long start,
122    long len) throws IOException {
123    return super.getFileBlockLocations(swizzleFileStatus(file, true),
124                                       start, len);
125  }
126
127  public FSDataInputStream open(Path f, int bufferSize) throws IOException {
128    return super.open(swizzleParamPath(f), bufferSize);
129  }
130
131  public FSDataOutputStream append(Path f, int bufferSize,
132      Progressable progress) throws IOException {
133    return super.append(swizzleParamPath(f), bufferSize, progress);
134  }
135
136  public FSDataOutputStream create(Path f, FsPermission permission,
137      boolean overwrite, int bufferSize, short replication, long blockSize,
138      Progressable progress) throws IOException {
139    return super.create(swizzleParamPath(f), permission,
140        overwrite, bufferSize, replication, blockSize, progress);
141  }
142
143  public boolean setReplication(Path src, short replication) throws IOException {
144    return super.setReplication(swizzleParamPath(src), replication);
145  }
146
147  public boolean rename(Path src, Path dst) throws IOException {
148    return super.rename(swizzleParamPath(src), swizzleParamPath(dst));
149  }
150  
151  public boolean delete(Path f, boolean recursive) throws IOException {
152    return super.delete(swizzleParamPath(f), recursive);
153  }
154
155  public boolean deleteOnExit(Path f) throws IOException {
156    return super.deleteOnExit(swizzleParamPath(f));
157  }    
158    
159  public FileStatus[] listStatus(Path f) throws IOException {
160    FileStatus[] orig = super.listStatus(swizzleParamPath(f));
161    FileStatus[] ret = new FileStatus [orig.length];
162    for (int i=0; i<orig.length; i++) {
163      ret[i] = swizzleFileStatus(orig[i], false);
164    }
165    return ret;
166  }
167  
168  public Path getHomeDirectory() {
169    return swizzleReturnPath(super.getHomeDirectory());
170  }
171
172  public void setWorkingDirectory(Path newDir) {
173    super.setWorkingDirectory(swizzleParamPath(newDir));
174  }
175  
176  public Path getWorkingDirectory() {
177    return swizzleReturnPath(super.getWorkingDirectory());
178  }
179  
180  public boolean mkdirs(Path f, FsPermission permission) throws IOException {
181    return super.mkdirs(swizzleParamPath(f), permission);
182  }
183
184  public void copyFromLocalFile(boolean delSrc, Path src, Path dst)
185    throws IOException {
186    super.copyFromLocalFile(delSrc, swizzleParamPath(src), swizzleParamPath(dst));
187  }
188
189  public void copyFromLocalFile(boolean delSrc, boolean overwrite, 
190                                Path[] srcs, Path dst)
191    throws IOException {
192    super.copyFromLocalFile(delSrc, overwrite, srcs, swizzleParamPath(dst));
193  }
194  
195  public void copyFromLocalFile(boolean delSrc, boolean overwrite, 
196                                Path src, Path dst)
197    throws IOException {
198    super.copyFromLocalFile(delSrc, overwrite, src, swizzleParamPath(dst));
199  }
200
201  public void copyToLocalFile(boolean delSrc, Path src, Path dst)
202    throws IOException {
203    super.copyToLocalFile(delSrc, swizzleParamPath(src), dst);
204  }
205
206  public Path startLocalOutput(Path fsOutputFile, Path tmpLocalFile)
207    throws IOException {
208    return super.startLocalOutput(swizzleParamPath(fsOutputFile), tmpLocalFile);
209  }
210
211  public void completeLocalOutput(Path fsOutputFile, Path tmpLocalFile)
212    throws IOException {
213    super.completeLocalOutput(swizzleParamPath(fsOutputFile), tmpLocalFile);
214  }
215
216  public long getUsed() throws IOException{
217    return super.getUsed();
218  }
219  
220  public long getDefaultBlockSize() {
221    return super.getDefaultBlockSize();
222  }
223  
224  public short getDefaultReplication() {
225    return super.getDefaultReplication();
226  }
227
228  public ContentSummary getContentSummary(Path f) throws IOException {
229    return super.getContentSummary(swizzleParamPath(f));
230  }
231
232  public FileStatus getFileStatus(Path f) throws IOException {
233    return swizzleFileStatus(super.getFileStatus(swizzleParamPath(f)), false);
234  }
235
236  public FileChecksum getFileChecksum(Path f) throws IOException {
237    return super.getFileChecksum(swizzleParamPath(f));
238  }
239  
240  public void setVerifyChecksum(boolean verifyChecksum) {
241    super.setVerifyChecksum(verifyChecksum);
242  }
243
244  public Configuration getConf() {
245    return super.getConf();
246  }
247  
248  public void close() throws IOException {
249    super.close();
250    super.close();
251  }
252
253  public void setOwner(Path p, String username, String groupname
254      ) throws IOException {
255    super.setOwner(swizzleParamPath(p), username, groupname);
256  }
257
258  public void setTimes(Path p, long mtime, long atime
259      ) throws IOException {
260    super.setTimes(swizzleParamPath(p), mtime, atime);
261  }
262
263  public void setPermission(Path p, FsPermission permission
264      ) throws IOException {
265    super.setPermission(swizzleParamPath(p), permission);
266  }
267}
268