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.ds;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.ArrayList;
21  import java.util.Date;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.stream.Collectors;
26  
27  import org.codelibs.core.lang.StringUtil;
28  import org.codelibs.fess.Constants;
29  import org.codelibs.fess.ds.callback.IndexUpdateCallback;
30  import org.codelibs.fess.es.config.exentity.DataConfig;
31  import org.codelibs.fess.helper.CrawlingInfoHelper;
32  import org.codelibs.fess.helper.SystemHelper;
33  import org.codelibs.fess.mylasta.direction.FessConfig;
34  import org.codelibs.fess.util.ComponentUtil;
35  import org.codelibs.fess.util.GroovyUtil;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  public abstract class AbstractDataStore implements DataStore {
40  
41      private static final Logger logger = LoggerFactory.getLogger(AbstractDataStore.class);
42  
43      public String mimeType = "application/datastore";
44  
45      protected boolean alive = true;
46  
47      public void register() {
48          ComponentUtil.getDataStoreFactory().add(getName(), this);
49      }
50  
51      protected abstract String getName();
52  
53      @Override
54      public void stop() {
55          alive = false;
56      }
57  
58      @Override
59      public void store(final DataConfig config, final IndexUpdateCallback callback, final Map<String, String> initParamMap) {
60          final Map<String, String> configParamMap = config.getHandlerParameterMap();
61          final Map<String, String> configScriptMap = config.getHandlerScriptMap();
62          final CrawlingInfoHelper crawlingInfoHelper = ComponentUtil.getCrawlingInfoHelper();
63          final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
64          final Date documentExpires = crawlingInfoHelper.getDocumentExpires(config);
65          final FessConfig fessConfig = ComponentUtil.getFessConfig();
66  
67          initParamMap.putAll(configParamMap);
68          final Map<String, String> paramMap = initParamMap;
69  
70          // default values
71          final Map<String, Object> defaultDataMap = new HashMap<>();
72  
73          // cid
74          final String configId = config.getConfigId();
75          if (configId != null) {
76              defaultDataMap.put(fessConfig.getIndexFieldConfigId(), configId);
77          }
78          //  expires
79          if (documentExpires != null) {
80              defaultDataMap.put(fessConfig.getIndexFieldExpires(), documentExpires);
81          }
82          // segment
83          defaultDataMap.put(fessConfig.getIndexFieldSegment(), initParamMap.get(Constants.SESSION_ID));
84          // created
85          defaultDataMap.put(fessConfig.getIndexFieldCreated(), systemHelper.getCurrentTime());
86          // boost
87          defaultDataMap.put(fessConfig.getIndexFieldBoost(), config.getBoost().toString());
88          // label: labelType
89          // role: roleType
90          final List<String> roleTypeList = new ArrayList<>();
91          stream(config.getPermissions()).of(stream -> stream.forEach(p -> roleTypeList.add(p)));
92          defaultDataMap.put(fessConfig.getIndexFieldRole(), roleTypeList);
93          // mimetype
94          defaultDataMap.put(fessConfig.getIndexFieldMimetype(), mimeType);
95          // title
96          // content
97          // cache
98          // digest
99          // host
100         // site
101         // url
102         // anchor
103         // content_length
104         // last_modified
105         // id
106         // virtual_host
107         defaultDataMap.put(fessConfig.getIndexFieldVirtualHost(),
108                 stream(config.getVirtualHosts()).get(stream -> stream.filter(StringUtil::isNotBlank).collect(Collectors.toList())));
109 
110         storeData(config, callback, paramMap, configScriptMap, defaultDataMap);
111 
112     }
113 
114     protected Object convertValue(final String template, final Map<String, Object> paramMap) {
115         if (StringUtil.isEmpty(template)) {
116             return StringUtil.EMPTY;
117         }
118 
119         if (paramMap.containsKey(template)) {
120             return paramMap.get(template);
121         }
122 
123         return GroovyUtil.evaluate(template, paramMap);
124     }
125 
126     protected long getReadInterval(final Map<String, String> paramMap) {
127         long readInterval = 0;
128         final String value = paramMap.get("readInterval");
129         if (StringUtil.isNotBlank(value)) {
130             try {
131                 readInterval = Long.parseLong(value);
132             } catch (final NumberFormatException e) {
133                 logger.warn("Invalid read interval: " + value);
134             }
135         }
136         return readInterval;
137     }
138 
139     protected void sleep(final long interval) {
140         try {
141             Thread.sleep(interval);
142         } catch (final Exception e) {
143             // ignore
144         }
145     }
146 
147     protected abstract void storeData(DataConfig dataConfig, IndexUpdateCallback callback, Map<String, String> paramMap,
148             Map<String, String> scriptMap, Map<String, Object> defaultDataMap);
149 }