Author: jwross
Date: Wed Apr 10 15:25:01 2013
New Revision: 1466517

URL: http://svn.apache.org/r1466517
Log:
State change wait timeouts and missed notifications.

Added a timeout when waiting for a subsystem state change when performing an 
operation. The timeout is 60 seconds and
currently not configurable. Might be good to make it configurable in the future.

Fixed an issue where there was a small window allowing a missed state change 
notification while waiting.

Modified:
    
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/AbstractAction.java
    
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/StartAction.java
    
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/StopAction.java
    
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/UninstallAction.java

Modified: 
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/AbstractAction.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/AbstractAction.java?rev=1466517&r1=1466516&r2=1466517&view=diff
==============================================================================
--- 
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/AbstractAction.java
 (original)
+++ 
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/AbstractAction.java
 Wed Apr 10 15:25:01 2013
@@ -15,8 +15,11 @@ package org.apache.aries.subsystem.core.
 
 import java.security.PrivilegedAction;
 
+import org.osgi.service.subsystem.Subsystem.State;
 import org.osgi.service.subsystem.SubsystemException;
 
+import com.sun.org.apache.bcel.internal.generic.GETSTATIC;
+
 public abstract class AbstractAction implements PrivilegedAction<Object> {
        protected final boolean disableRootCheck;
        protected final BasicSubsystem requestor;
@@ -39,13 +42,23 @@ public abstract class AbstractAction imp
                        throw new IllegalStateException("Detected stale 
subsystem instance: " + s);
        }
        
-       protected void waitForStateChange() {
+       protected void waitForStateChange(State fromState) {
+               long then = System.currentTimeMillis() + 60000;
                synchronized (target) {
-                       try {
-                               target.wait();
-                       }
-                       catch (InterruptedException e) {
-                               throw new SubsystemException(e);
+                       while (target.getState().equals(fromState)) {
+                               // State change has not occurred.
+                               long now = System.currentTimeMillis();
+                               if (then <= now)
+                                       // Wait time has expired.
+                                       throw new SubsystemException("Operation 
timed out while waiting for the subsystem to change state from " + fromState);
+                               try {
+                                       // Wait will never be called with zero 
or a negative
+                                       // argument due to previous check.
+                                       target.wait(then - now);
+                               }
+                               catch (InterruptedException e) {
+                                       throw new SubsystemException(e);
+                               }
                        }
                }
        }

Modified: 
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/StartAction.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/StartAction.java?rev=1466517&r1=1466516&r2=1466517&view=diff
==============================================================================
--- 
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/StartAction.java
 (original)
+++ 
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/StartAction.java
 Wed Apr 10 15:25:01 2013
@@ -68,7 +68,7 @@ public class StartAction extends Abstrac
                        throw new SubsystemException("Cannot stop from state " 
+ state);
                // The following states must wait.
                if (EnumSet.of(State.INSTALLING, State.RESOLVING, 
State.STARTING, State.STOPPING).contains(state)) {
-                       waitForStateChange();
+                       waitForStateChange(state);
                        return new StartAction(instigator, requestor, 
target).run();
                }
                // The following states mean the requested state has already 
been attained.

Modified: 
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/StopAction.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/StopAction.java?rev=1466517&r1=1466516&r2=1466517&view=diff
==============================================================================
--- 
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/StopAction.java
 (original)
+++ 
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/StopAction.java
 Wed Apr 10 15:25:01 2013
@@ -47,7 +47,7 @@ public class StopAction extends Abstract
                else if (EnumSet.of(State.INSTALL_FAILED, State.UNINSTALLING, 
State.UNINSTALLED).contains(state))
                        throw new IllegalStateException("Cannot stop from state 
" + state);
                else if (EnumSet.of(State.INSTALLING, State.RESOLVING, 
State.STARTING, State.STOPPING).contains(state)) {
-                       waitForStateChange();
+                       waitForStateChange(state);
                        return new StopAction(requestor, target, 
disableRootCheck).run();
                }
                target.setState(State.STOPPING);

Modified: 
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/UninstallAction.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/UninstallAction.java?rev=1466517&r1=1466516&r2=1466517&view=diff
==============================================================================
--- 
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/UninstallAction.java
 (original)
+++ 
aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/UninstallAction.java
 Wed Apr 10 15:25:01 2013
@@ -30,7 +30,7 @@ public class UninstallAction extends Abs
                if (EnumSet.of(State.UNINSTALLED).contains(state))
                        return null;
                else if (EnumSet.of(State.INSTALL_FAILED, State.INSTALLING, 
State.RESOLVING, State.STARTING, State.STOPPING, 
State.UNINSTALLING).contains(state)) {
-                       waitForStateChange();
+                       waitForStateChange(state);
                        target.uninstall();
                }
                else if (state.equals(State.ACTIVE)) {


Reply via email to