PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/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. package org.jboss.as.logging.validators;
  23. import static org.jboss.as.logging.LoggingMessages.MESSAGES;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.Collections;
  27. import java.util.Comparator;
  28. import java.util.List;
  29. import java.util.logging.Level;
  30. import org.jboss.as.controller.OperationFailedException;
  31. import org.jboss.as.controller.operations.validation.AllowedValuesValidator;
  32. import org.jboss.as.controller.operations.validation.ModelTypeValidator;
  33. import org.jboss.as.logging.util.ModelParser;
  34. import org.jboss.dmr.ModelNode;
  35. import org.jboss.dmr.ModelType;
  36. /**
  37. * Checks the value to see if it's a valid {@link Level}.
  38. * <p/>
  39. * Date: 13.07.2011
  40. *
  41. * @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
  42. */
  43. public final class LogLevelValidator extends ModelTypeValidator implements AllowedValuesValidator {
  44. private static final Level[] LEVELS = {
  45. org.jboss.logmanager.Level.ALL,
  46. org.jboss.logmanager.Level.CONFIG,
  47. org.jboss.logmanager.Level.DEBUG,
  48. org.jboss.logmanager.Level.ERROR,
  49. org.jboss.logmanager.Level.FATAL,
  50. org.jboss.logmanager.Level.FINE,
  51. org.jboss.logmanager.Level.FINER,
  52. org.jboss.logmanager.Level.FINEST,
  53. org.jboss.logmanager.Level.INFO,
  54. org.jboss.logmanager.Level.OFF,
  55. org.jboss.logmanager.Level.TRACE,
  56. org.jboss.logmanager.Level.WARN,
  57. org.jboss.logmanager.Level.WARNING
  58. };
  59. private final List<Level> allowedValues;
  60. private final List<ModelNode> nodeValues;
  61. public LogLevelValidator(final boolean nullable) {
  62. this(nullable, false);
  63. }
  64. public LogLevelValidator(final boolean nullable, final Level... levels) {
  65. this(nullable, false, levels);
  66. }
  67. public LogLevelValidator(final boolean nullable, final boolean allowExpressions) {
  68. this(nullable, allowExpressions, LEVELS);
  69. }
  70. public LogLevelValidator(final boolean nullable, final boolean allowExpressions, final Level... levels) {
  71. super(ModelType.STRING, nullable, allowExpressions);
  72. allowedValues = Arrays.asList(levels);
  73. Collections.sort(allowedValues, LevelComparator.INSTANCE);
  74. nodeValues = new ArrayList<ModelNode>(allowedValues.size());
  75. for (Level level : allowedValues) {
  76. nodeValues.add(new ModelNode().set(level.getName()));
  77. }
  78. }
  79. @Override
  80. public void validateParameter(final String parameterName, final ModelNode value) throws OperationFailedException {
  81. super.validateParameter(parameterName, value);
  82. if (value.isDefined()) {
  83. final String levelString = value.asString();
  84. try {
  85. final Level level = ModelParser.parseLevel(value);
  86. if (!allowedValues.contains(level)) {
  87. throw new OperationFailedException(new ModelNode().set(MESSAGES.invalidLogLevel(levelString)));
  88. }
  89. } catch (IllegalArgumentException e) {
  90. throw new OperationFailedException(new ModelNode().set(MESSAGES.invalidLogLevel(levelString)));
  91. }
  92. }
  93. }
  94. @Override
  95. public List<ModelNode> getAllowedValues() {
  96. return nodeValues;
  97. }
  98. private static class LevelComparator implements Comparator<Level> {
  99. static final int EQUAL = 0;
  100. static final int LESS = -1;
  101. static final int GREATER = 1;
  102. static final LevelComparator INSTANCE = new LevelComparator();
  103. @Override
  104. public int compare(final Level o1, final Level o2) {
  105. int result = EQUAL;
  106. final int left = o1.intValue();
  107. final int right = o2.intValue();
  108. if (left < right) {
  109. result = LESS;
  110. } else if (left > right) {
  111. result = GREATER;
  112. }
  113. return result;
  114. }
  115. }
  116. }