Init plugins' AllRequestFilters, even if they are loaded after startup

When installing a plugin with AllRequestFilters after Gerrit has been
started up, the filter's doFilter is called as expected. But the
filter's init method is never called, as FilterProxy did not keep
track of which filters have been around when its own init got called.

This lack of calling init, breaks the Filter contract, and makes the
javamelody plugin throw NPEs [1] for each request when installing it
in a running Gerrit. While restarting Gerrit renders the javamelody
plugin working, the root problem is the missing call to the
filter's init.

Hence, we make FilterProxy call init for plugins that did not get
initialized when FilterProxy itself got initialized. Thereby, the
javamelody plugin can be loaded directly into a running Gerrit again.

[1]
  [2015-08-03 23:20:01,379] WARN  org.eclipse.jetty.servlet.ServletHandler : /
  java.lang.NullPointerException
          at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:170)
          at com.googlesource.gerrit.plugins.javamelody.GerritMonitoringFilter.doFilter(GerritMonitoringFilter.java:73)
  [...]

Change-Id: I095ef517210911c982438d8ce3a7740d05c27eee
This commit is contained in:
Christian Aistleitner
2015-08-04 12:21:45 +02:00
committed by David Pursehouse
parent 4bc04bbb79
commit 40748e5bdc
5 changed files with 344 additions and 11 deletions

View File

@@ -0,0 +1,85 @@
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.extensions.registration;
import static com.google.common.truth.Truth.assertThat;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.inject.Key;
import com.google.inject.util.Providers;
import org.junit.Test;
public class DynamicSetTest {
@Test
public void testContainsWithEmpty() throws Exception {
DynamicSet<Integer> ds = new DynamicSet<>();
assertThat(ds).doesNotContain(2);
}
@Test
public void testContainsTrueWithSingleElement() throws Exception {
DynamicSet<Integer> ds = new DynamicSet<>();
ds.add(2);
assertThat(ds).contains(2);
}
@Test
public void testContainsFalseWithSingleElement() throws Exception {
DynamicSet<Integer> ds = new DynamicSet<>();
ds.add(2);
assertThat(ds).doesNotContain(3);
}
@Test
public void testContainsTrueWithTwoElements() throws Exception {
DynamicSet<Integer> ds = new DynamicSet<>();
ds.add(2);
ds.add(4);
assertThat(ds).contains(4);
}
@Test
public void testContainsFalseWithTwoElements() throws Exception {
DynamicSet<Integer> ds = new DynamicSet<>();
ds.add(2);
ds.add(4);
assertThat(ds).doesNotContain(3);
}
@Test
public void testContainsDynamic() throws Exception {
DynamicSet<Integer> ds = new DynamicSet<>();
ds.add(2);
Key<Integer> key = Key.get(Integer.class);
ReloadableRegistrationHandle<Integer> handle = ds.add(key, Providers.of(4));
ds.add(6);
// At first, 4 is contained.
assertThat(ds).contains(4);
// Then we remove 4.
handle.remove();
// And now 4 should no longer be contained.
assertThat(ds).doesNotContain(4);
}
}