Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

Force GuiceServiceLocatorGenerator to be the only ServiceLocatorGenerator #39

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
.settings
.classpath

.idea
*.iml

.gradle
gradlew.bat

build
bin
test-output
target
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public static void main(String[] args) {

```

#### META-INF

To ensure Jersey/HK2 SPIs will use the proper ServiceLocationGenerator, simply copy the META-INF folder into your root project's resources folder.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this is fair enough but how is it different from adding jersey2-guice-spi to the classpath. Both are subject to classpath ordering.


### Documentation

The [User's Guide](https://github.com/Squarespace/jersey2-guice/wiki) can be found in the Wiki.
Expand Down
21 changes: 19 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ allprojects {
configurations {
provided
}

apply plugin: 'maven'

task createPom << {
pom {
project {

inceptionYear '2008'
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
}
}.writeTo("pom.xml")
}
}

subprojects {
Expand Down Expand Up @@ -187,12 +205,11 @@ subprojects {
}

dependencies {
compile project(':jersey2-guice-spi')
compile project(':jersey2-guice-impl')
}

release {
failOnSnapshotDependencies = false
}

createReleaseTag.dependsOn ':jersey2-guice-spi:uploadArchives', ':jersey2-guice-spi:uploadSquarespace', ':jersey2-guice-impl:uploadArchives', ':jersey2-guice-impl:uploadSquarespace'
createReleaseTag.dependsOn ':jersey2-guice-impl:uploadArchives', ':jersey2-guice-impl:uploadSquarespace'
2 changes: 0 additions & 2 deletions jersey2-guice-impl/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

dependencies {
compile project(':jersey2-guice-spi')

compile 'org.slf4j:slf4j-api:1.7.16'

compile 'com.google.code.findbugs:jsr305:3.0.1'
Expand Down
76 changes: 76 additions & 0 deletions jersey2-guice-impl/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<parent>
<groupId>com.squarespace.jersey2-guice</groupId>
<artifactId>jersey2-guice</artifactId>
<version>1.0.6.2-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>jersey2-guice-impl</artifactId>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.16</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-servlet</artifactId>
<version>4.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-multibindings</artifactId>
<version>4.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23.1</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>javax.inject</artifactId>
<groupId>org.glassfish.hk2.external</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.23.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>javax.inject</artifactId>
<groupId>org.glassfish.hk2.external</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.9.v20150224</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.glassfish.hk2.extension.ServiceLocatorGenerator;
import org.jvnet.hk2.external.generator.ServiceLocatorGeneratorImpl;

class GuiceServiceLocatorGenerator implements ServiceLocatorGenerator {
public class GuiceServiceLocatorGenerator implements ServiceLocatorGenerator {

private final ServiceLocatorGenerator generator = new ServiceLocatorGeneratorImpl();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ServiceLocator create(String name, ServiceLocator parent) {
ServiceLocatorGenerator generator = GENERATOR_REF.get();

if (generator == null) {
throw new IllegalStateException("It appears there is no ServiceLocatorGenerator installed.");
generator = GENERATOR_REF.getAndSet( new GuiceServiceLocatorGenerator() );
}

return generator.create(name, parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.squarespace.jersey2.guice;

import com.google.inject.Injector;
import com.google.inject.servlet.RequestScoped;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
Expand All @@ -28,19 +31,26 @@

import org.glassfish.hk2.api.ServiceLocator;

import com.google.inject.Injector;
import com.google.inject.servlet.RequestScoped;

public class JerseyGuiceModule extends JerseyModule {

private final ServiceLocator locator;
private final boolean useGuiceServlet;

public JerseyGuiceModule(String name) {
this(JerseyGuiceUtils.newServiceLocator(name));
}

public JerseyGuiceModule(String name, boolean useGuiceServlet) {
this(JerseyGuiceUtils.newServiceLocator(name), useGuiceServlet);
}

public JerseyGuiceModule(ServiceLocator locator) {
this(locator, true);
}

public JerseyGuiceModule(ServiceLocator locator, boolean useGuiceServlet) {
this.locator = locator;
this.useGuiceServlet = useGuiceServlet;
}

@Override
Expand All @@ -49,30 +59,32 @@ protected void configure() {
Provider<Injector> injector = getProvider(Injector.class);
bind(ServiceLocator.class).toProvider(new ServiceLocatorProvider(injector, locator))
.in(Singleton.class);

Provider<ServiceLocator> provider = getProvider(ServiceLocator.class);

bind(Application.class)
.toProvider(new JerseyProvider<>(provider, Application.class));

bind(Providers.class)
.toProvider(new JerseyProvider<>(provider, Providers.class));

bind(UriInfo.class)
.toProvider(new JerseyProvider<>(provider, UriInfo.class))
.in(RequestScoped.class);

bind(HttpHeaders.class)
.toProvider(new JerseyProvider<>(provider, HttpHeaders.class))
.in(RequestScoped.class);

bind(SecurityContext.class)
.toProvider(new JerseyProvider<>(provider, SecurityContext.class))
.in(RequestScoped.class);

bind(Request.class)
.toProvider(new JerseyProvider<>(provider, Request.class))
.in(RequestScoped.class);

if (useGuiceServlet) {
Provider<ServiceLocator> provider = getProvider(ServiceLocator.class);

bind(Application.class)
.toProvider(new JerseyProvider<>(provider, Application.class));

bind(Providers.class)
.toProvider(new JerseyProvider<>(provider, Providers.class));

bind(UriInfo.class)
.toProvider(new JerseyProvider<>(provider, UriInfo.class))
.in(RequestScoped.class);

bind(HttpHeaders.class)
.toProvider(new JerseyProvider<>(provider, HttpHeaders.class))
.in(RequestScoped.class);

bind(SecurityContext.class)
.toProvider(new JerseyProvider<>(provider, SecurityContext.class))
.in(RequestScoped.class);

bind(Request.class)
.toProvider(new JerseyProvider<>(provider, Request.class))
.in(RequestScoped.class);
}
}

private static class JerseyProvider<T> implements Provider<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.glassfish.hk2.extension.ServiceLocatorGenerator;
import org.glassfish.hk2.utilities.Binder;
import org.glassfish.hk2.utilities.BuilderHelper;
import org.glassfish.jersey.internal.inject.Injections;
import org.glassfish.jersey.message.internal.MessagingBinders;
import org.jvnet.hk2.external.generator.ServiceLocatorGeneratorImpl;
import org.jvnet.hk2.internal.DefaultClassAnalyzer;
Expand Down Expand Up @@ -203,6 +204,17 @@ private static synchronized GuiceServiceLocatorFactory getOrCreateFactory() {
Class<?> clazz = ServiceLocatorFactory.class;
Field field = clazz.getDeclaredField("INSTANCE");

set(field, null, guiceServiceLocatorFactory);

clazz = Injections.class;
field = clazz.getDeclaredField("factory");

Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

field.setAccessible(true);

set(field, null, guiceServiceLocatorFactory);

return guiceServiceLocatorFactory;
Expand Down
1 change: 0 additions & 1 deletion jersey2-guice-spi/.gitignore

This file was deleted.

8 changes: 0 additions & 8 deletions jersey2-guice-spi/build.gradle

This file was deleted.

50 changes: 50 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pom file contains some (possibly) private information.

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.squarespace.jersey2-guice</groupId>
<artifactId>jersey2-guice</artifactId>
<version>1.0.7-8d-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
<module>jersey2-guice-impl</module>
</modules>

<build>
<pluginManagement>
<plugins>
<!-- COMPILER PLUGIN -->
<!-- Compile Source-Code -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<!-- DEPLOY -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<distributionManagement>
<repository>
<id>8drelease</id>
<url>https://nexus.priv.8d.com:2443/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>8dsnapshot</id>
<url>https://nexus.priv.8d.com:2443/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>

</project>