Created
December 27, 2015 08:53
-
-
Save psamsotha/981796428ed736977eed to your computer and use it in GitHub Desktop.
Custom injection of configuration properties
This file contains hidden or 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
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import java.util.logging.Logger; | |
import javax.inject.Singleton; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.core.Configuration; | |
import javax.ws.rs.core.Context; | |
import javax.ws.rs.core.Response; | |
import org.glassfish.hk2.api.Injectee; | |
import org.glassfish.hk2.api.InjectionResolver; | |
import org.glassfish.hk2.api.ServiceHandle; | |
import org.glassfish.hk2.api.TypeLiteral; | |
import org.glassfish.hk2.utilities.binding.AbstractBinder; | |
import org.glassfish.jersey.filter.LoggingFilter; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.test.JerseyTest; | |
import org.junit.Test; | |
import static junit.framework.Assert.assertEquals; | |
/** | |
* Run this test class like any other JUnit test. It makes use of Jersey Test | |
* Framework. You will need to following dependency to run it. | |
* | |
* <dependency> | |
* <groupId>org.glassfish.jersey.test-framework.providers</groupId> | |
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId> | |
* <version>${jersey2.version}</version> | |
* <scope>test</scope> | |
* </dependency> | |
*/ | |
public class CustomInjectPropsTest extends JerseyTest { | |
@Target(ElementType.FIELD) | |
@Retention(RetentionPolicy.RUNTIME) | |
public static @interface Config { | |
String value(); | |
} | |
private static final String PROP_KEY = "Prop_Key"; | |
private static final String PROP_VALUE = "PropValue"; | |
public static class ConfigInjectionResolver implements InjectionResolver<Config> { | |
@Context | |
private Configuration configuration; | |
@Override | |
public Object resolve(Injectee injectee, ServiceHandle<?> handle) { | |
if (String.class == injectee.getRequiredType()) { | |
Config annotation = injectee.getParent().getAnnotation(Config.class); | |
if (annotation != null) { | |
String prop = annotation.value(); | |
return (String)configuration.getProperty(prop); | |
} | |
} | |
return null; | |
} | |
@Override | |
public boolean isConstructorParameterIndicator() { return false; } | |
@Override | |
public boolean isMethodParameterIndicator() { return false; } | |
} | |
@Path("config") | |
public static class ConfigResource { | |
@Config(PROP_KEY) | |
private String propValue; | |
@GET | |
public String getConfigProp() { | |
return propValue; | |
} | |
} | |
@Override | |
public ResourceConfig configure() { | |
ResourceConfig config = new ResourceConfig(ConfigResource.class); | |
config.register(new LoggingFilter(Logger.getAnonymousLogger(), true)); | |
config.property(PROP_KEY, PROP_VALUE); | |
config.register(new AbstractBinder(){ | |
@Override | |
protected void configure() { | |
bind(ConfigInjectionResolver.class) | |
.to(new TypeLiteral<InjectionResolver<Config>>(){}) | |
.in(Singleton.class); | |
} | |
}); | |
return config; | |
} | |
@Test | |
public void should_return_config_property_value() { | |
Response response = target("config").request().get(); | |
assertEquals(200, response.getStatus()); | |
assertEquals(PROP_VALUE, response.readEntity(String.class)); | |
response.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment