OfficePluginManager.java
6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package cn.keking.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.apache.commons.lang3.StringUtils;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.artofsolving.jodconverter.office.OfficeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.convert.DurationStyle;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.hotent.base.util.AppUtil;
import com.hotent.base.util.StringUtil;
import com.hotent.base.util.string.StringPool;
import com.sun.star.document.UpdateDocMode;
import io.jsonwebtoken.lang.Assert;
/**
* 创建文件转换器
*
* @author yudian-it
* @date 2017/11/13
*/
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class OfficePluginManager {
private final Logger logger = LoggerFactory.getLogger(OfficePluginManager.class);
private OfficeManager officeManager;
@Value("${office.plugin.server.ports:2001,2002}")
private String serverPorts;
@Value("${office.plugin.server.enabled:true}")
private Boolean enabled;
@Value("${office.plugin.task.timeout:5m}")
private String timeOut;
/**
* 启动Office组件进程
*/
@PostConstruct
public void startOfficeManager(){
if(!enabled){
logger.warn("未启用office插件");
return;
}
File officeHome = OfficeUtils.getDefaultOfficeHome();
if (officeHome == null) {
// throw new RuntimeException("找不到office组件,请确认'office.home'配置是否有误");
logger.error("找不到office组件,请确认'office.home'配置是否有误");
return ;
}
boolean killOffice = killProcess();
if (killOffice) {
logger.warn("检测到有正在运行的office进程,已自动结束该进程");
}
try {
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
configuration.setOfficeHome(officeHome);
String []portsString = serverPorts.split(",");
int[] ports = Arrays.stream(portsString).mapToInt(Integer::parseInt).toArray();
configuration.setPortNumbers(ports);
long timeout = DurationStyle.detectAndParse(timeOut).toMillis();
// 设置任务执行超时为5分钟
configuration.setTaskExecutionTimeout(timeout);
// 设置任务队列超时为24小时
//configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
officeManager = configuration.buildOfficeManager();
officeManager.start();
} catch (Exception e) {
logger.error("启动office组件失败,请检查office组件是否可用");
throw e;
}
}
public OfficeDocumentConverter getDocumentConverter() {
Assert.notNull(officeManager, "office组件未启动,无法预览。");
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager, new OfficePluginExtendFormatRegistry());
converter.setDefaultLoadProperties(getLoadProperties());
return converter;
}
private Map<String,?> getLoadProperties() {
Map<String,Object> loadProperties = new HashMap<>(10);
loadProperties.put("Hidden", true);
loadProperties.put("ReadOnly", true);
loadProperties.put("UpdateDocMode", UpdateDocMode.QUIET_UPDATE);
loadProperties.put("CharacterSet", StandardCharsets.UTF_8.name());
return loadProperties;
}
private boolean killProcess() {
boolean flag = false;
Properties props = System.getProperties();
try {
String sofficePorts = AppUtil.getApplicaitonContext().getEnvironment().getProperty("office.plugin.server.ports");
if(StringUtil.isNotEmpty(sofficePorts)) {
sofficePorts = sofficePorts.replace(StringPool.COMMA,StringPool.PIPE);
}
if (props.getProperty("os.name").toLowerCase().contains("windows")) {
Process p = Runtime.getRuntime().exec("cmd /c tasklist ");
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();
if (s.contains("soffice.bin")) {
Runtime.getRuntime().exec("taskkill /im " + "soffice.bin" + " /f");
flag = true;
}
} else {
Process p = Runtime.getRuntime().exec(new String[]{"sh","-c","ps -ef | grep " + "soffice.bin"});
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();
if (StringUtils.ordinalIndexOf(s, "soffice.bin", 3) > 0) {
String killSofficePid = "kill -9 `ps -ef|grep " + "soffice.bin" + "'|awk '{print $2}'`";
if(StringUtils.isNotEmpty(sofficePorts)){
killSofficePid = "kill -9 `ps -ef|grep " + "soffice.bin" +"| grep -E '" + sofficePorts + "'" + "|awk '{print $2}'`";
}
String[] cmd ={"sh","-c",killSofficePid};
Runtime.getRuntime().exec(cmd);
flag = true;
}
}
} catch (IOException e) {
logger.error("检测office进程异常", e);
}
return flag;
}
@PreDestroy
public void destroyOfficeManager(){
if (null != officeManager && officeManager.isRunning()) {
logger.info("Shutting down office process");
officeManager.stop();
}
}
}