/*
 * 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);
    }
}

@@ -924,62 +924,8 @@
 l;%0A%0A
-import java.util.Collections;%0Aimport java.util.List;%0A%0A
 impo
@@ -1105,24 +1105,130 @@
 entManager;%0A
+import org.xwiki.component.phase.Initializable;%0Aimport org.xwiki.component.phase.InitializationException;%0A
 import org.x
@@ -1244,16 +1244,24 @@
 rvation.
+Abstract
 EventLis
@@ -1300,37 +1300,21 @@
 ion.
-e
+E
 vent
-.ApplicationStartedEvent
+Listener
 ;%0Aim
@@ -1790,16 +1790,46 @@
 istener 
+extends AbstractEventListener 
 implemen
@@ -1844,16 +1844,31 @@
 Listener
+, Initializable
 %0A%7B%0A    /
@@ -2018,212 +2018,269 @@
     
-@Override%0A    public List%3CEvent%3E getEvents()%0A    %7B%0A        return Collections.singletonList((Event) new ApplicationStartedEvent());%0A    %7D%0A%0A    @Override%0A    public String getName()%0A    %7B%0A        return %22C
+/**%0A     * The default constructor.%0A     */%0A    public ComponentManagerBridgeEventListener()%0A    %7B%0A        super(%22Component Manager Bridge Listener%22);%0A    %7D%0A%0A    @Override%0A    public void initialize() throws InitializationException%0A    %7B%0A        // Inject the c
 ompo
@@ -2288,32 +2288,92 @@
 ent 
-M
+m
 anager 
-Bridge Listener%22
+as soon as possible%0A        Utils.setComponentManager(this.componentManager)
 ;%0A  
@@ -2474,57 +2474,58 @@
     
-Utils.setComponentManager(this.componentManager);
+// The work is done in the listener initialization
 %0A   

Make Utils#getComponent available sooner