http-service: use guice for dependency injection
This commit is contained in:
@@ -63,6 +63,11 @@
|
|||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
<version>21.0</version>
|
<version>21.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.inject</groupId>
|
||||||
|
<artifactId>guice</artifactId>
|
||||||
|
<version>4.1.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
@@ -87,6 +92,12 @@
|
|||||||
<version>1.10.19</version>
|
<version>1.10.19</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.inject.extensions</groupId>
|
||||||
|
<artifactId>guice-testlib</artifactId>
|
||||||
|
<version>4.1.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -24,10 +24,8 @@
|
|||||||
*/
|
*/
|
||||||
package net.runelite.http.service;
|
package net.runelite.http.service;
|
||||||
|
|
||||||
import javax.naming.Context;
|
import com.google.inject.Guice;
|
||||||
import javax.naming.InitialContext;
|
import com.google.inject.Inject;
|
||||||
import javax.naming.NamingException;
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
import net.runelite.http.api.RuneliteAPI;
|
import net.runelite.http.api.RuneliteAPI;
|
||||||
import net.runelite.http.service.hiscore.HiscoreService;
|
import net.runelite.http.service.hiscore.HiscoreService;
|
||||||
import net.runelite.http.service.updatecheck.UpdateCheckService;
|
import net.runelite.http.service.updatecheck.UpdateCheckService;
|
||||||
@@ -42,20 +40,29 @@ public class Service implements SparkApplication
|
|||||||
{
|
{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(Service.class);
|
private static final Logger logger = LoggerFactory.getLogger(Service.class);
|
||||||
|
|
||||||
private DataSource dataSource;
|
|
||||||
|
|
||||||
private final JsonTransformer transformer = new JsonTransformer();
|
private final JsonTransformer transformer = new JsonTransformer();
|
||||||
|
|
||||||
private HiscoreService hiscores = new HiscoreService();
|
@Inject
|
||||||
private UpdateCheckService updateCheck = new UpdateCheckService(this);
|
private HiscoreService hiscores;
|
||||||
private WorldsService worlds = new WorldsService();
|
|
||||||
private XteaService xtea = new XteaService(this);
|
@Inject
|
||||||
|
private UpdateCheckService updateCheck;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private WorldsService worlds;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private XteaService xtea;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init()
|
public void init()
|
||||||
{
|
{
|
||||||
loadDatasource();
|
Guice.createInjector(new ServiceModule(this));
|
||||||
|
setupRoutes();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setupRoutes()
|
||||||
|
{
|
||||||
xtea.init();
|
xtea.init();
|
||||||
|
|
||||||
get("/version", (request, response) -> RuneliteAPI.getVersion());
|
get("/version", (request, response) -> RuneliteAPI.getVersion());
|
||||||
@@ -68,75 +75,4 @@ public class Service implements SparkApplication
|
|||||||
exception(Exception.class, (exception, request, response) -> logger.warn(null, exception));
|
exception(Exception.class, (exception, request, response) -> logger.warn(null, exception));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadDatasource()
|
|
||||||
{
|
|
||||||
if (dataSource != null)
|
|
||||||
{
|
|
||||||
return; // unit test?
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// It is difficult to inject things into Spark
|
|
||||||
Context initCtx = new InitialContext();
|
|
||||||
Context envCtx = (Context) initCtx.lookup("java:comp/env");
|
|
||||||
|
|
||||||
dataSource = (DataSource) envCtx.lookup("jdbc/runelite");
|
|
||||||
}
|
|
||||||
catch (NamingException ex)
|
|
||||||
{
|
|
||||||
throw new RuntimeException(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public HiscoreService getHiscores()
|
|
||||||
{
|
|
||||||
return hiscores;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setHiscores(HiscoreService hiscores)
|
|
||||||
{
|
|
||||||
this.hiscores = hiscores;
|
|
||||||
}
|
|
||||||
|
|
||||||
public UpdateCheckService getUpdateCheck()
|
|
||||||
{
|
|
||||||
return updateCheck;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUpdateCheck(UpdateCheckService updateCheck)
|
|
||||||
{
|
|
||||||
this.updateCheck = updateCheck;
|
|
||||||
}
|
|
||||||
|
|
||||||
public WorldsService getWorlds()
|
|
||||||
{
|
|
||||||
return worlds;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setWorlds(WorldsService worlds)
|
|
||||||
{
|
|
||||||
this.worlds = worlds;
|
|
||||||
}
|
|
||||||
|
|
||||||
public XteaService getXtea()
|
|
||||||
{
|
|
||||||
return xtea;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setXtea(XteaService xtea)
|
|
||||||
{
|
|
||||||
this.xtea = xtea;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DataSource getDataSource()
|
|
||||||
{
|
|
||||||
return dataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDataSource(DataSource dataSource)
|
|
||||||
{
|
|
||||||
this.dataSource = dataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2017, Adam <Adam@sigterm.info>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||||
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
package net.runelite.http.service;
|
||||||
|
|
||||||
|
import com.google.inject.AbstractModule;
|
||||||
|
import com.google.inject.Provides;
|
||||||
|
import com.google.inject.name.Named;
|
||||||
|
import javax.naming.Context;
|
||||||
|
import javax.naming.InitialContext;
|
||||||
|
import javax.naming.NamingException;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import net.runelite.http.service.hiscore.HiscoreService;
|
||||||
|
import net.runelite.http.service.updatecheck.UpdateCheckService;
|
||||||
|
import net.runelite.http.service.worlds.WorldsService;
|
||||||
|
import net.runelite.http.service.xtea.XteaService;
|
||||||
|
|
||||||
|
public class ServiceModule extends AbstractModule
|
||||||
|
{
|
||||||
|
private final Service service;
|
||||||
|
|
||||||
|
public ServiceModule(Service service)
|
||||||
|
{
|
||||||
|
this.service = service;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Named("Runelite JDBC")
|
||||||
|
DataSource provideDataSource()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// It is difficult to inject things into Spark
|
||||||
|
Context initCtx = new InitialContext();
|
||||||
|
Context envCtx = (Context) initCtx.lookup("java:comp/env");
|
||||||
|
|
||||||
|
return (DataSource) envCtx.lookup("jdbc/runelite");
|
||||||
|
}
|
||||||
|
catch (NamingException ex)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure()
|
||||||
|
{
|
||||||
|
bind(Service.class).toInstance(service);
|
||||||
|
|
||||||
|
bind(HiscoreService.class);
|
||||||
|
bind(UpdateCheckService.class);
|
||||||
|
bind(WorldsService.class);
|
||||||
|
bind(XteaService.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,6 +25,7 @@
|
|||||||
package net.runelite.http.service.updatecheck;
|
package net.runelite.http.service.updatecheck;
|
||||||
|
|
||||||
import com.google.common.base.Suppliers;
|
import com.google.common.base.Suppliers;
|
||||||
|
import com.google.inject.Inject;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@@ -38,7 +39,6 @@ import java.util.concurrent.TimeUnit;
|
|||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import net.runelite.http.api.RuneliteAPI;
|
import net.runelite.http.api.RuneliteAPI;
|
||||||
import net.runelite.http.api.worlds.WorldResult;
|
import net.runelite.http.api.worlds.WorldResult;
|
||||||
import net.runelite.http.service.Service;
|
|
||||||
import net.runelite.http.service.worlds.WorldsService;
|
import net.runelite.http.service.worlds.WorldsService;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -56,12 +56,13 @@ public class UpdateCheckService
|
|||||||
private static final int RESPONSE_OK = 0;
|
private static final int RESPONSE_OK = 0;
|
||||||
private static final int RESPONSE_OUTDATED = 6;
|
private static final int RESPONSE_OUTDATED = 6;
|
||||||
|
|
||||||
private final Service service;
|
private final WorldsService worldsService;
|
||||||
private final Supplier<Boolean> updateAvailable = Suppliers.memoizeWithExpiration(this::checkUpdate, 1, TimeUnit.MINUTES);
|
private final Supplier<Boolean> updateAvailable = Suppliers.memoizeWithExpiration(this::checkUpdate, 1, TimeUnit.MINUTES);
|
||||||
|
|
||||||
public UpdateCheckService(Service service)
|
@Inject
|
||||||
|
public UpdateCheckService(WorldsService worldsService)
|
||||||
{
|
{
|
||||||
this.service = service;
|
this.worldsService = worldsService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean check(Request request, Response response)
|
public Boolean check(Request request, Response response)
|
||||||
@@ -121,8 +122,6 @@ public class UpdateCheckService
|
|||||||
|
|
||||||
private int randomWorld()
|
private int randomWorld()
|
||||||
{
|
{
|
||||||
WorldsService worldsService = service.getWorlds();
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
WorldResult worlds = worldsService.listWorlds();
|
WorldResult worlds = worldsService.listWorlds();
|
||||||
|
|||||||
@@ -25,11 +25,13 @@
|
|||||||
package net.runelite.http.service.xtea;
|
package net.runelite.http.service.xtea;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import com.google.inject.name.Named;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import javax.sql.DataSource;
|
||||||
import net.runelite.http.api.xtea.XteaKey;
|
import net.runelite.http.api.xtea.XteaKey;
|
||||||
import net.runelite.http.api.xtea.XteaRequest;
|
import net.runelite.http.api.xtea.XteaRequest;
|
||||||
import net.runelite.http.service.Service;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.sql2o.Connection;
|
import org.sql2o.Connection;
|
||||||
@@ -52,17 +54,19 @@ public class XteaService
|
|||||||
+ " PRIMARY KEY (`rev`,`region`,`key1`,`key2`,`key3`,`key4`)\n"
|
+ " PRIMARY KEY (`rev`,`region`,`key1`,`key2`,`key3`,`key4`)\n"
|
||||||
+ ") ENGINE=InnoDB;";
|
+ ") ENGINE=InnoDB;";
|
||||||
|
|
||||||
private final Service service;
|
|
||||||
|
private final DataSource datasource;
|
||||||
private final Gson gson = new Gson();
|
private final Gson gson = new Gson();
|
||||||
|
|
||||||
public XteaService(Service service)
|
@Inject
|
||||||
|
public XteaService(@Named("Runelite JDBC") DataSource datasource)
|
||||||
{
|
{
|
||||||
this.service = service;
|
this.datasource = datasource;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init()
|
public void init()
|
||||||
{
|
{
|
||||||
Sql2o sql2o = new Sql2o(service.getDataSource());
|
Sql2o sql2o = new Sql2o(datasource);
|
||||||
|
|
||||||
try (Connection con = sql2o.beginTransaction())
|
try (Connection con = sql2o.beginTransaction())
|
||||||
{
|
{
|
||||||
@@ -75,7 +79,7 @@ public class XteaService
|
|||||||
{
|
{
|
||||||
XteaRequest xteaRequest = gson.fromJson(request.body(), XteaRequest.class);
|
XteaRequest xteaRequest = gson.fromJson(request.body(), XteaRequest.class);
|
||||||
|
|
||||||
Sql2o sql2o = new Sql2o(service.getDataSource());
|
Sql2o sql2o = new Sql2o(datasource);
|
||||||
|
|
||||||
try (Connection con = sql2o.beginTransaction())
|
try (Connection con = sql2o.beginTransaction())
|
||||||
{
|
{
|
||||||
@@ -104,7 +108,7 @@ public class XteaService
|
|||||||
String revStr = request.params("rev");
|
String revStr = request.params("rev");
|
||||||
int revision = Integer.parseInt(revStr);
|
int revision = Integer.parseInt(revStr);
|
||||||
|
|
||||||
Sql2o sql2o = new Sql2o(service.getDataSource());
|
Sql2o sql2o = new Sql2o(datasource);
|
||||||
|
|
||||||
try (Connection con = sql2o.open())
|
try (Connection con = sql2o.open())
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -25,43 +25,60 @@
|
|||||||
package net.runelite.http.service;
|
package net.runelite.http.service;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
import com.google.inject.Guice;
|
||||||
|
import com.google.inject.Injector;
|
||||||
|
import com.google.inject.name.Named;
|
||||||
|
import com.google.inject.testing.fieldbinder.Bind;
|
||||||
|
import com.google.inject.testing.fieldbinder.BoundFieldModule;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.sql.SQLException;
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import net.runelite.http.api.hiscore.HiscoreResult;
|
import net.runelite.http.api.hiscore.HiscoreResult;
|
||||||
import net.runelite.http.api.hiscore.Skill;
|
import net.runelite.http.api.hiscore.Skill;
|
||||||
import net.runelite.http.service.hiscore.HiscoreService;
|
import net.runelite.http.service.hiscore.HiscoreService;
|
||||||
import org.junit.AfterClass;
|
import org.junit.After;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
import org.mockito.Answers;
|
||||||
import static org.mockito.Mockito.mock;
|
import org.mockito.Mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
import spark.Spark;
|
import spark.Spark;
|
||||||
|
|
||||||
public class ServiceTest
|
public class ServiceTest
|
||||||
{
|
{
|
||||||
private static final String URL_BASE = "http://localhost:4567";
|
private static final String URL_BASE = "http://localhost:4567";
|
||||||
|
|
||||||
private static Service service;
|
private Service service;
|
||||||
|
|
||||||
@BeforeClass
|
@Bind
|
||||||
public static void before() throws SQLException
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
|
@Named("Runelite JDBC")
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
|
@Bind
|
||||||
|
@Mock
|
||||||
|
private HiscoreService hiscoreService;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before()
|
||||||
{
|
{
|
||||||
DataSource dataSource = mock(DataSource.class, RETURNS_DEEP_STUBS);
|
// Init mocks first, else we're binding null objects
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
// Inject everything in the test object
|
||||||
|
Injector injector = Guice.createInjector(BoundFieldModule.of(this));
|
||||||
|
injector.injectMembers(this);
|
||||||
|
|
||||||
service = new Service();
|
service = injector.getInstance(Service.class);
|
||||||
service.setDataSource(dataSource);
|
service.setupRoutes();
|
||||||
service.init();
|
|
||||||
|
|
||||||
Spark.awaitInitialization();
|
Spark.awaitInitialization();
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@After
|
||||||
public static void after()
|
public void after()
|
||||||
{
|
{
|
||||||
Spark.stop();
|
Spark.stop();
|
||||||
}
|
}
|
||||||
@@ -69,9 +86,6 @@ public class ServiceTest
|
|||||||
@Test
|
@Test
|
||||||
public void testInit() throws Exception
|
public void testInit() throws Exception
|
||||||
{
|
{
|
||||||
HiscoreService hiscoreService = mock(HiscoreService.class);
|
|
||||||
service.setHiscores(hiscoreService);
|
|
||||||
|
|
||||||
HiscoreResult result = new HiscoreResult();
|
HiscoreResult result = new HiscoreResult();
|
||||||
result.setAttack(new Skill(1, 99, 42));
|
result.setAttack(new Skill(1, 99, 42));
|
||||||
|
|
||||||
@@ -86,8 +100,6 @@ public class ServiceTest
|
|||||||
HiscoreResult res = gson.fromJson(new InputStreamReader(connection.getInputStream()), HiscoreResult.class);
|
HiscoreResult res = gson.fromJson(new InputStreamReader(connection.getInputStream()), HiscoreResult.class);
|
||||||
|
|
||||||
Assert.assertEquals(result, res);
|
Assert.assertEquals(result, res);
|
||||||
|
|
||||||
Spark.stop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user