Re: [xwiki-devs] [xwiki-notifications] r24164 - platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/officeimporter/internal/openoffice
On Oct 1, 2009, at 1:02 PM, asiri (SVN) wrote:
Author: asiri Date: 2009-10-01 13:02:17 +0200 (Thu, 01 Oct 2009) New Revision: 24164
Modified: platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/ officeimporter/internal/openoffice/DefaultOpenOfficeManager.java Log: XWIKI-4121: Restrict openoffice server manipulations to main xwiki.
* Implemented.
* Improved code.
Modified: platform/core/trunk/xwiki-officeimporter/src/main/java/org/ xwiki/officeimporter/internal/openoffice/DefaultOpenOfficeManager.java =================================================================== --- platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/ officeimporter/internal/openoffice/DefaultOpenOfficeManager.java 2009-10-01 09:41:25 UTC (rev 24163) +++ platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/ officeimporter/internal/openoffice/DefaultOpenOfficeManager.java 2009-10-01 11:02:17 UTC (rev 24164) @@ -29,6 +29,7 @@ import net.sf.jodconverter.office.OfficeException; import net.sf.jodconverter.office.OfficeManager;
+import org.xwiki.bridge.DocumentAccessBridge; import org.xwiki.component.annotation.Component; import org.xwiki.component.annotation.Requirement; import org.xwiki.component.logging.AbstractLogEnabled; @@ -52,10 +53,16 @@ private OpenOfficeConfiguration ooConfig;
/** + * Used to query the context document's wiki. + */ + @Requirement + private DocumentAccessBridge docBridge; + + /** * The {@link OfficeManager} used to control / connect openoffice server. */ private OfficeManager officeManager; - + /** * Current oo server process state. */ @@ -64,40 +71,42 @@ /** * Flag indicating whether the officeManager is initialized or not. */ - private boolean officeManagerInitialized; + private boolean initialized;
/** * The {@link OfficeDocumentConverter} used to convert office documents. - */ + */ private OfficeDocumentConverter documentConverter;
/** * Initializes the internal {@link OfficeManager}. */ - private void initializeOfficeManager() throws OpenOfficeManagerException + private void initialize() throws OpenOfficeManagerException { - OfficeConnectionMode connectionMode = OfficeConnectionMode.socket(ooConfig.getServerPort()); - if (ooConfig.getServerType() == OpenOfficeConfiguration.SERVER_TYPE_INTERNAL) { - File officeHome = new File(ooConfig.getHomePath()); - File officeProfile = new File(ooConfig.getProfilePath()); - ManagedProcessOfficeManagerConfiguration configuration = - new ManagedProcessOfficeManagerConfiguration(connectionMode); - configuration.setOfficeHome(officeHome); - configuration.setTemplateProfileDir(officeProfile); - configuration.setMaxTasksPerProcess(ooConfig.getMaxTasksPerProcess()); - configuration .setTaskExecutionTimeout(ooConfig.getTaskExecutionTimeout()); - this.officeManager = new ManagedProcessOfficeManager(configuration); - } else if (ooConfig.getServerType() == OpenOfficeConfiguration.SERVER_TYPE_EXTERNAL_LOCAL) { - ExternalProcessOfficeManager externalProcessOfficeManager = - new ExternalProcessOfficeManager(connectionMode); - externalProcessOfficeManager.setConnectOnStart(true); - this.officeManager = externalProcessOfficeManager; - } else { - this.currentState = ManagerState.CONF_ERROR; - throw new OpenOfficeManagerException("Invalid configuration."); + if (!initialized) { + OfficeConnectionMode connectionMode = OfficeConnectionMode.socket(ooConfig.getServerPort()); + if (ooConfig.getServerType() == OpenOfficeConfiguration.SERVER_TYPE_INTERNAL) { + File officeHome = new File(ooConfig.getHomePath()); + File officeProfile = new File(ooConfig.getProfilePath()); + ManagedProcessOfficeManagerConfiguration configuration = + new ManagedProcessOfficeManagerConfiguration(connectionMode); + configuration.setOfficeHome(officeHome); + configuration.setTemplateProfileDir(officeProfile); + configuration.setMaxTasksPerProcess(ooConfig.getMaxTasksPerProcess()); + configuration .setTaskExecutionTimeout(ooConfig.getTaskExecutionTimeout()); + this.officeManager = new ManagedProcessOfficeManager(configuration); + } else if (ooConfig.getServerType() == OpenOfficeConfiguration.SERVER_TYPE_EXTERNAL_LOCAL) { + ExternalProcessOfficeManager externalProcessOfficeManager = + new ExternalProcessOfficeManager(connectionMode); + externalProcessOfficeManager.setConnectOnStart(true); + this.officeManager = externalProcessOfficeManager; + } else { + this.currentState = ManagerState.CONF_ERROR; + throw new OpenOfficeManagerException("Invalid configuration."); + } + this.documentConverter = new OfficeDocumentConverter(officeManager); + this.initialized = true; } - this.documentConverter = new OfficeDocumentConverter(officeManager); - this.officeManagerInitialized = true; }
/** @@ -121,10 +130,10 @@ */ public void start() throws OpenOfficeManagerException { - if (ManagerState.CONNECTED != currentState) { - if (!officeManagerInitialized) { - initializeOfficeManager(); - } + if (isConnected()) { + return; + } else if (isMainXWiki()) { + initialize(); try { officeManager.start(); currentState = ManagerState.CONNECTED; @@ -133,6 +142,8 @@ currentState = ManagerState.ERROR; throw new OpenOfficeManagerException("Error while connecting / starting openoffice.", ex); } + } else { + throw new OpenOfficeManagerException("OpenOffice server administration is forbidden for sub-wikis."); } }
@@ -141,7 +152,9 @@ */ public void stop() throws OpenOfficeManagerException { - if (ManagerState.CONNECTED == currentState) { + if (!isConnected()) { + return; + } else if (isMainXWiki()) { try { officeManager.stop(); currentState = ManagerState.NOT_CONNECTED; @@ -150,8 +163,31 @@ currentState = ManagerState.ERROR; throw new OpenOfficeManagerException("Error while disconnecting / shutting down openoffice.", ex); } finally { - this.officeManagerInitialized = false; + this.initialized = false; } + } else { + throw new OpenOfficeManagerException("OpenOffice server administration is forbidden for sub-wikis."); } } + + /** + * Utility method for checking if current context document is from main xwiki. + * + * @return true if the current context document is from main xwiki. + */ + private boolean isMainXWiki() + { + String currentWiki = docBridge.getCurrentDocumentName().getWiki(); + return (currentWiki != null) && currentWiki.equals("xwiki");
This is bad.... We shouldn't hardcode the main wiki name. What if we change it later on? We'll need to check every places. It needs to be centralized and there's already a method for this in the XWiki class AFAIK. Thanks -Vincent
+ } + + /** + * Utility method for checking OpenOffice server instance state. + * + * @return true if the OpenOffice server is connected. + */ + private boolean isConnected() + { + return (currentState == ManagerState.CONNECTED); + } }
Hi,
+ private boolean isMainXWiki() + { + String currentWiki = docBridge.getCurrentDocumentName().getWiki(); + return (currentWiki != null) && currentWiki.equals("xwiki");
This is bad....
We shouldn't hardcode the main wiki name. What if we change it later on? We'll need to check every places. It needs to be centralized and there's already a method for this in the XWiki class AFAIK.
I can't find such a method which can be accessed from components. I agree that we need this, I asked this question today in jabber chat, nobody answered :( - Asiri
On Oct 1, 2009, at 1:10 PM, Asiri Rathnayake wrote:
Hi,
+ private boolean isMainXWiki() + { + String currentWiki = docBridge.getCurrentDocumentName().getWiki(); + return (currentWiki != null) && currentWiki.equals("xwiki");
This is bad....
We shouldn't hardcode the main wiki name. What if we change it later on? We'll need to check every places. It needs to be centralized and there's already a method for this in the XWiki class AFAIK.
I can't find such a method which can be accessed from components. I agree that we need this, I asked this question today in jabber chat, nobody answered :(
It's not because everyone is busy that you should do something not right... :) Thanks -Vincent
participants (2)
-
Asiri Rathnayake -
Vincent Massol