/*
 * 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 com.xpn.xwiki.tool.backup;

import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiConfig;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.web.Utils;
import com.xpn.xwiki.web.XWikiServletURLFactory;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.xwiki.component.embed.EmbeddableComponentManager;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.context.Execution;
import org.xwiki.context.ExecutionContext;
import org.xwiki.context.ExecutionContextException;
import org.xwiki.context.ExecutionContextManager;

/**
 * Common code for importing and exporting.
 * 
 * @version $Id$
 */
public class AbstractPackager
{
    /**
     * @param databaseName some database name (TODO: find out what this name is really)
     * @param hibernateConfig the Hibernate config fill containing the database definition (JDBC driver, username and
     *            password, etc)
     * @return a valid XWikiContext using the passed Hibernate configuration and passed database name
     * @throws Exception failed to initialize context.
     * @todo Replace the Hibernate config file with a list of parameters required for the packaging operation
     */
    protected XWikiContext createXWikiContext(String databaseName, File hibernateConfig) throws Exception
    {
        EmbeddableComponentManager ecm = new EmbeddableComponentManager();
        ecm.initialize(this.getClass().getClassLoader());
        Utils.setComponentManager(ecm);

        // We need to initialize the Component Manager so that the components can be looked up
        XWikiContext context = new XWikiContext();
        context.put(ComponentManager.class.getName(), ecm);

        // Initialize the Container fields (request, response, session).
        ExecutionContextManager ecim = Utils.getComponent(ExecutionContextManager.class);
        Execution execution = Utils.getComponent(Execution.class);
        try {
            ExecutionContext ec = new ExecutionContext();

            // Bridge with old XWiki Context, required for old code.
            ec.setProperty("xwikicontext", context);

            ecim.initialize(ec);
            execution.setContext(ec);
        } catch (ExecutionContextException e) {
            throw new Exception("Failed to initialize Execution Context.", e);
        }
        
        context.setUser("XWiki.superadmin");
        context.setDatabase(databaseName);
        context.setMainXWiki(databaseName);

        XWikiConfig config = new XWikiConfig();
        config.put("xwiki.store.class", "com.xpn.xwiki.store.XWikiHibernateStore");

        // The XWikiConfig object requires path to be in unix format (i.e. with forward slashes)
        String hibernateConfigInUnixFormat = hibernateConfig.getPath().replace('\\', '/');
        config.put("xwiki.store.hibernate.path", hibernateConfigInUnixFormat);

        config.put("xwiki.store.hibernate.updateschema", "1");
        config.put("xwiki.virtual", "1");

        // Enable backlinks so that when documents are imported their backlinks will be saved too
        config.put("xwiki.backlinks", "1");

        new XWiki(config, context);

        try {
            context.setURLFactory(new XWikiServletURLFactory(new URL("http://localhost:8080"), "xwiki/", "bin/"));
        } catch (MalformedURLException e) {
            // TODO: Remove that way of creating exceptions in XWiki as it's a real plain and
            // doesn't work with external code.
            throw new XWikiException(XWikiException.MODULE_XWIKI_PLUGINS, XWikiException.ERROR_XWIKI_UNKNOWN,
                "Failed to set up URL Factory", e);
        }

        return context;
    }
}

@@ -1057,16 +1057,56 @@
 .XWiki;%0A
+import com.xpn.xwiki.doc.XWikiDocument;%0A
 import c
@@ -1129,16 +1129,16 @@
 .Utils;%0A
-
 import c
@@ -1555,16 +1555,68 @@
 Manager;
+%0Aimport org.xwiki.model.reference.DocumentReference;
 %0A%0A/**%0A *
@@ -3501,24 +3501,766 @@
 baseName);%0A%0A
+        // Use a dummy URL so that XWiki's initialization can create a Servlet URL Factory. We could also have%0A        // registered a custom XWikiURLFactory against XWikiURLFactoryService but it's more work.%0A        context.setURL(new URL(%22http://localhost/xwiki/bin/DummyAction/DumySpace/DummyPage%22));%0A%0A        // Set a dummy Document in the context to act as the current document since when a document containing%0A        // objects is imported it'll generate Object diff events and the algorithm to compute an object diff%0A        // currently requires rendering object properties, which requires a current document in the context.%0A        context.setDoc(new XWikiDocument(new DocumentReference(databaseName, %22dummySpace%22, %22dummyPage%22)));%0A%0A
         XWik
@@ -4889,32 +4889,32 @@
 klinks%22, %221%22);%0A%0A
-
         new XWik
@@ -4922,32 +4922,44 @@
 (config, context
+, null, true
 );%0A%0A        try 

Remove error/warning stack traces being printed in the console when importing a XAR * 2 errors have been fixed, the third one (the missing xwiki.properties file has been fixed in core but requires that this plugin update its dependency to 3.0M1 or beyond)