diff options
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/database/DataManager.java | 20 | ||||
-rw-r--r-- | src/org/traccar/database/QueryBuilder.java | 23 | ||||
-rw-r--r-- | src/org/traccar/model/Command.java | 7 | ||||
-rw-r--r-- | src/org/traccar/model/Device.java | 7 | ||||
-rw-r--r-- | src/org/traccar/model/Factory.java | 22 | ||||
-rw-r--r-- | src/org/traccar/model/Permission.java | 7 | ||||
-rw-r--r-- | src/org/traccar/model/Position.java | 7 | ||||
-rw-r--r-- | src/org/traccar/model/Schema.java | 7 | ||||
-rw-r--r-- | src/org/traccar/model/Server.java | 7 | ||||
-rw-r--r-- | src/org/traccar/model/User.java | 7 | ||||
-rw-r--r-- | src/org/traccar/web/DeviceServlet.java | 6 | ||||
-rw-r--r-- | src/org/traccar/web/JsonConverter.java | 31 | ||||
-rw-r--r-- | src/org/traccar/web/ServerServlet.java | 2 | ||||
-rw-r--r-- | src/org/traccar/web/UserServlet.java | 6 |
14 files changed, 44 insertions, 115 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 66d94ac91..7d8966d9a 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -171,7 +171,7 @@ public class DataManager implements IdentityManager { if (schemaVersionQuery != null) { Schema schema = QueryBuilder.create(dataSource, schemaVersionQuery) - .executeQuerySingle(new Schema()); + .executeQuerySingle(Schema.class); int version = 0; if (schema != null) { @@ -246,7 +246,7 @@ public class DataManager implements IdentityManager { public User login(String email, String password) throws SQLException { User user = QueryBuilder.create(dataSource, getQuery("database.loginUser")) .setString("email", email) - .executeQuerySingle(new User()); + .executeQuerySingle(User.class); if (user != null && user.isPasswordValid(password)) { return user; } else { @@ -256,13 +256,13 @@ public class DataManager implements IdentityManager { public Collection<User> getUsers() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectUsersAll")) - .executeQuery(new User()); + .executeQuery(User.class); } public User getUser(long userId) throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectUser")) .setLong("id", userId) - .executeQuerySingle(new User()); + .executeQuerySingle(User.class); } public void addUser(User user) throws SQLException { @@ -297,18 +297,18 @@ public class DataManager implements IdentityManager { public Collection<Permission> getPermissions() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.getPermissionsAll")) - .executeQuery(new Permission()); + .executeQuery(Permission.class); } public Collection<Device> getAllDevices() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectDevicesAll")) - .executeQuery(new Device()); + .executeQuery(Device.class); } public Collection<Device> getDevices(long userId) throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectDevices")) .setLong("userId", userId) - .executeQuery(new Device()); + .executeQuery(Device.class); } public void addDevice(Device device) throws SQLException { @@ -365,7 +365,7 @@ public class DataManager implements IdentityManager { .setLong("deviceId", deviceId) .setDate("from", from) .setDate("to", to) - .executeQuery(new Position()); + .executeQuery(Position.class); } public void addPosition(Position position) throws SQLException { @@ -392,12 +392,12 @@ public class DataManager implements IdentityManager { public Collection<Position> getLatestPositions() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectLatestPositions")) - .executeQuery(new Position()); + .executeQuery(Position.class); } public Server getServer() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectServers")) - .executeQuerySingle(new Server()); + .executeQuerySingle(Server.class); } public void updateServer(Server server) throws SQLException { diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java index ca6335556..d0c1c649f 100644 --- a/src/org/traccar/database/QueryBuilder.java +++ b/src/org/traccar/database/QueryBuilder.java @@ -38,7 +38,6 @@ import javax.json.stream.JsonParsingException; import javax.sql.DataSource; import org.traccar.Context; import org.traccar.helper.Log; -import org.traccar.model.Factory; import org.traccar.model.MiscFormatter; public final class QueryBuilder { @@ -272,8 +271,8 @@ public final class QueryBuilder { void process(T object, ResultSet resultSet) throws SQLException; } - public <T extends Factory> T executeQuerySingle(T prototype) throws SQLException { - Collection<T> result = executeQuery(prototype); + public <T> T executeQuerySingle(Class<T> clazz) throws SQLException { + Collection<T> result = executeQuery(clazz); if (!result.isEmpty()) { return result.iterator().next(); } else { @@ -281,7 +280,7 @@ public final class QueryBuilder { } } - private <T extends Factory> void addProcessors( + private <T> void addProcessors( List<ResultSetProcessor<T>> processors, Class<?> parameterType, final Method method, final String name) { if (parameterType.equals(boolean.class)) { @@ -367,7 +366,7 @@ public final class QueryBuilder { } } - public <T extends Factory> Collection<T> executeQuery(T prototype) throws SQLException { + public <T> Collection<T> executeQuery(Class<T> clazz) throws SQLException { List<T> result = new LinkedList<>(); if (query != null) { @@ -380,7 +379,7 @@ public final class QueryBuilder { List<ResultSetProcessor<T>> processors = new LinkedList<>(); - Method[] methods = prototype.getClass().getMethods(); + Method[] methods = clazz.getMethods(); for (final Method method : methods) { if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) { @@ -404,11 +403,15 @@ public final class QueryBuilder { } while (resultSet.next()) { - T object = (T) prototype.create(); - for (ResultSetProcessor<T> processor : processors) { - processor.process(object, resultSet); + try { + T object = clazz.newInstance(); + for (ResultSetProcessor<T> processor : processors) { + processor.process(object, resultSet); + } + result.add(object); + } catch (InstantiationException | IllegalAccessException e) { + throw new IllegalArgumentException(); } - result.add(object); } } diff --git a/src/org/traccar/model/Command.java b/src/org/traccar/model/Command.java index fb3a37795..df62164c2 100644 --- a/src/org/traccar/model/Command.java +++ b/src/org/traccar/model/Command.java @@ -15,7 +15,7 @@ */ package org.traccar.model; -public class Command extends Extensible implements Factory { +public class Command extends Extensible { public static final String TYPE_POSITION_SINGLE = "positionSingle"; public static final String TYPE_POSITION_PERIODIC = "positionPeriodic"; @@ -35,9 +35,4 @@ public class Command extends Extensible implements Factory { public static final String KEY_DEVICE_PASSWORD = "devicePassword"; public static final String KEY_RADIUS = "radius"; - @Override - public Command create() { - return new Command(); - } - } diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java index fd62cc691..c82581c63 100644 --- a/src/org/traccar/model/Device.java +++ b/src/org/traccar/model/Device.java @@ -17,12 +17,7 @@ package org.traccar.model; import java.util.Date; -public class Device implements Factory { - - @Override - public Device create() { - return new Device(); - } +public class Device { private long id; diff --git a/src/org/traccar/model/Factory.java b/src/org/traccar/model/Factory.java deleted file mode 100644 index 150f52b80..000000000 --- a/src/org/traccar/model/Factory.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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.model; - -public interface Factory { - - Object create(); - -} diff --git a/src/org/traccar/model/Permission.java b/src/org/traccar/model/Permission.java index 065295ff9..393de2359 100644 --- a/src/org/traccar/model/Permission.java +++ b/src/org/traccar/model/Permission.java @@ -15,12 +15,7 @@ */ package org.traccar.model; -public class Permission implements Factory { - - @Override - public Permission create() { - return new Permission(); - } +public class Permission { private long userId; diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index 9494da042..1a38681ca 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -17,12 +17,7 @@ package org.traccar.model; import java.util.Date; -public class Position extends Event implements Factory { - - @Override - public Position create() { - return new Position(); - } +public class Position extends Event { private Date fixTime; diff --git a/src/org/traccar/model/Schema.java b/src/org/traccar/model/Schema.java index 3d1ce755f..0d0e58831 100644 --- a/src/org/traccar/model/Schema.java +++ b/src/org/traccar/model/Schema.java @@ -15,12 +15,7 @@ */ package org.traccar.model; -public class Schema implements Factory { - - @Override - public Schema create() { - return new Schema(); - } +public class Schema { private int version; diff --git a/src/org/traccar/model/Server.java b/src/org/traccar/model/Server.java index e96a40e27..880636a4c 100644 --- a/src/org/traccar/model/Server.java +++ b/src/org/traccar/model/Server.java @@ -15,12 +15,7 @@ */ package org.traccar.model; -public class Server implements Factory { - - @Override - public Server create() { - return new Server(); - } +public class Server { private long id; diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java index 31e54752a..1531ba9e4 100644 --- a/src/org/traccar/model/User.java +++ b/src/org/traccar/model/User.java @@ -18,12 +18,7 @@ package org.traccar.model; import org.traccar.helper.Hashing; import org.traccar.web.JsonIgnore; -public class User implements Factory { - - @Override - public User create() { - return new User(); - } +public class User { private long id; diff --git a/src/org/traccar/web/DeviceServlet.java b/src/org/traccar/web/DeviceServlet.java index 8f983ad78..a501bd8f6 100644 --- a/src/org/traccar/web/DeviceServlet.java +++ b/src/org/traccar/web/DeviceServlet.java @@ -70,7 +70,7 @@ public class DeviceServlet extends BaseServlet { } private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception { - Device device = JsonConverter.objectFromJson(req.getReader(), new Device()); + Device device = JsonConverter.objectFromJson(req.getReader(), Device.class); long userId = getUserId(req); Context.getDataManager().addDevice(device); Context.getDataManager().linkDevice(userId, device.getId()); @@ -79,14 +79,14 @@ public class DeviceServlet extends BaseServlet { } private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { - Device device = JsonConverter.objectFromJson(req.getReader(), new Device()); + Device device = JsonConverter.objectFromJson(req.getReader(), Device.class); Context.getPermissionsManager().checkDevice(getUserId(req), device.getId()); Context.getDataManager().updateDevice(device); sendResponse(resp.getWriter(), true); } private void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception { - Device device = JsonConverter.objectFromJson(req.getReader(), new Device()); + Device device = JsonConverter.objectFromJson(req.getReader(), Device.class); Context.getPermissionsManager().checkDevice(getUserId(req), device.getId()); Context.getDataManager().removeDevice(device); Context.getPermissionsManager().refresh(); diff --git a/src/org/traccar/web/JsonConverter.java b/src/org/traccar/web/JsonConverter.java index 2ef61fb13..8bf5607fb 100644 --- a/src/org/traccar/web/JsonConverter.java +++ b/src/org/traccar/web/JsonConverter.java @@ -35,7 +35,6 @@ import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; import org.traccar.helper.Log; -import org.traccar.model.Factory; import org.traccar.model.MiscFormatter; public final class JsonConverter { @@ -43,32 +42,12 @@ public final class JsonConverter { private JsonConverter() { } - private static <T> T newClassInstance(Class<T> clazz) { - try { - return clazz.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - throw new IllegalArgumentException(); - } - } - private static final DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.dateTime(); public static Date parseDate(String value) { return DATE_FORMAT.parseDateTime(value).toDate(); } - public static <T extends Factory> T objectFromJson(Reader reader, T prototype) throws ParseException { - try (JsonReader jsonReader = Json.createReader(reader)) { - return objectFromJson(jsonReader.readObject(), prototype); - } - } - - public static <T extends Factory> T objectFromJson(JsonObject json, T prototype) { - T object = (T) prototype.create(); - Method[] methods = object.getClass().getMethods(); - return objectFromJson(json, object, methods); - } - public static <T> T objectFromJson(Reader reader, Class<T> clazz) throws ParseException { try (JsonReader jsonReader = Json.createReader(reader)) { return objectFromJson(jsonReader.readObject(), clazz); @@ -76,9 +55,13 @@ public final class JsonConverter { } public static <T> T objectFromJson(JsonObject json, Class<T> clazz) { - T object = newClassInstance(clazz); - Method[] methods = object.getClass().getMethods(); - return objectFromJson(json, object, methods); + try { + T object = clazz.newInstance(); + Method[] methods = object.getClass().getMethods(); + return objectFromJson(json, object, methods); + } catch (InstantiationException | IllegalAccessException e) { + throw new IllegalArgumentException(); + } } private static <T> T objectFromJson(JsonObject json, T object, Method[] methods) { diff --git a/src/org/traccar/web/ServerServlet.java b/src/org/traccar/web/ServerServlet.java index 7ed096bc6..b0ee181da 100644 --- a/src/org/traccar/web/ServerServlet.java +++ b/src/org/traccar/web/ServerServlet.java @@ -44,7 +44,7 @@ public class ServerServlet extends BaseServlet { } private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { - Server server = JsonConverter.objectFromJson(req.getReader(), new Server()); + Server server = JsonConverter.objectFromJson(req.getReader(), Server.class); Context.getPermissionsManager().checkAdmin(getUserId(req)); Context.getDataManager().updateServer(server); sendResponse(resp.getWriter(), true); diff --git a/src/org/traccar/web/UserServlet.java b/src/org/traccar/web/UserServlet.java index 6bd870d4d..ef1888667 100644 --- a/src/org/traccar/web/UserServlet.java +++ b/src/org/traccar/web/UserServlet.java @@ -51,7 +51,7 @@ public class UserServlet extends BaseServlet { } private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception { - User user = JsonConverter.objectFromJson(req.getReader(), new User()); + User user = JsonConverter.objectFromJson(req.getReader(), User.class); Context.getPermissionsManager().checkUser(getUserId(req), user.getId()); Context.getDataManager().addUser(user); Context.getPermissionsManager().refresh(); @@ -59,7 +59,7 @@ public class UserServlet extends BaseServlet { } private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception { - User user = JsonConverter.objectFromJson(req.getReader(), new User()); + User user = JsonConverter.objectFromJson(req.getReader(), User.class); if (user.getAdmin()) { Context.getPermissionsManager().checkAdmin(getUserId(req)); } else { @@ -71,7 +71,7 @@ public class UserServlet extends BaseServlet { } private void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception { - User user = JsonConverter.objectFromJson(req.getReader(), new User()); + User user = JsonConverter.objectFromJson(req.getReader(), User.class); Context.getPermissionsManager().checkUser(getUserId(req), user.getId()); Context.getDataManager().removeUser(user); Context.getPermissionsManager().refresh(); |