Skip to content

Commit

Permalink
Host database console using Jetty
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Nov 27, 2015
1 parent 6eab101 commit 7db2eb1
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 34 deletions.
4 changes: 1 addition & 3 deletions debug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<entry key='web.old'>true</entry>-->
<entry key='web.path'>web</entry>
<entry key='web.debug'>true</entry>
<entry key='web.console'>true</entry>

<entry key='geocoder.enable'>true</entry>
<entry key='geocoder.type'>nominatim</entry>
Expand All @@ -25,9 +26,6 @@

<entry key='distance.enable'>true</entry>

<entry key='console.enable'>true</entry>
<entry key='console.port'>8083</entry>

<!--<entry key='filter.enable'>true</entry>
<entry key='filter.limit'>3600</entry>
<entry key='filter.invalid'>true</entry>
Expand Down
24 changes: 0 additions & 24 deletions src/org/traccar/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package org.traccar;

import com.ning.http.client.AsyncHttpClient;
import org.h2.server.web.ConnectionInfo;
import org.h2.tools.Server;
import org.traccar.database.ConnectionManager;
import org.traccar.database.DataManager;
import org.traccar.database.IdentityManager;
Expand All @@ -36,8 +34,6 @@
import org.traccar.location.OpenCellIdLocationProvider;
import org.traccar.web.WebServer;

import java.lang.reflect.Method;

public final class Context {

private Context() {
Expand Down Expand Up @@ -97,12 +93,6 @@ public static WebServer getWebServer() {
return webServer;
}

private static Server databaseConsole;

public static Server getDatabaseConsole() {
return databaseConsole;
}

private static ServerManager serverManager;

public static ServerManager getServerManager() {
Expand Down Expand Up @@ -185,20 +175,6 @@ public static void init(String[] arguments) throws Exception {
webServer = new WebServer(config, dataManager.getDataSource());
}

if (config.getBoolean("console.enable")) {
databaseConsole = Server.createWebServer("-webPort", config.getString("console.port"));
org.h2.server.web.WebServer databaseService = (org.h2.server.web.WebServer) databaseConsole.getService();

ConnectionInfo connectionInfo = new ConnectionInfo("Traccar|"
+ config.getString("database.driver") + "|"
+ config.getString("database.url") + "|"
+ config.getString("database.user"));

Method method = databaseService.getClass().getDeclaredMethod("updateSetting", ConnectionInfo.class);
method.setAccessible(true);
method.invoke(databaseService, connectionInfo);
}

connectionManager = new ConnectionManager(dataManager);

serverManager = new ServerManager();
Expand Down
6 changes: 0 additions & 6 deletions src/org/traccar/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,12 @@ public static void main(String[] args) throws Exception {
if (Context.getWebServer() != null) {
Context.getWebServer().start();
}
if (Context.getDatabaseConsole() != null) {
Context.getDatabaseConsole().start();
}

Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
Log.info("Shutting down server...");

if (Context.getDatabaseConsole() != null) {
Context.getDatabaseConsole().stop();
}
if (Context.getWebServer() != null) {
Context.getWebServer().stop();
}
Expand Down
1 change: 0 additions & 1 deletion src/org/traccar/protocol/H02ProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.traccar.helper.DateBuilder;
import org.traccar.helper.Parser;
import org.traccar.helper.PatternBuilder;
import org.traccar.helper.PatternUtil;
import org.traccar.model.Event;
import org.traccar.model.Position;

Expand Down
15 changes: 15 additions & 0 deletions src/org/traccar/web/CommandServlet.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.web;

import javax.json.Json;
Expand Down
52 changes: 52 additions & 0 deletions src/org/traccar/web/ConsoleServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.web;

import org.h2.server.web.ConnectionInfo;
import org.h2.server.web.WebServlet;
import org.traccar.Context;
import org.traccar.helper.Log;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ConsoleServlet extends WebServlet {

@Override
public void init() {
super.init();

try {
Field field = WebServlet.class.getDeclaredField("server");
field.setAccessible(true);
org.h2.server.web.WebServer server = (org.h2.server.web.WebServer) field.get(this);

ConnectionInfo connectionInfo = new ConnectionInfo("Traccar|"
+ Context.getConfig().getString("database.driver") + "|"
+ Context.getConfig().getString("database.url") + "|"
+ Context.getConfig().getString("database.user"));

Method method = org.h2.server.web.WebServer.class.getDeclaredMethod("updateSetting", ConnectionInfo.class);
method.setAccessible(true);
method.invoke(server, connectionInfo);

} catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
Log.warning(e);
}
}

}
10 changes: 10 additions & 0 deletions src/org/traccar/web/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public WebServer(Config config, DataSource dataSource) {
break;
case "new":
initApi();
if (config.getBoolean("web.console")) {
initConsole();
}
initWebApp();
break;
case "old":
Expand Down Expand Up @@ -110,6 +113,13 @@ private void initApi() {
handlers.addHandler(servletHandler);
}

private void initConsole() {
ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletHandler.setContextPath("/console");
servletHandler.addServlet(new ServletHolder(new ConsoleServlet()), "/*");
handlers.addHandler(servletHandler);
}

public void start() {
try {
server.start();
Expand Down

0 comments on commit 7db2eb1

Please sign in to comment.