PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/controller/src/main/java/org/jboss/as/controller/ControlledProcessState.java

#
Java | 159 lines | 85 code | 25 blank | 49 comment | 16 complexity | a47290f8a4401899289e5a8f4cbb9956 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;
  23. import java.util.concurrent.atomic.AtomicInteger;
  24. import java.util.concurrent.atomic.AtomicStampedReference;
  25. /**
  26. * The overall state of a process that is being managed by a {@link ModelController}.
  27. *
  28. * @author Brian Stansberry (c) 2011 Red Hat Inc.
  29. */
  30. public class ControlledProcessState {
  31. public static enum State {
  32. /**
  33. * The process is starting and its runtime state is being made consistent with its persistent configuration.
  34. */
  35. STARTING("starting"),
  36. /**
  37. * The process is started, is running normally and has a runtime state consistent with its persistent configuration.
  38. */
  39. RUNNING("running"),
  40. /**
  41. * The process requires a stop and re-start of its root service (but not a full process restart) in order to
  42. * ensure stable operation and/or to bring its running state in line with its persistent configuration. A
  43. * stop and restart of the root service (also known as a 'reload') will result in the removal of all other
  44. * services and creation of new services based on the current configuration, so its affect on availability to
  45. * handle external requests is similar to that of a full process restart. However, a reload can execute more
  46. * quickly than a full process restart.
  47. */
  48. RELOAD_REQUIRED("reload-required"),
  49. /**
  50. * The process must be terminated and replaced with a new process in order to ensure stable operation and/or to bring
  51. * the running state in line with the persistent configuration.
  52. */
  53. RESTART_REQUIRED("restart-required"),
  54. /** The process is stopping. */
  55. STOPPING("stopping");
  56. private final String stringForm;
  57. private State(final String stringForm) {
  58. this.stringForm = stringForm;
  59. }
  60. @Override
  61. public String toString() {
  62. return stringForm;
  63. }
  64. }
  65. private final AtomicInteger stamp = new AtomicInteger(0);
  66. private final AtomicStampedReference<State> state = new AtomicStampedReference<State>(State.STARTING, 0);
  67. private final boolean reloadSupported;
  68. public ControlledProcessState(final boolean reloadSupported) {
  69. this.reloadSupported = reloadSupported;
  70. }
  71. public State getState() {
  72. return state.getReference();
  73. }
  74. public boolean isReloadSupported() {
  75. return reloadSupported;
  76. }
  77. public void setStarting() {
  78. state.set(State.STARTING, stamp.incrementAndGet());
  79. }
  80. public void setRunning() {
  81. state.set(State.RUNNING, stamp.incrementAndGet());
  82. }
  83. public void setStopping() {
  84. state.set(State.STOPPING, stamp.incrementAndGet());
  85. }
  86. public Object setReloadRequired() {
  87. if (!reloadSupported) {
  88. return setRestartRequired();
  89. }
  90. AtomicStampedReference<State> stateRef = state;
  91. int newStamp = stamp.incrementAndGet();
  92. int[] receiver = new int[1];
  93. // Keep trying until stateRef is RELOAD_REQUIRED with our stamp
  94. for (;;) {
  95. State was = stateRef.get(receiver);
  96. if (was == State.STARTING || was == State.STOPPING || was == State.RESTART_REQUIRED) {
  97. break;
  98. }
  99. if (stateRef.compareAndSet(was, State.RELOAD_REQUIRED, receiver[0], newStamp)) {
  100. break;
  101. }
  102. }
  103. return Integer.valueOf(newStamp);
  104. }
  105. public Object setRestartRequired() {
  106. AtomicStampedReference<State> stateRef = state;
  107. int newStamp = stamp.incrementAndGet();
  108. int[] receiver = new int[1];
  109. // Keep trying until stateRef is RESTART_REQUIRED with our stamp
  110. for (;;) {
  111. State was = stateRef.get(receiver);
  112. if (was == State.STARTING || was == State.STOPPING) {
  113. break;
  114. }
  115. if (stateRef.compareAndSet(was, State.RESTART_REQUIRED, receiver[0], newStamp)) {
  116. break;
  117. }
  118. }
  119. return Integer.valueOf(newStamp);
  120. };
  121. public void revertReloadRequired(Object stamp) {
  122. if (!reloadSupported) {
  123. revertRestartRequired(stamp);
  124. }
  125. // If 'state' still has the state we last set in restartRequired(), change to RUNNING
  126. Integer theirStamp = Integer.class.cast(stamp);
  127. state.compareAndSet(State.RELOAD_REQUIRED, State.RUNNING, theirStamp, this.stamp.incrementAndGet());
  128. }
  129. public void revertRestartRequired(Object stamp) {
  130. // If 'state' still has the state we last set in restartRequired(), change to RUNNING
  131. Integer theirStamp = Integer.class.cast(stamp);
  132. state.compareAndSet(State.RESTART_REQUIRED, State.RUNNING, theirStamp, this.stamp.incrementAndGet());
  133. }
  134. }