<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.internal;

import java.util.Collections;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.observation.EventListener;
import org.xwiki.observation.event.ApplicationStartedEvent;
import org.xwiki.observation.event.Event;

import com.xpn.xwiki.web.Utils;

/**
 * This is a temporary bridge to allow non components to call Utils.getComponent() and get a component instance without
 * having to pass around a XWiki Context (in order to retrieve the Servlet Context to get the component manager from an
 * attribute).
 *
 * @version $Id$
 * @since 2.0M2
 */
@Component
@Singleton
@Named("componentManagerBridge")
public class ComponentManagerBridgeEventListener implements EventListener
{
    /**
     * Provided to {@link Utils#setComponentManager(ComponentManager)}.
     */
    @Inject
    private ComponentManager componentManager;

    @Override
    public List<Event> getEvents()
    {
        return Collections.singletonList((Event) new ApplicationStartedEvent());
    }

    @Override
    public String getName()
    {
        return "Component Manager Bridge Listener";
    }

    @Override
    public void onEvent(Event event, Object source, Object data)
    {
        Utils.setComponentManager(this.componentManager);
    }
}<commit_msg>XWIKI-16594: Make Utils#getComponent available sooner<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.internal;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.component.phase.Initializable;
import org.xwiki.component.phase.InitializationException;
import org.xwiki.observation.AbstractEventListener;
import org.xwiki.observation.EventListener;
import org.xwiki.observation.event.Event;

import com.xpn.xwiki.web.Utils;

/**
 * This is a temporary bridge to allow non components to call Utils.getComponent() and get a component instance without
 * having to pass around a XWiki Context (in order to retrieve the Servlet Context to get the component manager from an
 * attribute).
 *
 * @version $Id$
 * @since 2.0M2
 */
@Component
@Singleton
@Named("componentManagerBridge")
public class ComponentManagerBridgeEventListener extends AbstractEventListener implements EventListener, Initializable
{
    /**
     * Provided to {@link Utils#setComponentManager(ComponentManager)}.
     */
    @Inject
    private ComponentManager componentManager;

    /**
     * The default constructor.
     */
    public ComponentManagerBridgeEventListener()
    {
        super("Component Manager Bridge Listener");
    }

    @Override
    public void initialize() throws InitializationException
    {
        // Inject the component manager as soon as possible
        Utils.setComponentManager(this.componentManager);
    }

    @Override
    public void onEvent(Event event, Object source, Object data)
    {
        // The work is done in the listener initialization
    }
}
{'fraction_non_alphanumeric': 0.07565059511801493, 'fraction_numerical': 0.007464192051644139, 'mean_word_length': 3.967935871743487, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1095957', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1258, 'n_words': 515}