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.helper;
17  
18  import java.util.Map;
19  import java.util.concurrent.ConcurrentHashMap;
20  import java.util.concurrent.ExecutionException;
21  import java.util.concurrent.TimeUnit;
22  
23  import javax.annotation.PostConstruct;
24  
25  import org.codelibs.fess.app.service.DataConfigService;
26  import org.codelibs.fess.app.service.FileConfigService;
27  import org.codelibs.fess.app.service.WebConfigService;
28  import org.codelibs.fess.es.config.exentity.CrawlingConfig;
29  import org.codelibs.fess.es.config.exentity.CrawlingConfig.ConfigType;
30  import org.codelibs.fess.util.ComponentUtil;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import com.google.common.cache.Cache;
35  import com.google.common.cache.CacheBuilder;
36  
37  public class CrawlingConfigHelper {
38  
39      private static final Logger logger = LoggerFactory.getLogger(CrawlingConfigHelper.class);
40  
41      protected final Map<String, CrawlingConfig> crawlingConfigMap = new ConcurrentHashMap<>();
42  
43      protected int count = 1;
44  
45      protected Cache<String, CrawlingConfig> crawlingConfigCache;
46  
47      @PostConstruct
48      public void init() {
49          crawlingConfigCache = CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(10, TimeUnit.MINUTES).build();
50      }
51  
52      public ConfigType getConfigType(final String configId) {
53          if (configId == null || configId.length() < 2) {
54              return null;
55          }
56          final String configType = configId.substring(0, 1);
57          if (ConfigType.WEB.getTypePrefix().equals(configType)) {
58              return ConfigType.WEB;
59          } else if (ConfigType.FILE.getTypePrefix().equals(configType)) {
60              return ConfigType.FILE;
61          } else if (ConfigType.DATA.getTypePrefix().equals(configType)) {
62              return ConfigType.DATA;
63          }
64          return null;
65      }
66  
67      protected String getId(final String configId) {
68          if (configId == null || configId.length() < 2) {
69              return null;
70          }
71          return configId.substring(1);
72      }
73  
74      public CrawlingConfig getCrawlingConfig(final String configId) {
75          try {
76              return crawlingConfigCache.get(configId, () -> {
77                  final ConfigType configType = getConfigType(configId);
78                  if (configType == null) {
79                      return null;
80                  }
81                  final String id = getId(configId);
82                  if (id == null) {
83                      return null;
84                  }
85                  switch (configType) {
86                  case WEB:
87                      final WebConfigService webConfigService = ComponentUtil.getComponent(WebConfigService.class);
88                      return webConfigService.getWebConfig(id).get();
89                  case FILE:
90                      final FileConfigService fileConfigService = ComponentUtil.getComponent(FileConfigService.class);
91                      return fileConfigService.getFileConfig(id).get();
92                  case DATA:
93                      final DataConfigService dataConfigService = ComponentUtil.getComponent(DataConfigService.class);
94                      return dataConfigService.getDataConfig(id).get();
95                  default:
96                      return null;
97                  }
98              });
99          } catch (final ExecutionException e) {
100             logger.warn("Failed to access a crawling config cache: " + configId, e);
101             return null;
102         }
103     }
104 
105     public void refresh() {
106         crawlingConfigCache.invalidateAll();
107     }
108 
109     public synchronized String store(final String sessionId, final CrawlingConfig crawlingConfig) {
110         final String sessionCountId = sessionId + "-" + count;
111         crawlingConfigMap.put(sessionCountId, crawlingConfig);
112         count++;
113         return sessionCountId;
114     }
115 
116     public void remove(final String sessionId) {
117         crawlingConfigMap.remove(sessionId);
118     }
119 
120     public CrawlingConfig get(final String sessionId) {
121         return crawlingConfigMap.get(sessionId);
122     }
123 
124 }