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