/logging/src/main/java/org/jboss/as/logging/LoggerAdd.java

https://github.com/aprilhu0/jboss-as · Java · 84 lines · 48 code · 9 blank · 27 comment · 2 complexity · 3305ef59040b49dfb821969a2a4e671c MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2010, 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;
  23. import org.jboss.as.controller.AbstractAddStepHandler;
  24. import org.jboss.as.controller.OperationContext;
  25. import org.jboss.as.controller.OperationFailedException;
  26. import org.jboss.as.controller.PathAddress;
  27. import org.jboss.as.controller.ServiceVerificationHandler;
  28. import org.jboss.dmr.ModelNode;
  29. import org.jboss.msc.service.ServiceController;
  30. import org.jboss.msc.service.ServiceTarget;
  31. import java.util.List;
  32. import java.util.logging.Level;
  33. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
  34. import static org.jboss.as.logging.CommonAttributes.CATEGORY;
  35. import static org.jboss.as.logging.CommonAttributes.HANDLERS;
  36. import static org.jboss.as.logging.CommonAttributes.LEVEL;
  37. /**
  38. * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
  39. * @author Emanuel Muckenhuber
  40. */
  41. class LoggerAdd extends AbstractAddStepHandler {
  42. static final LoggerAdd INSTANCE = new LoggerAdd();
  43. @Override
  44. protected void populateModel(ModelNode operation, ModelNode model) throws OperationFailedException {
  45. LEVEL.validateAndSet(operation, model);
  46. CATEGORY.validateAndSet(operation, model);
  47. HANDLERS.validateAndSet(operation, model);
  48. }
  49. protected void performRuntime(OperationContext context, ModelNode operation, ModelNode model, ServiceVerificationHandler verificationHandler, List<ServiceController<?>> newControllers) throws OperationFailedException {
  50. final PathAddress address = PathAddress.pathAddress(operation.get(OP_ADDR));
  51. final String name = address.getLastElement().getValue();
  52. final ModelNode level = LEVEL.validateResolvedOperation(model);
  53. final ServiceTarget target = context.getServiceTarget();
  54. try {
  55. // Install logger service
  56. final LoggerService service = new LoggerService(name);
  57. if (level.isDefined()) service.setLevel(Level.parse(level.asString()));
  58. newControllers.add(target.addService(LogServices.loggerName(name), service)
  59. .addListener(verificationHandler)
  60. .setInitialMode(ServiceController.Mode.ACTIVE)
  61. .install());
  62. } catch (Throwable t) {
  63. throw new OperationFailedException(new ModelNode().set(t.getLocalizedMessage()));
  64. }
  65. try {
  66. // install logger handler services
  67. final ModelNode handlers = HANDLERS.validateResolvedOperation(model);
  68. if (handlers.isDefined()) {
  69. newControllers.addAll(LogServices.installLoggerHandlers(target, name, handlers, verificationHandler));
  70. }
  71. } catch (Throwable t) {
  72. throw new OperationFailedException(new ModelNode().set(t.getLocalizedMessage()));
  73. }
  74. }
  75. }