1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.officeimporter.internal.server; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.InputStream; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Singleton; |
27 |
|
|
28 |
|
import org.artofsolving.jodconverter.OfficeDocumentConverter; |
29 |
|
import org.artofsolving.jodconverter.document.JsonDocumentFormatRegistry; |
30 |
|
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; |
31 |
|
import org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration; |
32 |
|
import org.artofsolving.jodconverter.office.OfficeManager; |
33 |
|
import org.slf4j.Logger; |
34 |
|
import org.xwiki.component.annotation.Component; |
35 |
|
import org.xwiki.environment.Environment; |
36 |
|
import org.xwiki.officeimporter.converter.OfficeConverter; |
37 |
|
import org.xwiki.officeimporter.internal.converter.DefaultOfficeConverter; |
38 |
|
import org.xwiki.officeimporter.server.OfficeServer; |
39 |
|
import org.xwiki.officeimporter.server.OfficeServerConfiguration; |
40 |
|
import org.xwiki.officeimporter.server.OfficeServerException; |
41 |
|
|
42 |
|
|
43 |
|
@link |
44 |
|
|
45 |
|
@version |
46 |
|
@since |
47 |
|
|
48 |
|
@Component |
49 |
|
@Singleton |
|
|
| 18.7% |
Uncovered Elements: 61 (75) |
Complexity: 19 |
Complexity Density: 0.36 |
|
50 |
|
public class DefaultOfficeServer implements OfficeServer |
51 |
|
{ |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
private static final String DOCUMENT_FORMATS_PATH = "/document-formats.js"; |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
@Inject |
61 |
|
private OfficeServerConfiguration config; |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
@Inject |
67 |
|
private Environment environment; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@Inject |
73 |
|
private Logger logger; |
74 |
|
|
75 |
|
|
76 |
|
@link |
77 |
|
|
78 |
|
private OfficeManager jodManager; |
79 |
|
|
80 |
|
|
81 |
|
@link |
82 |
|
|
83 |
|
private OfficeDocumentConverter jodConverter; |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
private ServerState state; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
private OfficeConverter converter; |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
98 |
185 |
public DefaultOfficeServer()... |
99 |
|
{ |
100 |
185 |
setState(ServerState.NOT_CONNECTED); |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
@throws |
107 |
|
|
|
|
| 0% |
Uncovered Elements: 44 (44) |
Complexity: 9 |
Complexity Density: 0.28 |
|
108 |
0 |
public void initialize() throws OfficeServerException... |
109 |
|
{ |
110 |
0 |
if (this.config.getServerType() == OfficeServerConfiguration.SERVER_TYPE_INTERNAL) { |
111 |
0 |
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration(); |
112 |
0 |
configuration.setPortNumber(this.config.getServerPort()); |
113 |
|
|
114 |
0 |
String homePath = this.config.getHomePath(); |
115 |
0 |
if (homePath != null) { |
116 |
0 |
configuration.setOfficeHome(homePath); |
117 |
|
} |
118 |
|
|
119 |
0 |
String profilePath = this.config.getProfilePath(); |
120 |
0 |
if (profilePath != null) { |
121 |
0 |
configuration.setTemplateProfileDir(new File(profilePath)); |
122 |
|
} |
123 |
|
|
124 |
0 |
configuration.setMaxTasksPerProcess(this.config.getMaxTasksPerProcess()); |
125 |
0 |
configuration.setTaskExecutionTimeout(this.config.getTaskExecutionTimeout()); |
126 |
|
|
127 |
0 |
try { |
128 |
0 |
this.jodManager = configuration.buildOfficeManager(); |
129 |
|
} catch (Exception e) { |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
0 |
throw new OfficeServerException("Failed to start Office server. Reason: " + e.getMessage(), e); |
134 |
|
} |
135 |
0 |
} else if (this.config.getServerType() == OfficeServerConfiguration.SERVER_TYPE_EXTERNAL_LOCAL) { |
136 |
0 |
ExternalOfficeManagerConfiguration externalProcessOfficeManager = new ExternalOfficeManagerConfiguration(); |
137 |
0 |
externalProcessOfficeManager.setPortNumber(this.config.getServerPort()); |
138 |
0 |
externalProcessOfficeManager.setConnectOnStart(true); |
139 |
0 |
this.jodManager = externalProcessOfficeManager.buildOfficeManager(); |
140 |
|
} else { |
141 |
0 |
setState(ServerState.CONF_ERROR); |
142 |
0 |
throw new OfficeServerException("Invalid office server configuration."); |
143 |
|
} |
144 |
|
|
145 |
0 |
this.jodConverter = null; |
146 |
|
|
147 |
0 |
InputStream input = getClass().getResourceAsStream(DOCUMENT_FORMATS_PATH); |
148 |
0 |
if (input != null) { |
149 |
0 |
try { |
150 |
0 |
this.jodConverter = new OfficeDocumentConverter(this.jodManager, new JsonDocumentFormatRegistry(input)); |
151 |
|
} catch (Exception e) { |
152 |
0 |
this.logger.warn("Failed to parse {} . The default document format registry will be used instead.", |
153 |
|
DOCUMENT_FORMATS_PATH, e); |
154 |
|
} |
155 |
|
} else { |
156 |
0 |
this.logger.debug("{} is missing. The default document format registry will be used instead.", |
157 |
|
DOCUMENT_FORMATS_PATH); |
158 |
|
} |
159 |
0 |
if (this.jodConverter == null) { |
160 |
|
|
161 |
0 |
this.jodConverter = new OfficeDocumentConverter(this.jodManager); |
162 |
|
} |
163 |
|
|
164 |
0 |
File workDir = this.environment.getTemporaryDirectory(); |
165 |
0 |
this.converter = new DefaultOfficeConverter(this.jodConverter, workDir); |
166 |
|
} |
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
168 |
437 |
@Override... |
169 |
|
public ServerState getState() |
170 |
|
{ |
171 |
437 |
return this.state; |
172 |
|
} |
173 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
174 |
0 |
@Override... |
175 |
|
public void start() throws OfficeServerException |
176 |
|
{ |
177 |
|
|
178 |
0 |
stop(); |
179 |
|
|
180 |
0 |
initialize(); |
181 |
0 |
try { |
182 |
0 |
this.jodManager.start(); |
183 |
0 |
setState(ServerState.CONNECTED); |
184 |
0 |
this.logger.info("Open Office instance started."); |
185 |
|
} catch (Exception e) { |
186 |
0 |
setState(ServerState.ERROR); |
187 |
0 |
throw new OfficeServerException("Error while connecting / starting the office server.", e); |
188 |
|
} |
189 |
|
} |
190 |
|
|
|
|
| 50% |
Uncovered Elements: 5 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
191 |
32 |
@Override... |
192 |
|
public void stop() throws OfficeServerException |
193 |
|
{ |
194 |
|
|
195 |
|
|
196 |
32 |
boolean connected = checkState(ServerState.CONNECTED); |
197 |
32 |
try { |
198 |
32 |
this.jodManager.stop(); |
199 |
0 |
setState(ServerState.NOT_CONNECTED); |
200 |
0 |
this.logger.info("Open Office instance stopped."); |
201 |
|
} catch (Exception e) { |
202 |
32 |
if (connected) { |
203 |
0 |
setState(ServerState.ERROR); |
204 |
0 |
throw new OfficeServerException("Error while disconnecting / shutting down the office server.", e); |
205 |
|
} |
206 |
|
} |
207 |
|
} |
208 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
209 |
0 |
@Override... |
210 |
|
public OfficeConverter getConverter() |
211 |
|
{ |
212 |
0 |
return this.converter; |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
@param |
219 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
220 |
185 |
private void setState(ServerState newState)... |
221 |
|
{ |
222 |
185 |
this.state = newState; |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
@param |
229 |
|
@return |
230 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
231 |
32 |
private boolean checkState(ServerState expectedState)... |
232 |
|
{ |
233 |
32 |
return (this.state == expectedState); |
234 |
|
} |
235 |
|
} |