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