cleanup

Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/d81af3e2
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/d81af3e2
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/d81af3e2

Branch: refs/heads/master
Commit: d81af3e2e143fd5b0285c5f0dcb999aea0f9a6ad
Parents: af74958
Author: Svetoslav Neykov <svetoslav.ney...@cloudsoftcorp.com>
Authored: Wed Jan 25 17:10:21 2017 +0200
Committer: Svetoslav Neykov <svetoslav.ney...@cloudsoftcorp.com>
Committed: Wed Jan 25 17:10:21 2017 +0200

----------------------------------------------------------------------
 .../brooklyn/core/sensor/ReleaseableLatch.java  |  9 ++--
 .../base/AbstractSoftwareProcessDriver.java     | 23 +--------
 .../MachineLifecycleEffectorTasks.java          | 51 +++++++++++---------
 .../base/SoftwareProcessEntityLatchTest.java    | 17 ++++---
 4 files changed, 44 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d81af3e2/core/src/main/java/org/apache/brooklyn/core/sensor/ReleaseableLatch.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/brooklyn/core/sensor/ReleaseableLatch.java 
b/core/src/main/java/org/apache/brooklyn/core/sensor/ReleaseableLatch.java
index 8d28f93..97d4212 100644
--- a/core/src/main/java/org/apache/brooklyn/core/sensor/ReleaseableLatch.java
+++ b/core/src/main/java/org/apache/brooklyn/core/sensor/ReleaseableLatch.java
@@ -26,15 +26,18 @@ import org.apache.brooklyn.util.exceptions.Exceptions;
 
 import com.google.common.base.Function;
 
-// DeferredSupplier used as a marker interface to prevent coercion. When 
resolved it must evaluate to {@code Boolean.TRUE}.
 public interface ReleaseableLatch {
     /**
-     * Increment usage count for the {@code caller} entity
+     * Increment usage count for the {@code caller} entity. Implementations 
may ignore {@code caller}.
+     * 
+     * @param caller The entity latching on the object
      */
     void acquire(Entity caller);
 
     /**
-     * Decrement usage count for the {@code caller} entity
+     * Decrement usage count for the {@code caller} entity. Implementations 
may ignore {@code caller}.
+     *
+     * @param caller The entity latching on the object
      */
     void release(Entity caller);
 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d81af3e2/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessDriver.java
----------------------------------------------------------------------
diff --git 
a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessDriver.java
 
b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessDriver.java
index 3900b76..339dd75 100644
--- 
a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessDriver.java
+++ 
b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/AbstractSoftwareProcessDriver.java
@@ -38,7 +38,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Callable;
 
-import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.entity.EntityLocal;
 import org.apache.brooklyn.api.location.Location;
 import org.apache.brooklyn.api.mgmt.Task;
@@ -48,8 +47,8 @@ import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
 import org.apache.brooklyn.core.entity.EntityInternal;
 import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;
 import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
-import org.apache.brooklyn.core.sensor.ReleaseableLatch;
 import 
org.apache.brooklyn.entity.software.base.lifecycle.MachineLifecycleEffectorTasks;
+import 
org.apache.brooklyn.entity.software.base.lifecycle.MachineLifecycleEffectorTasks.CloseableLatch;
 import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.core.ResourceUtils;
 import org.apache.brooklyn.util.core.task.DynamicTasks;
@@ -217,26 +216,8 @@ public abstract class AbstractSoftwareProcessDriver 
implements SoftwareProcessDr
         }});
     }
 
