This repository has been archived by the owner on Feb 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
NoaConnectionLocalServer.java
283 lines (261 loc) · 10.2 KB
/
NoaConnectionLocalServer.java
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* This file is part of YaBS.
*
* YaBS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* YaBS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with YaBS. If not, see <http://www.gnu.org/licenses/>.
*/
package enoa.connection;
import ag.ion.bion.officelayer.application.IOfficeApplication;
import ag.ion.bion.officelayer.application.OfficeApplicationException;
import ag.ion.bion.officelayer.application.OfficeApplicationRuntime;
import ag.ion.bion.officelayer.desktop.IDesktopService;
import ag.ion.bion.officelayer.document.IDocumentService;
import ag.ion.noa.NOAException;
import com.sun.star.auth.InvalidArgumentException;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import mpv5.Main;
import mpv5.YabsViewProxy;
import mpv5.globals.LocalSettings;
import mpv5.globals.Messages;
import mpv5.logging.Log;
import mpv5.ui.dialogs.Notificator;
import mpv5.ui.dialogs.Popup;
import mpv5.utils.files.FileExecutor;
/**
* This class handles connections to remote and local OpenOffice installations
*/
public class NoaConnectionLocalServer extends NoaConnection {
private static List<Process> ooProcesses = new ArrayList<Process>();
/**
* Creates a connection, depending on the current local config.
*
* @return
*/
public synchronized static NoaConnection getConnection() {
if (LocalSettings.hasProperty(LocalSettings.OFFICE_HOST)) {
if (cachedConnection == null) {
try {
cachedConnection = new NoaConnectionLocalServer(LocalSettings.getProperty(LocalSettings.OFFICE_HOST), Integer.valueOf(LocalSettings.getProperty(LocalSettings.OFFICE_PORT)));
NoaConnection.officeAvailable=true;
} catch (Exception ex) {
mpv5.logging.Log.Debug(ex);//Logger.getLogger(NoaConnection.class.getName()).log(Level.SEVERE, null, ex);
YabsViewProxy.instance().addMessage(Messages.OOCONNERROR + "\n" + ex, Color.RED);
}
YabsViewProxy.instance().showOfficeStatus(cachedConnection != null, "Local server");
}
return cachedConnection;
} else {
throw new UnsupportedOperationException("OpenOffice is not configured yet.");
}
}
/**
* clears the onnnetion for testing puproses
*/
public static void clearConnection() {
cachedConnection = null;
}
/**
* New NoaConnection instance, connects to OO using the given parameters.
*
* @param connectionString The connection String. Can be a <b>Path<b/> or an
* <b>IP<b/>
* @param port The port to connect to. A port value of zero (0) indicates a
* <b>local<b/> connection
* @throws Exception If any Exception is thrown during the connection attempt
*/
public NoaConnectionLocalServer(String connectionString, int port) throws Exception {
Log.Debug(this, connectionString);
if (port == 0) {
port = 8100;
}
if (connectionString != null && connectionString.length() > 1 && port > 0 && port < 9999) {
createServerConnection(connectionString, port);
} else {
throw new Exception("Connection not possible with the given parameters: [" + connectionString + ":" + port + "]");
}
}
/**
* Creates a new connection
*
* @param host
* @param port
* @return
* @throws OfficeApplicationException
* @throws NOAException
* @throws InvalidArgumentException
*/
private synchronized boolean createServerConnection(String host, int port) throws OfficeApplicationException, NOAException, InvalidArgumentException {
if (host != null && port > 0) {
Map<String, String> configuration = new HashMap<String, String>();
configuration.put(IOfficeApplication.APPLICATION_TYPE_KEY,
IOfficeApplication.REMOTE_APPLICATION);
configuration.put(IOfficeApplication.APPLICATION_HOST_KEY, host.replace("http://", ""));
configuration.put(IOfficeApplication.APPLICATION_PORT_KEY, String.valueOf(port));
officeAplication =
OfficeApplicationRuntime.getApplication(configuration);
officeAplication.setConfiguration(configuration);
try {
officeAplication.activate(Notificator.getOfficeMonitor());
} catch (Throwable officeApplicationException) {
try {
Thread.sleep(6666);
Notificator.raiseNotification(Messages.OO_WAITING, false);
} catch (InterruptedException ex) {
}
try {
officeAplication.activate(Notificator.getOfficeMonitor());
} catch (Throwable officeApplicationException1) {
Log.Debug(officeApplicationException1);
Notificator.raiseNotification(officeApplicationException1, true);
}
}
if (officeAplication.isActive()) {
documentService = officeAplication.getDocumentService();
desktopService = officeAplication.getDesktopService();
} else {
throw new RuntimeException("Office " + officeAplication + " cannot get activated .. " + new ArrayList(configuration.values()));
}
setType(TYPE_REMOTE);
} else {
throw new InvalidArgumentException("Host cannot be null and port must be > 0: " + host + ":" + port);
}
return true;
}
public static void killConnection() {
try {
YabsViewProxy.instance().setWaiting(true);
YabsViewProxy.instance().setProgressRunning(true);
try {
if (cachedConnection != null) {
cachedConnection.getDesktopService().terminate();
}
if (officeAplication != null) {
officeAplication.deactivate();
}
} catch (Exception ex) {
Log.Debug(NoaConnectionLocalServer.class, ex.getMessage());
} finally {
cachedConnection = null;
officeAplication = null;
}
} catch (Exception ex) {
Logger.getLogger(NoaConnectionLocalServer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
for (int i = 0; i < ooProcesses.size(); i++) {
Process officeProcess = (Process) ooProcesses.get(i);
if (officeProcess != null) {
officeProcess.destroy();
officeProcess.waitFor();
}
}
} catch (Exception e) {
Logger.getLogger(NoaConnectionLocalServer.class.getName()).log(Level.SEVERE, null, e);
}
YabsViewProxy.instance().setWaiting(false);
YabsViewProxy.instance().setProgressReset();
YabsViewProxy.instance().showOfficeStatus(false, "");
}
}
/**
* -1 indicates no connection.
*
* @return the type
*/
public synchronized int getType() {
return type;
}
/**
* @param type the type to set
*/
private synchronized void setType(int type) {
this.type = type;
}
/**
* @return the documentService
*/
public synchronized IDocumentService getDocumentService() {
return documentService;
}
/**
* @return the desktopService
*/
public synchronized IDesktopService getDesktopService() {
return desktopService;
}
/**
* Tries to start OO in headless server mode.
* <code>Give it at least 3-4 seconds before attempting to use the server.</code>
*
* @param path The path where the OO installation resides
* @param port The port the server shall listen to
* @throws IOException
*/
public synchronized static void startOOServerIfNotRunning(final String path, final int port) {
// final String command = path.replace("\\", "\\\\") + File.separator + LocalSettings.getProperty(LocalSettings.OFFICE_BINARY_FOLDER) + File.separator + "soffice" + " "
// + "-headless" + " "
// + "-nofirststartwizard" + " "
// + "-norestore" + " "
// + "-nolockcheck" + " "
// + "-nocrashreport" + " "
// + "-nodefault" + " "
// + (Main.osIsWindows ? "-accept=socket,host=0.0.0.0,port=" + port + ";urp;StarOffice.Service"
// : "-accept='socket,host=0.0.0.0,port=" + port + ";urp;StarOffice.Service'");
try {
SocketAddress addr = new InetSocketAddress("127.0.0.1", port);
Socket socket = new Socket();
socket.connect(addr, 100);
throw new UnsupportedOperationException("Port " + port + " is already in use :-/. Not going to start OO here.");
} catch (IOException iOException) {
//nothing is running here
}
Runnable runnable = new Runnable() {
public void run() {
if (LocalSettings.hasProperty(LocalSettings.OFFICE_COMMAND)) {
FileExecutor.run(LocalSettings.getProperty(LocalSettings.OFFICE_COMMAND));
} else {
try {
startOOServer(path, port);
} catch (IOException iOException) {
Log.Debug(iOException);
}
}
}
};
Thread t = new Thread(runnable);
t.setDaemon(true);
t.start();
}
/**
* Tries to start OO in headless server mode.
* <code>Give it at least 3-4 seconds before attempting to use the server.</code>
*
* @param path The path where the OO installation resides
* @param port The port the server shall listen to
* @throws IOException
*/
public synchronized static void startOOServer(String path, int port) throws IOException {
FileExecutor.run(getOOArgs(path, port), ooProcesses);
}
}