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.ds.impl;
17  
18  import java.io.File;
19  import java.util.Map;
20  
21  import org.codelibs.fess.Constants;
22  import org.codelibs.fess.crawler.client.CrawlerClientFactory;
23  import org.codelibs.fess.ds.IndexUpdateCallback;
24  import org.codelibs.fess.es.config.exentity.DataConfig;
25  import org.codelibs.fess.exception.DataStoreException;
26  import org.codelibs.fess.util.ComponentUtil;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import com.orangesignal.csv.CsvConfig;
31  
32  public class CsvListDataStoreImpl extends CsvDataStoreImpl {
33  
34      private static final Logger logger = LoggerFactory.getLogger(CsvListDataStoreImpl.class);
35  
36      public boolean deleteProcessedFile = true;
37  
38      public long csvFileTimestampMargin = 60 * 1000L;// 1min
39  
40      public boolean ignoreDataStoreException = true;
41  
42      @Override
43      protected boolean isCsvFile(final File parentFile, final String filename) {
44          if (super.isCsvFile(parentFile, filename)) {
45              final File file = new File(parentFile, filename);
46              final long now = System.currentTimeMillis();
47              return now - file.lastModified() > csvFileTimestampMargin;
48          }
49          return false;
50      }
51  
52      @Override
53      protected void storeData(final DataConfig dataConfig, final IndexUpdateCallback callback, final Map<String, String> paramMap,
54              final Map<String, String> scriptMap, final Map<String, Object> defaultDataMap) {
55          int nThreads = 1;
56          if (paramMap.containsKey(Constants.NUM_OF_THREADS)) {
57              try {
58                  nThreads = Integer.parseInt(paramMap.get(Constants.NUM_OF_THREADS));
59              } catch (final NumberFormatException e) {
60                  logger.warn(Constants.NUM_OF_THREADS + " is not int value.", e);
61              }
62          }
63          final CrawlerClientFactory crawlerClientFactory = ComponentUtil.getCrawlerClientFactory();
64          dataConfig.initializeClientFactory(crawlerClientFactory);
65          try {
66              final FileListIndexUpdateCallbackImpl fileListIndexUpdateCallback =
67                      new FileListIndexUpdateCallbackImpl(callback, crawlerClientFactory, nThreads);
68              super.storeData(dataConfig, fileListIndexUpdateCallback, paramMap, scriptMap, defaultDataMap);
69              fileListIndexUpdateCallback.commit();
70          } catch (final Exception e) {
71              throw new DataStoreException(e);
72          }
73      }
74  
75      @Override
76      protected void processCsv(final DataConfig dataConfig, final IndexUpdateCallback callback, final Map<String, String> paramMap,
77              final Map<String, String> scriptMap, final Map<String, Object> defaultDataMap, final CsvConfig csvConfig, final File csvFile,
78              final long readInterval, final String csvFileEncoding, final boolean hasHeaderLine) {
79          try {
80              super.processCsv(dataConfig, callback, paramMap, scriptMap, defaultDataMap, csvConfig, csvFile, readInterval, csvFileEncoding,
81                      hasHeaderLine);
82  
83              // delete csv file
84              if (deleteProcessedFile && !csvFile.delete()) {
85                  logger.warn("Failed to delete {}", csvFile.getAbsolutePath());
86              }
87          } catch (final DataStoreException e) {
88              if (ignoreDataStoreException) {
89                  logger.error("Failed to process " + csvFile.getAbsolutePath(), e);
90                  // rename csv file, or delete it if failed
91                  if (!csvFile.renameTo(new File(csvFile.getParent(), csvFile.getName() + ".txt")) && !csvFile.delete()) {
92                      logger.warn("Failed to delete {}", csvFile.getAbsolutePath());
93                  }
94              } else {
95                  throw e;
96              }
97          }
98      }
99  
100 }