-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use SPI in High Level Rest Client to load XContent parsers (#25098)
This commit adds a NamedXContentProvider interface that can be implemented by plugins or modules using Java's SPI feature in order to provide additional NamedXContent parsers to external applications like the Java High Level Rest Client.
- Loading branch information
Showing
10 changed files
with
269 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
core/src/main/java/org/elasticsearch/plugins/spi/NamedXContentProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.plugins.spi; | ||
|
||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Provides named XContent parsers. | ||
*/ | ||
public interface NamedXContentProvider { | ||
|
||
/** | ||
* @return a list of {@link NamedXContentRegistry.Entry} that this plugin provides. | ||
*/ | ||
List<NamedXContentRegistry.Entry> getNamedXContentParsers(); | ||
} |
25 changes: 25 additions & 0 deletions
25
core/src/main/java/org/elasticsearch/plugins/spi/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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. | ||
*/ | ||
|
||
/** | ||
* This package contains interfaces for services provided by | ||
* Elasticsearch plugins to external applications like the | ||
* Java High Level Rest Client. | ||
*/ | ||
package org.elasticsearch.plugins.spi; |
81 changes: 81 additions & 0 deletions
81
core/src/test/java/org/elasticsearch/plugins/spi/NamedXContentProviderTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.plugins.spi; | ||
|
||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.io.Streams; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.search.aggregations.Aggregation; | ||
import org.elasticsearch.search.aggregations.pipeline.ParsedSimpleValue; | ||
import org.elasticsearch.search.suggest.Suggest; | ||
import org.elasticsearch.search.suggest.term.TermSuggestion; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
import java.util.function.Predicate; | ||
|
||
public class NamedXContentProviderTests extends ESTestCase { | ||
|
||
public void testSpiFileExists() throws IOException { | ||
String serviceFile = "/META-INF/services/" + NamedXContentProvider.class.getName(); | ||
List<String> implementations = new ArrayList<>(); | ||
try (InputStream input = NamedXContentProviderTests.class.getResourceAsStream(serviceFile)) { | ||
Streams.readAllLines(input, implementations::add); | ||
} | ||
|
||
assertEquals(1, implementations.size()); | ||
assertEquals(TestNamedXContentProvider.class.getName(), implementations.get(0)); | ||
} | ||
|
||
public void testNamedXContents() { | ||
final List<NamedXContentRegistry.Entry> namedXContents = new ArrayList<>(); | ||
for (NamedXContentProvider service : ServiceLoader.load(NamedXContentProvider.class)) { | ||
namedXContents.addAll(service.getNamedXContentParsers()); | ||
} | ||
|
||
assertEquals(2, namedXContents.size()); | ||
|
||
List<Predicate<NamedXContentRegistry.Entry>> predicates = new ArrayList<>(2); | ||
predicates.add(e -> Aggregation.class.equals(e.categoryClass) && "test_aggregation".equals(e.name.getPreferredName())); | ||
predicates.add(e -> Suggest.Suggestion.class.equals(e.categoryClass) && "test_suggestion".equals(e.name.getPreferredName())); | ||
predicates.forEach(predicate -> assertEquals(1, namedXContents.stream().filter(predicate).count())); | ||
} | ||
|
||
public static class TestNamedXContentProvider implements NamedXContentProvider { | ||
|
||
public TestNamedXContentProvider() { | ||
} | ||
|
||
@Override | ||
public List<NamedXContentRegistry.Entry> getNamedXContentParsers() { | ||
return Arrays.asList( | ||
new NamedXContentRegistry.Entry(Aggregation.class, new ParseField("test_aggregation"), | ||
(parser, context) -> ParsedSimpleValue.fromXContent(parser, (String) context)), | ||
new NamedXContentRegistry.Entry(Suggest.Suggestion.class, new ParseField("test_suggestion"), | ||
(parser, context) -> TermSuggestion.fromXContent(parser, (String) context)) | ||
); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../src/test/resources/META-INF/services/org.elasticsearch.plugins.spi.NamedXContentProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.elasticsearch.plugins.spi.NamedXContentProviderTests$TestNamedXContentProvider |
42 changes: 42 additions & 0 deletions
42
...va/org/elasticsearch/search/aggregations/matrix/spi/MatrixStatsNamedXContentProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.search.aggregations.matrix.spi; | ||
|
||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.xcontent.ContextParser; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.plugins.spi.NamedXContentProvider; | ||
import org.elasticsearch.search.aggregations.Aggregation; | ||
import org.elasticsearch.search.aggregations.matrix.stats.MatrixStatsAggregationBuilder; | ||
import org.elasticsearch.search.aggregations.matrix.stats.ParsedMatrixStats; | ||
|
||
import java.util.List; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
public class MatrixStatsNamedXContentProvider implements NamedXContentProvider { | ||
|
||
@Override | ||
public List<NamedXContentRegistry.Entry> getNamedXContentParsers() { | ||
ParseField parseField = new ParseField(MatrixStatsAggregationBuilder.NAME); | ||
ContextParser<Object, Aggregation> contextParser = (p, name) -> ParsedMatrixStats.fromXContent(p, (String) name); | ||
return singletonList(new NamedXContentRegistry.Entry(Aggregation.class, parseField, contextParser)); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../src/main/resources/META-INF/services/org.elasticsearch.plugins.spi.NamedXContentProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.elasticsearch.search.aggregations.matrix.spi.MatrixStatsNamedXContentProvider |
42 changes: 42 additions & 0 deletions
42
...parent-join/src/main/java/org/elasticsearch/join/spi/ParentJoinNamedXContentProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.join.spi; | ||
|
||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.xcontent.ContextParser; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.join.aggregations.ChildrenAggregationBuilder; | ||
import org.elasticsearch.join.aggregations.ParsedChildren; | ||
import org.elasticsearch.plugins.spi.NamedXContentProvider; | ||
import org.elasticsearch.search.aggregations.Aggregation; | ||
|
||
import java.util.List; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
public class ParentJoinNamedXContentProvider implements NamedXContentProvider { | ||
|
||
@Override | ||
public List<NamedXContentRegistry.Entry> getNamedXContentParsers() { | ||
ParseField parseField = new ParseField(ChildrenAggregationBuilder.NAME); | ||
ContextParser<Object, Aggregation> contextParser = (p, name) -> ParsedChildren.fromXContent(p, (String) name); | ||
return singletonList(new NamedXContentRegistry.Entry(Aggregation.class, parseField, contextParser)); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../src/main/resources/META-INF/services/org.elasticsearch.plugins.spi.NamedXContentProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.elasticsearch.join.spi.ParentJoinNamedXContentProvider |