/*
 * 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.plugin.lucene;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xwiki.container.Container;
import org.xwiki.container.daemon.DaemonContainerException;
import org.xwiki.container.daemon.DaemonContainerInitializer;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.web.Utils;

/**
 * Common methods for all XWiki thread classes in the Lucene plugin.
 */
public abstract class AbstractXWikiRunnable implements Runnable
{
    /**
     * Logging object.
     */
    private static final Log LOG = LogFactory.getLog(IndexUpdater.class);

    protected void initXWikiContainer(XWikiContext context)
    {
        DaemonContainerInitializer dci =
            (DaemonContainerInitializer) Utils.getComponent(DaemonContainerInitializer.ROLE);

        try {
            // Initialize the Container objects
            dci.initializeRequest(context);
        } catch (DaemonContainerException e) {
            // Note: We should raise an exception here but we cannot since XWikiDefaultPlugin has
            // overrident's
            // XWikiPluginInterface's init() method without declaring a throw XWikiException...
            LOG.error("Failed to initialize request. Behavior of the Lucene plugin could be "
                + "instable. We recommend stopping the container, fixing the issue and "
                + "restarting it.", e);
        }
    }

    protected void cleanupXWikiContainer(XWikiContext context)
    {
        Container container = (Container) Utils.getComponent(Container.ROLE);
        // We must ensure we clean the ThreadLocal variables located in the Container
        // component as otherwise we will have a potential memory leak.
        container.removeRequest();
    }
}

@@ -1035,79 +1035,121 @@
 cont
-ainer.Container;%0Aimport org.xwiki.container.daemon.DaemonContainerExcep
+ext.ExecutionContextInitializerManager;%0Aimport org.xwiki.context.ExecutionContext;%0Aimport org.xwiki.context.Execu
 tion
@@ -1175,36 +1175,28 @@
 cont
-ainer.daemon.Daem
+ext.Executi
 onCont
-ainer
+ext
 Init
@@ -1202,16 +1202,25 @@
 tializer
+Exception
 ;%0Aimport
@@ -1612,31 +1612,32 @@
         
-Daem
+Executi
 onCont
-ainer
+ext
 Initiali
@@ -1639,20 +1639,28 @@
 tializer
- d
+Manager e
 ci
+m
  =%0A     
@@ -1666,32 +1666,37 @@
         
+ 
-(Daem
+   (Executi
 onCont
-ainer
+ext
 Initiali
@@ -1698,16 +1698,23 @@
 tializer
+Manager
 ) Utils.
@@ -1726,31 +1726,32 @@
 mponent(
-Daem
+Executi
 onCont
-ainer
+ext
 Initiali
@@ -1757,78 +1757,297 @@
 izer
-.ROLE);%0A%0A        try %7B%0A            // Initialize the Container objects
+Manager.ROLE);%0A        Execution execution = (Execution) Utils.getComponent(Execution.ROLE);%0A%0A        try %7B%0A            ExecutionContext ec = new ExecutionContext();%0A%0A            // Bridge with old XWiki Context, required for old code.%0A            ec.setProperty(%22xwikicontext%22, context);%0A
 %0A   
@@ -2059,11 +2059,12 @@
     
-d
+e
 ci
+m
 .ini
@@ -2074,23 +2074,49 @@
 lize
-Request(c
+(ec);%0A            execution.setC
 ontext
+(ec
 );%0A 
@@ -2135,21 +2135,33 @@
 ch (
-DaemonContain
+ExecutionContextInitializ
 erEx
@@ -2298,31 +2298,16 @@
 rident's
-%0A            //
  XWikiPl
@@ -2428,14 +2428,24 @@
 ize 
-reques
+Execution Contex
 t. B
@@ -2711,40 +2711,34 @@
     
-Container container = (Container
+Execution ech = (Execution
 ) Ut
@@ -2754,25 +2754,25 @@
 mponent(
-Container
+Execution
 .ROLE);%0A
@@ -2847,25 +2847,25 @@
  in the 
-Container
+Execution
 %0A       
@@ -2941,30 +2941,24 @@
     
-container
+ech
 .remove
-Reques
+Contex
 t();

Introduce new Execution Context concept and refactor the Container component accordingly