Search in sources :

Example 1 with ListenerHandler

use of org.apache.felix.http.base.internal.handler.ListenerHandler in project felix by apache.

the class AbstractListenerManager method addingService.

@Override
public ServiceReference<EventListener> addingService(final ServiceReference<EventListener> reference) {
    SystemLogger.warning("Deprecation warning: " + "Listener registered through Apache Felix whiteboard service: " + reference + ". Please change your code to the OSGi Http Whiteboard Service.", null);
    final ListenerInfo info = new ListenerInfo(reference, true);
    this.allInfos.put((Long) reference.getProperty(Constants.SERVICE_ID), info);
    final ListenerHandler listenerHandler = new WhiteboardListenerHandler(HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID, // context is only relevant for ServletContextListener which is not supported here
    null, info, this.context);
    synchronized (lock) {
        registry.addListeners(listenerHandler);
    }
    return reference;
}
Also used : WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ListenerInfo(org.apache.felix.http.base.internal.runtime.ListenerInfo) WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ListenerHandler(org.apache.felix.http.base.internal.handler.ListenerHandler)

Example 2 with ListenerHandler

use of org.apache.felix.http.base.internal.handler.ListenerHandler in project felix by apache.

the class ListenerMap method getActiveListeners.

public Iterable<T> getActiveListeners() {
    final Iterator<ListenerHandler> iter = this.getActiveHandlers().iterator();
    final Iterator<T> newIter = new Iterator<T>() {

        private T next;

        {
            peek();
        }

        @SuppressWarnings("unchecked")
        private void peek() {
            next = null;
            while (next == null && iter.hasNext()) {
                final ListenerHandler handler = iter.next();
                next = (T) handler.getListener();
            }
        }

        @Override
        public boolean hasNext() {
            return this.next != null;
        }

        @Override
        public T next() {
            if (this.next == null) {
                throw new NoSuchElementException();
            }
            final T result = this.next;
            peek();
            return result;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    return new Iterable<T>() {

        @Override
        public Iterator<T> iterator() {
            return newIter;
        }
    };
}
Also used : Iterator(java.util.Iterator) ListenerHandler(org.apache.felix.http.base.internal.handler.ListenerHandler) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with ListenerHandler

use of org.apache.felix.http.base.internal.handler.ListenerHandler in project felix by apache.

the class WhiteboardManager method registerWhiteboardService.

/**
 * Register whiteboard service in the http service
 * @param handler Context handler
 * @param info Whiteboard service info
 */
private void registerWhiteboardService(final WhiteboardContextHandler handler, final WhiteboardServiceInfo<?> info) {
    try {
        int failureCode = -1;
        if (info instanceof ServletInfo) {
            final ExtServletContext servletContext = handler.getServletContext(info.getServiceReference().getBundle());
            if (servletContext == null) {
                failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
            } else {
                final ServletHandler servletHandler = new WhiteboardServletHandler(handler.getContextInfo().getServiceId(), servletContext, (ServletInfo) info, handler.getBundleContext(), info.getServiceReference().getBundle(), this.httpBundleContext.getBundle());
                handler.getRegistry().registerServlet(servletHandler);
            }
        } else if (info instanceof FilterInfo) {
            final ExtServletContext servletContext = handler.getServletContext(info.getServiceReference().getBundle());
            if (servletContext == null) {
                failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
            } else {
                final FilterHandler filterHandler = new WhiteboardFilterHandler(handler.getContextInfo().getServiceId(), servletContext, (FilterInfo) info, handler.getBundleContext());
                handler.getRegistry().registerFilter(filterHandler);
            }
        } else if (info instanceof ResourceInfo) {
            final ServletInfo servletInfo = ((ResourceInfo) info).getServletInfo();
            final ExtServletContext servletContext = handler.getServletContext(info.getServiceReference().getBundle());
            if (servletContext == null) {
                failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
            } else {
                final ServletHandler servleHandler = new HttpServiceServletHandler(handler.getContextInfo().getServiceId(), servletContext, servletInfo, new ResourceServlet(servletInfo.getPrefix()));
                handler.getRegistry().registerServlet(servleHandler);
            }
        } else if (info instanceof ListenerInfo) {
            final ExtServletContext servletContext = handler.getServletContext(info.getServiceReference().getBundle());
            if (servletContext == null) {
                failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
            } else {
                final ListenerHandler listenerHandler = new WhiteboardListenerHandler(handler.getContextInfo().getServiceId(), servletContext, (ListenerInfo) info, handler.getBundleContext());
                handler.getRegistry().registerListeners(listenerHandler);
            }
        } else {
            // This should never happen, but we log anyway
            SystemLogger.error("Unknown whiteboard service " + info.getServiceReference(), null);
        }
        if (failureCode != -1) {
            this.failureStateHandler.addFailure(info, handler.getContextInfo().getServiceId(), failureCode);
        }
    } catch (final Exception e) {
        this.failureStateHandler.addFailure(info, handler.getContextInfo().getServiceId(), FAILURE_REASON_UNKNOWN, e);
    }
}
Also used : WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ResourceInfo(org.apache.felix.http.base.internal.runtime.ResourceInfo) WhiteboardServletHandler(org.apache.felix.http.base.internal.handler.WhiteboardServletHandler) ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext) WhiteboardFilterHandler(org.apache.felix.http.base.internal.handler.WhiteboardFilterHandler) WhiteboardFilterHandler(org.apache.felix.http.base.internal.handler.WhiteboardFilterHandler) FilterHandler(org.apache.felix.http.base.internal.handler.FilterHandler) ServletException(javax.servlet.ServletException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) ListenerInfo(org.apache.felix.http.base.internal.runtime.ListenerInfo) ResourceServlet(org.apache.felix.http.base.internal.service.ResourceServlet) WhiteboardServletHandler(org.apache.felix.http.base.internal.handler.WhiteboardServletHandler) FilterInfo(org.apache.felix.http.base.internal.runtime.FilterInfo) WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ListenerHandler(org.apache.felix.http.base.internal.handler.ListenerHandler)

Example 4 with ListenerHandler

use of org.apache.felix.http.base.internal.handler.ListenerHandler in project felix by apache.

the class FilterRegistryTest method testSingleListener.

@Test
public void testSingleListener() throws InvalidSyntaxException, ServletException {
    final EventListenerRegistry reg = new EventListenerRegistry();
    final FailedDTOHolder holder = new FailedDTOHolder();
    final ServletContextDTO dto = new ServletContextDTO();
    // check DTO
    reg.getRuntimeInfo(dto, holder.failedListenerDTOs);
    assertEmpty(dto, holder);
    // register listener
    final ListenerHandler h1 = createListenerHandler(1L, 0, ServletContextListener.class);
    reg.addListeners(h1);
    // one entry in DTO
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedListenerDTOs);
    assertTrue(holder.failedListenerDTOs.isEmpty());
    assertNotNull(dto.listenerDTOs);
    assertEquals(1, dto.listenerDTOs.length);
    assertEquals(1, dto.listenerDTOs[0].types.length);
    assertEquals(ServletContextListener.class.getName(), dto.listenerDTOs[0].types[0]);
    // remove listener
    reg.removeListeners(h1.getListenerInfo());
    // empty again
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedListenerDTOs);
    assertEmpty(dto, holder);
}
Also used : FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) ServletContextListener(javax.servlet.ServletContextListener) WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ListenerHandler(org.apache.felix.http.base.internal.handler.ListenerHandler) Test(org.junit.Test)

