/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.officeimporter.internal.openoffice;

import org.artofsolving.jodconverter.office.OfficeUtils;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.configuration.ConfigurationSource;
import org.xwiki.officeimporter.openoffice.OpenOfficeConfiguration;

/**
 * Default implementation of {@link OpenOfficeConfiguration}.
 * 
 * @version $Id$
 * @since 1.8RC3
 */
@Component
public class DefaultOpenOfficeConfiguration implements OpenOfficeConfiguration
{
    /**
     * Prefix for configuration keys for the OpenOffice module.
     */
    private static final String PREFIX = "openoffice.";

    /**
     * @see OpenOfficeConfiguration#getServerType()
     */
    private static final int DEFAULT_SERVER_TYPE = SERVER_TYPE_INTERNAL;

    /**
     * @see OpenOfficeConfiguration#getServerPort();
     */
    private static final int DEFAULT_SERVER_PORT = 8100;

    /**
     * @see OpenOfficeConfiguration#getHomePath()
     */
    private static final String DEFAULT_HOME_PATH = OfficeUtils.getDefaultOfficeHome().getAbsolutePath();

    /**
     * @see OpenOfficeConfiguration#isAutoStart()
     */
    private static final boolean DEFAULT_AUTO_START = false;

    /**
     * @see OpenOfficeConfiguration#getMaxTasksPerProcess()
     */
    private static final int DEFAULT_MAX_TASKS_PER_PROCESS = 50;

    /**
     * @see OpenOfficeConfiguration#getTaskExecutionTimeout()
     */
    private static final long DEFAULT_TASK_EXECUTION_TIMEOUT = 30000L;
    
    /**
     * Defines from where to read the rendering configuration data.
     */
    @Requirement
    private ConfigurationSource configuration;

    /**
     * {@inheritDoc}
     * @see OpenOfficeConfiguration#getServerType()
     */
    public int getServerType()
    {
        return this.configuration.getProperty(PREFIX + "serverType", DEFAULT_SERVER_TYPE);
    }

    /**
     * {@inheritDoc}
     * @see OpenOfficeConfiguration#getServerPort()
     */
    public int getServerPort()
    {
        return this.configuration.getProperty(PREFIX + "serverPort", DEFAULT_SERVER_PORT);
    }

    /**
     * {@inheritDoc}
     * @see OpenOfficeConfiguration#isAutoStart()
     */
    public boolean isAutoStart()
    {
        return this.configuration.getProperty(PREFIX + "autoStart", DEFAULT_AUTO_START);
    }

    /**
     * {@inheritDoc}
     * @see OpenOfficeConfiguration#getHomePath()
     */
    public String getHomePath()
    {
        return this.configuration.getProperty(PREFIX + "homePath", DEFAULT_HOME_PATH);
    }

    /**
     * {@inheritDoc}
     * @see OpenOfficeConfiguration#getProfilePath()
     */
    public String getProfilePath()
    {
        return this.configuration.getProperty(PREFIX + "profilePath");
    }

    /**
     * {@inheritDoc}
     * @see OpenOfficeConfiguration#getMaxTasksPerProcess()
     */
    public int getMaxTasksPerProcess()
    {
        return this.configuration.getProperty(PREFIX + "maxTasksPerProcess", DEFAULT_MAX_TASKS_PER_PROCESS);
    }

    /**
     * {@inheritDoc}
     * @see OpenOfficeConfiguration#getTaskExecutionTimeout()
     */
    public long getTaskExecutionTimeout()
    {
        return this.configuration.getProperty(PREFIX + "taskExecutionTimeout", DEFAULT_TASK_EXECUTION_TIMEOUT);
    }
}

@@ -942,16 +942,67 @@
 ffice;%0A%0A
+import java.io.File;%0A%0Aimport javax.inject.Inject;%0A%0A
 import o
@@ -1130,30 +1130,85 @@
 ent.
-annotation.Requirement
+phase.Initializable;%0Aimport org.xwiki.component.phase.InitializationException
 ;%0Aim
@@ -1523,16 +1523,31 @@
 guration
+, Initializable
 %0A%7B%0A    /
@@ -1951,181 +1951,8 @@
 0;%0A%0A
-    /**%0A     * @see OpenOfficeConfiguration#getHomePath()%0A     */%0A    private static final String DEFAULT_HOME_PATH = OfficeUtils.getDefaultOfficeHome().getAbsolutePath();%0A%0A
     
@@ -2366,20 +2366,119 @@
 30000L;%0A
+%0A
     
+/**%0A     * @see OpenOfficeConfiguration#getHomePath()%0A     */%0A    private String defaultHomePath;%0A
 %0A    /**
@@ -2563,18 +2563,13 @@
    @
-Requiremen
+Injec
 t%0A  
@@ -2635,32 +2635,40 @@
 * %7B@inheritDoc%7D%0A
+     * %0A
      * @see Open
@@ -2867,32 +2867,40 @@
 * %7B@inheritDoc%7D%0A
+     * %0A
      * @see Open
@@ -3099,32 +3099,40 @@
 * %7B@inheritDoc%7D%0A
+     * %0A
      * @see Open
@@ -3329,32 +3329,40 @@
 * %7B@inheritDoc%7D%0A
+     * %0A
      * @see Open
@@ -3512,25 +3512,23 @@
 h%22, 
-DEFAULT_HOME_PATH
+defaultHomePath
 );%0A 
@@ -3554,32 +3554,40 @@
 * %7B@inheritDoc%7D%0A
+     * %0A
      * @see Open
@@ -3771,32 +3771,40 @@
 * %7B@inheritDoc%7D%0A
+     * %0A
      * @see Open
@@ -4041,24 +4041,32 @@
 inheritDoc%7D%0A
+     * %0A
      * @see 
@@ -4288,11 +4288,366 @@
 );%0A    %7D
+%0A%0A    /**%0A     * %7B@inheritDoc%7D%0A     * %0A     * @see Initializable#initialize()%0A     */%0A    @Override%0A    public void initialize() throws InitializationException%0A    %7B%0A        File defaultHomeFolder = OfficeUtils.getDefaultOfficeHome();%0A        defaultHomePath = defaultHomeFolder != null ? defaultHomeFolder.getAbsolutePath() : %22/usr/lib/openoffice%22;%0A    %7D
 %0A%7D%0A

Update jodconverter to support OpenOffice 3.3 Fixed DefaultOpenOfficeConfiguration initialization due to a change in OfficeUtils.getDefaultOfficeHome() implementation, which now can return null.