View Javadoc
1   /*
2    * Copyright 2001-2005 The Apache Software Foundation.
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.logging.log4j.catalog.api.dao;
17  
18  import java.io.IOException;
19  import java.nio.charset.StandardCharsets;
20  import java.nio.file.Files;
21  import java.nio.file.Path;
22  import java.nio.file.Paths;
23  import java.time.Instant;
24  import java.time.LocalDateTime;
25  import java.time.ZoneId;
26  import java.util.Map;
27  
28  import com.fasterxml.jackson.core.JsonFactory;
29  import com.fasterxml.jackson.core.JsonParser;
30  import com.fasterxml.jackson.databind.ObjectMapper;
31  import org.apache.logging.log4j.LogManager;
32  import org.apache.logging.log4j.Logger;
33  import org.apache.logging.log4j.catalog.api.CatalogData;
34  
35  /**
36   * Reads the catalog from the local file system.
37   */
38  public class FileCatalogReader extends AbstractCatalogReader {
39  
40      private static final Logger LOGGER = LogManager.getLogger(FileCatalogReader.class);
41      private static final String BASEDIR = "baseDir";
42  
43      private static final String CATALOG_ATTRIBUTE_NAME = "catalogFile";
44      private static final String DEFAULT_CATALOG_FILE = "src/main/resources/catalog.json";
45  
46      private final String catalog;
47      private LocalDateTime lastUpdated;
48  
49      public FileCatalogReader(Map<String, String> attributes) throws IOException {
50          StringBuilder catalogPath = new StringBuilder();
51          String basePath = attributes.get(BASEDIR);
52          String catalogFile = attributes.getOrDefault(CATALOG_ATTRIBUTE_NAME, DEFAULT_CATALOG_FILE);
53          if (basePath != null) {
54              catalogPath.append(attributes.get(BASEDIR));
55              if (basePath.endsWith("/")) {
56                  if (catalogFile.startsWith("/")) {
57                      catalogPath.append(catalogFile.substring(1));
58                  } else {
59                      catalogPath.append(catalogFile);
60                  }
61              } else {
62                  if (catalogFile.startsWith("/")) {
63                      catalogPath.append(catalogFile);
64                  } else {
65                      catalogPath.append("/").append(catalogFile);
66                  }
67              }
68          } else if (catalogFile != null){
69              catalogPath.append(catalogFile);
70          } else {
71              LOGGER.warn("No catalogFile attribute was provided. Using {}", DEFAULT_CATALOG_FILE);
72              catalogPath.append(DEFAULT_CATALOG_FILE);
73          }
74          Path path = Paths.get(catalogPath.toString());
75          lastUpdated = LocalDateTime.ofInstant(Instant.ofEpochMilli(path.toFile().lastModified()),
76                  ZoneId.systemDefault());
77          byte[] encoded = Files.readAllBytes(path);
78          catalog = new String(encoded, StandardCharsets.UTF_8);
79          JsonFactory factory = new JsonFactory();
80          factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
81          ObjectMapper objectMapper = new ObjectMapper(factory);
82          catalogData = objectMapper.readValue(catalog, CatalogData.class);
83      }
84  
85      public FileCatalogReader() throws IOException {
86          byte[] encoded = Files.readAllBytes(Paths.get(DEFAULT_CATALOG_FILE));
87          catalog = new String(encoded, StandardCharsets.UTF_8);
88          JsonFactory factory = new JsonFactory();
89          factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
90          ObjectMapper objectMapper = new ObjectMapper(factory);
91          catalogData = objectMapper.readValue(catalog, CatalogData.class);
92      }
93  
94      @Override
95      public String readCatalog() {
96          return catalog;
97      }
98  
99      @Override
100     public LocalDateTime getLastUpdated() {
101         return null;
102     }
103 }