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