Search in sources :

Example 6 with ListenerInfo

use of org.apache.felix.http.base.internal.runtime.ListenerInfo in project felix by apache.

the class WhiteboardManager method removeWhiteboardService.

/**
 * Remove whiteboard service from the registry.
 *
 * @param info The service id of the whiteboard service
 */
public void removeWhiteboardService(final WhiteboardServiceInfo<?> info) {
    synchronized (this.contextMap) {
        if (!failureStateHandler.remove(info)) {
            if (info instanceof PreprocessorInfo) {
                synchronized (this.preprocessorHandlers) {
                    final List<PreprocessorHandler> newList = new ArrayList<>(this.preprocessorHandlers);
                    final Iterator<PreprocessorHandler> iter = newList.iterator();
                    while (iter.hasNext()) {
                        final PreprocessorHandler handler = iter.next();
                        if (handler.getPreprocessorInfo().compareTo((PreprocessorInfo) info) == 0) {
                            iter.remove();
                            this.preprocessorHandlers = newList;
                            updateRuntimeChangeCount();
                            return;
                        }
                    }
                // not found, nothing to do
                }
                return;
            }
            final List<WhiteboardContextHandler> handlerList = this.servicesMap.remove(info);
            if (handlerList != null) {
                for (final WhiteboardContextHandler h : handlerList) {
                    if (!failureStateHandler.remove(info, h.getContextInfo().getServiceId())) {
                        if (info instanceof ListenerInfo && ((ListenerInfo) info).isListenerType(ServletContextListener.class.getName())) {
                            final ListenerHandler handler = h.getRegistry().getEventListenerRegistry().getServletContextListener((ListenerInfo) info);
                            if (handler != null) {
                                final ServletContextListener listener = (ServletContextListener) handler.getListener();
                                if (listener != null) {
                                    EventListenerRegistry.contextDestroyed(handler.getListenerInfo(), listener, new ServletContextEvent(handler.getContext()));
                                }
                            }
                        }
                        this.unregisterWhiteboardService(h, info);
                    }
                }
            }
        }
        this.failureStateHandler.removeAll(info);
    }
    updateRuntimeChangeCount();
}
Also used : ListenerInfo(org.apache.felix.http.base.internal.runtime.ListenerInfo) ServletContextListener(javax.servlet.ServletContextListener) PreprocessorHandler(org.apache.felix.http.base.internal.handler.PreprocessorHandler) ArrayList(java.util.ArrayList) PreprocessorInfo(org.apache.felix.http.base.internal.runtime.PreprocessorInfo) WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ListenerHandler(org.apache.felix.http.base.internal.handler.ListenerHandler) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 7 with ListenerInfo

use of org.apache.felix.http.base.internal.runtime.ListenerInfo in project felix by apache.

the class WhiteboardManager method addWhiteboardService.

/**
 * Add new whiteboard service to the registry
 *
 * @param info Whiteboard service info
 * @return {@code true} if it matches this http service runtime
 */
