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.math.BigDecimal;
19  
20  import org.apache.logging.log4j.catalog.api.ConstraintType;
21  import org.apache.logging.log4j.core.config.plugins.Plugin;
22  import static org.apache.logging.log4j.catalog.api.util.StringUtils.isBlank;
23  import static org.apache.logging.log4j.catalog.api.util.StringUtils.appendNewline;
24  
25  /**
26   *
27   */
28  @Plugin(name = "maxValue", category = ConstraintType.CATEGORY)
29  public class MaxValueConstraint implements ConstraintType {
30  
31      @Override
32      public void validate(boolean isRequestContext, String name, String value, String maxValue, StringBuilder error) {
33          if (isBlank(maxValue)) {
34              appendNewline(error);
35              if (isRequestContext) {
36                  error.append("ThreadContext key ");
37              }
38              error.append(name).append(" has no value for the minimum value defined");
39              return;
40          }
41          if (!isBlank(value)) {
42              try {
43                  BigDecimal minVal = new BigDecimal(maxValue);
44                  BigDecimal val = new BigDecimal(value);
45                  if (val.compareTo(minVal) > 0) {
46                      appendNewline(error);
47                      if (isRequestContext) {
48                          error.append("ThreadContext key ");
49                      }
50                      error.append(name).append(" is less than ").append(maxValue);
51                  }
52              } catch (Exception ex) {
53                  appendNewline(error);
54                  if (isRequestContext) {
55                      error.append("ThreadContext key ");
56                  }
57                  error.append(name).append(" encountered an error trying to determine the minimum value: ").append(ex.getMessage());
58              }
59          }
60      }
61  }