geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r1133656 - in /geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core: GeronimoServerBehaviourDelegate.java SynchronizeProjectOnServerTask.java
Date Thu, 09 Jun 2011 05:13:33 GMT
Author: gawor
Date: Thu Jun  9 05:13:32 2011
New Revision: 1133656

URL: http://svn.apache.org/viewvc?rev=1133656&view=rev
Log:
GERONIMODEVTOOLS-707: Ensure that SynchronizeProjectOnServerTask does not run while publishing

Modified:
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/GeronimoServerBehaviourDelegate.java
    geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/SynchronizeProjectOnServerTask.java

Modified: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/GeronimoServerBehaviourDelegate.java
URL: http://svn.apache.org/viewvc/geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/GeronimoServerBehaviourDelegate.java?rev=1133656&r1=1133655&r2=1133656&view=diff
==============================================================================
--- geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/GeronimoServerBehaviourDelegate.java
(original)
+++ geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/GeronimoServerBehaviourDelegate.java
Thu Jun  9 05:13:32 2011
@@ -33,6 +33,9 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Timer;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 
 import javax.enterprise.deploy.spi.Target;
 import javax.enterprise.deploy.spi.TargetModuleID;
@@ -112,6 +115,8 @@ abstract public class GeronimoServerBeha
     abstract protected ClassLoader getContextClassLoader();
 
     private PublishStateListener publishStateListener;
+    
+    private Lock publishLock = new ReentrantLock();
 
     /*
      * (non-Javadoc)
@@ -518,12 +523,37 @@ abstract public class GeronimoServerBeha
         Trace.tracePoint("Exit ", Activator.traceCore, "GeronimoServerBehaviourDelegate.publishModule");
     }
 
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.eclipse.wst.server.core.model.ServerBehaviourDelegate#publishFinish(org.eclipse.core.runtime.IProgressMonitor)
-     */
+    public Lock getPublishLock() {
+        return publishLock;
+    }
+    
+    @Override
+    public void publishStart(IProgressMonitor monitor) throws CoreException {
+        doPublishStart(monitor);
+        try {
+            if (!publishLock.tryLock(30, TimeUnit.SECONDS)) {
+                throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unable
to obtain publish lock"));
+            }
+        } catch (InterruptedException e) {
+            // ignore
+        }
+    }
+    
+    private void doPublishStart(IProgressMonitor monitor) throws CoreException {
+        Trace.tracePoint("Entry", Activator.traceCore, "GeronimoServerBehaviourDelegate.publishStart",
monitor);
+        Trace.tracePoint("Exit ", Activator.traceCore, "GeronimoServerBehaviourDelegate.publishStart");
+    }
+    
+    @Override
     public void publishFinish(IProgressMonitor monitor) throws CoreException {
+        try {
+            doPublishFinish(monitor);
+        } finally {
+            publishLock.unlock();
+        }
+    }
+    
+    private void doPublishFinish(IProgressMonitor monitor) throws CoreException  {
         Trace.tracePoint("Entry", Activator.traceCore, "GeronimoServerBehaviourDelegate.publishFinish",
monitor);
 
         IModule[] modules = this.getServer().getModules();
@@ -544,7 +574,7 @@ abstract public class GeronimoServerBeha
         }
 
         GeronimoConnectionFactory.getInstance().destroy(getServer());
-
+        
         Trace.tracePoint("Exit ", Activator.traceCore, "GeronimoServerBehaviourDelegate.publishFinish");
     }
 

Modified: geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/SynchronizeProjectOnServerTask.java
URL: http://svn.apache.org/viewvc/geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/SynchronizeProjectOnServerTask.java?rev=1133656&r1=1133655&r2=1133656&view=diff
==============================================================================
--- geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/SynchronizeProjectOnServerTask.java
(original)
+++ geronimo/devtools/eclipse-plugin/trunk/plugins/org.apache.geronimo.st.v30.core/src/main/java/org/apache/geronimo/st/v30/core/SynchronizeProjectOnServerTask.java
Thu Jun  9 05:13:32 2011
@@ -22,16 +22,17 @@ package org.apache.geronimo.st.v30.core;
 
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Iterator;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 import java.util.TimerTask;
-import java.util.TreeSet;
+import java.util.concurrent.locks.Lock;
 
 import javax.enterprise.deploy.spi.DeploymentManager;
 import javax.enterprise.deploy.spi.Target;
 import javax.enterprise.deploy.spi.TargetModuleID;
 
