Search in sources :

Example 1 with OfficeConnection

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();
}
Also used : OfficeConnection(com.haulmont.yarg.formatters.impl.doc.connector.OfficeConnection) NoFreePortsException(com.haulmont.yarg.formatters.impl.doc.connector.NoFreePortsException) ReportingInterruptedException(com.haulmont.yarg.exception.ReportingInterruptedException) BootstrapException(com.sun.star.comp.helper.BootstrapException) OpenOfficeException(com.haulmont.yarg.exception.OpenOfficeException) PreDestroy(javax.annotation.PreDestroy)

Example 2 with OfficeConnection

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);
    }
}
Also used : OpenOfficeException(com.haulmont.yarg.exception.OpenOfficeException) ReportingInterruptedException(com.haulmont.yarg.exception.ReportingInterruptedException) ReportingInterruptedException(com.haulmont.yarg.exception.ReportingInterruptedException) OfficeConnection(com.haulmont.yarg.formatters.impl.doc.connector.OfficeConnection) BootstrapException(com.sun.star.comp.helper.BootstrapException) SecurityContext(org.springframework.security.core.context.SecurityContext)

Aggregations

OpenOfficeException (com.haulmont.yarg.exception.OpenOfficeException)2 ReportingInterruptedException (com.haulmont.yarg.exception.ReportingInterruptedException)2 OfficeConnection (com.haulmont.yarg.formatters.impl.doc.connector.OfficeConnection)2 BootstrapException (com.sun.star.comp.helper.BootstrapException)2 NoFreePortsException (com.haulmont.yarg.formatters.impl.doc.connector.NoFreePortsException)1 PreDestroy (javax.annotation.PreDestroy)1 SecurityContext (org.springframework.security.core.context.SecurityContext)1