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