-
Notifications
You must be signed in to change notification settings - Fork 1.7k
AVRO-3441: ServiceLoader for LogicalTypeFactory #1590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AVRO-3441: ServiceLoader for LogicalTypeFactory #1590
Conversation
Automatically register LogicalTypeFactory classes using the Java 6 service loader upon startup. This works for any LogicalTypeFactory that does not need constructor parameters.
| static { | ||
| for (LogicalTypeFactory logicalTypeFactory : ServiceLoader.load(LogicalTypeFactory.class)) { | ||
| register(logicalTypeFactory); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the actual magic.
| */ | ||
| package org.apache.avro; | ||
|
|
||
| public class DummyLogicalTypeFactory implements LogicalTypes.LogicalTypeFactory { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is part 1 of what makes it work: a public class with a public no-arg constructor (a default constructor in this case, but that's not important).
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| org.apache.avro.DummyLogicalTypeFactory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is part 2 of what makes it work: a file in META-INF/services named after the binary name of the interface, containing names of classes that implement the interface (one per line); comments and empty lines are ignored.
| @Test | ||
| public void testRegisterLogicalTypeFactoryByServiceLoader() { | ||
| MatcherAssert.assertThat(LogicalTypes.getCustomRegisteredTypes(), | ||
| IsMapContaining.hasEntry(equalTo("service-example"), instanceOf(LogicalTypes.LogicalTypeFactory.class))); | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The actual test
| Assert.assertEquals("Should be equal (forward): " + message, o1, o2); | ||
| Assert.assertEquals("Should be equal (reverse): " + message, o2, o1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Boyscout rule (plus a few more)
Automatically register LogicalTypeFactory classes using the Java 6 service loader upon startup. This works for any LogicalTypeFactory that does not need constructor parameters.
Automatically register LogicalTypeFactory classes using the Java 6
service loader upon startup. This works for any LogicalTypeFactory that
does not need constructor parameters.
Make sure you have checked all steps below.
Jira
Tests
OR does not need testing for this extremely good reason:org.apache.avro.TestLogicalType#testRegisterLogicalTypeFactoryByServiceLoaderCommits
Documentation