View Javadoc
1   /*
2    * Copyright 2012-2021 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 org.apache.logging.log4j.LogManager;
19  import org.apache.logging.log4j.Logger;
20  import org.codelibs.fess.app.service.FailureUrlService;
21  import org.codelibs.fess.crawler.CrawlerContext;
22  import org.codelibs.fess.crawler.entity.UrlQueue;
23  import org.codelibs.fess.crawler.exception.MultipleCrawlingAccessException;
24  import org.codelibs.fess.crawler.helper.impl.LogHelperImpl;
25  import org.codelibs.fess.crawler.log.LogType;
26  import org.codelibs.fess.es.config.exentity.CrawlingConfig;
27  import org.codelibs.fess.exception.ContainerNotAvailableException;
28  import org.codelibs.fess.util.ComponentUtil;
29  
30  public class CrawlerLogHelper extends LogHelperImpl {
31      private static final Logger logger = LogManager.getLogger(CrawlerLogHelper.class);
32  
33      @Override
34      public void log(final LogType key, final Object... objs) {
35          if (!ComponentUtil.available()) {
36              if (logger.isDebugEnabled()) {
37                  logger.debug("container was destroyed.");
38              }
39              return;
40          }
41          try {
42              switch (key) {
43              case CRAWLING_ACCESS_EXCEPTION: {
44                  final CrawlerContext crawlerContext = (CrawlerContext) objs[0];
45                  final UrlQueue<?> urlQueue = (UrlQueue<?>) objs[1];
46                  Throwable e = (Throwable) objs[2];
47                  if (e instanceof MultipleCrawlingAccessException) {
48                      final Throwable[] causes = ((MultipleCrawlingAccessException) e).getCauses();
49                      if (causes.length > 0) {
50                          e = causes[causes.length - 1];
51                      }
52                  }
53  
54                  String errorName;
55                  final Throwable cause = e.getCause();
56                  if (cause != null) {
57                      errorName = cause.getClass().getCanonicalName();
58                  } else {
59                      errorName = e.getClass().getCanonicalName();
60                  }
61                  storeFailureUrl(crawlerContext, urlQueue, errorName, e);
62                  break;
63              }
64              case CRAWLING_EXCETPION: {
65                  final CrawlerContext crawlerContext = (CrawlerContext) objs[0];
66                  final UrlQueue<?> urlQueue = (UrlQueue<?>) objs[1];
67                  final Throwable e = (Throwable) objs[2];
68  
69                  storeFailureUrl(crawlerContext, urlQueue, e.getClass().getCanonicalName(), e);
70                  break;
71              }
72              default:
73                  break;
74              }
75          } catch (final ContainerNotAvailableException e) {
76              if (logger.isDebugEnabled()) {
77                  logger.debug("container was destroyed.");
78              }
79              return;
80          } catch (final Exception e) {
81              if (!ComponentUtil.available()) {
82                  if (logger.isDebugEnabled()) {
83                      logger.debug("container was destroyed.");
84                  }
85                  return;
86              }
87              logger.warn("Failed to store a failure url.", e);
88          }
89  
90          super.log(key, objs);
91      }
92  
93      private void storeFailureUrl(final CrawlerContext crawlerContext, final UrlQueue<?> urlQueue, final String errorName,
94              final Throwable e) {
95  
96          final CrawlingConfig crawlingConfig = getCrawlingConfig(crawlerContext.getSessionId());
97          final String url = urlQueue.getUrl();
98  
99          final FailureUrlService failureUrlService = ComponentUtil.getComponent(FailureUrlService.class);
100         failureUrlService.store(crawlingConfig, errorName, url, e);
101     }
102 
103     private CrawlingConfig getCrawlingConfig(final String sessionCountId) {
104         return ComponentUtil.getCrawlingConfigHelper().get(sessionCountId);
105     }
106 
107 }