-import org.apache.geronimo.st.v30.core.GeronimoServerBehaviourDelegate;
 import org.apache.geronimo.st.v30.core.commands.DeploymentCommandFactory;
 import org.apache.geronimo.st.v30.core.internal.Trace;
 import org.eclipse.core.resources.IProject;
@@ -46,135 +47,113 @@ import org.eclipse.wst.server.core.Serve
 
 public class SynchronizeProjectOnServerTask extends TimerTask {
 
-    private IGeronimoServerBehavior delegate;
+    private GeronimoServerBehaviourDelegate delegate;
 
     private IServer server;
+    
+    private Lock publishLock;
 
-    public SynchronizeProjectOnServerTask(IGeronimoServerBehavior delegate, IServer server)
{
-        super();
+    public SynchronizeProjectOnServerTask(GeronimoServerBehaviourDelegate delegate, IServer
server) {
         this.delegate = delegate;
         this.server = server;
+        this.publishLock = delegate.getPublishLock();
     }
 
     @Override
     public void run() {
-
         Trace.tracePoint("Entry ", Activator.traceCore, "SynchronizeProjectOnServerTask.run");
-        
-        if (canUpdateState()) {            
-           
+
+        if (canUpdateState() && publishLock.tryLock()) {
             try {
-                HashMap projectsOnServer = ModuleArtifactMapper.getInstance().getServerArtifactsMap(server);
-                
-                if(projectsOnServer!=null && projectsOnServer.size() != 0) {
-                    
-                    synchronized (projectsOnServer) {                        
-                        Iterator projectsIterator = projectsOnServer.keySet().iterator();
-                        TreeSet<String> removedConfigIds = new TreeSet<String>();
+                HashMap<String, String> projectsOnServer = ModuleArtifactMapper.getInstance().getServerArtifactsMap(server);
+
+                if (projectsOnServer != null && !projectsOnServer.isEmpty()) {
+                    synchronized (projectsOnServer) {
                         List<IModule> removedModules = new ArrayList<IModule>();
-                        
+
                         DeploymentManager dm = DeploymentCommandFactory.getDeploymentManager(server);
                         Target[] targets = dm.getTargets();
-                        TargetModuleID[] ids = dm.getAvailableModules(null, targets);
-                        
+
                         TargetModuleID[] runningIds = dm.getRunningModules(null, targets);
+                        Set<String> runningConfigIds = createSet(runningIds);
+
                         TargetModuleID[] nonRunningIds = dm.getNonRunningModules(null, targets);
-                        TreeSet<String> runningConfigIds = new TreeSet<String>();
-                        TreeSet<String> nonRunningConfigIds = new TreeSet<String>();
                       
-                        for (TargetModuleID running : runningIds) {
-                            runningConfigIds.add(running.getModuleID());
-                        }
-                        for (TargetModuleID nonRunning : nonRunningIds) {
-                            nonRunningConfigIds.add(nonRunning.getModuleID());
-                        }
-                        
-                        for ( ; projectsIterator.hasNext(); ) {
-                            String projectName = (String) projectsIterator.next();
-                            String configID = (String) projectsOnServer.get(projectName);
+                        Set<String> nonRunningConfigIds = createSet(nonRunningIds);
+
+                        for (Map.Entry<String, String> entry : projectsOnServer.entrySet())
{
+                            String projectName = entry.getKey();
+                            String configID = entry.getValue();
+
                             IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
                             IModule[] modules = GeronimoUtils.getModules(project);
 
-                            if (!isInstalledModule(ids, configID)) {
-                                removedConfigIds.add(configID);
+                            if (runningConfigIds.contains(configID)) {
+                                delegate.setModulesState(modules, IServer.STATE_STARTED);
+                            } else if (nonRunningConfigIds.contains(configID)) {
+                                delegate.setModulesState(modules, IServer.STATE_STOPPED);
+                            } else {
+                                // assume it's not installed
                                 for (IModule module : modules) {
                                     removedModules.add(module);
                                 }
-                            } else { 
-                                if (runningConfigIds.contains(configID)) {
-                                    setModuleState(modules, IServer.STATE_STARTED);
-                                } else if (nonRunningConfigIds.contains(configID)){
-                                    setModuleState(modules, IServer.STATE_STOPPED);
-                                }
                             }
                         }
-                        
-                        if (removedConfigIds.size() != 0 && removedModules.size()
!= 0) {
-                            IModule[] removedModules2 = new IModule[removedModules.size()];
-                            removedModules.toArray(removedModules2);
-                            removeModules(removedModules2);
+
+                        if (!removedModules.isEmpty()) {
+                            removeModules(removedModules);
                         } else {
                             Trace.trace(Trace.INFO, "SynchronizeProjectOnServerTask: no configuration
is removed outside eclipse on server: " + this.server.getId(), Activator.traceCore);
                         }
-                        
-                    }                    
-                    
+
+                    }
+
                 } else {
                     Trace.trace(Trace.INFO, "SynchronizeProjectOnServerTask: no project has
been deployed on server: " + this.server.getId(), Activator.traceCore);
-                }                
-                
+                }
+
             } catch (Exception e) {
-                e.printStackTrace();
+                Trace.trace(Trace.WARNING, "Error in SynchronizeProjectOnServerTask.run",
e, Activator.logCore);
+            } finally {
+                publishLock.unlock();
             }
         }
-        
-        Trace.tracePoint("Exit ", Activator.traceCore, "SynchronizeProjectOnServerTask.run");
-    }
 
-    private void setModuleState(IModule[] modules, int state) {
-        GeronimoServerBehaviourDelegate d = (GeronimoServerBehaviourDelegate) this.delegate;
-        d.setModulesState(modules, state);        
+        Trace.tracePoint("Exit ", Activator.traceCore, "SynchronizeProjectOnServerTask.run");
     }
 
-    private boolean isInstalledModule(TargetModuleID[] ids, String configId) {
-        
-        if(ids == null) {           
-            return false;
+    private static Set<String> createSet(TargetModuleID[] ids) {
+        Set<String> moduleIds = new HashSet<String>();
+        for (TargetModuleID id : ids) {
+            moduleIds.add(id.getModuleID());
         }
-        
-        if (ids != null) {
-            for (int i = 0; i < ids.length; i++) {
-                if (ids[i].getModuleID().equals(configId)) {                    
-                    return true;
-                }
-            }
-        }
-            
-        return false;
+        return moduleIds;
     }
-      
-    private void removeModules(IModule[] remove) {
-        
-        Trace.tracePoint("Entry ", Activator.traceCore, "SynchronizeProjectOnServerTask.removeModules",
remove);
-        
+
+    private void removeModules(List<IModule> removedModules) {
+        Trace.tracePoint("Entry ", Activator.traceCore, "SynchronizeProjectOnServerTask.removeModules",
removedModules);
+
+        IModule[] remove = new IModule[removedModules.size()];
+        removedModules.toArray(remove);
+
         IServerWorkingCopy wc = server.createWorkingCopy();
-        IProgressMonitor monitor = new NullProgressMonitor(); 
-        
+        IProgressMonitor monitor = new NullProgressMonitor();
+
         try {
             wc.modifyModules(null, remove, monitor);
-            server = wc.save(true, monitor); 
+            server = wc.save(true, monitor);
 
             if (remove != null) {
                 for (IModule module : remove) {
                     ModuleArtifactMapper.getInstance().removeArtifactBundleEntry(this.server,
module);
                 }
-            }           
+            }
         } catch (CoreException e) {
             Trace.trace(Trace.WARNING, "Could not remove module in SynchronizeProjectOnServerTask",
e, Activator.logCore);
         }
-        
+
         Trace.tracePoint("Exit ", Activator.traceCore, "SynchronizeProjectOnServerTask.removeModules");
     }
-    
+
     private boolean canUpdateState() {
         if (server.getServerState() != IServer.STATE_STARTED) {
             return false;
@@ -188,14 +167,13 @@ public class SynchronizeProjectOnServerT
             if (gs != null && !this.server.getId().equals(server.getId())) {
                 if (isSameConnectionURL(gs, thisServer)) {
                     if (!isSameRuntimeLocation(server) && server.getServerState()
!= IServer.STATE_STOPPED) {
-                        Trace.trace(Trace.WARNING, server.getId()
-                                + " Cannot update server state.  URL conflict between multiple
servers.", Activator.logCore);
+                        Trace.trace(Trace.WARNING, server.getId() + " Cannot update server
state.  URL conflict between multiple servers.", Activator.logCore);
                         return false;
                     }
                 }
             }
         }
-        
+
         return true;
     }
 
@@ -207,4 +185,4 @@ public class SynchronizeProjectOnServerT
         return server.getJMXServiceURL().equals(thisServer.getJMXServiceURL());
     }
 
-}
\ No newline at end of file
+}



Mime
View raw message