PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/searchcode/app/service/Singleton.java

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