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.shell; |
---|
19 | |
---|
20 | import java.io.*; |
---|
21 | |
---|
22 | import org.apache.hadoop.conf.Configuration; |
---|
23 | import org.apache.hadoop.conf.Configured; |
---|
24 | import org.apache.hadoop.fs.FileStatus; |
---|
25 | import org.apache.hadoop.fs.FileSystem; |
---|
26 | import org.apache.hadoop.fs.Path; |
---|
27 | import org.apache.hadoop.ipc.RemoteException; |
---|
28 | |
---|
29 | /** |
---|
30 | * An abstract class for the execution of a file system command |
---|
31 | */ |
---|
32 | abstract public class Command extends Configured { |
---|
33 | protected String[] args; |
---|
34 | |
---|
35 | /** Constructor */ |
---|
36 | protected Command(Configuration conf) { |
---|
37 | super(conf); |
---|
38 | } |
---|
39 | |
---|
40 | /** Return the command's name excluding the leading character - */ |
---|
41 | abstract public String getCommandName(); |
---|
42 | |
---|
43 | /** |
---|
44 | * Execute the command on the input path |
---|
45 | * |
---|
46 | * @param path the input path |
---|
47 | * @throws IOException if any error occurs |
---|
48 | */ |
---|
49 | abstract protected void run(Path path) throws IOException; |
---|
50 | |
---|
51 | /** |
---|
52 | * For each source path, execute the command |
---|
53 | * |
---|
54 | * @return 0 if it runs successfully; -1 if it fails |
---|
55 | */ |
---|
56 | public int runAll() { |
---|
57 | int exitCode = 0; |
---|
58 | for (String src : args) { |
---|
59 | try { |
---|
60 | Path srcPath = new Path(src); |
---|
61 | FileSystem fs = srcPath.getFileSystem(getConf()); |
---|
62 | FileStatus[] statuses = fs.globStatus(srcPath); |
---|
63 | if (statuses == null) { |
---|
64 | System.err.println("Can not find listing for " + src); |
---|
65 | exitCode = -1; |
---|
66 | } else { |
---|
67 | for(FileStatus s : statuses) { |
---|
68 | run(s.getPath()); |
---|
69 | } |
---|
70 | } |
---|
71 | } catch (RemoteException re) { |
---|
72 | exitCode = -1; |
---|
73 | String content = re.getLocalizedMessage(); |
---|
74 | int eol = content.indexOf('\n'); |
---|
75 | if (eol>=0) { |
---|
76 | content = content.substring(0, eol); |
---|
77 | } |
---|
78 | System.err.println(getCommandName() + ": " + content); |
---|
79 | } catch (IOException e) { |
---|
80 | exitCode = -1; |
---|
81 | System.err.println(getCommandName() + ": " + e.getLocalizedMessage()); |
---|
82 | } |
---|
83 | } |
---|
84 | return exitCode; |
---|
85 | } |
---|
86 | } |
---|