From 1df4baf489435f838d808fe935918a4665b7df31 Mon Sep 17 00:00:00 2001 From: Charles Allen Date: Mon, 9 Nov 2015 10:49:34 -0800 Subject: [PATCH] Move Jackson Guice adapters into io.druid * Removes access to protected methods in com.fasterxml * Eliminates druid-common's use of foreign package com.fasterxml --- .../GuiceAnnotationIntrospector.java | 54 ----------------- .../introspect/GuiceInjectableValues.java | 51 ---------------- .../io/druid/guice/DruidSecondaryModule.java | 2 - .../guice/GuiceAnnotationIntrospector.java | 59 +++++++++++++++++++ .../io/druid/guice/GuiceInjectableValues.java | 55 +++++++++++++++++ .../namespace/URIExtractionNamespaceTest.java | 4 +- .../IngestSegmentFirehoseFactoryTest.java | 4 +- .../druid/segment/loading/LoadSpecTest.java | 10 +--- 8 files changed, 120 insertions(+), 119 deletions(-) delete mode 100644 common/src/main/java/com/fasterxml/jackson/databind/introspect/GuiceAnnotationIntrospector.java delete mode 100644 common/src/main/java/com/fasterxml/jackson/databind/introspect/GuiceInjectableValues.java create mode 100644 common/src/main/java/io/druid/guice/GuiceAnnotationIntrospector.java create mode 100644 common/src/main/java/io/druid/guice/GuiceInjectableValues.java diff --git a/common/src/main/java/com/fasterxml/jackson/databind/introspect/GuiceAnnotationIntrospector.java b/common/src/main/java/com/fasterxml/jackson/databind/introspect/GuiceAnnotationIntrospector.java deleted file mode 100644 index d8d3ace4a529..000000000000 --- a/common/src/main/java/com/fasterxml/jackson/databind/introspect/GuiceAnnotationIntrospector.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Druid - a distributed column store. - * Copyright 2012 - 2015 Metamarkets Group Inc. - * - * 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.fasterxml.jackson.databind.introspect; - -import com.fasterxml.jackson.annotation.JacksonInject; -import com.google.inject.BindingAnnotation; -import com.google.inject.Key; -import com.metamx.common.IAE; - -import java.lang.annotation.Annotation; - -/** - */ -public class GuiceAnnotationIntrospector extends NopAnnotationIntrospector -{ - @Override - public Object findInjectableValueId(AnnotatedMember m) - { - if (m.getAnnotation(JacksonInject.class) == null) { - return null; - } - - Annotation guiceAnnotation = null; - for (Annotation annotation : m.getAllAnnotations()._annotations.values()) { - if (annotation.annotationType().isAnnotationPresent(BindingAnnotation.class)) { - guiceAnnotation = annotation; - break; - } - } - - if (guiceAnnotation == null) { - if (m instanceof AnnotatedMethod) { - throw new IAE("Annotated methods don't work very well yet..."); - } - return Key.get(m.getGenericType()); - } - return Key.get(m.getGenericType(), guiceAnnotation); - } -} diff --git a/common/src/main/java/com/fasterxml/jackson/databind/introspect/GuiceInjectableValues.java b/common/src/main/java/com/fasterxml/jackson/databind/introspect/GuiceInjectableValues.java deleted file mode 100644 index db300741222a..000000000000 --- a/common/src/main/java/com/fasterxml/jackson/databind/introspect/GuiceInjectableValues.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Druid - a distributed column store. - * Copyright 2012 - 2015 Metamarkets Group Inc. - * - * 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.fasterxml.jackson.databind.introspect; - -import com.fasterxml.jackson.databind.BeanProperty; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.InjectableValues; -import com.google.inject.Injector; -import com.google.inject.Key; -import com.metamx.common.IAE; - -import java.lang.reflect.Type; - -/** -*/ -public class GuiceInjectableValues extends InjectableValues -{ - private final Injector injector; - - public GuiceInjectableValues(Injector injector) {this.injector = injector;} - - @Override - public Object findInjectableValue( - Object valueId, DeserializationContext ctxt, BeanProperty forProperty, Object beanInstance - ) - { - // From the docs: "Object that identifies value to inject; may be a simple name or more complex identifier object, - // whatever provider needs" - // Currently we should only be dealing with `Key` instances, and anything more advanced should be handled with - // great care - if(valueId instanceof Key){ - return injector.getInstance((Key) valueId); - } - throw new IAE("Unknown class type [%s] for valueId [%s]", valueId.getClass().getCanonicalName(), valueId.toString()); - } -} diff --git a/common/src/main/java/io/druid/guice/DruidSecondaryModule.java b/common/src/main/java/io/druid/guice/DruidSecondaryModule.java index d1e4c6e4aea7..679e72a40688 100644 --- a/common/src/main/java/io/druid/guice/DruidSecondaryModule.java +++ b/common/src/main/java/io/druid/guice/DruidSecondaryModule.java @@ -19,8 +19,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair; -import com.fasterxml.jackson.databind.introspect.GuiceAnnotationIntrospector; -import com.fasterxml.jackson.databind.introspect.GuiceInjectableValues; import com.google.inject.Binder; import com.google.inject.Inject; import com.google.inject.Injector; diff --git a/common/src/main/java/io/druid/guice/GuiceAnnotationIntrospector.java b/common/src/main/java/io/druid/guice/GuiceAnnotationIntrospector.java new file mode 100644 index 000000000000..b5667892f062 --- /dev/null +++ b/common/src/main/java/io/druid/guice/GuiceAnnotationIntrospector.java @@ -0,0 +1,59 @@ +/* + * Licensed to Metamarkets Group Inc. (Metamarkets) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Metamarkets 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 io.druid.guice; + +import com.fasterxml.jackson.annotation.JacksonInject; +import com.fasterxml.jackson.databind.introspect.AnnotatedMember; +import com.fasterxml.jackson.databind.introspect.AnnotatedMethod; +import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector; +import com.google.inject.BindingAnnotation; +import com.google.inject.Key; +import com.metamx.common.IAE; + +import java.lang.annotation.Annotation; + +/** + */ +public class GuiceAnnotationIntrospector extends NopAnnotationIntrospector +{ + @Override + public Object findInjectableValueId(AnnotatedMember m) + { + if (m.getAnnotation(JacksonInject.class) == null) { + return null; + } + + Annotation guiceAnnotation = null; + for (Annotation annotation : m.annotations()) { + if (annotation.annotationType().isAnnotationPresent(BindingAnnotation.class)) { + guiceAnnotation = annotation; + break; + } + } + + if (guiceAnnotation == null) { + if (m instanceof AnnotatedMethod) { + throw new IAE("Annotated methods don't work very well yet..."); + } + return Key.get(m.getGenericType()); + } + return Key.get(m.getGenericType(), guiceAnnotation); + } +} diff --git a/common/src/main/java/io/druid/guice/GuiceInjectableValues.java b/common/src/main/java/io/druid/guice/GuiceInjectableValues.java new file mode 100644 index 000000000000..96c5a452a535 --- /dev/null +++ b/common/src/main/java/io/druid/guice/GuiceInjectableValues.java @@ -0,0 +1,55 @@ +/* + * Licensed to Metamarkets Group Inc. (Metamarkets) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Metamarkets 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 io.druid.guice; + +import com.fasterxml.jackson.databind.BeanProperty; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.InjectableValues; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.metamx.common.IAE; + +/** + */ +public class GuiceInjectableValues extends InjectableValues +{ + private final Injector injector; + + public GuiceInjectableValues(Injector injector) {this.injector = injector;} + + @Override + public Object findInjectableValue( + Object valueId, DeserializationContext ctxt, BeanProperty forProperty, Object beanInstance + ) + { + // From the docs: "Object that identifies value to inject; may be a simple name or more complex identifier object, + // whatever provider needs" + // Currently we should only be dealing with `Key` instances, and anything more advanced should be handled with + // great care + if (valueId instanceof Key) { + return injector.getInstance((Key) valueId); + } + throw new IAE( + "Unknown class type [%s] for valueId [%s]", + valueId.getClass().getCanonicalName(), + valueId.toString() + ); + } +} diff --git a/extensions/namespace-lookup/src/test/java/io/druid/query/extraction/namespace/URIExtractionNamespaceTest.java b/extensions/namespace-lookup/src/test/java/io/druid/query/extraction/namespace/URIExtractionNamespaceTest.java index 34fe831c6d21..acea090249ea 100644 --- a/extensions/namespace-lookup/src/test/java/io/druid/query/extraction/namespace/URIExtractionNamespaceTest.java +++ b/extensions/namespace-lookup/src/test/java/io/druid/query/extraction/namespace/URIExtractionNamespaceTest.java @@ -21,8 +21,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair; -import com.fasterxml.jackson.databind.introspect.GuiceAnnotationIntrospector; -import com.fasterxml.jackson.databind.introspect.GuiceInjectableValues; +import io.druid.guice.GuiceAnnotationIntrospector; +import io.druid.guice.GuiceInjectableValues; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.inject.Binder; diff --git a/indexing-service/src/test/java/io/druid/indexing/firehose/IngestSegmentFirehoseFactoryTest.java b/indexing-service/src/test/java/io/druid/indexing/firehose/IngestSegmentFirehoseFactoryTest.java index b7a81d32d311..5d35dbe2ccda 100644 --- a/indexing-service/src/test/java/io/druid/indexing/firehose/IngestSegmentFirehoseFactoryTest.java +++ b/indexing-service/src/test/java/io/druid/indexing/firehose/IngestSegmentFirehoseFactoryTest.java @@ -19,8 +19,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair; -import com.fasterxml.jackson.databind.introspect.GuiceAnnotationIntrospector; -import com.fasterxml.jackson.databind.introspect.GuiceInjectableValues; +import io.druid.guice.GuiceAnnotationIntrospector; +import io.druid.guice.GuiceInjectableValues; import com.fasterxml.jackson.databind.module.SimpleModule; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; diff --git a/server/src/test/java/io/druid/segment/loading/LoadSpecTest.java b/server/src/test/java/io/druid/segment/loading/LoadSpecTest.java index 8a8f8703070e..b50e996c094a 100644 --- a/server/src/test/java/io/druid/segment/loading/LoadSpecTest.java +++ b/server/src/test/java/io/druid/segment/loading/LoadSpecTest.java @@ -18,21 +18,15 @@ package io.druid.segment.loading; import com.fasterxml.jackson.annotation.JsonTypeName; -import com.fasterxml.jackson.core.Version; -import com.fasterxml.jackson.databind.BeanProperty; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.InjectableValues; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair; -import com.fasterxml.jackson.databind.introspect.GuiceAnnotationIntrospector; -import com.fasterxml.jackson.databind.introspect.GuiceInjectableValues; +import io.druid.guice.GuiceAnnotationIntrospector; +import io.druid.guice.GuiceInjectableValues; import com.fasterxml.jackson.databind.module.SimpleModule; import com.google.common.collect.ImmutableList; import com.google.inject.Binder; import com.google.inject.Injector; -import com.google.inject.Key; import com.google.inject.Module; -import com.metamx.common.IAE; import io.druid.guice.GuiceInjectors; import io.druid.jackson.DefaultObjectMapper; import org.junit.Assert;