提交 1e8bfc8a 编写于 作者: K Kohsuke Kawaguchi

Eliminate the use of static field to keep filters.

This helps with the situation where multiple Jenkins instances exist in memory (at the same time or at different times sequentially), such as during unit tests.
上级 b47d450d
/*
* The MIT License
*
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
......@@ -25,17 +25,22 @@ package hudson.util;
import hudson.ExtensionPoint;
import hudson.security.SecurityRealm;
import jenkins.model.Jenkins;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Servlet {@link Filter} that chains multiple {@link Filter}s, provided by plugins
......@@ -51,61 +56,82 @@ import java.util.Vector;
* @see SecurityRealm
*/
public class PluginServletFilter implements Filter, ExtensionPoint {
private final List<Filter> list = new CopyOnWriteArrayList<Filter>();
private /*almost final*/ FilterConfig config;
private static final List<Filter> LIST = new Vector<Filter>();
private static FilterConfig filterConfig;
public PluginServletFilter() {
/**
* For backward compatibility with plugins that might register filters before Jenkins.getInstance()
* starts functioning, when we are not sure which Jenkins instance a filter belongs to, put it here,
* and let the first Jenkins instance take over.
*/
private static final List<Filter> LEGACY = new Vector<Filter>();
private static final String KEY = PluginServletFilter.class.getName();
/**
* Lookup the instance from servlet context.
*/
private static PluginServletFilter getInstance(ServletContext c) {
return (PluginServletFilter)c.getAttribute(KEY);
}
@edu.umd.cs.findbugs.annotations.SuppressWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
public void init(FilterConfig filterConfig) throws ServletException {
PluginServletFilter.filterConfig = filterConfig;
synchronized (LIST) {
for (Filter f : LIST) {
f.init(filterConfig);
}
}
public void init(FilterConfig config) throws ServletException {
this.config = config;
synchronized (LEGACY) {
list.addAll(LEGACY);
LEGACY.clear();
}
for (Filter f : list) {
f.init(config);
}
config.getServletContext().setAttribute(KEY,this);
}
public static void addFilter(Filter filter) throws ServletException {
synchronized (LIST) {
if (filterConfig != null) {
filter.init(filterConfig);
}
LIST.add(filter);
}
Jenkins j = Jenkins.getInstance();
if (j==null) {
// report who is doing legacy registration
LOGGER.log(Level.WARNING, "Filter instance is registered too early: "+filter, new Exception());
LEGACY.add(filter);
} else {
PluginServletFilter container = getInstance(j.servletContext);
filter.init(container.config);
container.list.add(filter);
}
}
public static void removeFilter(Filter filter) throws ServletException {
synchronized (LIST) {
LIST.remove(filter);
}
Jenkins j = Jenkins.getInstance();
if (j==null) {
LEGACY.remove(filter);
} else {
getInstance(j.servletContext).list.remove(filter);
}
}
public void doFilter(ServletRequest request, ServletResponse response, final FilterChain chain) throws IOException, ServletException {
new FilterChain() {
private int position=0;
// capture the array for thread-safety
private final Filter[] filters = LIST.toArray(new Filter[LIST.size()]);
private final Iterator<Filter> itr = list.iterator();
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
if(position==filters.length) {
if(itr.hasNext()) {
// call next
itr.next().doFilter(request, response, this);
} else {
// reached to the end
chain.doFilter(request,response);
} else {
// call next
filters[position++].doFilter(request,response,this);
}
}
}.doFilter(request,response);
}
public void destroy() {
synchronized (LIST) {
for (Filter f : LIST)
f.destroy();
for (Filter f : list) {
f.destroy();
}
list.clear();
}
private static final Logger LOGGER = Logger.getLogger(PluginServletFilter.class.getName());
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册