[LIB-13] Stub scanners for some portals
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / ChessHogScanner.java
1 /*
2  * Copyright (c) 2019-2020. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.scanner;
18
19 import java.net.MalformedURLException;
20 import java.net.URL;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.hedgecode.chess.scanner.entity.PGNGame;
27 import org.hedgecode.chess.scanner.portal.Chess24Scanner;
28 import org.hedgecode.chess.scanner.portal.ChessBombScanner;
29 import org.hedgecode.chess.scanner.portal.ChessComScanner;
30 import org.hedgecode.chess.scanner.portal.ChessGamesScanner;
31 import org.hedgecode.chess.scanner.portal.LiChessScanner;
32
33 /**
34  * ChessHogScanner
35  *
36  * @author Dmitry Samoshin aka gotty
37  */
38 public final class ChessHogScanner {
39
40     private static ChessHogScanner singleton;
41
42     private final Map<ScannerType, Scanner> scanners = new HashMap<ScannerType, Scanner>() {
43         {
44             put( ScannerType.CHESSGAMES, new ChessGamesScanner() );
45             put( ScannerType.LICHESS,    new LiChessScanner()    );
46             put( ScannerType.CHESSBOMB,  new ChessBombScanner()  );
47             put( ScannerType.CHESS24,    new Chess24Scanner()    );
48             put( ScannerType.CHESSCOM,   new ChessComScanner()   );
49         }
50     };
51
52     private final Set<ScannerType> initScanners = new HashSet<>();
53
54     /**
55      * Get Scanner by {@code ScannerType}.
56      *
57      * @param type Scanner type (binding to specific chess portal).
58      * @return Implementation of {@code Scanner} for specific portal.
59      */
60     public static Scanner scanner(ScannerType type) {
61         return getStore().getScanner(type);
62     }
63
64     /**
65      * Scan PGN game by URL (for all scanner types).
66      *
67      * @param url URL string for scan.
68      * @return PGN game.
69      * @throws ChessHogScannerException Incorrect URL or unknown chess portal.
70      */
71     public static PGNGame scan(String url) throws ChessHogScannerException {
72         String hostName;
73         try {
74             hostName = new URL(url).getHost();
75         } catch (MalformedURLException cause) {
76             throw new ChessHogScannerException(
77                     String.format("Incorrect  URL: %s", url), cause
78             );
79         }
80         ScannerType type = ScannerType.byHost(hostName);
81         if (type == null) {
82             throw new ChessHogScannerException(
83                     String.format("Host %s is not among the known for the scanner", hostName)
84             );
85         }
86         return scanner(type).scanUrl(url);
87     }
88
89     private void init() {
90         for (ScannerType type : scanners.keySet()) {
91             init(type);
92         }
93     }
94
95     private void init(ScannerType type) {
96         Scanner scanner = scanners.get(type);
97         try {
98             if (scanner instanceof Initiable) {
99                 Initiable initiableScanner = (Initiable) scanner;
100                 initiableScanner.init();
101             }
102             initScanners.add(type);
103         } catch (ChessHogScannerException e) {
104             throw new RuntimeException(e);
105         }
106     }
107
108
109     private Scanner getScanner(ScannerType type) {
110         if (!initScanners.contains(type)) {
111             init(type);
112         }
113         return scanners.get(type);
114     }
115
116     private ChessHogScanner() {
117         if (!ChessHogScannerProperties.is("scanner.lazy.init")) {
118             init();
119         }
120     }
121
122     private static ChessHogScanner getStore() {
123         if (singleton == null) {
124             singleton = new ChessHogScanner();
125         }
126         return singleton;
127     }
128
129 }