Skip to content
Permalink
Browse files
Injecting instance of Storage into the resources
  • Loading branch information
Jaroslav Tulach committed Mar 15, 2017
1 parent 64e7b6d commit e5be764bb61c1c251d3d795eb517f559f25fddfc
Showing 4 changed files with 34 additions and 10 deletions.
@@ -59,7 +59,7 @@
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${jersey.version}</version>
<scope>runtime</scope>
<scope>compile</scope>
</dependency>
<dependency>
<artifactId>ko-ws-tyrus</artifactId>
@@ -2,9 +2,11 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
@@ -18,13 +20,20 @@
import javax.ws.rs.core.Response;
import org.apidesign.gate.timing.shared.Contact;

@Singleton
public final class ContactsResource {
private final List<Contact> contacts = new ArrayList<>();
private final Storage storage;

@Inject
private Storage storage;
private int counter;

ContactsResource(Storage storage) throws IOException {
this.storage = storage;

public ContactsResource() throws IOException {
}

@PostConstruct
public void init() throws IOException {
this.storage.readInto("people", Contact.class, contacts);
for (Contact c : contacts) {
if (c.getId() > counter) {
@@ -10,6 +10,7 @@
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;

@@ -37,8 +38,15 @@ public static void main(String... args) throws Exception {

static HttpServer createServer(URI u) {
ResourceConfig rc = new ResourceConfig(
TimingResource.class, Main.class
TimingResource.class, ContactsResource.class, Main.class
);
final Storage storage = new Storage();
rc.registerInstances(new AbstractBinder() {
@Override
protected void configure() {
bind(storage).to(Storage.class);
}
});
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(u, rc);
return server;
}
@@ -9,6 +9,8 @@
import java.util.Map;
import java.util.NavigableSet;
import java.util.TreeSet;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
@@ -25,12 +27,17 @@
public final class TimingResource {
private final NavigableSet<Event> events = new TreeSet<>(Events.COMPARATOR);
private final Map<AsyncResponse,Long> awaiting = new HashMap<>();
private final Storage storage = new Storage();
private final ContactsResource contacts;
private int counter;
@Inject
private Storage storage;
@Inject
private ContactsResource contacts;

public TimingResource() throws IOException {
this.contacts = new ContactsResource(storage);
public TimingResource() {
}

@PostConstruct
public void init() throws IOException {
this.storage.readInto("timing", Event.class, events);
for (Event e : events) {
if (e.getId() > counter) {

0 comments on commit e5be764

Please sign in to comment.