Search in sources :

Example 11 with ServletInfo

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

the class FailureStateHandlerTest method testAddRemoveContext.

@Test
public void testAddRemoveContext() {
    final ServletInfo info1 = new ServletInfo("test", "/test", Collections.<String, String>emptyMap());
    final ServletInfo info2 = new ServletInfo("test", "/test", Collections.<String, String>emptyMap());
    final FailureStateHandler handler = new FailureStateHandler();
    handler.addFailure(info1, 1L, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
    handler.addFailure(info2, 2L, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
    final FailedDTOHolder holder = new FailedDTOHolder();
    handler.getRuntimeInfo(holder);
    assertEquals(2, holder.failedServletDTOs.size());
    assertEquals(DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, holder.failedServletDTOs.get(0).failureReason);
    assertEquals(DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, holder.failedServletDTOs.get(1).failureReason);
    assertContainsExactly(holder.failedServletDTOs, new long[] { 1L, 2L });
    handler.remove(info1, 1L);
    handler.remove(info2, 2L);
    holder.failedServletDTOs.clear();
    handler.getRuntimeInfo(holder);
    assertEquals(0, holder.failedServletDTOs.size());
}
Also used : ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) Test(org.junit.Test)

Example 12 with ServletInfo

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

the class ServletRegistry method removeServlet.

/**
 * Remove a servlet
 * @param info The servlet info
 */
public synchronized void removeServlet(@Nonnull final ServletInfo info, final boolean destroy) {
    if (info.getPatterns() != null) {
        final List<PathResolver> resolvers = new ArrayList<PathResolver>(this.activeResolvers);
        final Map<ServletInfo, RegistrationStatus> newMap = new TreeMap<ServletInfo, ServletRegistry.RegistrationStatus>(this.mapping);
        newMap.remove(info);
        ServletHandler cleanupHandler = null;
        // used for detecting duplicates
        final Set<String> patterns = new HashSet<String>();
        for (final String pattern : info.getPatterns()) {
            if (patterns.contains(pattern)) {
                continue;
            }
            patterns.add(pattern);
            final PathResolver regHandler = this.findResolver(resolvers, pattern);
            if (regHandler != null && regHandler.getServletHandler().getServletInfo().equals(info)) {
                cleanupHandler = regHandler.getServletHandler();
                removeFromNameMapping(cleanupHandler.getName(), cleanupHandler);
                final List<ServletHandler> inactiveList = this.inactiveServletMappings.get(pattern);
                if (inactiveList == null) {
                    resolvers.remove(regHandler);
                } else {
                    boolean done = false;
                    while (!done) {
                        final ServletHandler h = inactiveList.remove(0);
                        boolean activate = h.getServlet() == null;
                        final RegistrationStatus oldStatus = newMap.get(h.getServletInfo());
                        if (oldStatus != null) {
                            final RegistrationStatus newOldStatus = new RegistrationStatus();
                            newOldStatus.handler = oldStatus.handler;
                            newOldStatus.statusToPath = new HashMap<Integer, String[]>(oldStatus.statusToPath);
                            removePattern(newOldStatus, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, pattern);
                            newMap.put(h.getServletInfo(), newOldStatus);
                            done = this.tryToActivate(resolvers, pattern, h, newOldStatus, regHandler);
                            if (done && activate) {
                                this.addToNameMapping(h);
                            }
                        }
                        if (!done) {
                            done = inactiveList.isEmpty();
                        }
                    }
                    if (inactiveList.isEmpty()) {
                        this.inactiveServletMappings.remove(pattern);
                    }
                }
            } else {
                final List<ServletHandler> inactiveList = this.inactiveServletMappings.get(pattern);
                if (inactiveList != null) {
                    final Iterator<ServletHandler> i = inactiveList.iterator();
                    while (i.hasNext()) {
                        final ServletHandler h = i.next();
                        if (h.getServletInfo().equals(info)) {
                            i.remove();
                            cleanupHandler = h;
                            break;
                        }
                    }
                    if (inactiveList.isEmpty()) {
                        this.inactiveServletMappings.remove(pattern);
                    }
                }
            }
        }
        Collections.sort(resolvers);
        this.activeResolvers = resolvers;
        this.mapping = newMap;
        if (cleanupHandler != null) {
            cleanupHandler.dispose();
        }
    } else if (!info.isResource() && info.getName() != null) {
        final Map<ServletInfo, RegistrationStatus> newMap = new TreeMap<ServletInfo, ServletRegistry.RegistrationStatus>(this.mapping);
        final RegistrationStatus status = newMap.remove(info);
        if (status != null) {
            removeFromNameMapping(info.getName(), status.handler);
            this.mapping = newMap;
            status.handler.dispose();
        }
    }
}
Also used : ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

ServletInfo (org.apache.felix.http.base.internal.runtime.ServletInfo)12 Servlet (javax.servlet.Servlet)5 ExtServletContext (org.apache.felix.http.base.internal.context.ExtServletContext)4 HttpServiceServletHandler (org.apache.felix.http.base.internal.handler.HttpServiceServletHandler)4 HashMap (java.util.HashMap)3 ServletHandler (org.apache.felix.http.base.internal.handler.ServletHandler)3 FailedDTOHolder (org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder)3 Test (org.junit.Test)3 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ServletException (javax.servlet.ServletException)2 FilterInfo (org.apache.felix.http.base.internal.runtime.FilterInfo)2 ListenerInfo (org.apache.felix.http.base.internal.runtime.ListenerInfo)2 ResourceInfo (org.apache.felix.http.base.internal.runtime.ResourceInfo)2 Bundle (org.osgi.framework.Bundle)2 BundleContext (org.osgi.framework.BundleContext)2 FailedResourceDTO (org.osgi.service.http.runtime.dto.FailedResourceDTO)2 FailedServletDTO (org.osgi.service.http.runtime.dto.FailedServletDTO)2 IOException (java.io.IOException)1