Project

General

Profile

1
package eu.dnetlib.iis.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
	static <T> T[] concatAll(T[] first, T[]... rest) {
45
		// can add some sanity checks
46
		int totalLength = first.length;
47
		for (T[] array : rest) {
48
			totalLength += array.length;
49
		}
50
		T[] result = Arrays.copyOf(first, totalLength);
51
		int offset = first.length;
52
		for (T[] array : rest) {
53
			System.arraycopy(array, 0, result, offset, array.length);
54
			offset += array.length;
55
		}
56
		return result;
57
	}
58

    
59
	public static void changePermissions(FileSystem fs, Configuration config, 
60
			Op op, boolean recursive, String group, String uri) {
61
		changePermissions(fs, config, op, recursive, group, new String[] {uri});
62
	}
63
	
64
	public static void changePermissions(FileSystem fs, Configuration config, 
65
			Op op, boolean recursive, String group, String... uris) {
66
		String[] argvs;
67
		if (recursive) {
68
			argvs = new String[1];
69
			argvs[0] = "-R";
70
		} else {
71
			argvs = new String[0];
72
		}
73
		argvs = concatAll(argvs, new String[] { group }, uris);
74

    
75
		// Hadoop 1.0.x
76
		if (!IS_HADOOP_20X) {
77
			Class<?> cls = ClassUtils.resolveClassName("org.apache.hadoop.fs.FsShellPermissions", config.getClass().getClassLoader());
78
			Object[] args = new Object[] { fs, op.getCmd(), argvs, 0, new FsShell(config) };
79

    
80
			Method m = ReflectionUtils.findMethod(cls, "changePermissions", FileSystem.class, String.class, String[].class, int.class, FsShell.class);
81
			ReflectionUtils.makeAccessible(m);
82
			ReflectionUtils.invokeMethod(m, null, args);
83
		}
84
		// Hadoop 2.x
85
		else {
86
			Class<?> cmd = ClassUtils.resolveClassName("org.apache.hadoop.fs.shell.Command", config.getClass().getClassLoader());
87
			Class<?> targetClz = ClassUtils.resolveClassName("org.apache.hadoop.fs.FsShellPermissions$Chmod", config.getClass().getClassLoader());
88
			Configurable target = (Configurable) BeanUtils.instantiate(targetClz);
89
			target.setConf(config);
90
			// run(String...) swallows the exceptions - re-implement it here
91
			//
92
			LinkedList<String> args = new LinkedList<String>(Arrays.asList(argvs));
93
			try {
94
				Method m = ReflectionUtils.findMethod(cmd, "processOptions", LinkedList.class);
95
				ReflectionUtils.makeAccessible(m);
96
				ReflectionUtils.invokeMethod(m, target, args);
97
				m = ReflectionUtils.findMethod(cmd, "processRawArguments", LinkedList.class);
98
				ReflectionUtils.makeAccessible(m);
99
				ReflectionUtils.invokeMethod(m, target, args);
100
			} catch (IllegalStateException ex){
101
				throw new RuntimeException("Cannot change permissions/ownership " + ex);
102
			}
103
		}
104
	}
105
}
(2-2/3)