Skip to content

Commit

Permalink
[issue #59] added support for starting OOo as a different user
Browse files Browse the repository at this point in the history
  • Loading branch information
mirko.nasato committed Nov 22, 2009
1 parent 061dbb1 commit 7868c72
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class DefaultOfficeManagerConfiguration {
private OfficeConnectionProtocol connectionProtocol = OfficeConnectionProtocol.SOCKET;
private int[] portNumbers = new int[] { 2002 };
private String[] pipeNames = new String[] { "office" };
private String[] runAsArgs = null;
private File templateProfileDir = null;
private long taskQueueTimeout = 30000L; // 30 seconds
private long taskExecutionTimeout = 120000L; // 2 minutes
Expand Down Expand Up @@ -83,6 +84,11 @@ public DefaultOfficeManagerConfiguration setPipeNames(String... pipeNames) throw
return this;
}

public DefaultOfficeManagerConfiguration setRunAsArgs(String... runAsArgs) {
this.runAsArgs = runAsArgs;
return this;
}

public DefaultOfficeManagerConfiguration setTemplateProfileDir(File templateProfileDir) throws IllegalArgumentException {
if (templateProfileDir != null) {
checkArgument("templateProfileDir", templateProfileDir.isDirectory(), "must exist and be a directory");
Expand Down Expand Up @@ -131,7 +137,7 @@ public OfficeManager buildOfficeManager() throws IllegalStateException {
for (int i = 0; i < numInstances; i++) {
unoUrls[i] = (connectionProtocol == OfficeConnectionProtocol.PIPE) ? UnoUrl.pipe(pipeNames[i]) : UnoUrl.socket(portNumbers[i]);
}
return new ProcessPoolOfficeManager(officeHome, unoUrls, templateProfileDir, taskQueueTimeout, taskExecutionTimeout, maxTasksPerProcess, processManager);
return new ProcessPoolOfficeManager(officeHome, unoUrls, runAsArgs, templateProfileDir, taskQueueTimeout, taskExecutionTimeout, maxTasksPerProcess, processManager);
}

private ProcessManager findBestProcessManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ManagedOfficeProcess {

public ManagedOfficeProcess(ManagedOfficeProcessSettings settings) throws OfficeException {
this.settings = settings;
process = new OfficeProcess(settings.getOfficeHome(), settings.getUnoUrl(), settings.getTemplateProfileDir(), settings.getProcessManager());
process = new OfficeProcess(settings.getOfficeHome(), settings.getUnoUrl(), settings.getRunAsArgs(), settings.getTemplateProfileDir(), settings.getProcessManager());
connection = new OfficeConnection(settings.getUnoUrl());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ManagedOfficeProcessSettings {

private final UnoUrl unoUrl;
private File officeHome = OfficeUtils.getDefaultOfficeHome();
private String[] runAsArgs;
private File templateProfileDir;
private ProcessManager processManager = new PureJavaProcessManager();
private long retryTimeout = DEFAULT_RETRY_TIMEOUT;
Expand All @@ -52,6 +53,14 @@ public void setOfficeHome(File officeHome) {
this.officeHome = officeHome;
}

public String[] getRunAsArgs() {
return runAsArgs;
}

public void setRunAsArgs(String[] runAsArgs) {
this.runAsArgs = runAsArgs;
}

public File getTemplateProfileDir() {
return templateProfileDir;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
Expand All @@ -35,6 +36,7 @@ class OfficeProcess {

private final File officeHome;
private final UnoUrl unoUrl;
private final String[] runAsArgs;
private final File templateProfileDir;
private final File instanceProfileDir;
private final ProcessManager processManager;
Expand All @@ -44,23 +46,27 @@ class OfficeProcess {

private final Logger logger = Logger.getLogger(getClass().getName());

public OfficeProcess(File officeHome, UnoUrl unoUrl, File templateProfileDir, ProcessManager processManager) {
public OfficeProcess(File officeHome, UnoUrl unoUrl, String[] runAsArgs, File templateProfileDir, ProcessManager processManager) {
this.officeHome = officeHome;
this.unoUrl = unoUrl;
this.runAsArgs = runAsArgs;
this.templateProfileDir = templateProfileDir;
this.instanceProfileDir = getInstanceProfileDir(unoUrl);
this.processManager = processManager;
}

public void start() throws IOException {
String processRegex = "soffice.*" + Pattern.quote(unoUrl.getAcceptString());
String existingPid = processManager.findPid(processRegex);
if (existingPid != null) {
throw new IllegalStateException(String.format("a process with acceptString '%s' is already running; pid %s", unoUrl.getAcceptString(), existingPid));
String processRegex = "soffice.*" + Pattern.quote(unoUrl.getAcceptString());
String existingPid = findExistingPid(processRegex);
if (existingPid != null) {
throw new IllegalStateException(String.format("a process with acceptString '%s' is already running; pid %s", unoUrl.getAcceptString(), existingPid));
}
prepareInstanceProfileDir();
List<String> command = new ArrayList<String>();
File executable = OfficeUtils.getOfficeExecutable(officeHome);
if (runAsArgs != null) {
command.addAll(Arrays.asList(runAsArgs));
}
command.add(executable.getAbsolutePath());
command.add("-accept=" + unoUrl.getAcceptString() + ";urp;");
command.add("-env:UserInstallation=" + OfficeUtils.toUrl(instanceProfileDir));
Expand All @@ -81,6 +87,20 @@ public void start() throws IOException {
logger.info("started process" + (pid != null ? "; pid = " + pid : ""));
}

private String findExistingPid(String processRegex) throws IOException {
String existingPid = processManager.findPid(processRegex);
if (existingPid != null) {
// retry in case the process table is returning a stale result from a process we just killed
try {
Thread.sleep(100);
} catch (InterruptedException interruptedException) {
// continue
}
return processManager.findPid(processRegex);
}
return null;
}

private File getInstanceProfileDir(UnoUrl unoUrl) {
String dirName = ".jodconverter_" + unoUrl.getAcceptString().replace(',', '_').replace('=', '-');
return new File(System.getProperty("java.io.tmpdir"), dirName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public static PropertyValue property(String name, Object value) {
return propertyValue;
}

@SuppressWarnings("unchecked")
public static PropertyValue[] toUnoProperties(Map<String,?> properties) {
PropertyValue[] propertyValues = new PropertyValue[properties.size()];
int i = 0;
for (Map.Entry<String,?> entry : properties.entrySet()) {
Object value = entry.getValue();
if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String,Object> subProperties = (Map<String,Object>) value;
value = toUnoProperties(subProperties);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ class ProcessPoolOfficeManager implements OfficeManager {

private final Logger logger = Logger.getLogger(ProcessPoolOfficeManager.class.getName());

public ProcessPoolOfficeManager(File officeHome, UnoUrl[] unoUrls, File templateProfileDir,
public ProcessPoolOfficeManager(File officeHome, UnoUrl[] unoUrls, String[] runAsArgs, File templateProfileDir,
long taskQueueTimeout, long taskExecutionTimeout, int maxTasksPerProcess, ProcessManager processManager) {
this.taskQueueTimeout = taskQueueTimeout;
this.taskQueueTimeout = taskQueueTimeout;
pool = new ArrayBlockingQueue<PooledOfficeManager>(unoUrls.length);
pooledManagers = new PooledOfficeManager[unoUrls.length];
for (int i = 0; i < unoUrls.length; i++) {
PooledOfficeManagerSettings settings = new PooledOfficeManagerSettings(unoUrls[i]);
settings.setRunAsArgs(runAsArgs);
settings.setTemplateProfileDir(templateProfileDir);
settings.setOfficeHome(officeHome);
settings.setTaskExecutionTimeout(taskExecutionTimeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ private static JSONObject toJson(DocumentFormat format) throws JSONException {
}
return jsonFormat;
}


@SuppressWarnings("unchecked")
private static JSONObject toJson(Map<String,?> properties) throws JSONException {
JSONObject jsonProperties = new SortedJsonObject();
for (Map.Entry<String,?> entry : properties.entrySet()) {
if (entry.getValue() instanceof Map) {
@SuppressWarnings("unchecked")
Map<String,?> jsonValue = (Map<String,?>) entry.getValue();
jsonProperties.put(entry.getKey(), toJson(jsonValue));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ExternalOfficeManagerTest {

public void executeTask() throws Exception {
UnoUrl unoUrl = UnoUrl.socket(2002);
OfficeProcess officeProcess = new OfficeProcess(OfficeUtils.getDefaultOfficeHome(), unoUrl, null, new PureJavaProcessManager());
OfficeProcess officeProcess = new OfficeProcess(OfficeUtils.getDefaultOfficeHome(), unoUrl, null, null, new PureJavaProcessManager());
officeProcess.start();
Thread.sleep(2000);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
//
package org.artofsolving.jodconverter.office;

import static org.artofsolving.jodconverter.office.OfficeUtils.*;
import static org.testng.Assert.*;
import static org.artofsolving.jodconverter.office.OfficeUtils.toUrl;
import static org.testng.Assert.assertEquals;

import java.io.File;

Expand Down

0 comments on commit 7868c72

Please sign in to comment.