001    /**
002     * Copyright 2010-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.util;
017    
018    import java.util.ArrayList;
019    import java.util.Arrays;
020    import java.util.Collections;
021    import java.util.List;
022    
023    public class UnixCmds {
024            private static final String SU = "su";
025            private static final String MKDIR = "mkdir";
026            private static final String RM = "rm";
027            private static final String CHOWN = "chown";
028            private static final String CHMOD = "chmod";
029            private static final String HOSTNAME = "hostname";
030            private static final String PS = "ps";
031            private static final String KILL = "kill";
032            private static final String NOHUP = "nohup";
033    
034            public String nohup(String command) {
035                    return nohup(command, new ArrayList<String>());
036            }
037    
038            public String nohup(String command, List<String> nohupArgs) {
039                    Assert.hasText(command);
040                    List<String> args = new ArrayList<String>();
041                    args.add(command);
042                    args.addAll(nohupArgs);
043                    return cmd(NOHUP, args);
044            }
045    
046            public String kill(int pid) {
047                    return kill(Arrays.asList(pid));
048            }
049    
050            public String kill(List<Integer> pids) {
051                    Assert.notEmpty(pids);
052                    List<String> args = new ArrayList<String>();
053                    for (Integer pid : pids) {
054                            args.add(pid.toString());
055                    }
056                    return cmd(KILL, args);
057            }
058    
059            public String ps() {
060                    return cmd(PS);
061            }
062    
063            public String ps(String user) {
064                    Assert.hasText(user);
065                    return cmd(PS, Arrays.asList("-u", user));
066            }
067    
068            public String psf(String user) {
069                    Assert.hasText(user);
070                    List<String> args = new ArrayList<String>();
071                    args.add("-u");
072                    args.add(user);
073                    args.add("-f");
074                    return cmd(PS, args);
075            }
076    
077            /**
078             * Returns the current hostname
079             */
080            public String hostname() {
081                    return cmd(HOSTNAME);
082            }
083    
084            /**
085             * Sets the current hostname
086             */
087            public String hostname(String hostname) {
088                    return cmd(HOSTNAME, Arrays.asList(hostname));
089            }
090    
091            public String chmod(String mode, String path) {
092                    Assert.notBlank(path);
093                    return chmod(mode, Collections.singletonList(path));
094            }
095    
096            public String chmod(String mode, List<String> paths) {
097                    return chmod(null, mode, paths);
098    
099            }
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    }