/jboss-as-7.1.1.Final/logging/src/main/java/org/jboss/as/logging/validators/LogLevelValidator.java
Java | 132 lines | 88 code | 16 blank | 28 comment | 6 complexity | 7dcdf1496ba270a0d7e04dab20d7c5c4 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
1/*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2011, Red Hat, Inc., and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22
23package org.jboss.as.logging.validators;
24
25import static org.jboss.as.logging.LoggingMessages.MESSAGES;
26
27import java.util.ArrayList;
28import java.util.Arrays;
29import java.util.Collections;
30import java.util.Comparator;
31import java.util.List;
32import java.util.logging.Level;
33
34import org.jboss.as.controller.OperationFailedException;
35import org.jboss.as.controller.operations.validation.AllowedValuesValidator;
36import org.jboss.as.controller.operations.validation.ModelTypeValidator;
37import org.jboss.as.logging.util.ModelParser;
38import org.jboss.dmr.ModelNode;
39import org.jboss.dmr.ModelType;
40
41/**
42 * Checks the value to see if it's a valid {@link Level}.
43 * <p/>
44 * Date: 13.07.2011
45 *
46 * @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
47 */
48public final class LogLevelValidator extends ModelTypeValidator implements AllowedValuesValidator {
49 private static final Level[] LEVELS = {
50 org.jboss.logmanager.Level.ALL,
51 org.jboss.logmanager.Level.CONFIG,
52 org.jboss.logmanager.Level.DEBUG,
53 org.jboss.logmanager.Level.ERROR,
54 org.jboss.logmanager.Level.FATAL,
55 org.jboss.logmanager.Level.FINE,
56 org.jboss.logmanager.Level.FINER,
57 org.jboss.logmanager.Level.FINEST,
58 org.jboss.logmanager.Level.INFO,
59 org.jboss.logmanager.Level.OFF,
60 org.jboss.logmanager.Level.TRACE,
61 org.jboss.logmanager.Level.WARN,
62 org.jboss.logmanager.Level.WARNING
63 };
64
65 private final List<Level> allowedValues;
66 private final List<ModelNode> nodeValues;
67
68 public LogLevelValidator(final boolean nullable) {
69 this(nullable, false);
70 }
71
72 public LogLevelValidator(final boolean nullable, final Level... levels) {
73 this(nullable, false, levels);
74 }
75
76 public LogLevelValidator(final boolean nullable, final boolean allowExpressions) {
77 this(nullable, allowExpressions, LEVELS);
78 }
79
80 public LogLevelValidator(final boolean nullable, final boolean allowExpressions, final Level... levels) {
81 super(ModelType.STRING, nullable, allowExpressions);
82 allowedValues = Arrays.asList(levels);
83 Collections.sort(allowedValues, LevelComparator.INSTANCE);
84 nodeValues = new ArrayList<ModelNode>(allowedValues.size());
85 for (Level level : allowedValues) {
86 nodeValues.add(new ModelNode().set(level.getName()));
87 }
88 }
89
90 @Override
91 public void validateParameter(final String parameterName, final ModelNode value) throws OperationFailedException {
92 super.validateParameter(parameterName, value);
93 if (value.isDefined()) {
94 final String levelString = value.asString();
95 try {
96 final Level level = ModelParser.parseLevel(value);
97 if (!allowedValues.contains(level)) {
98 throw new OperationFailedException(new ModelNode().set(MESSAGES.invalidLogLevel(levelString)));
99 }
100 } catch (IllegalArgumentException e) {
101 throw new OperationFailedException(new ModelNode().set(MESSAGES.invalidLogLevel(levelString)));
102 }
103 }
104 }
105
106 @Override
107 public List<ModelNode> getAllowedValues() {
108 return nodeValues;
109 }
110
111 private static class LevelComparator implements Comparator<Level> {
112
113 static final int EQUAL = 0;
114 static final int LESS = -1;
115 static final int GREATER = 1;
116
117 static final LevelComparator INSTANCE = new LevelComparator();
118
119 @Override
120 public int compare(final Level o1, final Level o2) {
121 int result = EQUAL;
122 final int left = o1.intValue();
123 final int right = o2.intValue();
124 if (left < right) {
125 result = LESS;
126 } else if (left > right) {
127 result = GREATER;
128 }
129 return result;
130 }
131 }
132}