* Server: added comments support in news feed (server.msg.txt), removed outdated code;

This commit is contained in:
Oleg Agafonov 2020-09-04 02:48:07 +04:00
parent cf640b734b
commit fb1c230761

View file

@ -25,14 +25,12 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
*/ */
public enum ServerMessagesUtil { public enum ServerMessagesUtil {
instance; instance;
private static final Logger log = Logger.getLogger(ServerMessagesUtil.class);
private static final Logger LOGGER = Logger.getLogger(ServerMessagesUtil.class);
private static final String SERVER_MSG_TXT_FILE = "server.msg.txt"; private static final String SERVER_MSG_TXT_FILE = "server.msg.txt";
private final List<String> messages = new ArrayList<>(); private final List<String> messages = new ArrayList<>();
private final ReadWriteLock lock = new ReentrantReadWriteLock(); private final ReadWriteLock lock = new ReentrantReadWriteLock();
private static String pathToExternalMessages = null;
private static boolean ignore = false; private static boolean ignore = false;
private static long startDate; private static long startDate;
@ -43,16 +41,11 @@ public enum ServerMessagesUtil {
private static final AtomicInteger lostConnection = new AtomicInteger(0); private static final AtomicInteger lostConnection = new AtomicInteger(0);
private static final AtomicInteger reconnects = new AtomicInteger(0); private static final AtomicInteger reconnects = new AtomicInteger(0);
static {
pathToExternalMessages = System.getProperty("messagesPath");
}
ServerMessagesUtil() { ServerMessagesUtil() {
ScheduledExecutorService updateExecutor = Executors.newSingleThreadScheduledExecutor(); ScheduledExecutorService updateExecutor = Executors.newSingleThreadScheduledExecutor();
updateExecutor.scheduleAtFixedRate(this::reloadMessages, 5, 5 * 60, TimeUnit.SECONDS); updateExecutor.scheduleAtFixedRate(this::reloadMessages, 5, 5 * 60, TimeUnit.SECONDS);
} }
public List<String> getMessages() { public List<String> getMessages() {
lock.readLock().lock(); lock.readLock().lock();
try { try {
@ -63,7 +56,7 @@ public enum ServerMessagesUtil {
} }
private void reloadMessages() { private void reloadMessages() {
log.debug("Reading server messages..."); LOGGER.debug("Reading server messages...");
List<String> motdMessages = readFromFile(); List<String> motdMessages = readFromFile();
List<String> newMessages = new ArrayList<>(); List<String> newMessages = new ArrayList<>();
newMessages.addAll(motdMessages); newMessages.addAll(motdMessages);
@ -83,54 +76,36 @@ public enum ServerMessagesUtil {
if (ignore) { if (ignore) {
return Collections.emptyList(); return Collections.emptyList();
} }
File externalFile = null;
if (pathToExternalMessages != null) {
externalFile = new File(pathToExternalMessages);
if (!externalFile.exists()) {
log.warn("Couldn't find server.msg.txt using external path: " + pathToExternalMessages);
pathToExternalMessages = null; // not to repeat error action again
externalFile = null;
} else if (!externalFile.canRead()) {
log.warn("Couldn't read (no access) server.msg.txt using external path: " + pathToExternalMessages);
pathToExternalMessages = null; // not to repeat error action again
}
}
InputStream is = null; InputStream is = null;
if (externalFile != null) {
try {
is = new FileInputStream(externalFile);
} catch (Exception f) {
log.error(f, f);
pathToExternalMessages = null; // not to repeat error action again
}
} else {
File file = new File(SERVER_MSG_TXT_FILE); File file = new File(SERVER_MSG_TXT_FILE);
if (!file.exists() || !file.canRead()) { if (!file.exists() || !file.canRead()) {
log.warn("Couldn't find server.msg.txt using path: " + SERVER_MSG_TXT_FILE); LOGGER.warn("Couldn't find server messages file using path: " + file.getAbsolutePath());
} else { } else {
try { try {
is = new FileInputStream(file); is = new FileInputStream(file);
ignore = false;
} catch (Exception f) { } catch (Exception f) {
log.error(f, f); LOGGER.error(f, f);
ignore = true; ignore = true;
} }
} }
}
if (is == null) { if (is == null) {
log.warn("Couldn't find server.msg");
return Collections.emptyList(); return Collections.emptyList();
} }
List<String> newMessages = new ArrayList<>(); List<String> newMessages = new ArrayList<>();
try (Scanner scanner = new Scanner(is)) { try (Scanner scanner = new Scanner(is)) {
while (scanner.hasNextLine()) { while (scanner.hasNextLine()) {
String message = scanner.nextLine(); String message = scanner.nextLine().trim();
if (!message.trim().isEmpty()) { if (message.startsWith("//") || message.isEmpty()) {
continue;
}
newMessages.add(message.trim()); newMessages.add(message.trim());
} }
}
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); LOGGER.error(e.getMessage(), e);
} finally { } finally {
StreamUtils.closeQuietly(is); StreamUtils.closeQuietly(is);
} }
@ -157,11 +132,6 @@ public enum ServerMessagesUtil {
return statistics; return statistics;
} }
// private Timer timer = new Timer(1000 * 60, new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// reloadMessages();
// }
// });
public void setStartDate(long milliseconds) { public void setStartDate(long milliseconds) {
startDate = milliseconds; startDate = milliseconds;
} }