use of com.haulmont.yarg.formatters.impl.doc.connector.OfficeConnection in project jmix by jmix-framework.
the class JmixOfficeIntegration method destroyOfficeIntegration.
@PreDestroy
protected void destroyOfficeIntegration() {
connectionsQueue.clear();
for (OfficeConnection connection : connections) {
try {
connection.close();
} catch (Exception e) {
// Do nothing
}
}
executor.shutdown();
}
use of com.haulmont.yarg.formatters.impl.doc.connector.OfficeConnection in project jmix by jmix-framework.
the class JmixOfficeIntegration method runTaskWithTimeout.
@Override
public void runTaskWithTimeout(final OfficeTask officeTask, int timeoutInSeconds) throws NoFreePortsException {
final SecurityContext securityContext = SecurityContextHolder.getContext();
final OfficeConnection connection = acquireConnection();
Future future = null;
try {
Callable<Void> task = () -> {
SecurityContextHolder.setContext(securityContext);
connection.open();
officeTask.processTaskInOpenOffice(connection.getOOResourceProvider());
SecurityContextHolder.clearContext();
return null;
};
future = executor.submit(task);
future.get(timeoutInSeconds, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
throw new ReportingInterruptedException("LibreOffice task interrupted");
} catch (ExecutionException ex) {
connection.close();
if (ex.getCause() instanceof BootstrapException) {
throw new OpenOfficeException("Failed to connect to LibreOffice. Please check LibreOffice path " + openOfficePath, ex);
}
if (ex.getCause() instanceof OpenOfficeException) {
throw (OpenOfficeException) ex.getCause();
}
throw new RuntimeException(ex.getCause());
} catch (OpenOfficeException ex) {
connection.close();
throw ex;
} catch (TimeoutException tex) {
try {
if (Thread.interrupted()) {
throw new ReportingInterruptedException("LibreOffice task interrupted");
}
} finally {
connection.close();
}
if (tex.getCause() instanceof BootstrapException) {
throw new OpenOfficeException("Failed to connect to LibreOffice. Please check LibreOffice path " + openOfficePath, tex);
}
throw new OpenOfficeException(tex);
} catch (Throwable ex) {
connection.close();
if (ex.getCause() instanceof BootstrapException) {
throw new OpenOfficeException("Failed to connect to LibreOffice. Please check LibreOffice path " + openOfficePath, ex);
}
throw new OpenOfficeException(ex);
} finally {
if (future != null) {
future.cancel(true);
}
releaseConnection(connection);
}
}
Aggregations