View Javadoc
1   /*
2    * Copyright 2012-2017 CodeLibs Project and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.util;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.function.Consumer;
21  
22  import org.apache.lucene.queryparser.classic.QueryParser;
23  import org.codelibs.core.crypto.CachedCipher;
24  import org.codelibs.core.misc.DynamicProperties;
25  import org.codelibs.fess.api.WebApiManagerFactory;
26  import org.codelibs.fess.auth.AuthenticationManager;
27  import org.codelibs.fess.crawler.client.CrawlerClientFactory;
28  import org.codelibs.fess.crawler.entity.EsAccessResult;
29  import org.codelibs.fess.crawler.extractor.ExtractorFactory;
30  import org.codelibs.fess.crawler.service.DataService;
31  import org.codelibs.fess.dict.DictionaryManager;
32  import org.codelibs.fess.ds.DataStoreFactory;
33  import org.codelibs.fess.es.client.FessEsClient;
34  import org.codelibs.fess.exception.ContainerNotAvailableException;
35  import org.codelibs.fess.helper.ActivityHelper;
36  import org.codelibs.fess.helper.CrawlingConfigHelper;
37  import org.codelibs.fess.helper.CrawlingInfoHelper;
38  import org.codelibs.fess.helper.DocumentHelper;
39  import org.codelibs.fess.helper.DuplicateHostHelper;
40  import org.codelibs.fess.helper.FileTypeHelper;
41  import org.codelibs.fess.helper.IndexingHelper;
42  import org.codelibs.fess.helper.IntervalControlHelper;
43  import org.codelibs.fess.helper.JobHelper;
44  import org.codelibs.fess.helper.KeyMatchHelper;
45  import org.codelibs.fess.helper.LabelTypeHelper;
46  import org.codelibs.fess.helper.PathMappingHelper;
47  import org.codelibs.fess.helper.PermissionHelper;
48  import org.codelibs.fess.helper.PopularWordHelper;
49  import org.codelibs.fess.helper.ProcessHelper;
50  import org.codelibs.fess.helper.QueryHelper;
51  import org.codelibs.fess.helper.RoleQueryHelper;
52  import org.codelibs.fess.helper.SambaHelper;
53  import org.codelibs.fess.helper.SearchLogHelper;
54  import org.codelibs.fess.helper.SuggestHelper;
55  import org.codelibs.fess.helper.SystemHelper;
56  import org.codelibs.fess.helper.UserAgentHelper;
57  import org.codelibs.fess.helper.UserInfoHelper;
58  import org.codelibs.fess.helper.ViewHelper;
59  import org.codelibs.fess.indexer.IndexUpdater;
60  import org.codelibs.fess.job.JobExecutor;
61  import org.codelibs.fess.ldap.LdapManager;
62  import org.codelibs.fess.mylasta.direction.FessConfig;
63  import org.codelibs.fess.mylasta.direction.FessProp;
64  import org.codelibs.fess.sso.SsoManager;
65  import org.codelibs.fess.thumbnail.ThumbnailManager;
66  import org.lastaflute.core.message.MessageManager;
67  import org.lastaflute.core.security.PrimaryCipher;
68  import org.lastaflute.di.core.SingletonLaContainer;
69  import org.lastaflute.di.core.factory.SingletonLaContainerFactory;
70  import org.lastaflute.di.core.smart.hot.HotdeployUtil;
71  import org.lastaflute.job.JobManager;
72  import org.lastaflute.web.servlet.request.RequestManager;
73  import org.lastaflute.web.servlet.request.ResponseManager;
74  import org.slf4j.Logger;
75  import org.slf4j.LoggerFactory;
76  
77  public final class ComponentUtil {
78  
79      private static final Logger logger = LoggerFactory.getLogger(ComponentUtil.class);
80  
81      private static final String AUTHENTICATION_MANAGER = "authenticationManager";
82  
83      private static final String THUMBNAIL_MANAGER = "thumbnailManager";
84  
85      private static final String SSO_MANAGER = "ssoManager";
86  
87      private static final String PERMISSION_HELPER = "permissionHelper";
88  
89      private static final String QUERY_PARSER = "queryParser";
90  
91      private static final String DOCUMENT_HELPER = "documentHelper";
92  
93      private static final String ACTIVITY_HELPER = "activityHelper";
94  
95      private static final String LDAP_MANAGER = "ldapManager";
96  
97      private static final String ROLE_QUERY_HELPER = "roleQueryHelper";
98  
99      private static final String SUGGEST_HELPER = "suggestHelper";
100 
101     private static final String FESS_ES_CLIENT = "fessEsClient";
102 
103     private static final String DICTIONARY_MANAGER = "dictionaryManager";
104 
105     private static final String DATA_SERVICE = "dataService";
106 
107     private static final String MESSAGE_MANAGER = "messageManager";
108 
109     private static final String USER_AGENT_NAME = "userAgentName";
110 
111     private static final String INDEX_UPDATER = "indexUpdater";
112 
113     private static final String FILE_TYPE_HELPER = "fileTypeHelper";
114 
115     private static final String EXTRACTOR_FACTORY = "extractorFactory";
116 
117     private static final String INTERVAL_CONTROL_HELPER = "intervalControlHelper";
118 
119     private static final String DATA_STORE_FACTORY = "dataStoreFactory";
120 
121     private static final String USER_AGENT_HELPER = "userAgentHelper";
122 
123     private static final String USER_INFO_HELPER = "userInfoHelper";
124 
125     private static final String WEB_API_MANAGER_FACTORY = "webApiManagerFactory";
126 
127     private static final String PROCESS_HELPER = "processHelper";
128 
129     private static final String JOB_HELPER = "jobHelper";
130 
131     private static final String DUPLICATE_HOST_HELPER = "duplicateHostHelper";
132 
133     private static final String PATH_MAPPING_HELPER = "pathMappingHelper";
134 
135     private static final String POPULAR_WORD_HELPER = "popularWordHelper";
136 
137     private static final String CRAWLING_INFO_HELPER = "crawlingInfoHelper";
138 
139     private static final String CRAWLING_CONFIG_HELPER = "crawlingConfigHelper";
140 
141     private static final String SEARCH_LOG_HELPER = "searchLogHelper";
142 
143     private static final String LABEL_TYPE_HELPER = "labelTypeHelper";
144 
145     private static final String QUERY_HELPER = "queryHelper";
146 
147     private static final String SAMBA_HELPER = "sambaHelper";
148 
149     private static final String VIEW_HELPER = "viewHelper";
150 
151     private static final String SYSTEM_HELPER = "systemHelper";
152 
153     private static final String CRAWLER_PROPERTIES = "systemProperties";
154 
155     private static final String QUERY_RESPONSE_LIST = "queryResponseList";
156 
157     private static final String JOB_EXECUTOR_SUFFIX = "JobExecutor";
158 
159     private static final String KEY_MATCH_HELPER = "keyMatchHelper";
160 
161     private static final String INDEXING_HELPER = "indexingHelper";
162 
163     private static IndexingHelper indexingHelper;
164 
165     private static CrawlingConfigHelper crawlingConfigHelper;
166 
167     private static SystemHelper systemHelper;
168 
169     private static FessConfig fessConfig;
170 
171     private static List<Runnable> initProcesses = new ArrayList<>();
172 
173     private ComponentUtil() {
174     }
175 
176     public static void processAfterContainerInit(final Runnable process) {
177         if (available()) {
178             process.run();
179         } else {
180             initProcesses.add(process);
181         }
182     }
183 
184     public static void doInitProcesses(final Consumer<? super Runnable> action) {
185         try {
186             initProcesses.forEach(action);
187         } finally {
188             initProcesses.clear();
189         }
190     }
191 
192     public static CachedCipher getCipher(final String cipherName) {
193         return getComponent(cipherName);
194     }
195 
196     public static QueryResponseList getQueryResponseList() {
197         return getComponent(QUERY_RESPONSE_LIST);
198     }
199 
200     public static DynamicProperties getSystemProperties() {
201         return getComponent(CRAWLER_PROPERTIES);
202     }
203 
204     public static SystemHelper getSystemHelper() {
205         if (systemHelper == null || HotdeployUtil.isHotdeploy()) {
206             systemHelper = getComponent(SYSTEM_HELPER);
207         }
208         return systemHelper;
209     }
210 
211     public static ViewHelper getViewHelper() {
212         return getComponent(VIEW_HELPER);
213     }
214 
215     public static SambaHelper getSambaHelper() {
216         return getComponent(SAMBA_HELPER);
217     }
218 
219     public static QueryHelper getQueryHelper() {
220         return getComponent(QUERY_HELPER);
221     }
222 
223     public static LabelTypeHelper getLabelTypeHelper() {
224         return getComponent(LABEL_TYPE_HELPER);
225     }
226 
227     public static SearchLogHelper getSearchLogHelper() {
228         return getComponent(SEARCH_LOG_HELPER);
229     }
230 
231     public static CrawlingConfigHelper getCrawlingConfigHelper() {
232         if (crawlingConfigHelper == null || HotdeployUtil.isHotdeploy()) {
233             crawlingConfigHelper = getComponent(CRAWLING_CONFIG_HELPER);
234         }
235         return crawlingConfigHelper;
236     }
237 
238     public static CrawlingInfoHelper getCrawlingInfoHelper() {
239         return getComponent(CRAWLING_INFO_HELPER);
240     }
241 
242     public static PopularWordHelper getPopularWordHelper() {
243         return getComponent(POPULAR_WORD_HELPER);
244     }
245 
246     public static PathMappingHelper getPathMappingHelper() {
247         return getComponent(PATH_MAPPING_HELPER);
248     }
249 
250     public static DuplicateHostHelper getDuplicateHostHelper() {
251         return getComponent(DUPLICATE_HOST_HELPER);
252     }
253 
254     public static ProcessHelper getProcessHelper() {
255         return getComponent(PROCESS_HELPER);
256     }
257 
258     public static JobHelper getJobHelper() {
259         return getComponent(JOB_HELPER);
260     }
261 
262     public static WebApiManagerFactory getWebApiManagerFactory() {
263         return getComponent(WEB_API_MANAGER_FACTORY);
264     }
265 
266     public static UserAgentHelper getUserAgentHelper() {
267         return getComponent(USER_AGENT_HELPER);
268     }
269 
270     public static DataStoreFactory getDataStoreFactory() {
271         return getComponent(DATA_STORE_FACTORY);
272     }
273 
274     public static IntervalControlHelper getIntervalControlHelper() {
275         return getComponent(INTERVAL_CONTROL_HELPER);
276     }
277 
278     public static ExtractorFactory getExtractorFactory() {
279         return getComponent(EXTRACTOR_FACTORY);
280     }
281 
282     public static JobExecutor getJobExecutor(final String name) {
283         return getComponent(name + JOB_EXECUTOR_SUFFIX);
284     }
285 
286     public static FileTypeHelper getFileTypeHelper() {
287         return getComponent(FILE_TYPE_HELPER);
288     }
289 
290     public static IndexUpdater getIndexUpdater() {
291         return getComponent(INDEX_UPDATER);
292     }
293 
294     public static String getUserAgentName() {
295         return getComponent(USER_AGENT_NAME);
296     }
297 
298     public static KeyMatchHelper getKeyMatchHelper() {
299         return getComponent(KEY_MATCH_HELPER);
300     }
301 
302     public static IndexingHelper getIndexingHelper() {
303         if (indexingHelper == null || HotdeployUtil.isHotdeploy()) {
304             indexingHelper = getComponent(INDEXING_HELPER);
305         }
306         return indexingHelper;
307     }
308 
309     public static UserInfoHelper getUserInfoHelper() {
310         return getComponent(USER_INFO_HELPER);
311     }
312 
313     public static MessageManager getMessageManager() {
314         return getComponent(MESSAGE_MANAGER);
315     }
316 
317     public static DictionaryManager getDictionaryManager() {
318         return getComponent(DICTIONARY_MANAGER);
319     }
320 
321     public static DataService<EsAccessResult> getDataService() {
322         return getComponent(DATA_SERVICE);
323     }
324 
325     public static FessEsClient getFessEsClient() {
326         return getComponent(FESS_ES_CLIENT);
327     }
328 
329     public static FessConfig getFessConfig() {
330         if (fessConfig != null) {
331             return fessConfig;
332         }
333         return getComponent(FessConfig.class);
334     }
335 
336     public static SuggestHelper getSuggestHelper() {
337         return getComponent(SUGGEST_HELPER);
338     }
339 
340     public static RoleQueryHelper getRoleQueryHelper() {
341         return getComponent(ROLE_QUERY_HELPER);
342     }
343 
344     public static LdapManager getLdapManager() {
345         return getComponent(LDAP_MANAGER);
346     }
347 
348     public static ActivityHelper getActivityHelper() {
349         return getComponent(ACTIVITY_HELPER);
350     }
351 
352     public static RequestManager getRequestManager() {
353         return getComponent(RequestManager.class);
354     }
355 
356     public static ResponseManager getResponseManager() {
357         return getComponent(ResponseManager.class);
358     }
359 
360     public static JobManager getJobManager() {
361         return getComponent(JobManager.class);
362     }
363 
364     public static DocumentHelper getDocumentHelper() {
365         return getComponent(DOCUMENT_HELPER);
366     }
367 
368     public static QueryParser getQueryParser() {
369         return getComponent(QUERY_PARSER);
370     }
371 
372     public static PermissionHelper getPermissionHelper() {
373         return getComponent(PERMISSION_HELPER);
374     }
375 
376     public static SsoManager getSsoManager() {
377         return getComponent(SSO_MANAGER);
378     }
379 
380     public static ThumbnailManager getThumbnailManager() {
381         return getComponent(THUMBNAIL_MANAGER);
382     }
383 
384     public static AuthenticationManager getAuthenticationManager() {
385         return getComponent(AUTHENTICATION_MANAGER);
386     }
387 
388     public static PrimaryCipher getPrimaryCipher() {
389         return getComponent(PrimaryCipher.class);
390     }
391 
392     public static CrawlerClientFactory getCrawlerClientFactory() {
393         return getComponent(CrawlerClientFactory.class);
394     }
395 
396     public static <T> T getComponent(final Class<T> clazz) {
397         try {
398             return SingletonLaContainer.getComponent(clazz);
399         } catch (final NullPointerException e) {
400             if (logger.isDebugEnabled()) {
401                 throw new ContainerNotAvailableException(clazz.getCanonicalName(), e);
402             } else {
403                 throw new ContainerNotAvailableException(clazz.getCanonicalName());
404             }
405         }
406     }
407 
408     public static <T> T getComponent(final String componentName) {
409         try {
410             return SingletonLaContainer.getComponent(componentName);
411         } catch (final NullPointerException e) {
412             if (logger.isDebugEnabled()) {
413                 throw new ContainerNotAvailableException(componentName, e);
414             } else {
415                 throw new ContainerNotAvailableException(componentName);
416             }
417         }
418     }
419 
420     public static boolean hasQueryHelper() {
421         return SingletonLaContainerFactory.getContainer().hasComponentDef(QUERY_HELPER);
422     }
423 
424     public static boolean available() {
425         try {
426             return SingletonLaContainer.getComponent(SYSTEM_HELPER) != null;
427         } catch (final Exception e) {
428             // ignore
429         }
430         return false;
431     }
432 
433     /**
434      * For test purpose only.
435      *
436      * @param fessConfig fessConfig instance
437      */
438     public static void setFessConfig(final FessConfig fessConfig) {
439         ComponentUtil.fessConfig = fessConfig;
440         if (fessConfig == null) {
441             FessProp.propMap.clear();
442         }
443     }
444 
445 }