2 * Copyright (c) 2019-2020. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.chess.scanner;
19 import java.net.MalformedURLException;
21 import java.util.HashMap;
22 import java.util.HashSet;
26 import org.hedgecode.chess.scanner.entity.PGNGame;
27 import org.hedgecode.chess.scanner.portal.ChessBombScanner;
28 import org.hedgecode.chess.scanner.portal.ChessGamesScanner;
29 import org.hedgecode.chess.scanner.portal.LiChessScanner;
34 * @author Dmitry Samoshin aka gotty
36 public final class ChessHogScanner {
38 private static ChessHogScanner singleton;
40 private final Map<ScannerType, Scanner> scanners = new HashMap<ScannerType, Scanner>() {
42 put( ScannerType.CHESSGAMES, new ChessGamesScanner() );
43 put( ScannerType.LICHESS, new LiChessScanner() );
44 put( ScannerType.CHESSBOMB, new ChessBombScanner() );
48 private final Set<ScannerType> initScanners = new HashSet<>();
51 * Get Scanner by {@code ScannerType}.
53 * @param type Scanner type (binding to specific chess portal).
54 * @return Implementation of {@code Scanner} for specific portal.
56 public static Scanner scanner(ScannerType type) {
57 return getStore().getScanner(type);
61 * Scan PGN game by URL (for all scanner types).
63 * @param url URL string for scan.
65 * @throws ChessHogScannerException Incorrect URL or unknown chess portal.
67 public static PGNGame scan(String url) throws ChessHogScannerException {
70 hostName = new URL(url).getHost();
71 } catch (MalformedURLException cause) {
72 throw new ChessHogScannerException(
73 String.format("Incorrect URL: %s", url), cause
76 ScannerType type = ScannerType.byHost(hostName);
78 throw new ChessHogScannerException(
79 String.format("Host %s is not among the known for the scanner", hostName)
82 return scanner(type).scanUrl(url);
86 for (ScannerType type : scanners.keySet()) {
91 private void init(ScannerType type) {
92 Scanner scanner = scanners.get(type);
94 if (scanner instanceof Initiable) {
95 Initiable initiableScanner = (Initiable) scanner;
96 initiableScanner.init();
98 initScanners.add(type);
99 } catch (ChessHogScannerException e) {
100 throw new RuntimeException(e);
105 private Scanner getScanner(ScannerType type) {
106 if (!initScanners.contains(type)) {
109 return scanners.get(type);
112 private ChessHogScanner() {
113 if (!ChessHogScannerProperties.is("scanner.lazy.init")) {
118 private static ChessHogScanner getStore() {
119 if (singleton == null) {
120 singleton = new ChessHogScanner();