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());
}
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();
}
}
}
Aggregations