/src/main/java/com/searchcode/app/service/Singleton.java
Java | 413 lines | 305 code | 84 blank | 24 comment | 64 complexity | 4b59a621d3297dc80a4d64e54f56d181 MD5 | raw file
1/*
2 * Copyright (c) 2016 Boyter Online Services
3 *
4 * Use of this software is governed by the Fair Source License included
5 * in the LICENSE.TXT file, but will be eventually open under GNU General Public License Version 3
6 * see the README.md for when this clause will take effect
7 *
8 * Version 1.3.15
9 */
10
11package com.searchcode.app.service;
12
13import com.searchcode.app.config.IDatabaseConfig;
14import com.searchcode.app.config.MySQLDatabaseConfig;
15import com.searchcode.app.config.SQLiteDatabaseConfig;
16import com.searchcode.app.config.Values;
17import com.searchcode.app.dao.*;
18import com.searchcode.app.dto.CodeIndexDocument;
19import com.searchcode.app.dto.RunningIndexJob;
20import com.searchcode.app.service.index.IIndexService;
21import com.searchcode.app.service.index.IndexService;
22import com.searchcode.app.service.index.SphinxIndexService;
23import com.searchcode.app.service.route.TimeSearchRouteService;
24import com.searchcode.app.util.*;
25import org.quartz.Scheduler;
26import org.quartz.SchedulerException;
27import org.quartz.SchedulerFactory;
28import org.quartz.impl.StdSchedulerFactory;
29
30import java.util.AbstractMap;
31import java.util.Queue;
32import java.util.concurrent.ConcurrentHashMap;
33import java.util.concurrent.ConcurrentLinkedQueue;
34
35/**
36 * Lazy Singleton Implementation
37 * Generally used to share anything that the Quartz jobs require and to create all the shared objects
38 * No idea if it really needs to be lazy other than it saves us creating everything on start although it all
39 * gets called pretty quickly anyway so thats probably a moot point
40 */
41public final class Singleton {
42
43 private static AbstractMap<String, RunningIndexJob> runningIndexRepoJobs = null; // Used to know which jobs are currently running
44 private static ISpellingCorrector spellingCorrectorInstance = null;
45 private static Queue<CodeIndexDocument> codeIndexQueue = null; // Documents ready to be indexed
46
47 private static SearchCodeLib searchcodeLib = null;
48 private static FileClassifier fileClassifier = null;
49 private static LoggerWrapper loggerWrapper = null;
50 private static Scheduler scheduler = null;
51 private static IRepo repo = null;
52 private static Data data = null;
53 private static Api api = null;
54 private static Source source = null;
55 private static SourceCode sourceCode = null;
56 private static LanguageType languageType = null;
57 private static ApiService apiService = null;
58 private static DataService dataService = null;
59 private static TimeSearchRouteService timeSearchRouteService = null;
60 private static StatsService statsService = null;
61 private static JobService jobService = null;
62 private static IDatabaseConfig databaseConfig = null;
63 private static Helpers helpers = null;
64 private static ValidatorService validatorService = null;
65
66 private static IIndexService indexService = null;
67 private static CodeMatcher codematcher = null;
68 private static SlocCounter slocCounter = null;
69
70 private static OWASPClassifier owaspClassifier = null;
71 private static RepositorySource repositorySource = null;
72 private static Highlight highlight = null;
73
74 private static UniqueRepoQueue uniqueGitRepoQueue = null; // Used to queue the next repository to be indexed
75 private static UniqueRepoQueue uniqueFileRepoQueue = null; // Used to queue the next repository to be indexed
76 private static UniqueRepoQueue uniqueSvnRepoQueue = null; // Used to queue the next repository to be indexed
77
78 private static boolean enqueueRepositoryJobFirstRun = true;
79 private static boolean enqueueFileRepositoryJobFirstRun = true;
80
81 private static MySQLDatabaseConfig mysqlDatabaseConfig = null;
82
83 public static synchronized void setEnqueueRepositoryJobFirstRun(boolean value) {
84 enqueueRepositoryJobFirstRun = value;
85 }
86
87 public static synchronized void setEnqueueFileRepositoryJob(boolean value) {
88 enqueueFileRepositoryJobFirstRun = value;
89 }
90
91 public static synchronized boolean getEnqueueRepositoryJobFirstRun() {
92 return enqueueRepositoryJobFirstRun;
93 }
94
95 public static synchronized boolean getEnqueueFileRepositoryJobFirstRun() {
96 return enqueueFileRepositoryJobFirstRun;
97 }
98
99 public static Timer getNewTimer() {
100 return new Timer();
101 }
102
103 public static synchronized CodeMatcher getCodeMatcher() {
104 if (codematcher == null) {
105 codematcher = new CodeMatcher();
106 }
107 return codematcher;
108 }
109
110 public static synchronized UniqueRepoQueue getUniqueGitRepoQueue() {
111 if (uniqueGitRepoQueue == null) {
112 uniqueGitRepoQueue = new UniqueRepoQueue(new ConcurrentLinkedQueue<>());
113 }
114 return uniqueGitRepoQueue;
115 }
116
117 public static synchronized UniqueRepoQueue getUniqueFileRepoQueue() {
118 if (uniqueFileRepoQueue == null) {
119 uniqueFileRepoQueue = new UniqueRepoQueue(new ConcurrentLinkedQueue<>());
120 }
121 return uniqueFileRepoQueue;
122 }
123
124 public static synchronized UniqueRepoQueue getUniqueSvnRepoQueue() {
125 if (uniqueSvnRepoQueue == null) {
126 uniqueSvnRepoQueue = new UniqueRepoQueue(new ConcurrentLinkedQueue<>());
127 }
128
129 return uniqueSvnRepoQueue;
130 }
131
132 public static synchronized IIndexService getIndexService() {
133 if (indexService == null) {
134 String index = Properties.getProperties().getProperty(Values.INDEX_SERVICE, Values.DEFAULT_INDEX_SERVICE);
135
136 switch (index) {
137 case "sphinx":
138 indexService = new SphinxIndexService();
139 break;
140 case "internal":
141 default:
142 indexService = new IndexService();
143 break;
144 }
145 }
146
147 return indexService;
148 }
149
150 public static synchronized SlocCounter getSlocCounter() {
151 if (slocCounter == null) {
152 slocCounter = new SlocCounter();
153 }
154
155 return slocCounter;
156 }
157
158 /**
159 * Used as cheap attempt to not have all the jobs trying to process the same thing note this has a race condition
160 * and should be resolved at some point
161 * TODO investigate usage and resolve race conditions
162 */
163 public static synchronized AbstractMap<String, RunningIndexJob> getRunningIndexRepoJobs() {
164 if (runningIndexRepoJobs == null) {
165 runningIndexRepoJobs = new ConcurrentHashMap<>();
166 }
167
168 return runningIndexRepoJobs;
169 }
170
171 public static synchronized IRepo getRepo() {
172 if (repo == null) {
173 String index = Properties.getProperties().getProperty(Values.INDEX_SERVICE, Values.DEFAULT_INDEX_SERVICE);
174
175 switch (index) {
176 case "sphinx":
177 repo = new MySQLRepo();
178 break;
179 case "internal":
180 default:
181 repo = new SQLiteRepo();
182 break;
183 }
184 }
185
186 return repo;
187 }
188
189 public static synchronized ISpellingCorrector getSpellingCorrector() {
190 if (spellingCorrectorInstance == null) {
191 spellingCorrectorInstance = new SearchcodeSpellingCorrector();
192 }
193
194 return spellingCorrectorInstance;
195 }
196
197 public static synchronized RepositorySource getRepositorySource() {
198 if (repositorySource == null) {
199 repositorySource = new RepositorySource();
200 }
201
202 return repositorySource;
203 }
204
205 public static synchronized Highlight getHighlight() {
206 if (highlight == null) {
207 highlight = new Highlight();
208 }
209
210 return highlight;
211 }
212
213 public static synchronized OWASPClassifier getOwaspClassifier() {
214 if (owaspClassifier == null) {
215 owaspClassifier = new OWASPClassifier();
216 }
217
218 return owaspClassifier;
219 }
220
221 public static synchronized Queue<CodeIndexDocument> getCodeIndexQueue() {
222 if (codeIndexQueue == null) {
223 codeIndexQueue = new ConcurrentLinkedQueue<>();
224 }
225
226 return codeIndexQueue;
227 }
228
229 public static synchronized Scheduler getScheduler() {
230 try {
231 if (scheduler == null || scheduler.isShutdown()) {
232 try {
233 SchedulerFactory sf = new StdSchedulerFactory();
234 scheduler = sf.getScheduler();
235 } catch (SchedulerException ex) {
236 Singleton.getLogger().severe(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
237 }
238 }
239 } catch (SchedulerException e) {
240 }
241
242 return scheduler;
243 }
244
245 public static synchronized LoggerWrapper getLogger() {
246 if (loggerWrapper == null) {
247 loggerWrapper = new LoggerWrapper();
248 }
249
250 return loggerWrapper;
251 }
252
253 public static synchronized SearchCodeLib getSearchCodeLib() {
254 if (searchcodeLib == null) {
255 searchcodeLib = new SearchCodeLib();
256 }
257
258 return searchcodeLib;
259 }
260
261 public static synchronized TimeSearchRouteService getTimeSearchRouteService() {
262 if (timeSearchRouteService == null) {
263 timeSearchRouteService = new TimeSearchRouteService();
264 }
265
266 return timeSearchRouteService;
267 }
268
269 /**
270 * Overwrites the internal searchcode lib with the new one which will refresh the data it needs. Mainly used to
271 * change the minified settings.
272 */
273 public static synchronized SearchCodeLib getNewSearchcodeLib() {
274 searchcodeLib = new SearchCodeLib();
275
276 return searchcodeLib;
277 }
278
279
280 public static synchronized StatsService getStatsService() {
281 if (statsService == null) {
282 statsService = new StatsService();
283 }
284
285 return statsService;
286 }
287
288 public static synchronized ValidatorService getValidatorService() {
289 if (validatorService == null) {
290 validatorService = new ValidatorService();
291 }
292
293 return validatorService;
294 }
295
296 public static synchronized void setStatsService(StatsService statsService) {
297 Singleton.statsService = statsService;
298 }
299
300 public static synchronized Data getData() {
301 if (data == null) {
302 data = new Data();
303 }
304
305 return data;
306 }
307
308 public static synchronized void setData(Data data) {
309 Singleton.data = data;
310 }
311
312 public static synchronized Api getApi() {
313 if (api == null) {
314 api = new Api();
315 }
316
317 return api;
318 }
319
320 public static synchronized void setApi(Api api) {
321 Singleton.api = api;
322 }
323
324 public static synchronized LanguageType getLanguageType() {
325 if (languageType == null) {
326 languageType = new LanguageType();
327 }
328
329 return languageType;
330 }
331
332 public static synchronized Source getSource() {
333 if (source == null) {
334 source = new Source();
335 }
336
337 return source;
338 }
339
340 public static synchronized SourceCode getSourceCode() {
341 if (sourceCode == null) {
342 sourceCode = new SourceCode();
343 }
344
345 return sourceCode;
346 }
347
348 public static synchronized ApiService getApiService() {
349 if (apiService == null) {
350 apiService = new ApiService();
351 }
352
353 return apiService;
354 }
355
356 public static synchronized DataService getDataService() {
357 if (dataService == null) {
358 dataService = new DataService();
359 }
360
361 return dataService;
362 }
363
364 public static synchronized void setJobService(JobService jobService) {
365 Singleton.jobService = jobService;
366 }
367
368 public static synchronized JobService getJobService() {
369 if (jobService == null) {
370 jobService = new JobService();
371 }
372
373 return jobService;
374 }
375
376 public static synchronized FileClassifier getFileClassifier() {
377 if (fileClassifier == null) {
378 fileClassifier = new FileClassifier();
379 }
380
381 return fileClassifier;
382 }
383
384 public static synchronized IDatabaseConfig getDatabaseConfig() {
385 if (databaseConfig == null) {
386 String index = Properties.getProperties().getProperty(Values.INDEX_SERVICE, Values.DEFAULT_INDEX_SERVICE);
387
388 switch (index) {
389 case "sphinx":
390 databaseConfig = new MySQLDatabaseConfig();
391 break;
392 case "internal":
393 default:
394 databaseConfig = new SQLiteDatabaseConfig();
395 break;
396 }
397 }
398
399 return databaseConfig;
400 }
401
402 public static synchronized Helpers getHelpers() {
403 if (helpers == null) {
404 helpers = new Helpers();
405 }
406
407 return helpers;
408 }
409
410 public static synchronized void setDatabaseConfig(IDatabaseConfig databaseConfig) {
411 Singleton.databaseConfig = databaseConfig;
412 }
413}