diff -r 6497ddae98ca j2eeserver/src/org/netbeans/modules/j2ee/deployment/impl/ServerInstance.java --- a/j2eeserver/src/org/netbeans/modules/j2ee/deployment/impl/ServerInstance.java Thu Jun 21 18:33:27 2012 +0400 +++ b/j2eeserver/src/org/netbeans/modules/j2ee/deployment/impl/ServerInstance.java Fri Jun 22 00:37:55 2012 +0200 @@ -72,6 +72,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Level; import java.util.logging.Logger; import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException; @@ -181,7 +182,7 @@ private final ChangeSupport managerChangeSupport = new ChangeSupport(this); - private volatile static ServerInstance profiledServerInstance; + private static AtomicReference profiledServerInstance = new AtomicReference(); private final DebuggerStateListener debuggerStateListener; @@ -376,7 +377,7 @@ try { int oldState = getServerState(); setServerState(STATE_WAITING); - if (ServerInstance.this == profiledServerInstance) { + if (ServerInstance.this == profiledServerInstance.get()) { updateStateFromProfiler(); return; } @@ -1107,7 +1108,7 @@ throws ServerException { // check whether another server not already running in profile mode // and ask whether it is ok to stop it - ServerInstance tmpProfiledServerInstance = profiledServerInstance; + ServerInstance tmpProfiledServerInstance = profiledServerInstance.get(); if (tmpProfiledServerInstance != null && tmpProfiledServerInstance != this) { String msg = NbBundle.getMessage( ServerInstance.class, @@ -1138,7 +1139,7 @@ try { setServerState(STATE_WAITING); boolean inDebug = isDebuggable(null); - boolean inProfile = profiledServerInstance == this; + boolean inProfile = profiledServerInstance.get() == this; boolean stopped = true; if (inProfile || isReallyRunning() || isSuspended()) { @@ -1166,7 +1167,7 @@ public void stop(ProgressUI ui) throws ServerException { try { setServerState(STATE_WAITING); - if (profiledServerInstance == this || isReallyRunning() || isSuspended()) { + if (profiledServerInstance.get() == this || isReallyRunning() || isSuspended()) { _stop(ui); } debugInfo.clear(); @@ -1488,19 +1489,19 @@ Target target, boolean forceRestart, ProgressUI ui) throws ServerException { - ServerInstance tmpProfiledServerInstance = profiledServerInstance; + ServerInstance tmpProfiledServerInstance = profiledServerInstance.get(); if (tmpProfiledServerInstance == this && !forceRestart) { return; // server is already runnning in profile mode, no need to restart the server } if (tmpProfiledServerInstance != null && tmpProfiledServerInstance != this) { // another server currently running in profiler mode tmpProfiledServerInstance.stop(ui); - profiledServerInstance = null; + profiledServerInstance.compareAndSet(tmpProfiledServerInstance, null); } - if (profiledServerInstance == this || isReallyRunning() || isDebuggable(target)) { + if (profiledServerInstance.get() == this || isReallyRunning() || isDebuggable(target)) { _stop(ui); debugInfo.clear(); - profiledServerInstance = null; + //profiledServerInstance = null; } final Profiler profiler = ServerRegistry.getProfiler(); @@ -1510,6 +1511,22 @@ } final ScheduledExecutorService statusUpdater = Executors.newSingleThreadScheduledExecutor(); + + final StateListener l = new StateListener() { + + @Override + public void stateChanged(int oldState, int newState) { + if (oldState != newState && newState == STATE_STOPPED) { + ServerInstance.this.removeStateListener(this); + statusUpdater.shutdownNow(); + profiledServerInstance.compareAndSet(ServerInstance.this, null); + } + } + }; + + this.addStateListener(l); + profiler.notifyStarting(); + statusUpdater.scheduleAtFixedRate(new Runnable() { @Override @@ -1518,21 +1535,6 @@ } }, 50, 100, TimeUnit.MILLISECONDS); - final StateListener l = new StateListener() { - - @Override - public void stateChanged(int oldState, int newState) { - if (oldState != newState && newState == STATE_STOPPED) { - ServerInstance.this.removeStateListener(this); - statusUpdater.shutdownNow(); - profiledServerInstance = null; - } - } - }; - - this.addStateListener(l); - - profiler.notifyStarting(); ProgressObject po = getStartServer().startProfiling(target); try { boolean completedSuccessfully = ProgressObjectUtil.trackProgressObject(ui, po, DEFAULT_TIMEOUT); @@ -1543,7 +1545,7 @@ String msg = NbBundle.getMessage(ServerInstance.class, "MSG_StartProfileTimeout", getDisplayName()); throw new ServerException(msg); } - profiledServerInstance = this; + profiledServerInstance.set(this); synchronized (this) { managerStartedByIde = true; // coTarget = null; @@ -1573,9 +1575,10 @@ // stopDeploymentManager private void _stop(ProgressUI ui) throws ServerException { // if the server is started in profile mode, deattach profiler first - if (profiledServerInstance == this) { + if (profiledServerInstance.get() == this) { shutdownProfiler(ui); - profiledServerInstance = null; + // TODO logger + profiledServerInstance.compareAndSet(this, null); } synchronized (this) { // if the server is suspended, the debug session has to be terminated first