View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.List;
22  
23  public class UnixCmds {
24  	private static final String SU = "su";
25  	private static final String MKDIR = "mkdir";
26  	private static final String RM = "rm";
27  	private static final String CHOWN = "chown";
28  	private static final String CHMOD = "chmod";
29  	private static final String HOSTNAME = "hostname";
30  	private static final String PS = "ps";
31  	private static final String KILL = "kill";
32  	private static final String NOHUP = "nohup";
33  
34  	public String nohup(String command) {
35  		return nohup(command, new ArrayList<String>());
36  	}
37  
38  	public String nohup(String command, List<String> nohupArgs) {
39  		Assert.hasText(command);
40  		List<String> args = new ArrayList<String>();
41  		args.add(command);
42  		args.addAll(nohupArgs);
43  		return cmd(NOHUP, args);
44  	}
45  
46  	public String kill(int pid) {
47  		return kill(Arrays.asList(pid));
48  	}
49  
50  	public String kill(List<Integer> pids) {
51  		Assert.notEmpty(pids);
52  		List<String> args = new ArrayList<String>();
53  		for (Integer pid : pids) {
54  			args.add(pid.toString());
55  		}
56  		return cmd(KILL, args);
57  	}
58  
59  	public String ps() {
60  		return cmd(PS);
61  	}
62  
63  	public String ps(String user) {
64  		Assert.hasText(user);
65  		return cmd(PS, Arrays.asList("-u", user));
66  	}
67  
68  	public String psf(String user) {
69  		Assert.hasText(user);
70  		List<String> args = new ArrayList<String>();
71  		args.add("-u");
72  		args.add(user);
73  		args.add("-f");
74  		return cmd(PS, args);
75  	}
76  
77  	/**
78  	 * Returns the current hostname
79  	 */
80  	public String hostname() {
81  		return cmd(HOSTNAME);
82  	}
83  
84  	/**
85  	 * Sets the current hostname
86  	 */
87  	public String hostname(String hostname) {
88  		return cmd(HOSTNAME, Arrays.asList(hostname));
89  	}
90  
91  	public String chmod(String mode, String path) {
92  		Assert.notBlank(path);
93  		return chmod(mode, Collections.singletonList(path));
94  	}
95  
96  	public String chmod(String mode, List<String> paths) {
97  		return chmod(null, mode, paths);
98  
99  	}
100 
101 	public String chmod(List<String> options, String mode, List<String> paths) {
102 		Assert.hasLength(mode);
103 		Assert.notEmpty(paths);
104 		return cmd(CHMOD, CollectionUtils.combineStrings(options, mode, paths));
105 	}
106 
107 	public String mkdirp(String path) {
108 		Assert.notBlank(path);
109 		return mkdirp(null, Collections.singletonList(path));
110 	}
111 
112 	public String mkdirp(List<String> paths) {
113 		return mkdirp(null, paths);
114 	}
115 
116 	public String mkdirp(List<String> options, List<String> paths) {
117 		List<String> parents = Arrays.asList("-p");
118 		if (options == null) {
119 			return mkdir(parents, paths);
120 		} else {
121 			return mkdir(CollectionUtils.combineStringsUniquely(options, parents), paths);
122 		}
123 
124 	}
125 
126 	public String mkdir(String path) {
127 		Assert.notBlank(path);
128 		return mkdir(null, Collections.singletonList(path));
129 	}
130 
131 	public String mkdir(List<String> options, List<String> paths) {
132 		Assert.notEmpty(paths);
133 		return cmd(MKDIR, CollectionUtils.combineStrings(options, paths));
134 	}
135 
136 	public String su(String login, String command) {
137 		return su(null, login, command);
138 	}
139 
140 	public String su(List<String> options, String login, String command) {
141 		return su(options, login, Arrays.asList("--command", command));
142 	}
143 
144 	public String su(List<String> options, String login, List<String> args) {
145 		List<String> list2 = login == null ? null : Arrays.asList("-", login);
146 		return cmd(SU, CollectionUtils.combineStrings(options, list2, args));
147 	}
148 
149 	public String rmrf(List<String> paths) {
150 		return rmrf(null, paths);
151 	}
152 
153 	public String rmrf(String path) {
154 		Assert.notBlank(path);
155 		return rmrf(null, Collections.singletonList(path));
156 	}
157 
158 	public String rmrf(List<String> options, List<String> paths) {
159 		List<String> recursiveSilent = Arrays.asList("-r", "-f");
160 		if (options == null) {
161 			return rm(recursiveSilent, paths);
162 		} else {
163 			return rm(CollectionUtils.combineStringsUniquely(options, recursiveSilent), paths);
164 		}
165 	}
166 
167 	public String rm(List<String> paths) {
168 		return rm(null, paths);
169 	}
170 
171 	public String rm(String path) {
172 		Assert.notBlank(path);
173 		return rm(null, Collections.singletonList(path));
174 	}
175 
176 	public String rm(List<String> options, List<String> paths) {
177 		Assert.notEmpty(paths);
178 		return cmd(RM, CollectionUtils.combineStrings(options, paths));
179 	}
180 
181 	public String chownr(String owner, String group, String path) {
182 		Assert.notBlank(path);
183 		return chownr(owner, group, Collections.singletonList(path));
184 	}
185 
186 	public String chownr(String owner, String group, List<String> paths) {
187 		return chownr(null, owner, group, paths);
188 	}
189 
190 	public String chownr(List<String> options, String owner, String group, List<String> paths) {
191 		List<String> recursive = Arrays.asList("-R");
192 		if (options == null) {
193 			return chown(recursive, owner, group, paths);
194 		} else {
195 			return chown(CollectionUtils.combineStringsUniquely(options, recursive), owner, group, paths);
196 		}
197 	}
198 
199 	public String chown(List<String> options, String owner, String group, String path) {
200 		Assert.notBlank(path);
201 		return chown(options, owner, group, Collections.singletonList(path));
202 	}
203 
204 	public String chown(List<String> options, String owner, String group, List<String> paths) {
205 		Assert.notEmpty(paths);
206 		Assert.notBlank(owner, group);
207 		return cmd(CHOWN, CollectionUtils.combineStrings(options, owner + ":" + group, paths));
208 	}
209 
210 	public String chown(String owner, String group, String path) {
211 		Assert.notBlank(path);
212 		return chown(null, owner, group, Collections.singletonList(path));
213 	}
214 
215 	public String cmd(String executable) {
216 		return cmd(executable, null);
217 	}
218 
219 	public String cmd(String executable, List<String> args) {
220 		StringBuilder sb = new StringBuilder();
221 		sb.append(executable);
222 		if (!CollectionUtils.isEmpty(args)) {
223 			sb.append(" ");
224 			sb.append(CollectionUtils.getSpaceSeparatedString(args));
225 		}
226 		return sb.toString();
227 	}
228 
229 }