Example 5 with ListenerHandler

use of org.apache.felix.http.base.internal.handler.ListenerHandler in project felix by apache.

the class ListenerMap method getActiveHandlers.

public Iterable<ListenerHandler> getActiveHandlers() {
    final List<ListenerRegistrationStatus<T>> list = this.handlers;
    final Iterator<ListenerRegistrationStatus<T>> iter = list.iterator();
    final Iterator<ListenerHandler> newIter = new Iterator<ListenerHandler>() {

        private ListenerHandler next;

        {
            peek();
        }

        private void peek() {
            next = null;
            if (iter.hasNext()) {
                final ListenerRegistrationStatus<T> status = iter.next();
                if (status.getResult() == -1) {
                    next = status.getHandler();
                }
            }
        }

        @Override
        public boolean hasNext() {
            return this.next != null;
        }

        @Override
        public ListenerHandler next() {
            if (this.next == null) {
                throw new NoSuchElementException();
            }
            final ListenerHandler result = this.next;
            peek();
            return result;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    return new Iterable<ListenerHandler>() {

        @Override
        public Iterator<ListenerHandler> iterator() {
            return newIter;
        }
    };
}
Also used : Iterator(java.util.Iterator) ListenerHandler(org.apache.felix.http.base.internal.handler.ListenerHandler) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

ListenerHandler (org.apache.felix.http.base.internal.handler.ListenerHandler)8 WhiteboardListenerHandler (org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler)5 ListenerInfo (org.apache.felix.http.base.internal.runtime.ListenerInfo)5 ServletContextListener (javax.servlet.ServletContextListener)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 NoSuchElementException (java.util.NoSuchElementException)2 ServletContextEvent (javax.servlet.ServletContextEvent)2 PreprocessorHandler (org.apache.felix.http.base.internal.handler.PreprocessorHandler)2 PreprocessorInfo (org.apache.felix.http.base.internal.runtime.PreprocessorInfo)2 IOException (java.io.IOException)1 ServletContext (javax.servlet.ServletContext)1 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)1 ServletException (javax.servlet.ServletException)1 ExtServletContext (org.apache.felix.http.base.internal.context.ExtServletContext)1 FilterHandler (org.apache.felix.http.base.internal.handler.FilterHandler)1 HttpServiceServletHandler (org.apache.felix.http.base.internal.handler.HttpServiceServletHandler)1 ServletHandler (org.apache.felix.http.base.internal.handler.ServletHandler)1 WhiteboardFilterHandler (org.apache.felix.http.base.internal.handler.WhiteboardFilterHandler)1 WhiteboardServletHandler (org.apache.felix.http.base.internal.handler.WhiteboardServletHandler)1