PageRenderTime 133ms CodeModel.GetById 55ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/logging/src/main/java/org/jboss/as/logging/handlers/async/AsyncHandlerAssignSubhandler.java

#
Java | 139 lines | 80 code | 15 blank | 44 comment | 6 complexity | 3b80fdb98a0f5ae6b1e549285f24d534 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.handlers.async;
  23. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
  24. import static org.jboss.as.logging.CommonAttributes.NAME;
  25. import static org.jboss.as.logging.CommonAttributes.SUBHANDLERS;
  26. import static org.jboss.as.logging.LoggingMessages.MESSAGES;
  27. import org.jboss.as.controller.AttributeDefinition;
  28. import org.jboss.as.controller.OperationContext;
  29. import org.jboss.as.controller.OperationFailedException;
  30. import org.jboss.as.controller.PathAddress;
  31. import org.jboss.as.controller.ServiceVerificationHandler;
  32. import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
  33. import org.jboss.as.logging.LoggingLogger;
  34. import org.jboss.as.logging.loggers.AbstractLogHandlerAssignmentHandler;
  35. import org.jboss.as.logging.util.LogServices;
  36. import org.jboss.dmr.ModelNode;
  37. import org.jboss.dmr.ModelType;
  38. import org.jboss.msc.service.ServiceController;
  39. import org.jboss.msc.service.ServiceRegistry;
  40. import org.jboss.msc.value.InjectedValue;
  41. import java.util.List;
  42. import java.util.logging.Handler;
  43. /**
  44. * Operation responsible assigning a subhandler to an async handler.
  45. *
  46. * @author Stan Silvert
  47. */
  48. public class AsyncHandlerAssignSubhandler extends AbstractLogHandlerAssignmentHandler {
  49. public static final String OPERATION_NAME = "assign-subhandler";
  50. public static final AsyncHandlerAssignSubhandler INSTANCE = new AsyncHandlerAssignSubhandler();
  51. @Override
  52. protected void updateModel(ModelNode operation, ModelNode model) throws OperationFailedException {
  53. NAME.validateAndSet(operation, model);
  54. updateHandlersForAssign(SUBHANDLERS, operation, model);
  55. }
  56. @Override
  57. protected void performRuntime(final OperationContext context, final ModelNode operation, final ModelNode model,
  58. final ServiceVerificationHandler verificationHandler, final List<ServiceController<?>> newControllers) throws OperationFailedException {
  59. PathAddress address = PathAddress.pathAddress(operation.require(OP_ADDR));
  60. String asyncHandlerName = address.getLastElement().getValue();
  61. String handlerNameToAdd = NAME.resolveModelAttribute(context, model).asString();
  62. addHandler(context, asyncHandlerName, handlerNameToAdd);
  63. }
  64. @Override
  65. protected void rollbackRuntime(OperationContext context, final ModelNode operation, final ModelNode model, List<ServiceController<?>> controllers) {
  66. PathAddress address = PathAddress.pathAddress(operation.require(OP_ADDR));
  67. String asyncHandlerName = address.getLastElement().getValue();
  68. String handlerName = model.get(NAME.getName()).asString();
  69. try {
  70. AsyncHandlerUnassignSubhandler.removeHandler(context, asyncHandlerName, handlerName);
  71. } catch (OperationFailedException e) {
  72. LoggingLogger.ROOT_LOGGER.errorRevertingOperation(e, getClass().getSimpleName(),
  73. operation.require(ModelDescriptionConstants.OP).asString(),
  74. PathAddress.pathAddress(operation.require(ModelDescriptionConstants.OP_ADDR)));
  75. }
  76. }
  77. @Override
  78. protected String getHandlerName(final ModelNode model) throws OperationFailedException {
  79. return NAME.validateOperation(model).asString();
  80. }
  81. /**
  82. * Adds the handler, represented by the {@code handlerNameToRemove} parameter, from the async handler.
  83. *
  84. * @param context the context of the operation.
  85. * @param asyncHandlerName the async handler name.
  86. * @param handlerNameToAdd the name of the handler to add.
  87. *
  88. * @throws OperationFailedException if an error occurs.
  89. */
  90. public static void addHandler(final OperationContext context, final String asyncHandlerName, final String handlerNameToAdd) throws OperationFailedException {
  91. final ServiceRegistry serviceRegistry = context.getServiceRegistry(true);
  92. @SuppressWarnings("unchecked")
  93. final ServiceController<Handler> asyncHandlerController = (ServiceController<Handler>) serviceRegistry.getService(LogServices.handlerName(asyncHandlerName));
  94. @SuppressWarnings("unchecked")
  95. final ServiceController<Handler> handlerToAssignController = (ServiceController<Handler>) serviceRegistry.getService(LogServices.handlerName(handlerNameToAdd));
  96. if (handlerToAssignController == null) {
  97. throw createFailureMessage(MESSAGES.handlerNotFound(handlerNameToAdd));
  98. }
  99. final AsyncHandlerService service = AsyncHandlerService.class.cast(asyncHandlerController.getService());
  100. final InjectedValue<Handler> injectedHandler = new InjectedValue<Handler>();
  101. injectedHandler.inject(handlerToAssignController.getValue());
  102. service.addHandler(injectedHandler);
  103. }
  104. /**
  105. * Adds the subhandlers to the async handler.
  106. *
  107. * @param attribute the attribute definition.
  108. * @param node the model node to extract the subhandlers from.
  109. * @param context the context of the operation.
  110. *
  111. * @throws OperationFailedException if an error occurs.
  112. */
  113. public static void addHandlers(final AttributeDefinition attribute, final ModelNode node, final OperationContext context,
  114. final String asyncHandlerName) throws OperationFailedException {
  115. final ModelNode handlers = attribute.resolveModelAttribute(context, node);
  116. if (handlers.isDefined()) {
  117. if (handlers.getType() == ModelType.LIST) {
  118. for (ModelNode handler : handlers.asList()) {
  119. addHandler(context, asyncHandlerName, handler.asString());
  120. }
  121. }
  122. }
  123. }
  124. }