Skip to content

Commit

Permalink
Set user directory for openoffice processes (#125)
Browse files Browse the repository at this point in the history
* Set user directory for the openOffice processes (-env:UserInstallation parameter)
  • Loading branch information
Ilya-c authored and subbotin committed Nov 7, 2020
1 parent 8c0715c commit 9d4c50c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

package com.haulmont.yarg.formatters.impl.doc.connector;

import com.google.common.collect.Lists;
import com.sun.star.comp.helper.BootstrapException;
import com.sun.star.lib.util.NativeLibraryLoader;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -48,6 +53,7 @@ public class OOServer {
*/
private String oooExecFolder;

private String temporaryDirPath;

private String host;

Expand All @@ -56,7 +62,7 @@ public class OOServer {
/**
* The options for starting the OOo server.
*/
private List oooOptions;
private List<String> oooOptions;

private ProcessManager processManager;

Expand All @@ -68,13 +74,15 @@ public class OOServer {
* @param oooExecFolder The folder of the OOo installation containing the soffice executable
* @param oooOptions The list of options
*/
public OOServer(String oooExecFolder, List oooOptions, String host, int port, ProcessManager processManager) {
public OOServer(String oooExecFolder, List<String> oooOptions, String host, int port,
String temporaryDirPath, ProcessManager processManager) {
this.oooProcess = null;
this.oooExecFolder = oooExecFolder;
this.host = host;
this.port = port;
this.oooOptions = oooOptions;
this.processManager = processManager;
this.temporaryDirPath = temporaryDirPath;
}

/**
Expand Down Expand Up @@ -102,25 +110,32 @@ public synchronized void start() throws BootstrapException, IOException {
if (fOffice == null)
throw new BootstrapException("no office executable found!");

// create call with arguments
int arguments = (oooOptions != null) ? oooOptions.size() + 1 : 1;
arguments++;
String[] oooCommand = new String[arguments];
oooCommand[0] = fOffice.getPath();
List<String> argumentsList = Lists.newLinkedList();
argumentsList.add(fOffice.getPath());

for (int i = 0; i < oooOptions.size(); i++) {
oooCommand[i + 1] = (String) oooOptions.get(i);
}
if (CollectionUtils.isNotEmpty(oooOptions))
argumentsList.addAll(oooOptions);

oooCommand[arguments - 1] = oooAcceptOption;
String instanceProfilePath = getInstanceProfilePath();
if (StringUtils.isNotEmpty(instanceProfilePath)) {
prepareInstanceProfileDir();
argumentsList.add("-env:UserInstallation=" + toUrl(new File(instanceProfilePath)));
}
argumentsList.add(oooAcceptOption);

// start office process
oooProcess = Runtime.getRuntime().exec(oooCommand);
oooProcess = Runtime.getRuntime().exec(argumentsList.toArray(new String[0]));

pipe(oooProcess.getInputStream(), "OUT");
pipe(oooProcess.getErrorStream(), "ERR");
}

public String toUrl(File file) {
String path = file.toURI().getRawPath();
String url = path.startsWith("//") ? "file:" + path : "file:https://" + path;
return url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
}

/**
* Kills the OOo server process from the previous start.
* If there has been no previous start of the OOo server, the kill does
Expand All @@ -133,6 +148,7 @@ public synchronized void kill() {
List<Long> pids = processManager.findPid(host, port);
processManager.kill(oooProcess, pids);
oooProcess = null;
deleteProfileDir();
}
}

Expand Down Expand Up @@ -176,4 +192,40 @@ public static List<String> getDefaultOOoOptions() {

return options;
}

protected void prepareInstanceProfileDir() {
String instanceProfilePath = getInstanceProfilePath();
if (StringUtils.isNotEmpty(instanceProfilePath)) {
File instanceProfileDir = new File(instanceProfilePath);
if (instanceProfileDir.exists()) {
log.debug(String.format("Office server profile dir '%s' already exists; deleting", instanceProfileDir));
deleteProfileDir();
}
}
}

protected void deleteProfileDir() {
String instanceProfilePath = getInstanceProfilePath();
if (StringUtils.isNotEmpty(instanceProfilePath)) {
File instanceProfileDir = new File(instanceProfilePath);
try {
FileUtils.deleteDirectory(instanceProfileDir);
} catch (IOException ioException) {
File oldProfileDir = new File(instanceProfileDir.getParentFile(),
instanceProfileDir.getName() + ".old." + System.currentTimeMillis());
if (instanceProfileDir.renameTo(oldProfileDir)) {
log.warn("could not delete profileDir: " + ioException.getMessage() + "; renamed it to " + oldProfileDir);
} else {
log.error("could not delete profileDir: " + ioException.getMessage());
}
}
}
}

protected String getInstanceProfilePath() {
if (StringUtils.isNotEmpty(temporaryDirPath)) {
return String.format("%s.server_%s_%s", temporaryDirPath, host, port);
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public class OfficeConnection {
public OfficeConnection(String openOfficePath, Integer port, ProcessManager processManager, OfficeIntegration officeIntegration) {
this.port = port;
this.officeIntegration = officeIntegration;
this.oooServer = new OOServer(openOfficePath, OOServer.getDefaultOOoOptions(), "localhost", port, processManager);
this.oooServer = new OOServer(openOfficePath, OOServer.getDefaultOOoOptions(),
"localhost", port, officeIntegration.getTemporaryDirPath(), processManager);
this.bsc = new BootstrapSocketConnector(oooServer);
this.openOfficePath = openOfficePath;
}
Expand Down

0 comments on commit 9d4c50c

Please sign in to comment.