public boolean addWhiteboardService(@Nonnull final WhiteboardServiceInfo<?> info) {
    // no logging and no DTO if other target service
    if (isMatchingService(info)) {
        if (info.isValid()) {
            if (info instanceof PreprocessorInfo) {
                final PreprocessorHandler handler = new PreprocessorHandler(this.httpBundleContext, this.webContext, ((PreprocessorInfo) info));
                final int result = handler.init();
                if (result == -1) {
                    synchronized (this.preprocessorHandlers) {
                        final List<PreprocessorHandler> newList = new ArrayList<>(this.preprocessorHandlers);
                        newList.add(handler);
                        Collections.sort(newList);
                        this.preprocessorHandlers = newList;
                    }
                } else {
                    this.failureStateHandler.addFailure(info, FAILURE_REASON_VALIDATION_FAILED);
                }
                updateRuntimeChangeCount();
                return true;
            }
            synchronized (this.contextMap) {
                final List<WhiteboardContextHandler> handlerList = this.getMatchingContexts(info);
                this.servicesMap.put(info, handlerList);
                if (handlerList.isEmpty()) {
                    this.failureStateHandler.addFailure(info, FAILURE_REASON_NO_SERVLET_CONTEXT_MATCHING);
                } else {
                    for (final WhiteboardContextHandler h : handlerList) {
                        final int result = this.checkForServletRegistrationInHttpServiceContext(h, info);
                        if (result == -1) {
                            this.registerWhiteboardService(h, info);
                            if (info instanceof ListenerInfo && ((ListenerInfo) info).isListenerType(ServletContextListener.class.getName())) {
                                final ListenerHandler handler = h.getRegistry().getEventListenerRegistry().getServletContextListener((ListenerInfo) info);
                                if (handler != null) {
                                    final ServletContextListener listener = (ServletContextListener) handler.getListener();
                                    if (listener != null) {
                                        EventListenerRegistry.contextInitialized(handler.getListenerInfo(), listener, new ServletContextEvent(handler.getContext()));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else {
            this.failureStateHandler.addFailure(info, FAILURE_REASON_VALIDATION_FAILED);
        }
        updateRuntimeChangeCount();
        return true;
    }
    return false;
}
Also used : ListenerInfo(org.apache.felix.http.base.internal.runtime.ListenerInfo) ServletContextListener(javax.servlet.ServletContextListener) PreprocessorHandler(org.apache.felix.http.base.internal.handler.PreprocessorHandler) ArrayList(java.util.ArrayList) PreprocessorInfo(org.apache.felix.http.base.internal.runtime.PreprocessorInfo) WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ListenerHandler(org.apache.felix.http.base.internal.handler.ListenerHandler) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 8 with ListenerInfo

use of org.apache.felix.http.base.internal.runtime.ListenerInfo in project felix by apache.

the class ServletContextImplTest method setUp.

@Before
public void setUp() {
    this.bundle = Mockito.mock(Bundle.class);
    ServletContext globalContext = new MockServletContext();
    this.httpContext = Mockito.mock(HttpContext.class);
    this.listener = new AttributeListener();
    final HandlerRegistry reg = new HandlerRegistry();
    reg.init();
    contextRegistry = reg.getRegistry(HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID);
    final EventListenerRegistry eventReg = contextRegistry.getEventListenerRegistry();
    final ListenerInfo info = Mockito.mock(ListenerInfo.class);
    when(info.getListenerTypes()).thenReturn(new String[] { ServletContextAttributeListener.class.getName() });
    when(info.isListenerType(ServletContextAttributeListener.class.getName())).thenReturn(true);
    final ListenerHandler handler = Mockito.mock(ListenerHandler.class);
    when(handler.getListenerInfo()).thenReturn(info);
    when(handler.getContextServiceId()).thenReturn(HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID);
    when(handler.getListener()).thenReturn(listener);
    when(handler.init()).thenReturn(-1);
    eventReg.addListeners(handler);
    this.context = new ServletContextImpl(this.bundle, globalContext, this.httpContext, false, contextRegistry);
}
Also used : ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) HandlerRegistry(org.apache.felix.http.base.internal.registry.HandlerRegistry) PerContextHandlerRegistry(org.apache.felix.http.base.internal.registry.PerContextHandlerRegistry) ListenerInfo(org.apache.felix.http.base.internal.runtime.ListenerInfo) Bundle(org.osgi.framework.Bundle) ServletContextImpl(org.apache.felix.http.base.internal.service.ServletContextImpl) HttpContext(org.osgi.service.http.HttpContext) ServletContext(javax.servlet.ServletContext) ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) ListenerHandler(org.apache.felix.http.base.internal.handler.ListenerHandler) EventListenerRegistry(org.apache.felix.http.base.internal.registry.EventListenerRegistry) Before(org.junit.Before)

Aggregations

ListenerInfo (org.apache.felix.http.base.internal.runtime.ListenerInfo)8 ListenerHandler (org.apache.felix.http.base.internal.handler.ListenerHandler)5 WhiteboardListenerHandler (org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler)5 PreprocessorInfo (org.apache.felix.http.base.internal.runtime.PreprocessorInfo)3 ArrayList (java.util.ArrayList)2 ServletContextEvent (javax.servlet.ServletContextEvent)2 ServletContextListener (javax.servlet.ServletContextListener)2 ExtServletContext (org.apache.felix.http.base.internal.context.ExtServletContext)2 PreprocessorHandler (org.apache.felix.http.base.internal.handler.PreprocessorHandler)2 FilterInfo (org.apache.felix.http.base.internal.runtime.FilterInfo)2 ResourceInfo (org.apache.felix.http.base.internal.runtime.ResourceInfo)2 ServletInfo (org.apache.felix.http.base.internal.runtime.ServletInfo)2 Bundle (org.osgi.framework.Bundle)2 IOException (java.io.IOException)1 EventListener (java.util.EventListener)1 ServletContext (javax.servlet.ServletContext)1 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)1 ServletException (javax.servlet.ServletException)1 FilterHandler (org.apache.felix.http.base.internal.handler.FilterHandler)1 HttpServiceServletHandler (org.apache.felix.http.base.internal.handler.HttpServiceServletHandler)1