From 2ff620d3326b2f30564e19ede0d361a2719bcf6b Mon Sep 17 00:00:00 2001 From: gotty Date: Sun, 12 Jan 2020 03:11:11 +0300 Subject: [PATCH] [LIB-13] Add chesshog-scanner general purpose classes --- .../chess/scanner/ChessHogScannerConsole.java | 111 +++++++++++++++++++++ .../chess/scanner/ChessHogScannerConstants.java | 49 +++++++++ .../chess/scanner/ChessHogScannerException.java | 38 +++++++ .../chess/scanner/ChessHogScannerProperties.java | 91 +++++++++++++++++ src/main/resources/scanner.properties | 21 ++++ 5 files changed, 310 insertions(+) create mode 100644 src/main/java/org/hedgecode/chess/scanner/ChessHogScannerConsole.java create mode 100644 src/main/java/org/hedgecode/chess/scanner/ChessHogScannerConstants.java create mode 100644 src/main/java/org/hedgecode/chess/scanner/ChessHogScannerException.java create mode 100644 src/main/java/org/hedgecode/chess/scanner/ChessHogScannerProperties.java create mode 100644 src/main/resources/scanner.properties diff --git a/src/main/java/org/hedgecode/chess/scanner/ChessHogScannerConsole.java b/src/main/java/org/hedgecode/chess/scanner/ChessHogScannerConsole.java new file mode 100644 index 0000000..b3becd9 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/ChessHogScannerConsole.java @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * 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.hedgecode.chess.scanner; + +import java.time.LocalDateTime; +import java.util.Calendar; + +/** + * ChessHog Scanner utility class for working with console. + * + * @author Dmitry Samoshin aka gotty + */ +public class ChessHogScannerConsole { + + private static final String[] ASCII_LOGO = { + " _______ _ _ ", + " | | _|_ _| |___ ___ ___ ___ _| |___ ", + " | | -_| . | . | -_| _| . | . | -_| ", + " | |___|___|_ |___|___|___|___|___| ", + " _ |___|___| _ |___| ", + " ___| |_ ___ ___ ___| |_ ___ ___ ___ ___ ___ ___ ___ ___ ___ ", + " | _| | -_|_ -|_ -| | . | . | |_ -| _| .'| | | -_| _| ", + " |___|_|_|___|___|___|_|_|___|_ | |___|___|__,|_|_|_|_|___|_| ", + " |___| " + }; + + private static final String COPYRIGHT = "Copyright (c) Hedgecode"; + + private static boolean logMode = false; + + private ChessHogScannerConsole() { + } + + public static void setLogMode(boolean isLogMode) { + logMode = isLogMode; + } + + public static void console(String message) { + System.out.println( + String.format( + logMode ? "[%1$tT] %2$s" : "%2$s", + LocalDateTime.now(), + message == null ? "" : message + ) + ); + } + + public static void empty() { + console(null); + } + + public static void init() { + asciiLogo(); + empty(); + copyright(); + version(); + empty(); + } + + private static void asciiLogo() { + for (String line : ASCII_LOGO) { + console(line); + } + } + + private static void copyright() { + int inceptionYear = Integer.parseInt( + ChessHogScannerProperties.get("scanner.inception.year") + ); + int currentYear = Calendar.getInstance().get(Calendar.YEAR); + String copyright = currentYear > inceptionYear + ? String.format("%s-%s %s", inceptionYear, currentYear, COPYRIGHT) + : String.format("%s %s", currentYear, COPYRIGHT); + console( + alignCenter(copyright) + ); + } + + private static void version() { + console( + alignCenter( + String.format( + "v. %s", ChessHogScannerProperties.get("scanner.version") + ) + ) + ); + } + + private static String alignCenter(String message) { + int indent = (ASCII_LOGO[0].length() + message.length()) / 2; + return String.format( + "%" + indent + "s", + message + ); + } + +} diff --git a/src/main/java/org/hedgecode/chess/scanner/ChessHogScannerConstants.java b/src/main/java/org/hedgecode/chess/scanner/ChessHogScannerConstants.java new file mode 100644 index 0000000..651a3e4 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/ChessHogScannerConstants.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * 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.hedgecode.chess.scanner; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +/** + * Store of ChessHogScanner constants. + * + * @author Dmitry Samoshin aka gotty + */ +public final class ChessHogScannerConstants { + + public static final Charset CHARSET = StandardCharsets.UTF_8; + + public static final String CRLF = System.getProperty("line.separator"); + + public static final String PORTAL_CHESSGAMES = "chessgames"; + public static final String PORTAL_LICHESS = "lichess"; + public static final String PORTAL_CHESSBOMB = "chessbomb"; + public static final String PORTAL_CHESS24 = "chess24"; + public static final String PORTAL_CHESSCOM = "chesscom"; + + public static final String PROXY_UNDEFINED = "undefined"; + public static final String PROXY_HTTP = "http"; + public static final String PROXY_SOCKS = "socks"; + + private ChessHogScannerConstants() { + throw new AssertionError( + String.format("No %s instances!", ChessHogScannerConstants.class.getName()) + ); + } + +} diff --git a/src/main/java/org/hedgecode/chess/scanner/ChessHogScannerException.java b/src/main/java/org/hedgecode/chess/scanner/ChessHogScannerException.java new file mode 100644 index 0000000..4636585 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/ChessHogScannerException.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * 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.hedgecode.chess.scanner; + +/** + * ChessHogScannerException + * + * @author Dmitry Samoshin aka gotty + */ +public class ChessHogScannerException extends Exception { + + public ChessHogScannerException(String message) { + super(message); + } + + public ChessHogScannerException(Throwable cause) { + super(cause); + } + + public ChessHogScannerException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/org/hedgecode/chess/scanner/ChessHogScannerProperties.java b/src/main/java/org/hedgecode/chess/scanner/ChessHogScannerProperties.java new file mode 100644 index 0000000..e7565bc --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/ChessHogScannerProperties.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2019-2020. Developed by Hedgecode. + * + * 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.hedgecode.chess.scanner; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +/** + * ChessHogScanner properties holder. + * + * @author Dmitry Samoshin aka gotty + */ +public class ChessHogScannerProperties { + + /** Properties file. */ + private static final String PROPERTIES_FILE = "scanner.properties"; + + /** Properties. */ + private static final Properties PROPERTIES; + + /** + * Static properties initialization. + */ + static { + PROPERTIES = new Properties(); + readProperties(PROPERTIES_FILE, PROPERTIES, true); + } + + /** + * @param propsFile Properties file name. + * @param props Properties. + * @param throwExc Flag indicating whether to throw an exception or not. + */ + public static void readProperties(String propsFile, Properties props, boolean throwExc) { + try (InputStream is = ChessHogScannerProperties.class.getClassLoader().getResourceAsStream(propsFile)) + { + if (is == null) { + if (throwExc) { + throw new RuntimeException( + String.format("Failed to find properties file: %s", propsFile) + ); + } else { + return; + } + } + props.load(is); + } catch (IOException cause) { + throw new RuntimeException( + String.format("Failed to read properties file: %s", propsFile), + cause + ); + } + } + + /** + * Empty string for not found properties. + */ + private static final String EMPTY = ""; + + /** + * Gets property value. + * + * @param key Property key. + * @return Property value (possibly empty string, but never {@code null}). + */ + public static String get(String key) { + return PROPERTIES.getProperty(key, EMPTY); + } + + /** + * Private constructor. + */ + private ChessHogScannerProperties() { + } + +} diff --git a/src/main/resources/scanner.properties b/src/main/resources/scanner.properties new file mode 100644 index 0000000..ea03876 --- /dev/null +++ b/src/main/resources/scanner.properties @@ -0,0 +1,21 @@ +# Copyright (c) 2019-2020. Developed by Hedgecode. +# +# 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. + +scanner.name=chesshog-scanner +scanner.version=0.1-SNAPSHOT +scanner.inception.year=2019 + +scanner.use.proxy=false +scanner.proxy.server= +scanner.proxy.auth= -- 2.10.0