PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/controller/src/main/java/org/jboss/as/controller/operations/common/ProcessEnvironment.java

#
Java | 186 lines | 86 code | 28 blank | 72 comment | 12 complexity | 18339de8f10c455b044f40ceeb6cbbd2 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.controller.operations.common;
  23. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.VALUE;
  24. import java.net.InetAddress;
  25. import java.net.UnknownHostException;
  26. import java.util.UUID;
  27. import org.jboss.as.controller.AttributeDefinition;
  28. import org.jboss.as.controller.ControlledProcessState;
  29. import org.jboss.as.controller.ControllerMessages;
  30. import org.jboss.as.controller.OperationContext;
  31. import org.jboss.as.controller.OperationFailedException;
  32. import org.jboss.as.controller.OperationStepHandler;
  33. import org.jboss.as.controller.PathAddress;
  34. import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
  35. import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
  36. import org.jboss.as.controller.interfaces.InetAddressUtil;
  37. import org.jboss.dmr.ModelNode;
  38. import org.jboss.dmr.ModelType;
  39. /**
  40. * Base class for objects that store environment information for a process.
  41. *
  42. * @author Brian Stansberry (c) 2011 Red Hat Inc.
  43. */
  44. public abstract class ProcessEnvironment {
  45. /** The special process name value that triggers calculation of a UUID */
  46. public static final String JBOSS_DOMAIN_UUID = "jboss.domain.uuid";
  47. /** {@link AttributeDefinition} for the {@code name} attribute for a processes root resource */
  48. public static final AttributeDefinition NAME = new SimpleAttributeDefinitionBuilder(ModelDescriptionConstants.NAME, ModelType.STRING, true)
  49. .setAllowExpression(true).build();
  50. /**
  51. * Gets an {@link OperationStepHandler} that can read the {@code name} attribute for a processes root resource
  52. * @return the handler
  53. */
  54. public OperationStepHandler getProcessNameReadHandler() {
  55. return new ProcessNameReadAttributeHandler();
  56. }
  57. /**
  58. * Gets an {@link OperationStepHandler} that can write the {@code name} attribute for a processes root resource
  59. * @return the handler
  60. */
  61. public OperationStepHandler getProcessNameWriteHandler() {
  62. return new ProcessNameWriteAttributeHandler();
  63. }
  64. /**
  65. * Gets the resolved name of this process; a value previously passed to {@link #setProcessName(String)} or
  66. * a value derived from the environment.
  67. *
  68. * @return the process name. Cannot be {@code null}
  69. */
  70. protected abstract String getProcessName();
  71. /**
  72. * Sets the process name. This method can only be called by the handler returned by
  73. * {@link #getProcessNameWriteHandler()}; its visibility is protected only because subclasses need to implement it.
  74. *
  75. * @param processName the process name. May be {@code null} in which case a default process name should be used.
  76. */
  77. protected abstract void setProcessName(String processName);
  78. /**
  79. * Gets whether updating the runtime system properties with the given property is allowed.
  80. *
  81. * @param propertyName the name of the property. Cannot be {@code null}
  82. * @param propertyValue the value of the property. May be {@code null}
  83. * @param bootTime {@code true} if the process is currently booting
  84. *
  85. * @return {@code true} if the update can be applied to the runtime system properties; {@code} false if it
  86. * should just be stored in the persistent configuration and the process should be put into
  87. * {@link ControlledProcessState.State#RELOAD_REQUIRED reload-required state}.
  88. *
  89. * @throws OperationFailedException if a change to the given property is not allowed at all; e.g. changing
  90. * {@code jboss.server.base.dir} after primordial boot is not allowed; the
  91. * property can only be set from the command line
  92. */
  93. protected abstract boolean isRuntimeSystemPropertyUpdateAllowed(String propertyName,
  94. String propertyValue,
  95. boolean bootTime) throws OperationFailedException;
  96. /**
  97. * Notifies this {@code ProcessEnvironment} that the runtime value of the given system property has been updated,
  98. * allowing it to update any state that was originally set via the system property during primordial process boot.
  99. * This method should only be invoked after a call to {@link #isRuntimeSystemPropertyUpdateAllowed(String, String, boolean)}
  100. * has returned {@code true}.
  101. *
  102. * @param propertyName the name of the property. Cannot be {@code null}
  103. * @param propertyValue the value of the property. May be {@code null}
  104. */
  105. protected abstract void systemPropertyUpdated(String propertyName, String propertyValue);
  106. protected static String resolveGUID(final String unresolvedName) {
  107. String result;
  108. if (JBOSS_DOMAIN_UUID.equals(unresolvedName)) {
  109. try {
  110. InetAddress localhost = InetAddressUtil.getLocalHost();
  111. result = UUID.nameUUIDFromBytes(localhost.getAddress()).toString();
  112. } catch (UnknownHostException e) {
  113. throw ControllerMessages.MESSAGES.cannotResolveProcessUUID(e);
  114. }
  115. } else {
  116. result = unresolvedName;
  117. }
  118. return result;
  119. }
  120. private class ProcessNameWriteAttributeHandler implements OperationStepHandler {
  121. @Override
  122. public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
  123. final ModelNode model = context.readResourceForUpdate(PathAddress.EMPTY_ADDRESS).getModel();
  124. final ModelNode newValue = operation.hasDefined(VALUE) ? operation.get(VALUE) : new ModelNode();
  125. final ModelNode mockOp = new ModelNode();
  126. mockOp.get(NAME.getName()).set(newValue);
  127. NAME.validateAndSet(mockOp, model);
  128. boolean booting = context.isBooting();
  129. String resolved = null;
  130. if (context.isBooting()) {
  131. final ModelNode resolvedNode = NAME.resolveModelAttribute(context, model);
  132. resolved = resolvedNode.isDefined() ? resolvedNode.asString() : null;
  133. resolved = resolved == null ? null : resolveGUID(resolved);
  134. } else {
  135. context.reloadRequired();
  136. }
  137. if (context.completeStep() == OperationContext.ResultAction.KEEP) {
  138. if (booting) {
  139. ProcessEnvironment.this.setProcessName(resolved);
  140. }
  141. } else if (!booting) {
  142. context.revertReloadRequired();
  143. }
  144. }
  145. }
  146. private class ProcessNameReadAttributeHandler implements OperationStepHandler {
  147. @Override
  148. public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
  149. final ModelNode model = context.readResource(PathAddress.EMPTY_ADDRESS).getModel();
  150. if (model.hasDefined(NAME.getName())) {
  151. context.getResult().set(model.get(NAME.getName()));
  152. } else {
  153. context.getResult().set(ProcessEnvironment.this.getProcessName());
  154. }
  155. context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
  156. }
  157. }
  158. }