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  
18  package org.apache.log4j.rule;
19  
20  import java.lang.reflect.InvocationTargetException;
21  import java.lang.reflect.Method;
22  import java.util.Collection;
23  import java.util.LinkedList;
24  import java.util.Stack;
25  import org.apache.log4j.LogManager;
26  
27  /***
28   * A Factory class which, given a string representation of the rule,
29   * and a context stack, will
30   * return a Rule ready for evaluation against events.
31   * If an operator is requested that isn't supported, 
32   * an IllegalArgumentException is thrown.
33   *
34   * @author Scott Deboy (sdeboy@apache.org)
35   */
36  public final class RuleFactory {
37      /***
38       * Singleton instance.
39       */
40    private static final RuleFactory FACTORY = new RuleFactory();
41      /***
42       * Rules.
43       */
44    private static final Collection RULES = new LinkedList();
45      /***
46       * AND operator literal.
47       */
48    private static final String AND_RULE = "&&";
49      /***
50       * OR operator literal.
51       */
52    private static final String OR_RULE = "||";
53      /***
54       * NOT operator literal.
55       */
56    private static final String NOT_RULE = "!";
57      /***
58       * Inequality operator literal.
59       */
60    private static final String NOT_EQUALS_RULE = "!=";
61      /***
62       * Equality operator literal.
63       */
64    private static final String EQUALS_RULE = "==";
65      /***
66       * Partial match operator literal.
67       */
68    private static final String PARTIAL_TEXT_MATCH_RULE = "~=";
69      /***
70       * Like operator literal.
71       */
72    private static final String LIKE_RULE = "like";
73      /***
74       * Exists operator literal.
75       */
76    private static final String EXISTS_RULE = "exists";
77      /***
78       * Less than operator literal.
79       */
80    private static final String LESS_THAN_RULE = "<";
81      /***
82       * Greater than operator literal.
83       */
84    private static final String GREATER_THAN_RULE = ">";
85      /***
86       * Less than or equal operator literal.
87       */
88    private static final String LESS_THAN_EQUALS_RULE = "<=";
89      /***
90       * Greater than or equal operator literal.
91       */
92    private static final String GREATER_THAN_EQUALS_RULE = ">=";
93  
94    static {
95      RULES.add(AND_RULE);
96      RULES.add(OR_RULE);
97      RULES.add(NOT_RULE);
98      RULES.add(NOT_EQUALS_RULE);
99      RULES.add(EQUALS_RULE);
100     RULES.add(PARTIAL_TEXT_MATCH_RULE);
101     RULES.add(LIKE_RULE);
102     RULES.add(EXISTS_RULE);
103     RULES.add(LESS_THAN_RULE);
104     RULES.add(GREATER_THAN_RULE);
105     RULES.add(LESS_THAN_EQUALS_RULE);
106     RULES.add(GREATER_THAN_EQUALS_RULE);
107   }
108 
109     /***
110      * Create instance.
111      */
112   private RuleFactory() {
113         super();
114     }
115 
116     /***
117      * Get instance.
118      * @return rule factory instance.
119      */
120   public static RuleFactory getInstance() {
121       return FACTORY;
122   }
123 
124     /***
125      * Determine if specified string is a known operator.
126      * @param symbol string
127      * @return true if string is a known operator
128      */
129   public boolean isRule(final String symbol) {
130     return ((symbol != null) && (RULES.contains(symbol.toLowerCase())));
131   }
132 
133     /***
134      * Create rule from applying operator to stack.
135      * @param symbol symbol
136      * @param stack stack
137      * @return new instance
138      */
139   public Rule getRule(final String symbol, final Stack stack) {
140     if (AND_RULE.equals(symbol)) {
141       return AndRule.getRule(stack);
142     }
143 
144     if (OR_RULE.equals(symbol)) {
145       return OrRule.getRule(stack);
146     }
147 
148     if (NOT_RULE.equals(symbol)) {
149       return NotRule.getRule(stack);
150     }
151 
152     if (NOT_EQUALS_RULE.equals(symbol)) {
153       return NotEqualsRule.getRule(stack);
154     }
155 
156     if (EQUALS_RULE.equals(symbol)) {
157       return EqualsRule.getRule(stack);
158     }
159 
160     if (PARTIAL_TEXT_MATCH_RULE.equals(symbol)) {
161       return PartialTextMatchRule.getRule(stack);
162     }
163 
164     if (RULES.contains(LIKE_RULE) && LIKE_RULE.equalsIgnoreCase(symbol)) {
165       return LikeRule.getRule(stack);
166     }
167 
168     if (EXISTS_RULE.equalsIgnoreCase(symbol)) {
169       return ExistsRule.getRule(stack);
170     }
171 
172     if (LESS_THAN_RULE.equals(symbol)) {
173       return InequalityRule.getRule(LESS_THAN_RULE, stack);
174     }
175 
176     if (GREATER_THAN_RULE.equals(symbol)) {
177       return InequalityRule.getRule(GREATER_THAN_RULE, stack);
178     }
179 
180     if (LESS_THAN_EQUALS_RULE.equals(symbol)) {
181       return InequalityRule.getRule(LESS_THAN_EQUALS_RULE, stack);
182     }
183 
184     if (GREATER_THAN_EQUALS_RULE.equals(symbol)) {
185       return InequalityRule.getRule(GREATER_THAN_EQUALS_RULE, stack);
186     }
187     throw new IllegalArgumentException("Invalid rule: " + symbol);
188   }
189 }