AbstractDokeosOpenOfficeConnection.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // DokeosConverter using JODConverter - Java OpenDocument Converter
  3. // Eric Marguin <e.marguin@elixir-interactive.com>
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. // http://www.gnu.org/copyleft/lesser.html
  15. //
  16. import java.net.ConnectException;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
  20. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException;
  21. import com.sun.star.beans.XPropertySet;
  22. import com.sun.star.bridge.XBridge;
  23. import com.sun.star.bridge.XBridgeFactory;
  24. import com.sun.star.comp.helper.Bootstrap;
  25. import com.sun.star.connection.NoConnectException;
  26. import com.sun.star.connection.XConnection;
  27. import com.sun.star.connection.XConnector;
  28. import com.sun.star.frame.XComponentLoader;
  29. import com.sun.star.lang.EventObject;
  30. import com.sun.star.lang.XComponent;
  31. import com.sun.star.lang.XEventListener;
  32. import com.sun.star.lang.XMultiComponentFactory;
  33. import com.sun.star.ucb.XFileIdentifierConverter;
  34. import com.sun.star.uno.UnoRuntime;
  35. import com.sun.star.uno.XComponentContext;
  36. public abstract class AbstractDokeosOpenOfficeConnection implements OpenOfficeConnection, XEventListener {
  37. protected final Logger logger = LoggerFactory.getLogger(getClass());
  38. private String connectionString;
  39. private XComponent bridgeComponent;
  40. protected XMultiComponentFactory serviceManager;
  41. protected XBridge bridge;
  42. protected XComponentContext componentContext;
  43. private boolean connected = false;
  44. private boolean expectingDisconnection = false;
  45. protected AbstractDokeosOpenOfficeConnection(String connectionString) {
  46. this.connectionString = connectionString;
  47. }
  48. public synchronized void connect() throws ConnectException {
  49. logger.debug("connecting");
  50. try {
  51. XComponentContext localContext = Bootstrap.createInitialComponentContext(null);
  52. XMultiComponentFactory localServiceManager = localContext.getServiceManager();
  53. XConnector connector = (XConnector) UnoRuntime.queryInterface(XConnector.class,
  54. localServiceManager.createInstanceWithContext("com.sun.star.connection.Connector", localContext));
  55. XConnection connection = connector.connect(connectionString);
  56. XBridgeFactory bridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(XBridgeFactory.class,
  57. localServiceManager.createInstanceWithContext("com.sun.star.bridge.BridgeFactory", localContext));
  58. bridge = bridgeFactory.createBridge("", "urp", connection, null);
  59. bridgeComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, bridge);
  60. bridgeComponent.addEventListener(this);
  61. serviceManager = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class,
  62. bridge.getInstance("StarOffice.ServiceManager"));
  63. XPropertySet properties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, serviceManager);
  64. componentContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class,
  65. properties.getPropertyValue("DefaultContext"));
  66. connected = true;
  67. logger.info("connected");
  68. } catch (NoConnectException connectException) {
  69. throw new ConnectException("connection failed: "+ connectionString +": " + connectException.getMessage());
  70. } catch (Exception exception) {
  71. throw new OpenOfficeException("connection failed: "+ connectionString, exception);
  72. }
  73. }
  74. public synchronized void disconnect() {
  75. logger.debug("disconnecting");
  76. expectingDisconnection = true;
  77. bridgeComponent.dispose();
  78. }
  79. public boolean isConnected() {
  80. return connected;
  81. }
  82. public void disposing(EventObject event) {
  83. connected = false;
  84. if (expectingDisconnection) {
  85. logger.info("disconnected");
  86. } else {
  87. logger.error("disconnected unexpectedly");
  88. }
  89. expectingDisconnection = false;
  90. }
  91. // for unit tests only
  92. void simulateUnexpectedDisconnection() {
  93. disposing(null);
  94. bridgeComponent.dispose();
  95. }
  96. private Object getService(String className) {
  97. try {
  98. if (!connected) {
  99. logger.info("trying to (re)connect");
  100. connect();
  101. }
  102. return serviceManager.createInstanceWithContext(className, componentContext);
  103. } catch (Exception exception) {
  104. throw new OpenOfficeException("could not obtain service: " + className, exception);
  105. }
  106. }
  107. public XComponentLoader getDesktop() {
  108. return (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,
  109. getService("com.sun.star.frame.Desktop"));
  110. }
  111. public XFileIdentifierConverter getFileContentProvider() {
  112. return (XFileIdentifierConverter) UnoRuntime.queryInterface(XFileIdentifierConverter.class,
  113. getService("com.sun.star.ucb.FileContentProvider"));
  114. }
  115. }