Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 6b9a138

Browse files
author
Petr Bouda
committed
Removing HK2 @optional annotation.
Change-Id: I139fb9b7c58c6b952de0f9a97c4cf2cd2fe33017
1 parent 0062bdf commit 6b9a138

File tree

9 files changed

+53
-95
lines changed

9 files changed

+53
-95
lines changed

core-common/src/test/java/org/glassfish/jersey/internal/inject/ReferencingFactoryTest.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@
5454

5555
import org.glassfish.hk2.api.PerLookup;
5656

57-
import org.jvnet.hk2.annotations.Optional;
58-
5957
import org.junit.Test;
6058
import static org.junit.Assert.assertSame;
6159

@@ -77,10 +75,8 @@ private Foo(int value) {
7775
private static class ValueInjected {
7876

7977
@Inject
80-
@Optional
8178
Foo foo;
8279
@Inject
83-
@Optional
8480
List<Integer> integers;
8581
@Inject
8682
List<String> strings;
@@ -143,11 +139,6 @@ protected void configure() {
143139
public void testReferencedBinding() {
144140
InjectionManager injectionManager = Injections.createInjectionManager(this);
145141

146-
ValueInjected emptyValues = injectionManager.createAndInitialize(ValueInjected.class);
147-
assertSame(expectedFoo, emptyValues.foo);
148-
assertSame(expectedIntegers, emptyValues.integers);
149-
assertSame(expectedStrings, emptyValues.strings);
150-
151142
RefInjected refValues = injectionManager.createAndInitialize(RefInjected.class);
152143
expectedFoo = new Foo(10);
153144
refValues.foo.set(expectedFoo);

core-common/src/test/java/org/glassfish/jersey/process/internal/ExecutorProvidersTest.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@
6262
import org.glassfish.jersey.spi.ScheduledThreadPoolExecutorProvider;
6363
import org.glassfish.jersey.spi.ThreadPoolExecutorProvider;
6464

65-
import org.glassfish.hk2.api.Unqualified;
66-
67-
import org.jvnet.hk2.annotations.Optional;
68-
6965
import org.hamcrest.Matchers;
7066
import org.junit.Before;
7167
import org.junit.Test;
@@ -189,16 +185,6 @@ public static class InjectedExecutorClient {
189185
@Inject
190186
private PreDestroyNotifier preDestroyNotifier;
191187

192-
@Inject
193-
@Unqualified // this will prevent HK2 from injecting using qualified injection bindings
194-
@Optional // This will prevent HK2 to fail due to an unsatisfied injection binding
195-
private ExecutorService unqualifiedExecutor;
196-
197-
@Inject
198-
@Unqualified // this will prevent HK2 from injecting using qualified injection bindings
199-
@Optional // This will prevent HK2 to fail due to an unsatisfied injection binding
200-
private ScheduledExecutorService unqualifiedScheduler;
201-
202188
@Inject
203189
@CustomExecutor
204190
private ExecutorService customExecutor;
@@ -207,16 +193,6 @@ public static class InjectedExecutorClient {
207193
@Named("custom-executor")
208194
private ExecutorService customNamedExecutor;
209195

210-
@Inject
211-
@Optional
212-
@CustomExecutor
213-
private ScheduledExecutorService customExecutorAsScheduler;
214-
215-
@Inject
216-
@Named("custom-executor")
217-
@Optional
218-
private ScheduledExecutorService customNamedExecutorAsScheduler;
219-
220196
@Inject
221197
@CustomScheduler
222198
private ScheduledExecutorService customScheduler;
@@ -264,14 +240,8 @@ public void setup() {
264240
public void testExecutorInjectionAndReleasing() throws Exception {
265241
final InjectedExecutorClient executorClient = Injections.getOrCreate(injectionManager, InjectedExecutorClient.class);
266242

267-
// Check expected injection points state
268-
assertThat(executorClient.unqualifiedExecutor, Matchers.nullValue());
269-
assertThat(executorClient.unqualifiedScheduler, Matchers.nullValue());
270-
271243
assertThat(executorClient.customExecutor, Matchers.notNullValue());
272244
assertThat(executorClient.customNamedExecutor, Matchers.notNullValue());
273-
assertThat(executorClient.customExecutorAsScheduler, Matchers.nullValue());
274-
assertThat(executorClient.customNamedExecutorAsScheduler, Matchers.nullValue());
275245

276246
assertThat(executorClient.customScheduler, Matchers.notNullValue());
277247
assertThat(executorClient.customNamedScheduler, Matchers.notNullValue());

ext/mvc-freemarker/src/main/java/org/glassfish/jersey/server/mvc/freemarker/FreemarkerDefaultConfigurationFactory.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2015-2016 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2015-2017 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -40,13 +40,11 @@
4040

4141
package org.glassfish.jersey.server.mvc.freemarker;
4242

43-
4443
import java.io.File;
4544
import java.io.IOException;
4645
import java.util.ArrayList;
4746
import java.util.List;
4847

49-
import javax.inject.Inject;
5048
import javax.servlet.ServletContext;
5149

5250
import freemarker.cache.ClassTemplateLoader;
@@ -55,7 +53,6 @@
5553
import freemarker.cache.TemplateLoader;
5654
import freemarker.cache.WebappTemplateLoader;
5755
import freemarker.template.Configuration;
58-
import org.jvnet.hk2.annotations.Optional;
5956

6057
/**
6158
* Handy {@link FreemarkerConfigurationFactory} that supplies a minimally
@@ -73,8 +70,7 @@ public class FreemarkerDefaultConfigurationFactory implements FreemarkerConfigur
7370

7471
protected final Configuration configuration;
7572

76-
@Inject
77-
public FreemarkerDefaultConfigurationFactory(@Optional final ServletContext servletContext) {
73+
public FreemarkerDefaultConfigurationFactory(ServletContext servletContext) {
7874
super();
7975

8076
// Create different loaders.

ext/mvc-freemarker/src/main/java/org/glassfish/jersey/server/mvc/freemarker/FreemarkerMvcFeature.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -45,6 +45,10 @@
4545
import javax.ws.rs.core.Feature;
4646
import javax.ws.rs.core.FeatureContext;
4747

48+
import javax.inject.Inject;
49+
import javax.servlet.ServletContext;
50+
51+
import org.glassfish.jersey.internal.inject.InjectionManager;
4852
import org.glassfish.jersey.server.mvc.MvcFeature;
4953

5054
/**
@@ -60,6 +64,9 @@ public final class FreemarkerMvcFeature implements Feature {
6064

6165
private static final String SUFFIX = ".freemarker";
6266

67+
@Inject
68+
private InjectionManager injectionManager;
69+
6370
/**
6471
* {@link String} property defining the base path to Freemarker templates. If set, the value of the property is added in front
6572
* of the template name defined in:
@@ -132,7 +139,9 @@ public boolean configure(final FeatureContext context) {
132139

133140
if (!config.isRegistered(FreemarkerViewProcessor.class)) {
134141
// Template Processor.
135-
context.register(FreemarkerViewProcessor.class);
142+
Configuration configuration = injectionManager.getInstance(Configuration.class);
143+
ServletContext servletContext = injectionManager.getInstance(ServletContext.class);
144+
context.register(new FreemarkerViewProcessor(configuration, servletContext, injectionManager::createAndInitialize));
136145

137146
// MvcFeature.
138147
if (!config.isRegistered(MvcFeature.class)) {

ext/mvc-freemarker/src/main/java/org/glassfish/jersey/server/mvc/freemarker/FreemarkerViewProcessor.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,18 @@
4747
import java.nio.charset.Charset;
4848
import java.util.HashMap;
4949
import java.util.Map;
50+
import java.util.function.Function;
5051

5152
import javax.ws.rs.core.MediaType;
5253
import javax.ws.rs.core.MultivaluedMap;
5354

54-
import javax.inject.Inject;
5555
import javax.servlet.ServletContext;
5656

57-
import org.glassfish.jersey.internal.inject.InjectionManager;
5857
import org.glassfish.jersey.internal.util.collection.Values;
5958
import org.glassfish.jersey.server.ContainerException;
6059
import org.glassfish.jersey.server.mvc.Viewable;
6160
import org.glassfish.jersey.server.mvc.spi.AbstractTemplateProcessor;
6261

63-
import org.jvnet.hk2.annotations.Optional;
64-
6562
import freemarker.template.Configuration;
6663
import freemarker.template.Template;
6764
import freemarker.template.TemplateException;
@@ -81,17 +78,16 @@ final class FreemarkerViewProcessor extends AbstractTemplateProcessor<Template>
8178
* Create an instance of this processor with injected {@link javax.ws.rs.core.Configuration config} and
8279
* (optional) {@link javax.servlet.ServletContext servlet context}.
8380
*
84-
* @param config config to configure this processor from.
85-
* @param injectionManager injection manager to initialize template object factory if needed.
86-
* @param servletContext (optional) servlet context to obtain template resources from.
81+
* @param config config to configure this processor from.
82+
* @param servletContext servlet context to obtain template resources from.
83+
* @param createInstance function that delegates a creation and an initialization to injection manager.
8784
*/
88-
@Inject
89-
public FreemarkerViewProcessor(final javax.ws.rs.core.Configuration config, final InjectionManager injectionManager,
90-
@Optional final ServletContext servletContext) {
85+
public FreemarkerViewProcessor(javax.ws.rs.core.Configuration config, ServletContext servletContext,
86+
Function<Class<?>, ?> createInstance) {
9187
super(config, servletContext, "freemarker", "ftl");
9288

93-
this.factory = getTemplateObjectFactory(injectionManager, FreemarkerConfigurationFactory.class, () -> {
94-
Configuration configuration = getTemplateObjectFactory(injectionManager, Configuration.class, Values.empty());
89+
this.factory = getTemplateObjectFactory(createInstance, FreemarkerConfigurationFactory.class, () -> {
90+
Configuration configuration = getTemplateObjectFactory(createInstance, Configuration.class, Values.empty());
9591
if (configuration == null) {
9692
return new FreemarkerDefaultConfigurationFactory(servletContext);
9793
} else {

ext/mvc-mustache/src/main/java/org/glassfish/jersey/server/mvc/mustache/MustacheMvcFeature.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -46,6 +46,10 @@
4646
import javax.ws.rs.core.Feature;
4747
import javax.ws.rs.core.FeatureContext;
4848

49+
import javax.inject.Inject;
50+
import javax.servlet.ServletContext;
51+
52+
import org.glassfish.jersey.internal.inject.InjectionManager;
4953
import org.glassfish.jersey.server.mvc.MvcFeature;
5054

5155
/**
@@ -61,6 +65,9 @@ public class MustacheMvcFeature implements Feature {
6165

6266
private static final String SUFFIX = ".mustache";
6367

68+
@Inject
69+
private InjectionManager injectionManager;
70+
6471
/**
6572
* {@link String} property defining the base path to Mustache templates. If set, the value of the property is added in front
6673
* of the template name defined in:
@@ -123,7 +130,9 @@ public boolean configure(final FeatureContext context) {
123130

124131
if (!config.isRegistered(MustacheTemplateProcessor.class)) {
125132
// Template Processor.
126-
context.register(MustacheTemplateProcessor.class);
133+
Configuration configuration = injectionManager.getInstance(Configuration.class);
134+
ServletContext servletContext = injectionManager.getInstance(ServletContext.class);
135+
context.register(new MustacheTemplateProcessor(configuration, servletContext, injectionManager::createAndInitialize));
127136

128137
// MvcFeature.
129138
if (!config.isRegistered(MvcFeature.class)) {

ext/mvc-mustache/src/main/java/org/glassfish/jersey/server/mvc/mustache/MustacheTemplateProcessor.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,19 @@
4545
import java.io.OutputStreamWriter;
4646
import java.io.Reader;
4747
import java.nio.charset.Charset;
48+
import java.util.function.Function;
4849

4950
import javax.ws.rs.core.Configuration;
5051
import javax.ws.rs.core.MediaType;
5152
import javax.ws.rs.core.MultivaluedMap;
5253

53-
import javax.inject.Inject;
5454
import javax.inject.Singleton;
5555
import javax.servlet.ServletContext;
5656

57-
import org.glassfish.jersey.internal.inject.InjectionManager;
5857
import org.glassfish.jersey.server.mvc.Viewable;
5958
import org.glassfish.jersey.server.mvc.spi.AbstractTemplateProcessor;
6059
import org.glassfish.jersey.server.mvc.spi.TemplateProcessor;
6160

62-
import org.jvnet.hk2.annotations.Optional;
63-
6461
import com.github.mustachejava.DefaultMustacheFactory;
6562
import com.github.mustachejava.Mustache;
6663
import com.github.mustachejava.MustacheFactory;
@@ -78,19 +75,17 @@ final class MustacheTemplateProcessor extends AbstractTemplateProcessor<Mustache
7875
private final MustacheFactory factory;
7976

8077
/**
81-
* Create an instance of this processor with injected {@link Configuration config} and
82-
* (optional) {@link ServletContext servlet context}.
78+
* Create an instance of this processor with injected {@link Configuration config} and (nullable)
79+
* {@link ServletContext servlet context}.
8380
*
84-
* @param config configuration to configure this processor from.
85-
* @param injectionManager injection manager to initialize template object factory if needed.
86-
* @param servletContext (optional) servlet context to obtain template resources from.
81+
* @param config configuration to configure this processor from.
82+
* @param servletContext servlet context to obtain template resources from.
83+
* @param createInstance function that delegates a creation and an initialization to injection manager.
8784
*/
88-
@Inject
89-
public MustacheTemplateProcessor(final Configuration config, final InjectionManager injectionManager,
90-
@Optional final ServletContext servletContext) {
85+
public MustacheTemplateProcessor(Configuration config, ServletContext servletContext, Function<Class<?>, ?> createInstance) {
9186
super(config, servletContext, "mustache", "mustache");
9287

93-
this.factory = getTemplateObjectFactory(injectionManager, MustacheFactory.class, DefaultMustacheFactory::new);
88+
this.factory = getTemplateObjectFactory(createInstance, MustacheFactory.class, DefaultMustacheFactory::new);
9489
}
9590

9691
@Override

ext/mvc/src/main/java/org/glassfish/jersey/server/mvc/spi/AbstractTemplateProcessor.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.Map;
5656
import java.util.Set;
5757
import java.util.concurrent.ConcurrentMap;
58+
import java.util.function.Function;
5859
import java.util.logging.Level;
5960
import java.util.logging.Logger;
6061
import java.util.stream.Collectors;
@@ -66,7 +67,6 @@
6667

6768
import javax.servlet.ServletContext;
6869

69-
import org.glassfish.jersey.internal.inject.InjectionManager;
7070
import org.glassfish.jersey.internal.util.PropertiesHelper;
7171
import org.glassfish.jersey.internal.util.ReflectionHelper;
7272
import org.glassfish.jersey.internal.util.collection.DataStructures;
@@ -262,14 +262,13 @@ private Collection<String> getTemplatePaths(final String name) {
262262
* Retrieve a template object factory. The factory is, at first, looked for in
263263
* {@link javax.ws.rs.core.Configuration configuration} and if not found, given default value is used.
264264
*
265-
* @param injectionManager injection manager to initialize factory if configured as class or class-name.
266-
* @param type type of requested template object factory.
267-
* @param defaultValue default value to be used if no factory reference is present in configuration.
265+
* @param createInstance function that delegates a creation and an initialization to injection manager.
266+
* @param type type of requested template object factory.
267+
* @param defaultValue default value to be used if no factory reference is present in configuration.
268268
* @param <F> type of requested template object factory.
269269
* @return non-{@code null} template object factory.
270270
*/
271-
protected <F> F getTemplateObjectFactory(final InjectionManager injectionManager, final Class<F> type,
272-
final Value<F> defaultValue) {
271+
protected <F> F getTemplateObjectFactory(Function<Class<?>, ?> createInstance, Class<F> type, Value<F> defaultValue) {
273272
final Object objectFactoryProperty = config.getProperty(MvcFeature.TEMPLATE_OBJECT_FACTORY + suffix);
274273

275274
if (objectFactoryProperty != null) {
@@ -286,7 +285,7 @@ protected <F> F getTemplateObjectFactory(final InjectionManager injectionManager
286285

287286
if (factoryClass != null) {
288287
if (type.isAssignableFrom(factoryClass)) {
289-
return type.cast(injectionManager.createAndInitialize(factoryClass));
288+
return type.cast(createInstance.apply(factoryClass));
290289
} else {
291290
LOGGER.log(Level.CONFIG, LocalizationMessages.WRONG_TEMPLATE_OBJECT_FACTORY(factoryClass, type));
292291
}

tests/e2e/src/test/java/org/glassfish/jersey/tests/e2e/server/mvc/spi/AbstractTemplateProcessorTest.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@
6565
import org.glassfish.jersey.test.JerseyTest;
6666
import org.glassfish.jersey.test.spi.TestContainerException;
6767

68-
import org.jvnet.hk2.annotations.Optional;
69-
7068
import org.junit.Test;
7169
import org.junit.runner.RunWith;
7270
import org.junit.runners.Suite;
@@ -119,16 +117,11 @@ public static class FactoryTemplateProcessor extends AbstractTemplateProcessor<S
119117
private final TestFactory factory;
120118

121119
@Inject
122-
public FactoryTemplateProcessor(final Configuration config, final InjectionManager injectionManager,
123-
@Optional final ServletContext servletContext) {
124-
super(config, servletContext, "factory", "fct");
125-
126-
this.factory = getTemplateObjectFactory(injectionManager, TestFactory.class, Values.lazy(new Value<TestFactory>() {
127-
@Override
128-
public TestFactory get() {
129-
return new TestFactory("Default Test Factory");
130-
}
131-
}));
120+
public FactoryTemplateProcessor(Configuration config, InjectionManager injectionManager) {
121+
super(config, injectionManager.getInstance(ServletContext.class), "factory", "fct");
122+
123+
this.factory = getTemplateObjectFactory(injectionManager::createAndInitialize, TestFactory.class, Values.lazy(
124+
(Value<TestFactory>) () -> new TestFactory("Default Test Factory")));
132125
}
133126

134127
@Override
@@ -213,8 +206,8 @@ public static class CacheTemplateProcessor extends AbstractTemplateProcessor<Str
213206
private int i = 0;
214207

215208
@Inject
216-
public CacheTemplateProcessor(final Configuration config, @Optional final ServletContext servletContext) {
217-
super(config, servletContext, "factory", "fct");
209+
public CacheTemplateProcessor(Configuration config, InjectionManager injectionManager) {
210+
super(config, injectionManager.getInstance(ServletContext.class), "factory", "fct");
218211
}
219212

220213
@Override

0 commit comments

Comments
 (0)