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

Commit 9709e30

Browse files
author
Petr Bouda
committed
Removing HK2 @optional annotation.
Change-Id: I139fb9b7c58c6b952de0f9a97c4cf2cd2fe33017
1 parent 1b7acea commit 9709e30

File tree

9 files changed

+64
-115
lines changed

9 files changed

+64
-115
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: 4 additions & 7 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 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
@@ -39,20 +39,18 @@
3939
*/
4040
package org.glassfish.jersey.server.mvc.freemarker;
4141

42-
43-
import javax.inject.Inject;
44-
import javax.servlet.ServletContext;
4542
import java.io.File;
4643
import java.io.IOException;
4744
import java.util.List;
4845

46+
import javax.servlet.ServletContext;
47+
4948
import freemarker.cache.ClassTemplateLoader;
5049
import freemarker.cache.FileTemplateLoader;
5150
import freemarker.cache.MultiTemplateLoader;
5251
import freemarker.cache.TemplateLoader;
5352
import freemarker.cache.WebappTemplateLoader;
5453
import freemarker.template.Configuration;
55-
import org.jvnet.hk2.annotations.Optional;
5654
import jersey.repackaged.com.google.common.collect.Lists;
5755

5856

@@ -72,8 +70,7 @@ public class FreemarkerDefaultConfigurationFactory implements FreemarkerConfigur
7270

7371
protected final Configuration configuration;
7472

75-
@Inject
76-
public FreemarkerDefaultConfigurationFactory(@Optional final ServletContext servletContext) {
73+
public FreemarkerDefaultConfigurationFactory(ServletContext servletContext) {
7774
super();
7875

7976
// 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

0 commit comments

Comments
 (0)