+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package org.argeo.server.backup;
import java.io.ByteArrayOutputStream;
private Map<String, String> variables = new HashMap<String, String>();
private Executor executor = new DefaultExecutor();
+ private Map<String, String> environment = new HashMap<String, String>();
+
+ /** Name of the sudo user, root if "", not sudo if null */
+ private String sudo = null;
+
public OsCallBackup() {
}
@Override
public void writeBackup(FileObject targetFo) {
- CommandLine commandLine = CommandLine.parse(command, variables);
+ String commandToUse = command;
+
+ // sudo
+ if (sudo != null) {
+ if (sudo.equals(""))
+ commandToUse = "sudo " + commandToUse;
+ else
+ commandToUse = "sudo -u " + sudo + " " + commandToUse;
+ }
+
+ CommandLine commandLine = CommandLine.parse(commandToUse, variables);
ByteArrayOutputStream errBos = new ByteArrayOutputStream();
if (log.isTraceEnabled())
log.trace(commandLine.toString());
ExecuteStreamHandler streamHandler = new PumpStreamHandler(
targetContent.getOutputStream(), errBos);
executor.setStreamHandler(streamHandler);
- executor.execute(commandLine);
+ executor.execute(commandLine, environment);
} catch (ExecuteException e) {
byte[] err = errBos.toByteArray();
String errStr = new String(err);
- throw new ArgeoException("Process " + commandLine
- + " failed with exit value " + e.getExitValue() + ": "
- + errStr, e);
+ throw new ArgeoException("Process " + commandLine + " failed ("
+ + e.getExitValue() + "): " + errStr, e);
} catch (Exception e) {
byte[] err = errBos.toByteArray();
String errStr = new String(err);
return command;
}
+ /**
+ * A reference to the environment variables that will be passed to the
+ * process. Empty by default.
+ */
+ protected Map<String, String> getEnvironment() {
+ return environment;
+ }
+
protected Map<String, String> getVariables() {
return variables;
}
this.executor = executor;
}
+ public void setSudo(String sudo) {
+ this.sudo = sudo;
+ }
+
}