<commit_before>/*
 * 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.context.ExecutionContext;
import org.xwiki.context.Execution;
import org.xwiki.context.ExecutionContextException;
import org.xwiki.context.ExecutionContextManager;

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)
    {
        ExecutionContextManager ecim =
                (ExecutionContextManager) Utils.getComponent(ExecutionContextManager.class);
        Execution 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) {
            // 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 Execution Context. 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)
    {
        Execution ech = (Execution) Utils.getComponent(Execution.class);
        // We must ensure we clean the ThreadLocal variables located in the Execution
        // component as otherwise we will have a potential memory leak.
        ech.removeContext();
    }
}<commit_msg>[cleanup] Apply codestyle<commit_after>/*
 * 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.context.ExecutionContext;
import org.xwiki.context.Execution;
import org.xwiki.context.ExecutionContextException;
import org.xwiki.context.ExecutionContextManager;

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)
    {
        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) {
            // 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 Execution Context. 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)
    {
        Execution ech = Utils.getComponent(Execution.class);
        // We must ensure we clean the ThreadLocal variables located in the Execution
        // component as otherwise we will have a potential memory leak.
        ech.removeContext();
    }
}
{'fraction_non_alphanumeric': 0.07294361096740817, 'fraction_numerical': 0.004483531643386791, 'mean_word_length': 3.20899854862119, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2897768', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1438, 'n_words': 581}