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