From 7db2eb1ad224bf00608ebbcfe13bfa7fd519a7fc Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 27 Nov 2015 15:12:28 +1300 Subject: Host database console using Jetty --- src/org/traccar/Context.java | 24 ----------- src/org/traccar/Main.java | 6 --- src/org/traccar/protocol/H02ProtocolDecoder.java | 1 - src/org/traccar/web/CommandServlet.java | 15 +++++++ src/org/traccar/web/ConsoleServlet.java | 52 ++++++++++++++++++++++++ src/org/traccar/web/WebServer.java | 10 +++++ 6 files changed, 77 insertions(+), 31 deletions(-) create mode 100644 src/org/traccar/web/ConsoleServlet.java (limited to 'src') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 675465aff..c833d0c2e 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -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; @@ -36,8 +34,6 @@ import org.traccar.location.MozillaLocationProvider; import org.traccar.location.OpenCellIdLocationProvider; import org.traccar.web.WebServer; -import java.lang.reflect.Method; - public final class Context { private Context() { @@ -97,12 +93,6 @@ public final class Context { return webServer; } - private static Server databaseConsole; - - public static Server getDatabaseConsole() { - return databaseConsole; - } - private static ServerManager serverManager; public static ServerManager getServerManager() { @@ -185,20 +175,6 @@ public final class Context { 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(); diff --git a/src/org/traccar/Main.java b/src/org/traccar/Main.java index 7c2e00ca0..7ff93fda6 100644 --- a/src/org/traccar/Main.java +++ b/src/org/traccar/Main.java @@ -33,18 +33,12 @@ public final class Main { 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(); } diff --git a/src/org/traccar/protocol/H02ProtocolDecoder.java b/src/org/traccar/protocol/H02ProtocolDecoder.java index b941e332d..31bdd9aa9 100644 --- a/src/org/traccar/protocol/H02ProtocolDecoder.java +++ b/src/org/traccar/protocol/H02ProtocolDecoder.java @@ -27,7 +27,6 @@ import org.traccar.helper.ChannelBufferTools; 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; diff --git a/src/org/traccar/web/CommandServlet.java b/src/org/traccar/web/CommandServlet.java index 958f1a888..67bca2d57 100644 --- a/src/org/traccar/web/CommandServlet.java +++ b/src/org/traccar/web/CommandServlet.java @@ -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; diff --git a/src/org/traccar/web/ConsoleServlet.java b/src/org/traccar/web/ConsoleServlet.java new file mode 100644 index 000000000..b219eaba4 --- /dev/null +++ b/src/org/traccar/web/ConsoleServlet.java @@ -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); + } + } + +} diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 36c43736d..f5a6acdd9 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -59,6 +59,9 @@ public class WebServer { break; case "new": initApi(); + if (config.getBoolean("web.console")) { + initConsole(); + } initWebApp(); break; case "old": @@ -110,6 +113,13 @@ public class WebServer { 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(); -- cgit v1.2.3