-    // Removes the checked Exception from the method signature
-    private static class CloseableLatch implements AutoCloseable {
-        private Entity caller;
-        private ReleaseableLatch releaseableLatch;
-
-        public CloseableLatch(Entity caller, ReleaseableLatch 
releaseableLatch) {
-            this.caller = caller;
-            this.releaseableLatch = releaseableLatch;
-        }
-
-        @Override
-        public void close() {
-            DynamicTasks.drain(null, false);
-            releaseableLatch.release(caller);
-        }
-    }
-
     private CloseableLatch waitForLatch(ConfigKey<Boolean> configKey) {
-        ReleaseableLatch releaseableLatch = 
MachineLifecycleEffectorTasks.waitForLatch((EntityInternal)entity, configKey);
-        return new CloseableLatch(entity, releaseableLatch);
+        return 
MachineLifecycleEffectorTasks.waitForCloseableLatch((EntityInternal)entity, 
configKey);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d81af3e2/software/base/src/main/java/org/apache/brooklyn/entity/software/base/lifecycle/MachineLifecycleEffectorTasks.java
----------------------------------------------------------------------
diff --git 
a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/lifecycle/MachineLifecycleEffectorTasks.java
 
b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/lifecycle/MachineLifecycleEffectorTasks.java
index e945b47..98ca2cb 100644
--- 
a/software/base/src/main/java/org/apache/brooklyn/entity/software/base/lifecycle/MachineLifecycleEffectorTasks.java
+++ 
b/software/base/src/main/java/org/apache/brooklyn/entity/software/base/lifecycle/MachineLifecycleEffectorTasks.java
@@ -25,7 +25,6 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Callable;
-import java.util.concurrent.atomic.AtomicReference;
 
 import javax.annotation.Nullable;
 
@@ -113,9 +112,9 @@ import com.google.common.reflect.TypeToken;
  * <ul>
  *  <li> {@link #startProcessesAtMachine(Supplier)} (required)
  *  <li> {@link #stopProcessesAtMachine()} (required, but can be left blank if 
you assume the VM will be destroyed)
- *  <li> {@link #preStartCustom(MachineLocation, AtomicReference)}
+ *  <li> {@link #preStartCustom(MachineLocation)}
  *  <li> {@link #postStartCustom()}
- *  <li> {@link #preStopConfirmCustom(AtomicReference)}
+ *  <li> {@link #preStopConfirmCustom()}
  *  <li> {@link #postStopCustom()}
  * </ul>
  * Note methods at this level typically look after the {@link 
Attributes#SERVICE_STATE_ACTUAL} sensor.
@@ -365,20 +364,12 @@ public abstract class MachineLifecycleEffectorTasks {
         Preconditions.checkState(locationS != null, "Unsupported location 
"+location+", when starting "+entity());
 
         final Supplier<MachineLocation> locationSF = locationS;
-        final AtomicReference<ReleaseableLatch> startLatchRef = new 
AtomicReference<>();
 
         // Opportunity to block startup until other dependent components are 
available
-        startLatchRef.set(waitForLatch(entity(), SoftwareProcess.START_LATCH));
-        try {
+        try (CloseableLatch latch = waitForCloseableLatch(entity(), 
SoftwareProcess.START_LATCH)) {
             preStartAtMachineAsync(locationSF);
             DynamicTasks.queue("start (processes)", new 
StartProcessesAtMachineTask(locationSF));
             postStartAtMachineAsync();
-        } finally {
-            DynamicTasks.drain(null, false);
-            ReleaseableLatch startLatch = startLatchRef.get();
-            if (startLatch != null) {
-                startLatch.release(entity());
-            }
         }
     }
 
@@ -463,9 +454,7 @@ public abstract class MachineLifecycleEffectorTasks {
 
     /**
      * Wraps a call to {@link #preStartCustom(MachineLocation)}, after setting 
the hostname and address.
-     * @deprecated since 0.11.0. Use {@link #preStartAtMachineAsync(Supplier, 
AtomicReference)} instead.
      */
-    @Deprecated
     protected void preStartAtMachineAsync(final Supplier<MachineLocation> 
machineS) {
         DynamicTasks.queue("pre-start", new PreStartTask(machineS.get()));
     }
@@ -756,16 +745,8 @@ public abstract class MachineLifecycleEffectorTasks {
     }
 
     protected void doStopLatching(ConfigBag parameters, 
Callable<StopMachineDetails<Integer>> stopTask) {
-        AtomicReference<ReleaseableLatch> stopLatchRef = new 
AtomicReference<>();
-        try {
-            stopLatchRef.set(waitForLatch(entity(), 
SoftwareProcess.STOP_LATCH));
+        try (CloseableLatch latch = waitForCloseableLatch(entity(), 
SoftwareProcess.STOP_LATCH)) {
             doStop(parameters, stopTask);
-        } finally {
-            DynamicTasks.drain(null, false);
-            ReleaseableLatch stopLatch = stopLatchRef.get();
-            if (stopLatch != null) {
-                stopLatch.release(entity());
-            }
         }
     }
 
@@ -1104,7 +1085,29 @@ public abstract class MachineLifecycleEffectorTasks {
         entity().sensors().set(Attributes.SUBNET_ADDRESS, null);
     }
 
-    public static ReleaseableLatch waitForLatch(EntityInternal entity, 
ConfigKey<Boolean> configKey) {
+    // Removes the checked Exception from the method signature
+    public static class CloseableLatch implements AutoCloseable {
+        private Entity caller;
+        private ReleaseableLatch releaseableLatch;
+
+        public CloseableLatch(Entity caller, ReleaseableLatch 
releaseableLatch) {
+            this.caller = caller;
+            this.releaseableLatch = releaseableLatch;
+        }
+
+        @Override
+        public void close() {
+            DynamicTasks.drain(null, false);
+            releaseableLatch.release(caller);
+        }
+    }
+
+    public static CloseableLatch waitForCloseableLatch(Entity entity, 
ConfigKey<Boolean> configKey) {
+        ReleaseableLatch releaseableLatch = 
waitForLatch((EntityInternal)entity, configKey);
+        return new CloseableLatch(entity, releaseableLatch);
+    }
+
+    private static ReleaseableLatch waitForLatch(EntityInternal entity, 
ConfigKey<Boolean> configKey) {
         Maybe<?> rawValue = entity.config().getRaw(configKey);
         if (rawValue.isAbsent()) {
             return ReleaseableLatch.NOP;

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d81af3e2/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityLatchTest.java
----------------------------------------------------------------------
diff --git 
a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityLatchTest.java
 
b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityLatchTest.java
index ee7e77a..c2225b8 100644
--- 
a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityLatchTest.java
+++ 
b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessEntityLatchTest.java
@@ -128,9 +128,7 @@ public class SoftwareProcessEntityLatchTest extends 
BrooklynAppUnitTestSupport {
             public Void apply(MyService entity) {
                 String taskName = (latch == SoftwareProcess.STOP_LATCH) ? 
"stop" : "start";
                 assertEffectorBlockingDetailsEventually(entity, taskName, 
"Acquiring " + latch + " " + latchSemaphore);
-                if (latch != SoftwareProcess.START_LATCH) {
-                    assertDriverEventsEquals(entity, preLatchEvents);
-                }
+                assertDriverEventsEquals(entity, preLatchEvents);
                 latchSemaphore.release(entity);
                 return null;
             }
@@ -153,9 +151,7 @@ public class SoftwareProcessEntityLatchTest extends 
BrooklynAppUnitTestSupport {
         }
 
         assertEffectorBlockingDetailsEventually(entity, task.getDisplayName(), 
"Waiting for config " + latch.getName());
-        if (latch != SoftwareProcess.START_LATCH) {
-            assertDriverEventsEquals(entity, preLatchEvents);
-        }
+        assertDriverEventsEquals(entity, preLatchEvents);
         assertFalse(task.isDone());
 
         app.sensors().set(latchSensor, latchValue);
@@ -246,8 +242,13 @@ public class SoftwareProcessEntityLatchTest extends 
BrooklynAppUnitTestSupport {
     }
 
     private void assertDriverEventsEquals(MyService entity, List<String> 
expectedEvents) {
-        List<String> events = ((SimulatedDriver)entity.getDriver()).events;
-        assertEquals(events, expectedEvents, "events="+events);
+        SimulatedDriver driver = (SimulatedDriver)entity.getDriver();
+        if (driver != null) {
+            List<String> events = driver.events;
+            assertEquals(events, expectedEvents, "events="+events);
+        } else {
+            assertEquals(expectedEvents.size(), 0);
+        }
     }
 
     private void assertEffectorBlockingDetailsEventually(final Entity entity, 
final String effectorName, final String blockingDetailsSnippet) {

Reply via email to