View Javadoc

1   /*
2    *  Copyright 2010 Felix Roethenbacher
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 ch.syabru.nagios.broker.jmx;
17  
18  import java.util.regex.Pattern;
19  import java.util.regex.PatternSyntaxException;
20  
21  import org.apache.commons.lang.Validate;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import ch.syabru.nagios.broker.ExternalCommand;
26  import ch.syabru.nagios.broker.ExternalCommandFactory;
27  import ch.syabru.nagios.broker.Matcher;
28  import ch.syabru.nagios.broker.MatcherException;
29  import ch.syabru.nagios.broker.Message;
30  
31  /**
32   * JMX matcher implementation.
33   *
34   * @author Felix Roethenbacher
35   *
36   */
37  public class JmxMatcher implements Matcher {
38      private static final Logger logger =
39          LoggerFactory.getLogger(JmxMatcher.class);
40  
41      private static final Pattern PATTERN_ANY = Pattern.compile(".*", Pattern.DOTALL);
42  
43      private Pattern classNamePattern = PATTERN_ANY;
44      private Pattern messagePattern = PATTERN_ANY;
45      private Pattern typePattern = PATTERN_ANY;
46      private Pattern sourcePattern = PATTERN_ANY;
47      private ExternalCommandFactory commandFactory;
48  
49      /**
50       * C'tor.
51       * @param classNameRegex Class name regular expression.
52       * @param messageRegex Message regular expression.
53       * @param typeRegex Type regular expression.
54       * @param sourceRegex Source regular expression.
55       * @param commandFactory Command factory.
56       * @throws MatcherException XX
57       */
58      public JmxMatcher(String classNameRegex, String messageRegex,
59              String typeRegex, String sourceRegex,
60              ExternalCommandFactory commandFactory)
61      throws MatcherException
62      {
63          Validate.notNull(commandFactory, "commandFactory must not be null");
64          try {
65              if (classNameRegex != null)
66                  this.classNamePattern = Pattern.compile(classNameRegex);
67              if (messageRegex != null)
68                  this.messagePattern = Pattern.compile(messageRegex);
69              if (typeRegex != null)
70                  this.typePattern = Pattern.compile(typeRegex);
71              if (sourceRegex != null)
72                  this.sourcePattern = Pattern.compile(sourceRegex);
73          } catch (PatternSyntaxException e) {
74              throw new MatcherException("Error compiling matcher regex", e);
75          }
76          this.commandFactory = commandFactory;
77      }
78  
79      @Override
80      public ExternalCommand matches(Message message) {
81          ExternalCommand matchingCommand = null;
82          if (message instanceof JmxMessage) {
83              JmxMessage jmxMsg = (JmxMessage) message;
84              java.util.regex.Matcher classNameMatcher =
85                  classNamePattern.matcher(jmxMsg.getClassName());
86              String msgText = jmxMsg.getMessage() != null ?
87                      jmxMsg.getMessage() : "";
88              java.util.regex.Matcher messageMatcher =
89                  messagePattern.matcher(msgText);
90              java.util.regex.Matcher typeMatcher =
91                  typePattern.matcher(jmxMsg.getType());
92              java.util.regex.Matcher sourceMatcher =
93                  sourcePattern.matcher(jmxMsg.getSource());
94              if (classNameMatcher.matches() &&
95                      messageMatcher.matches() &&
96                      typeMatcher.matches() &&
97                      sourceMatcher.matches())
98              {
99                  matchingCommand = commandFactory.createCommand();
100                 logger.debug("Message matched [classNamePattern={}, " +
101                         "messagePattern={}, typePattern={}, " +
102                         "sourcePattern={}, " + "message={}]",
103                         new Object[] { classNamePattern, messagePattern,
104                         typePattern, sourcePattern, message });
105             } else {
106                 logger.debug("Message did not match [classNamePattern={}, " +
107                         "messagePattern={}, typePattern={}, " +
108                         "sourcePattern={}, message={}]", new Object[] {
109                         classNamePattern, messagePattern, typePattern,
110                         sourcePattern, message });
111             }
112         }
113         return matchingCommand;
114     }
115 }