PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.fs;
  19. import java.io.*;
  20. import java.net.URI;
  21. import java.net.URISyntaxException;
  22. import org.apache.hadoop.conf.Configuration;
  23. import org.apache.hadoop.fs.permission.FsPermission;
  24. import org.apache.hadoop.util.Progressable;
  25. /****************************************************************
  26. * A FileSystem that can serve a given scheme/authority using some
  27. * other file system. In that sense, it serves as a proxy for the
  28. * real/underlying file system
  29. *****************************************************************/
  30. public class ProxyFileSystem extends FilterFileSystem {
  31. protected String myScheme;
  32. protected String myAuthority;
  33. protected URI myUri;
  34. protected String realScheme;
  35. protected String realAuthority;
  36. protected URI realUri;
  37. private Path swizzleParamPath(Path p) {
  38. return new Path (realScheme, realAuthority, p.toUri().getPath());
  39. }
  40. private Path swizzleReturnPath(Path p) {
  41. return new Path (myScheme, myAuthority, p.toUri().getPath());
  42. }
  43. private FileStatus swizzleFileStatus(FileStatus orig, boolean isParam) {
  44. FileStatus ret =
  45. new FileStatus(orig.getLen(), orig.isDir(), orig.getReplication(),
  46. orig.getBlockSize(), orig.getModificationTime(),
  47. orig.getAccessTime(), orig.getPermission(),
  48. orig.getOwner(), orig.getGroup(),
  49. isParam ? swizzleParamPath(orig.getPath()) :
  50. swizzleReturnPath(orig.getPath()));
  51. return ret;
  52. }
  53. public ProxyFileSystem() {
  54. throw new RuntimeException ("Unsupported constructor");
  55. }
  56. public ProxyFileSystem(FileSystem fs) {
  57. throw new RuntimeException ("Unsupported constructor");
  58. }
  59. /**
  60. * Create a proxy file system for fs.
  61. *
  62. * @param fs FileSystem to create proxy for
  63. * @param myUri URI to use as proxy. Only the scheme and authority from
  64. * this are used right now
  65. */
  66. public ProxyFileSystem(FileSystem fs, URI myUri) {
  67. super(fs);
  68. URI realUri = fs.getUri();
  69. this.realScheme = realUri.getScheme();
  70. this.realAuthority=realUri.getAuthority();
  71. this.realUri = realUri;
  72. this.myScheme = myUri.getScheme();
  73. this.myAuthority=myUri.getAuthority();
  74. this.myUri = myUri;
  75. }
  76. public void initialize(URI name, Configuration conf) throws IOException {
  77. try {
  78. URI realUri = new URI (realScheme, realAuthority,
  79. name.getPath(), name.getQuery(), name.getFragment());
  80. super.initialize(realUri, conf);
  81. } catch (URISyntaxException e) {
  82. throw new RuntimeException(e);
  83. }
  84. }
  85. public URI getUri() {
  86. return myUri;
  87. }
  88. public String getName() {
  89. return getUri().toString();
  90. }
  91. public Path makeQualified(Path path) {
  92. return swizzleReturnPath(super.makeQualified(swizzleParamPath(path)));
  93. }
  94. protected void checkPath(Path path) {
  95. super.checkPath(swizzleParamPath(path));
  96. }
  97. public BlockLocation[] getFileBlockLocations(FileStatus file, long start,
  98. long len) throws IOException {
  99. return super.getFileBlockLocations(swizzleFileStatus(file, true),
  100. start, len);
  101. }
  102. public FSDataInputStream open(Path f, int bufferSize) throws IOException {
  103. return super.open(swizzleParamPath(f), bufferSize);
  104. }
  105. public FSDataOutputStream append(Path f, int bufferSize,
  106. Progressable progress) throws IOException {
  107. return super.append(swizzleParamPath(f), bufferSize, progress);
  108. }
  109. public FSDataOutputStream create(Path f, FsPermission permission,
  110. boolean overwrite, int bufferSize, short replication, long blockSize,
  111. Progressable progress) throws IOException {
  112. return super.create(swizzleParamPath(f), permission,
  113. overwrite, bufferSize, replication, blockSize, progress);
  114. }
  115. public boolean setReplication(Path src, short replication) throws IOException {
  116. return super.setReplication(swizzleParamPath(src), replication);
  117. }
  118. public boolean rename(Path src, Path dst) throws IOException {
  119. return super.rename(swizzleParamPath(src), swizzleParamPath(dst));
  120. }
  121. public boolean delete(Path f, boolean recursive) throws IOException {
  122. return super.delete(swizzleParamPath(f), recursive);
  123. }
  124. public boolean deleteOnExit(Path f) throws IOException {
  125. return super.deleteOnExit(swizzleParamPath(f));
  126. }
  127. public FileStatus[] listStatus(Path f) throws IOException {
  128. FileStatus[] orig = super.listStatus(swizzleParamPath(f));
  129. FileStatus[] ret = new FileStatus [orig.length];
  130. for (int i=0; i<orig.length; i++) {
  131. ret[i] = swizzleFileStatus(orig[i], false);
  132. }
  133. return ret;
  134. }
  135. public Path getHomeDirectory() {
  136. return swizzleReturnPath(super.getHomeDirectory());
  137. }
  138. public void setWorkingDirectory(Path newDir) {
  139. super.setWorkingDirectory(swizzleParamPath(newDir));
  140. }
  141. public Path getWorkingDirectory() {
  142. return swizzleReturnPath(super.getWorkingDirectory());
  143. }
  144. public boolean mkdirs(Path f, FsPermission permission) throws IOException {
  145. return super.mkdirs(swizzleParamPath(f), permission);
  146. }
  147. public void copyFromLocalFile(boolean delSrc, Path src, Path dst)
  148. throws IOException {
  149. super.copyFromLocalFile(delSrc, swizzleParamPath(src), swizzleParamPath(dst));
  150. }
  151. public void copyFromLocalFile(boolean delSrc, boolean overwrite,
  152. Path[] srcs, Path dst)
  153. throws IOException {
  154. super.copyFromLocalFile(delSrc, overwrite, srcs, swizzleParamPath(dst));
  155. }
  156. public void copyFromLocalFile(boolean delSrc, boolean overwrite,
  157. Path src, Path dst)
  158. throws IOException {
  159. super.copyFromLocalFile(delSrc, overwrite, src, swizzleParamPath(dst));
  160. }
  161. public void copyToLocalFile(boolean delSrc, Path src, Path dst)
  162. throws IOException {
  163. super.copyToLocalFile(delSrc, swizzleParamPath(src), dst);
  164. }
  165. public Path startLocalOutput(Path fsOutputFile, Path tmpLocalFile)
  166. throws IOException {
  167. return super.startLocalOutput(swizzleParamPath(fsOutputFile), tmpLocalFile);
  168. }
  169. public void completeLocalOutput(Path fsOutputFile, Path tmpLocalFile)
  170. throws IOException {
  171. super.completeLocalOutput(swizzleParamPath(fsOutputFile), tmpLocalFile);
  172. }
  173. public long getUsed() throws IOException{
  174. return super.getUsed();
  175. }
  176. public long getDefaultBlockSize() {
  177. return super.getDefaultBlockSize();
  178. }
  179. public short getDefaultReplication() {
  180. return super.getDefaultReplication();
  181. }
  182. public ContentSummary getContentSummary(Path f) throws IOException {
  183. return super.getContentSummary(swizzleParamPath(f));
  184. }
  185. public FileStatus getFileStatus(Path f) throws IOException {
  186. return swizzleFileStatus(super.getFileStatus(swizzleParamPath(f)), false);
  187. }
  188. public FileChecksum getFileChecksum(Path f) throws IOException {
  189. return super.getFileChecksum(swizzleParamPath(f));
  190. }
  191. public void setVerifyChecksum(boolean verifyChecksum) {
  192. super.setVerifyChecksum(verifyChecksum);
  193. }
  194. public Configuration getConf() {
  195. return super.getConf();
  196. }
  197. public void close() throws IOException {
  198. super.close();
  199. super.close();
  200. }
  201. public void setOwner(Path p, String username, String groupname
  202. ) throws IOException {
  203. super.setOwner(swizzleParamPath(p), username, groupname);
  204. }
  205. public void setTimes(Path p, long mtime, long atime
  206. ) throws IOException {
  207. super.setTimes(swizzleParamPath(p), mtime, atime);
  208. }
  209. public void setPermission(Path p, FsPermission permission
  210. ) throws IOException {
  211. super.setPermission(swizzleParamPath(p), permission);
  212. }
  213. }