1
|
package eu.dnetlib.dhp.common;
|
2
|
|
3
|
import java.lang.reflect.Method;
|
4
|
import java.util.Arrays;
|
5
|
import java.util.LinkedList;
|
6
|
|
7
|
import org.apache.hadoop.conf.Configurable;
|
8
|
import org.apache.hadoop.conf.Configuration;
|
9
|
import org.apache.hadoop.fs.FileSystem;
|
10
|
import org.apache.hadoop.fs.FsShell;
|
11
|
import org.springframework.beans.BeanUtils;
|
12
|
import org.springframework.util.ClassUtils;
|
13
|
import org.springframework.util.ReflectionUtils;
|
14
|
|
15
|
/**
|
16
|
* Extracted from:
|
17
|
* https://github.com/spring-projects/spring-hadoop/blob/master/spring-hadoop-core/src/main/java/org/springframework/data/hadoop/fs/FsShellPermissions.java
|
18
|
*
|
19
|
* Utility class for accessing Hadoop FsShellPermissions (which is not public)
|
20
|
* without having to duplicate its code.
|
21
|
* @author Costin Leau
|
22
|
*
|
23
|
*/
|
24
|
public class FsShellPermissions {
|
25
|
|
26
|
private static boolean IS_HADOOP_20X = ClassUtils.isPresent("org.apache.hadoop.fs.FsShellPermissions$Chmod",
|
27
|
FsShellPermissions.class.getClassLoader());
|
28
|
|
29
|
public enum Op {
|
30
|
CHOWN("-chown"), CHMOD("-chmod"), CHGRP("-chgrp");
|
31
|
|
32
|
private final String cmd;
|
33
|
|
34
|
Op(String cmd) {
|
35
|
this.cmd = cmd;
|
36
|
}
|
37
|
|
38
|
public String getCmd() {
|
39
|
return cmd;
|
40
|
}
|
41
|
}
|
42
|
|
43
|
// TODO: move this into Spring Core (but add JDK 1.5 compatibility first)
|
44
|
@SafeVarargs
|
45
|
static <T> T[] concatAll(T[] first, T[]... rest) {
|
46
|
// can add some sanity checks
|
47
|
int totalLength = first.length;
|
48
|
for (T[] array : rest) {
|
49
|
totalLength += array.length;
|
50
|
}
|
51
|
T[] result = Arrays.copyOf(first, totalLength);
|
52
|
int offset = first.length;
|
53
|
for (T[] array : rest) {
|
54
|
System.arraycopy(array, 0, result, offset, array.length);
|
55
|
offset += array.length;
|
56
|
}
|
57
|
return result;
|
58
|
}
|
59
|
|
60
|
public static void changePermissions(FileSystem fs, Configuration config,
|
61
|
Op op, boolean recursive, String group, String uri) {
|
62
|
changePermissions(fs, config, op, recursive, group, new String[] {uri});
|
63
|
}
|
64
|
|
65
|
public static void changePermissions(FileSystem fs, Configuration config,
|
66
|
Op op, boolean recursive, String group, String... uris) {
|
67
|
String[] argvs;
|
68
|
if (recursive) {
|
69
|
argvs = new String[1];
|
70
|
argvs[0] = "-R";
|
71
|
} else {
|
72
|
argvs = new String[0];
|
73
|
}
|
74
|
argvs = concatAll(argvs, new String[] { group }, uris);
|
75
|
|
76
|
// Hadoop 1.0.x
|
77
|
if (!IS_HADOOP_20X) {
|
78
|
Class<?> cls = ClassUtils.resolveClassName("org.apache.hadoop.fs.FsShellPermissions", config.getClass().getClassLoader());
|
79
|
Object[] args = new Object[] { fs, op.getCmd(), argvs, 0, new FsShell(config) };
|
80
|
|
81
|
Method m = ReflectionUtils.findMethod(cls, "changePermissions", FileSystem.class, String.class, String[].class, int.class, FsShell.class);
|
82
|
ReflectionUtils.makeAccessible(m);
|
83
|
ReflectionUtils.invokeMethod(m, null, args);
|
84
|
}
|
85
|
// Hadoop 2.x
|
86
|
else {
|
87
|
Class<?> cmd = ClassUtils.resolveClassName("org.apache.hadoop.fs.shell.Command", config.getClass().getClassLoader());
|
88
|
Class<?> targetClz = ClassUtils.resolveClassName("org.apache.hadoop.fs.FsShellPermissions$Chmod", config.getClass().getClassLoader());
|
89
|
Configurable target = (Configurable) BeanUtils.instantiate(targetClz);
|
90
|
target.setConf(config);
|
91
|
// run(String...) swallows the exceptions - re-implement it here
|
92
|
//
|
93
|
LinkedList<String> args = new LinkedList<String>(Arrays.asList(argvs));
|
94
|
try {
|
95
|
Method m = ReflectionUtils.findMethod(cmd, "processOptions", LinkedList.class);
|
96
|
ReflectionUtils.makeAccessible(m);
|
97
|
ReflectionUtils.invokeMethod(m, target, args);
|
98
|
m = ReflectionUtils.findMethod(cmd, "processRawArguments", LinkedList.class);
|
99
|
ReflectionUtils.makeAccessible(m);
|
100
|
ReflectionUtils.invokeMethod(m, target, args);
|
101
|
} catch (IllegalStateException ex){
|
102
|
throw new RuntimeException("Cannot change permissions/ownership " + ex);
|
103
|
}
|
104
|
}
|
105
|
}
|
106
|
}
|