2020-12-26 02:31:34 +08:00
|
|
|
|
package cn.keking.service;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
2022-12-15 18:19:30 +08:00
|
|
|
|
import cn.keking.utils.LocalOfficeUtils;
|
2019-11-29 14:40:42 +08:00
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2022-12-15 18:19:30 +08:00
|
|
|
|
import org.jodconverter.core.office.InstalledOfficeManagerHolder;
|
|
|
|
|
|
import org.jodconverter.core.office.OfficeException;
|
|
|
|
|
|
import org.jodconverter.core.office.OfficeUtils;
|
|
|
|
|
|
import org.jodconverter.core.util.OSUtils;
|
|
|
|
|
|
import org.jodconverter.local.office.LocalOfficeManager;
|
2019-11-21 17:00:07 +08:00
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2021-03-12 21:32:01 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
|
import org.springframework.boot.convert.DurationStyle;
|
2020-12-26 02:31:34 +08:00
|
|
|
|
import org.springframework.core.Ordered;
|
|
|
|
|
|
import org.springframework.core.annotation.Order;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
|
import javax.annotation.PreDestroy;
|
2019-11-21 17:00:07 +08:00
|
|
|
|
import java.io.ByteArrayOutputStream;
|
2020-05-15 18:09:19 +08:00
|
|
|
|
import java.io.File;
|
2019-11-21 17:00:07 +08:00
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
import java.io.InputStream;
|
2021-03-12 21:32:01 +08:00
|
|
|
|
import java.util.Arrays;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 创建文件转换器
|
|
|
|
|
|
*
|
2022-12-15 18:19:30 +08:00
|
|
|
|
* @author chenjh
|
|
|
|
|
|
* @since 2022-12-15
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
|
@Component
|
2020-12-26 02:31:34 +08:00
|
|
|
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
2020-12-26 19:13:50 +08:00
|
|
|
|
public class OfficePluginManager {
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
2020-12-26 19:13:50 +08:00
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(OfficePluginManager.class);
|
2019-11-21 17:00:07 +08:00
|
|
|
|
|
2022-12-15 18:19:30 +08:00
|
|
|
|
private LocalOfficeManager officeManager;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
2021-03-12 21:32:01 +08:00
|
|
|
|
@Value("${office.plugin.server.ports:2001,2002}")
|
|
|
|
|
|
private String serverPorts;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${office.plugin.task.timeout:5m}")
|
|
|
|
|
|
private String timeOut;
|
|
|
|
|
|
|
2020-12-26 02:31:34 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 启动Office组件进程
|
|
|
|
|
|
*/
|
2021-07-06 11:12:07 +08:00
|
|
|
|
@PostConstruct
|
2022-12-15 18:19:30 +08:00
|
|
|
|
public void startOfficeManager() throws OfficeException {
|
|
|
|
|
|
File officeHome = LocalOfficeUtils.getDefaultOfficeHome();
|
2019-11-21 17:00:07 +08:00
|
|
|
|
if (officeHome == null) {
|
|
|
|
|
|
throw new RuntimeException("找不到office组件,请确认'office.home'配置是否有误");
|
|
|
|
|
|
}
|
2020-05-15 18:09:19 +08:00
|
|
|
|
boolean killOffice = killProcess();
|
2019-11-21 17:00:07 +08:00
|
|
|
|
if (killOffice) {
|
|
|
|
|
|
logger.warn("检测到有正在运行的office进程,已自动结束该进程");
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
2022-07-21 03:50:09 +00:00
|
|
|
|
String[] portsString = serverPorts.split(",");
|
2021-03-12 21:32:01 +08:00
|
|
|
|
int[] ports = Arrays.stream(portsString).mapToInt(Integer::parseInt).toArray();
|
|
|
|
|
|
long timeout = DurationStyle.detectAndParse(timeOut).toMillis();
|
2022-12-15 18:19:30 +08:00
|
|
|
|
officeManager = LocalOfficeManager.builder()
|
|
|
|
|
|
.officeHome(officeHome)
|
|
|
|
|
|
.portNumbers(ports)
|
|
|
|
|
|
.processTimeout(timeout)
|
|
|
|
|
|
.build();
|
2019-11-21 17:00:07 +08:00
|
|
|
|
officeManager.start();
|
2022-12-15 18:19:30 +08:00
|
|
|
|
InstalledOfficeManagerHolder.setInstance(officeManager);
|
2019-11-21 17:00:07 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
logger.error("启动office组件失败,请检查office组件是否可用");
|
|
|
|
|
|
throw e;
|
|
|
|
|
|
}
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
|
private boolean killProcess() {
|
2019-11-21 17:00:07 +08:00
|
|
|
|
boolean flag = false;
|
|
|
|
|
|
try {
|
2022-12-15 18:19:30 +08:00
|
|
|
|
if (OSUtils.IS_OS_WINDOWS) {
|
2019-11-21 17:00:07 +08:00
|
|
|
|
Process p = Runtime.getRuntime().exec("cmd /c tasklist ");
|
|
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
|
|
InputStream os = p.getInputStream();
|
2020-05-15 18:09:19 +08:00
|
|
|
|
byte[] b = new byte[256];
|
2019-11-21 17:00:07 +08:00
|
|
|
|
while (os.read(b) > 0) {
|
|
|
|
|
|
baos.write(b);
|
|
|
|
|
|
}
|
|
|
|
|
|
String s = baos.toString();
|
2020-05-15 18:09:19 +08:00
|
|
|
|
if (s.contains("soffice.bin")) {
|
|
|
|
|
|
Runtime.getRuntime().exec("taskkill /im " + "soffice.bin" + " /f");
|
2019-11-21 17:00:07 +08:00
|
|
|
|
flag = true;
|
|
|
|
|
|
}
|
2022-12-15 18:19:30 +08:00
|
|
|
|
} else if (OSUtils.IS_OS_MAC || OSUtils.IS_OS_MAC_OSX) {
|
|
|
|
|
|
Process p = Runtime.getRuntime().exec(new String[]{"sh", "-c", "ps -ef | grep " + "soffice.bin"});
|
2022-07-21 03:50:09 +00:00
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
|
|
InputStream os = p.getInputStream();
|
|
|
|
|
|
byte[] b = new byte[256];
|
|
|
|
|
|
while (os.read(b) > 0) {
|
|
|
|
|
|
baos.write(b);
|
|
|
|
|
|
}
|
|
|
|
|
|
String s = baos.toString();
|
2022-12-15 18:19:30 +08:00
|
|
|
|
if (StringUtils.ordinalIndexOf(s, "soffice.bin", 3) > 0) {
|
|
|
|
|
|
String[] cmd = {"sh", "-c", "kill -15 `ps -ef|grep " + "soffice.bin" + "|awk 'NR==1{print $2}'`"};
|
2022-07-21 03:50:09 +00:00
|
|
|
|
Runtime.getRuntime().exec(cmd);
|
|
|
|
|
|
flag = true;
|
|
|
|
|
|
}
|
2019-11-29 14:40:42 +08:00
|
|
|
|
} else {
|
2022-12-15 18:19:30 +08:00
|
|
|
|
Process p = Runtime.getRuntime().exec(new String[]{"sh", "-c", "ps -ef | grep " + "soffice.bin" + " |grep -v grep | wc -l"});
|
2019-11-21 17:00:07 +08:00
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
|
|
InputStream os = p.getInputStream();
|
2020-05-15 18:09:19 +08:00
|
|
|
|
byte[] b = new byte[256];
|
2019-11-21 17:00:07 +08:00
|
|
|
|
while (os.read(b) > 0) {
|
|
|
|
|
|
baos.write(b);
|
|
|
|
|
|
}
|
|
|
|
|
|
String s = baos.toString();
|
2022-12-15 18:19:30 +08:00
|
|
|
|
if (!s.startsWith("0")) {
|
|
|
|
|
|
String[] cmd = {"sh", "-c", "ps -ef | grep soffice.bin | grep -v grep | awk '{print \"kill -9 \"$2}' | sh"};
|
2019-11-21 17:00:07 +08:00
|
|
|
|
Runtime.getRuntime().exec(cmd);
|
|
|
|
|
|
flag = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
logger.error("检测office进程异常", e);
|
|
|
|
|
|
}
|
|
|
|
|
|
return flag;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
|
@PreDestroy
|
2022-07-21 03:50:09 +00:00
|
|
|
|
public void destroyOfficeManager() {
|
2018-01-17 14:10:40 +08:00
|
|
|
|
if (null != officeManager && officeManager.isRunning()) {
|
2021-07-06 09:10:19 +08:00
|
|
|
|
logger.info("Shutting down office process");
|
2022-12-15 18:19:30 +08:00
|
|
|
|
OfficeUtils.stopQuietly(officeManager);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|