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.plugins;
17  
18  import java.util.Collections;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Locale;
22  import java.util.Map;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  import org.apache.logging.log4j.catalog.api.ConstraintType;
27  import org.apache.logging.log4j.catalog.api.exception.ConstraintCreationException;
28  import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
29  import org.apache.logging.log4j.core.config.plugins.util.PluginType;
30  import org.apache.logging.log4j.core.util.ReflectionUtil;
31  
32  /**
33   *
34   */
35  
36  public class ConstraintPlugins {
37  
38      private static Logger LOGGER = LogManager.getLogger(ConstraintPlugins.class);
39  
40      private static Map<String, ConstraintType> constraintMap = new HashMap<>();
41  
42      private static volatile ConstraintPlugins instance = null;
43  
44      private static final Object LOCK = new Object();
45  
46      public static ConstraintPlugins getInstance() {
47          if (instance == null) {
48              synchronized(LOCK) {
49                  if (instance == null) {
50                      instance = new ConstraintPlugins();
51                  }
52              }
53          }
54          return instance;
55      }
56  
57      private ConstraintPlugins() {
58  
59          final PluginManager manager = new PluginManager(ConstraintType.CATEGORY);
60          if (LOGGER instanceof org.apache.logging.log4j.core.Logger) {
61              List<String> pluginPackages =
62                      ((org.apache.logging.log4j.core.Logger) LOGGER).getContext().getConfiguration().getPluginPackages();
63              manager.collectPlugins(pluginPackages);
64          } else {
65              manager.collectPlugins();
66          }
67          final Map<String, PluginType<?>> plugins = manager.getPlugins();
68          for (Map.Entry<String, PluginType<?>> entry : plugins.entrySet()) {
69              try {
70                  final Class<? extends ConstraintType> clazz = entry.getValue().getPluginClass().asSubclass(ConstraintType.class);
71                  ConstraintType constraintType = ReflectionUtil.instantiate(clazz);
72                  constraintMap.put(entry.getKey(), constraintType);
73              } catch (final Throwable t) {
74                  throw new ConstraintCreationException("Unable to create constraint for " + entry.getKey(), t);
75              }
76          }
77      }
78  
79      public void validateConstraint(boolean isRequestContext, String constraint, String name,
80                                            String value, String constraintValue, StringBuilder errors) {
81          ConstraintType constraintType = constraintMap.get(constraint.toLowerCase(Locale.US));
82          if (constraintType == null) {
83              if (errors.length() > 0) {
84                  errors.append("\n");
85              }
86              errors.append("Unable to locate constraint type ").append(constraint);
87              if (isRequestContext) {
88                  errors.append(" for ThreadContext key ");
89              } else {
90                  errors.append(" for key ");
91              }
92              errors.append(name);
93              return;
94          }
95          constraintType.validate(isRequestContext, name, value, constraintValue, errors);
96      }
97  
98      public ConstraintType findByName(String name) {
99          return constraintMap.get(name.toLowerCase(Locale.US));
100     }
101 
102     public Map<String, ConstraintType> getConstraintMap() {
103         return Collections.unmodifiableMap(constraintMap);
104     }
105 }