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