View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.audit.service.catalog;
18  
19  import javax.annotation.PostConstruct;
20  import java.sql.Timestamp;
21  import java.time.Instant;
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.concurrent.ConcurrentHashMap;
27  
28  import org.apache.logging.log4j.LogManager;
29  import org.apache.logging.log4j.Logger;
30  import org.apache.logging.log4j.audit.catalog.CatalogManagerImpl;
31  import org.apache.logging.log4j.catalog.api.Attribute;
32  import org.apache.logging.log4j.catalog.api.CatalogReader;
33  import org.apache.logging.log4j.catalog.api.Category;
34  import org.apache.logging.log4j.catalog.api.Event;
35  import org.apache.logging.log4j.catalog.api.Product;
36  import org.apache.logging.log4j.catalog.jpa.model.CatalogModel;
37  import org.apache.logging.log4j.catalog.jpa.service.CatalogService;
38  import org.apache.logging.log4j.catalog.jpa.converter.AttributeConverter;
39  import org.apache.logging.log4j.catalog.jpa.converter.CategoryConverter;
40  import org.apache.logging.log4j.catalog.jpa.converter.EventConverter;
41  import org.apache.logging.log4j.catalog.jpa.converter.ProductConverter;
42  import org.apache.logging.log4j.catalog.jpa.model.AttributeModel;
43  import org.apache.logging.log4j.catalog.jpa.model.CategoryModel;
44  import org.apache.logging.log4j.catalog.jpa.model.EventModel;
45  import org.apache.logging.log4j.catalog.jpa.model.ProductModel;
46  import org.apache.logging.log4j.catalog.jpa.service.AttributeService;
47  import org.apache.logging.log4j.catalog.jpa.service.CategoryService;
48  import org.apache.logging.log4j.catalog.jpa.service.EventService;
49  import org.apache.logging.log4j.catalog.jpa.service.ProductService;
50  import org.springframework.beans.factory.annotation.Autowired;
51  
52  public class AuditCatalogManager extends CatalogManagerImpl implements AuditManager {
53  
54      private static Logger logger = LogManager.getLogger();
55  
56      @Autowired
57      private CatalogService catalogService;
58  
59      @Autowired
60      AttributeService attributeService;
61  
62      @Autowired
63      EventService eventService;
64  
65      @Autowired
66      CategoryService categoryService;
67  
68      @Autowired
69      ProductService productService;
70  
71      @Autowired
72      AttributeConverter attributeConverter;
73  
74      @Autowired
75      EventConverter eventConverter;
76  
77      @Autowired
78      CategoryConverter categoryConverter;
79  
80      @Autowired
81      ProductConverter productConverter;
82  
83      private final CatalogReader catalogReader;
84  
85  
86      public AuditCatalogManager(CatalogReader catalogReader) {
87          super(catalogReader);
88          this.catalogReader = catalogReader;
89      }
90  
91      @PostConstruct
92      public void initialize() {
93          CatalogModel catalogModel = catalogService.getCatalogModel();
94          if (catalogModel == null) {
95              catalogModel = new CatalogModel();
96              initialize(catalogModel);
97          } else if (catalogModel.getLastUpdate().toLocalDateTime().isBefore(catalogReader.getLastUpdated())) {
98              initialize(catalogModel);
99          }
100     }
101 
102     @Override
103     public EventModel saveEvent(Event event) {
104         EventModel model = eventConverter.convert(event);
105         model = eventService.saveEvent(model);
106         Map<String, Map<String, CatalogInfo>> infoMap = getInfoMap();
107         addEntry(infoMap, event);
108         return model;
109     }
110 
111     @Override
112     public void saveAttribute(Attribute attribute) {
113         Map<String, Attribute> attrMap = attributeMap.get(attribute.getCatalogId());
114         if (attrMap == null) {
115             attrMap = new ConcurrentHashMap<>();
116             attributeMap.put(attribute.getCatalogId(), attrMap);
117         }
118         attrMap.put(attribute.getName(), attribute);
119     }
120 
121     private void initialize(CatalogModel catalogModel) {
122         logger.debug("Updating static catalog");
123 
124         logger.debug("Loading attributes");
125         List<AttributeModel> attributeModels = new ArrayList<>();
126         Map<String, Attribute> attributeMap = new HashMap<>();
127         for (Attribute attribute : catalogData.getAttributes()) {
128             AttributeModel model = attributeConverter.convert(attribute);
129             attributeService.saveAttribute(model);
130             attributeModels.add(model);
131             attributeMap.put(attribute.getName(), attribute);
132         }
133         attributeModels.stream().filter(m->!attributeMap.containsKey(m.getName()));
134         for (AttributeModel attributeModel : attributeModels) {
135             if (!attributeMap.containsKey(attributeModel.getName())) {
136                 attributeService.deleteAttribute(attributeModel.getId());
137             }
138         }
139         Map<String, Event> eventMap = new HashMap<>();
140         List<EventModel> eventModels = new ArrayList<>();
141         logger.debug("Loading events");
142         for (Event event : catalogData.getEvents()) {
143             logger.debug("Processing Event: {}", event);
144             EventModel model = eventConverter.convert(event);
145             eventMap.put(event.getName(), event);
146             eventModels.add(model);
147             eventService.saveEvent(model);
148         }
149         for (EventModel eventModel : eventModels) {
150             if (!eventMap.containsKey(eventModel.getName())) {
151                 eventService.deleteEvent(eventModel.getId());
152             }
153         }
154         List<CategoryModel> categoryModels = new ArrayList<>();
155         Map<String, Category> categoryMap = new HashMap<>();
156         logger.debug("Loading categories");
157         for (Category category : catalogData.getCategories()) {
158             CategoryModel model = categoryConverter.convert(category);
159             categoryModels.add(model);
160             categoryMap.put(category.getName(), category);
161             categoryService.saveCategory(model);
162         }
163         for (CategoryModel categoryModel : categoryModels) {
164             if (!categoryMap.containsKey(categoryModel.getName())) {
165                 categoryService.deleteCategory(categoryModel.getId());
166             }
167         }
168         List<ProductModel> productModels = new ArrayList<>();
169         Map<String, Product> productMap = new HashMap<>();
170         logger.debug("loading products");
171         for (Product product : catalogData.getProducts()) {
172             ProductModel model = productConverter.convert(product);
173             productModels.add(model);
174             productMap.put(product.getName(), product);
175             productService.saveProduct(model);
176         }
177         for (ProductModel productModel : productModels) {
178             if (!productMap.containsKey(productModel.getName())) {
179                 productService.deleteProduct(productModel.getId());
180             }
181         }
182 
183         catalogModel.setLastUpdate(Timestamp.from(Instant.now()));
184         catalogService.saveCatalog(catalogModel);
185     }
186 }