From 14840af2abd9976b8f5634af8e77f4a7126dfac1 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 27 Jun 2018 15:55:19 +0500 Subject: - Rename NotificationException to MessageException - Simplify Notificator instantiation - Make sms configuration more clear - Move some defaults in code - Some cleanup --- src/org/traccar/Context.java | 12 +++--- .../traccar/api/resource/NotificationResource.java | 6 +-- src/org/traccar/notification/MessageException.java | 25 +++++++++++ .../notification/NotificationException.java | 25 ----------- .../notification/NotificationFormatter.java | 7 +-- src/org/traccar/notification/NotificationMail.java | 4 +- src/org/traccar/notification/NotificationSms.java | 22 ++++------ src/org/traccar/notification/NotificationWeb.java | 3 +- src/org/traccar/notification/Notificator.java | 6 +-- .../traccar/notification/NotificatorManager.java | 50 ++++++++++++---------- src/org/traccar/smpp/SmppClient.java | 14 +++--- src/org/traccar/sms/SMSException.java | 28 ------------ src/org/traccar/sms/SMSManager.java | 24 ----------- src/org/traccar/sms/SmsManager.java | 27 ++++++++++++ 14 files changed, 112 insertions(+), 141 deletions(-) create mode 100644 src/org/traccar/notification/MessageException.java delete mode 100644 src/org/traccar/notification/NotificationException.java delete mode 100644 src/org/traccar/sms/SMSException.java delete mode 100644 src/org/traccar/sms/SMSManager.java create mode 100644 src/org/traccar/sms/SmsManager.java (limited to 'src/org/traccar') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 80be1ddc6..2afdde3b9 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -79,7 +79,7 @@ import org.traccar.notification.EventForwarder; import org.traccar.notification.JsonTypeEventForwarder; import org.traccar.notification.NotificatorManager; import org.traccar.reports.model.TripsConfig; -import org.traccar.sms.SMSManager; +import org.traccar.sms.SmsManager; import org.traccar.web.WebServer; import javax.ws.rs.client.Client; @@ -261,9 +261,9 @@ public final class Context { return statisticsManager; } - private static SMSManager smsManager; + private static SmsManager smsManager; - public static SMSManager getSmsManager() { + public static SmsManager getSmsManager() { return smsManager; } @@ -396,11 +396,11 @@ public final class Context { tripsConfig = initTripsConfig(); - if (config.getBoolean("sms.smpp.enable")) { + if (config.getBoolean("sms.enable")) { final String smsManagerClass = config.getString("sms.manager.class", "org.traccar.smpp.SmppClient"); try { - smsManager = (SMSManager) Class.forName(smsManagerClass).newInstance(); - } catch (ClassNotFoundException e) { + smsManager = (SmsManager) Class.forName(smsManagerClass).newInstance(); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { Log.warning("Error loading SMS Manager class : " + smsManagerClass, e); } } diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 0d7a7982d..9631a52b7 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -31,7 +31,7 @@ import org.traccar.api.ExtendedObjectResource; import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Typed; -import org.traccar.notification.NotificationException; +import org.traccar.notification.MessageException; @Path("notifications") @@ -57,7 +57,7 @@ public class NotificationResource extends ExtendedObjectResource { @POST @Path("test") - public Response testMessage() throws NotificationException, InterruptedException { + public Response testMessage() throws MessageException, InterruptedException { for (Typed method : Context.getNotificatorManager().getAllNotificatorTypes()) { Context.getNotificatorManager() .getNotificator(method.getType()).sendSync(getUserId(), new Event("test", 0), null); @@ -68,7 +68,7 @@ public class NotificationResource extends ExtendedObjectResource { @POST @Path("test/{notificator}") public Response testMessage(@PathParam("notificator") String notificator) - throws NotificationException, InterruptedException { + throws MessageException, InterruptedException { Context.getNotificatorManager().getNotificator(notificator).sendSync(getUserId(), new Event("test", 0), null); return Response.noContent().build(); } diff --git a/src/org/traccar/notification/MessageException.java b/src/org/traccar/notification/MessageException.java new file mode 100644 index 000000000..ce4b9f6ee --- /dev/null +++ b/src/org/traccar/notification/MessageException.java @@ -0,0 +1,25 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) + * + * 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.notification; + +public class MessageException extends Exception { + + public MessageException(Throwable cause) { + super(cause); + } + +} diff --git a/src/org/traccar/notification/NotificationException.java b/src/org/traccar/notification/NotificationException.java deleted file mode 100644 index 525e5ee37..000000000 --- a/src/org/traccar/notification/NotificationException.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2018 Anton Tananaev (anton@traccar.org) - * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) - * - * 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.notification; - -public class NotificationException extends Exception { - - public NotificationException(Throwable cause) { - super(cause); - } - -} diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index a1abdd0cc..c011403c5 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -89,17 +89,14 @@ public final class NotificationFormatter { } public static FullMessage formatFullMessage(long userId, Event event, Position position) { - String templatePath = Context.getConfig().getString("message.full.templatesPath", "full"); VelocityContext velocityContext = prepareContext(userId, event, position); - String formattedMessage = formatMessage(velocityContext, userId, event, position, templatePath); + String formattedMessage = formatMessage(velocityContext, userId, event, position, "full"); return new FullMessage((String) velocityContext.get("subject"), formattedMessage); } public static String formatShortMessage(long userId, Event event, Position position) { - String templatePath = Context.getConfig().getString("message.short.templatesPath", "short"); - - return formatMessage(null, userId, event, position, templatePath); + return formatMessage(null, userId, event, position, "short"); } private static String formatMessage(VelocityContext vc, Long userId, Event event, Position position, diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index 808acca76..c2ee67299 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -82,7 +82,7 @@ public final class NotificationMail extends Notificator { } @Override - public void sendSync(long userId, Event event, Position position) throws NotificationException { + public void sendSync(long userId, Event event, Position position) throws MessageException { User user = Context.getPermissionsManager().getUser(userId); Properties properties = null; @@ -125,7 +125,7 @@ public final class NotificationMail extends Notificator { transport.close(); } } catch (MessagingException e) { - throw new NotificationException(e); + throw new MessageException(e); } } diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index dd3304d85..ed651ac11 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -20,41 +20,37 @@ import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; -import org.traccar.sms.SMSManager; -import org.traccar.sms.SMSException; +import org.traccar.sms.SmsManager; public final class NotificationSms extends Notificator { - private final SMSManager sms; + private final SmsManager smsManager; - public NotificationSms(String methodId) throws ClassNotFoundException, - InstantiationException, IllegalAccessException { - final String smsClass = Context.getConfig().getString("notificator." + methodId + ".sms.class", ""); + public NotificationSms() throws ClassNotFoundException, InstantiationException, IllegalAccessException { + final String smsClass = Context.getConfig().getString("notificator.sms.manager.class", ""); if (smsClass.length() > 0) { - sms = (SMSManager) Class.forName(smsClass).newInstance(); + smsManager = (SmsManager) Class.forName(smsClass).newInstance(); } else { - sms = Context.getSmsManager(); + smsManager = Context.getSmsManager(); } } - @Override public void sendAsync(long userId, Event event, Position position) { final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - sms.sendMessageAsync(user.getPhone(), + smsManager.sendMessageAsync(user.getPhone(), NotificationFormatter.formatShortMessage(userId, event, position), false); } } @Override - public void sendSync(long userId, Event event, Position position) throws SMSException, - InterruptedException { + public void sendSync(long userId, Event event, Position position) throws MessageException, InterruptedException { final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - sms.sendMessageSync(user.getPhone(), + smsManager.sendMessageSync(user.getPhone(), NotificationFormatter.formatShortMessage(userId, event, position), false); } } diff --git a/src/org/traccar/notification/NotificationWeb.java b/src/org/traccar/notification/NotificationWeb.java index 8e04fc2c4..afc401d24 100644 --- a/src/org/traccar/notification/NotificationWeb.java +++ b/src/org/traccar/notification/NotificationWeb.java @@ -23,8 +23,7 @@ import org.traccar.model.Position; public final class NotificationWeb extends Notificator { @Override - public void sendSync(long userId, Event event, Position position) throws NotificationException, - InterruptedException { + public void sendSync(long userId, Event event, Position position) { Context.getConnectionManager().updateEvent(userId, event); } diff --git a/src/org/traccar/notification/Notificator.java b/src/org/traccar/notification/Notificator.java index 09f98d3ad..d912b445d 100644 --- a/src/org/traccar/notification/Notificator.java +++ b/src/org/traccar/notification/Notificator.java @@ -27,14 +27,14 @@ public abstract class Notificator { public void run() { try { sendSync(userId, event, position); - } catch (NotificationException | InterruptedException error) { + } catch (MessageException | InterruptedException error) { Log.warning(error); } } }).start(); } - public abstract void sendSync(long userId, Event event, Position position) - throws NotificationException, InterruptedException; + throws MessageException, InterruptedException; + } diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index bb55844f3..147de47d3 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -16,8 +16,6 @@ */ package org.traccar.notification; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -29,31 +27,40 @@ import org.traccar.model.Typed; public final class NotificatorManager { + private static final String DEFAULT_WEB_NOTIFICATOR = "org.traccar.notification.NotificationWeb"; + private static final String DEFAULT_MAIL_NOTIFICATOR = "org.traccar.notification.NotificationMail"; + private static final String DEFAULT_SMS_NOTIFICATOR = "org.traccar.notification.NotificationSms"; + + private final Map notificators = new HashMap<>(); + private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + public NotificatorManager() { final String[] types = Context.getConfig().getString("notificator.types", "").split(","); for (String type : types) { - final String className = Context.getConfig().getString("notificator." + type + ".class", ""); - if (className.length() > 0) { - try { - final Class clazz = (Class) Class.forName(className); - try { - final Constructor constructor = clazz.getConstructor(new Class[]{String.class}); - notificators.put(type, constructor.newInstance(type)); - } catch (NoSuchMethodException e) { - // No constructor with String argument - notificators.put(type, clazz.newInstance()); - } - } catch (ClassNotFoundException | InstantiationException - | IllegalAccessException | InvocationTargetException e) { - Log.error("Unable to load notificator class for " + type + " " + className + " " + e.getMessage()); - } + String defaultNotificator = ""; + switch (type) { + case "web": + defaultNotificator = DEFAULT_WEB_NOTIFICATOR; + break; + case "mail": + defaultNotificator = DEFAULT_MAIL_NOTIFICATOR; + break; + case "sms": + defaultNotificator = DEFAULT_SMS_NOTIFICATOR; + break; + default: + break; + } + final String className = Context.getConfig() + .getString("notificator." + type + ".class", defaultNotificator); + try { + notificators.put(type, (Notificator) Class.forName(className).newInstance()); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { + Log.error("Unable to load notificator class for " + type + " " + className + " " + e.getMessage()); } } } - private final Map notificators = new HashMap<>(); - private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); - public Notificator getNotificator(String type) { final Notificator notificator = notificators.get(type); if (notificator == null) { @@ -63,7 +70,6 @@ public final class NotificatorManager { return notificator; } - public Set getAllNotificatorTypes() { Set result = new HashSet<>(); for (String notificator : notificators.keySet()) { @@ -72,6 +78,4 @@ public final class NotificatorManager { return result; } - } - diff --git a/src/org/traccar/smpp/SmppClient.java b/src/org/traccar/smpp/SmppClient.java index d5e6820ab..ddda4cb4f 100644 --- a/src/org/traccar/smpp/SmppClient.java +++ b/src/org/traccar/smpp/SmppClient.java @@ -25,8 +25,8 @@ import java.util.concurrent.TimeUnit; import org.traccar.Context; import org.traccar.helper.Log; -import org.traccar.sms.SMSException; -import org.traccar.sms.SMSManager; +import org.traccar.notification.MessageException; +import org.traccar.sms.SmsManager; import com.cloudhopper.commons.charset.CharsetUtil; import com.cloudhopper.smpp.SmppBindType; @@ -44,7 +44,7 @@ import com.cloudhopper.smpp.type.SmppChannelException; import com.cloudhopper.smpp.type.SmppTimeoutException; import com.cloudhopper.smpp.type.UnrecoverablePduException; -public class SmppClient implements SMSManager { +public class SmppClient implements SmsManager { private SmppSessionConfiguration sessionConfig = new SmppSessionConfiguration(); private SmppSession smppSession; @@ -216,7 +216,7 @@ public class SmppClient implements SMSManager { @Override public synchronized void sendMessageSync(String destAddress, String message, boolean command) - throws SMSException, InterruptedException, IllegalStateException { + throws MessageException, InterruptedException, IllegalStateException { if (getSession() != null && getSession().isBound()) { try { SubmitSm submit = new SubmitSm(); @@ -245,10 +245,10 @@ public class SmppClient implements SMSManager { } } catch (SmppChannelException | RecoverablePduException | SmppTimeoutException | UnrecoverablePduException error) { - throw new SMSException(error); + throw new MessageException(error); } } else { - throw new SMSException(new SmppChannelException("SMPP session is not connected")); + throw new MessageException(new SmppChannelException("SMPP session is not connected")); } } @@ -259,7 +259,7 @@ public class SmppClient implements SMSManager { public void run() { try { sendMessageSync(destAddress, message, command); - } catch (SMSException | InterruptedException | IllegalStateException error) { + } catch (MessageException | InterruptedException | IllegalStateException error) { Log.warning(error); } } diff --git a/src/org/traccar/sms/SMSException.java b/src/org/traccar/sms/SMSException.java deleted file mode 100644 index 22ca95296..000000000 --- a/src/org/traccar/sms/SMSException.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2018 Anton Tananaev (anton@traccar.org) - * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) - * - * 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.sms; - - -import org.traccar.notification.NotificationException; - -public class SMSException extends NotificationException { - - public SMSException(Throwable cause) { - super(cause); - } - -} diff --git a/src/org/traccar/sms/SMSManager.java b/src/org/traccar/sms/SMSManager.java deleted file mode 100644 index c4cb68fb7..000000000 --- a/src/org/traccar/sms/SMSManager.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2018 Anton Tananaev (anton@traccar.org) - * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) - * - * 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.sms; - -public interface SMSManager { - - void sendMessageSync(String destAddress, String message, boolean command) throws InterruptedException, SMSException; - void sendMessageAsync(final String destAddress, final String message, final boolean command); - -} diff --git a/src/org/traccar/sms/SmsManager.java b/src/org/traccar/sms/SmsManager.java new file mode 100644 index 000000000..4bc4bd009 --- /dev/null +++ b/src/org/traccar/sms/SmsManager.java @@ -0,0 +1,27 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) + * + * 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.sms; + +import org.traccar.notification.MessageException; + +public interface SmsManager { + + void sendMessageSync(String destAddress, String message, boolean command) + throws InterruptedException, MessageException; + void sendMessageAsync(final String destAddress, final String message, final boolean command); + +} -- cgit v1.2.3