You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by te...@apache.org on 2022/05/15 01:06:53 UTC
[pulsar] branch master updated: [improve][broker] Add .yaml suffix for broker filter config file (#15600)
This is an automated email from the ASF dual-hosted git repository.
technoboy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/master by this push:
new 9a8708d510d [improve][broker] Add .yaml suffix for broker filter config file (#15600)
9a8708d510d is described below
commit 9a8708d510d0a2936a9b8c44d0afeb1e39d9e071
Author: lipenghui <pe...@apache.org>
AuthorDate: Sun May 15 09:06:46 2022 +0800
[improve][broker] Add .yaml suffix for broker filter config file (#15600)
---
.../broker/service/plugin/EntryFilterProvider.java | 16 ++++-
.../service/plugin/EntryFilterProviderTest.java | 70 ++++++++++++++++++++++
site2/docs/develop-plugin.md | 2 +-
3 files changed, 84 insertions(+), 4 deletions(-)
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/plugin/EntryFilterProvider.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/plugin/EntryFilterProvider.java
index d92796521ad..48c8d9a7683 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/plugin/EntryFilterProvider.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/plugin/EntryFilterProvider.java
@@ -19,11 +19,13 @@
package org.apache.pulsar.broker.service.plugin;
import static com.google.common.base.Preconditions.checkArgument;
+import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
+import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import lombok.extern.slf4j.Slf4j;
@@ -36,7 +38,8 @@ import org.apache.pulsar.common.util.ObjectMapperFactory;
@Slf4j
public class EntryFilterProvider {
- static final String ENTRY_FILTER_DEFINITION_FILE = "entry_filter.yml";
+ @VisibleForTesting
+ static final String ENTRY_FILTER_DEFINITION_FILE = "entry_filter";
/**
* create entry filter instance.
@@ -112,8 +115,15 @@ public class EntryFilterProvider {
}
}
- private static EntryFilterDefinition getEntryFilterDefinition(NarClassLoader ncl) throws IOException {
- String configStr = ncl.getServiceDefinition(ENTRY_FILTER_DEFINITION_FILE);
+ @VisibleForTesting
+ static EntryFilterDefinition getEntryFilterDefinition(NarClassLoader ncl) throws IOException {
+ String configStr;
+
+ try {
+ configStr = ncl.getServiceDefinition(ENTRY_FILTER_DEFINITION_FILE + ".yaml");
+ } catch (NoSuchFileException e) {
+ configStr = ncl.getServiceDefinition(ENTRY_FILTER_DEFINITION_FILE + ".yml");
+ }
return ObjectMapperFactory.getThreadLocalYaml().readValue(
configStr, EntryFilterDefinition.class
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/plugin/EntryFilterProviderTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/plugin/EntryFilterProviderTest.java
new file mode 100644
index 00000000000..4c8f787b713
--- /dev/null
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/plugin/EntryFilterProviderTest.java
@@ -0,0 +1,70 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.pulsar.broker.service.plugin;
+
+import static org.apache.pulsar.broker.service.plugin.EntryFilterProvider.ENTRY_FILTER_DEFINITION_FILE;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.pulsar.common.nar.NarClassLoader;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+import java.io.IOException;
+import java.nio.file.NoSuchFileException;
+
+@Test(groups = "broker")
+public class EntryFilterProviderTest {
+
+ @Test
+ public void testReadYamlFile() throws IOException {
+ try (NarClassLoader cl = mock(NarClassLoader.class)) {
+ when(cl.getServiceDefinition(ENTRY_FILTER_DEFINITION_FILE + ".yaml"))
+ .thenThrow(new NoSuchFileException(""));
+ try {
+ EntryFilterProvider.getEntryFilterDefinition(cl);
+ Assert.fail();
+ } catch (Exception e) {
+ Assert.assertFalse(e instanceof NoSuchFileException);
+ }
+ }
+ try (NarClassLoader cl = mock(NarClassLoader.class)) {
+ when(cl.getServiceDefinition(ENTRY_FILTER_DEFINITION_FILE + ".yml"))
+ .thenThrow(new NoSuchFileException(""));
+ try {
+ EntryFilterProvider.getEntryFilterDefinition(cl);
+ Assert.fail();
+ } catch (Exception e) {
+ Assert.assertFalse(e instanceof NoSuchFileException);
+ }
+ }
+ try (NarClassLoader cl = mock(NarClassLoader.class)) {
+ when(cl.getServiceDefinition(ENTRY_FILTER_DEFINITION_FILE + ".yaml"))
+ .thenThrow(new NoSuchFileException(""));
+ when(cl.getServiceDefinition(ENTRY_FILTER_DEFINITION_FILE + ".yml"))
+ .thenThrow(new NoSuchFileException(""));
+ try {
+ EntryFilterProvider.getEntryFilterDefinition(cl);
+ Assert.fail();
+ } catch (Exception e) {
+ Assert.assertTrue(e instanceof NoSuchFileException);
+ }
+ }
+ }
+}
diff --git a/site2/docs/develop-plugin.md b/site2/docs/develop-plugin.md
index fe7129923d3..f35c7468c3a 100644
--- a/site2/docs/develop-plugin.md
+++ b/site2/docs/develop-plugin.md
@@ -57,7 +57,7 @@ For how to create a Maven project, see [here](https://maven.apache.org/guides/ge
3. Describe a NAR file.
- Create an `entry_filter.yml` file in the `resources/META-INF/services` directory to describe a NAR file.
+ Create an `entry_filter.yml` or `entry_filter.yaml` file in the `resources/META-INF/services` directory to describe a NAR file.
```conf
# Entry filter name, which should be configured in the broker.conf file later