From faee4048fe006f9b3814f2ec8d6aee3f694cd908 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Sat, 11 Aug 2007 20:03:42 +0000 Subject: [PATCH] converted anonmous inner classes to static inner classes to avoid implicit parent pointer, which affects serialization during remoting. (#702) git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@4209 71c3de6d-444a-0410-be80-ed276b4c234a --- .../main/java/hudson/scm/SubversionSCM.java | 226 ++++++++++-------- 1 file changed, 131 insertions(+), 95 deletions(-) diff --git a/core/src/main/java/hudson/scm/SubversionSCM.java b/core/src/main/java/hudson/scm/SubversionSCM.java index 89d672415a..db0d500562 100644 --- a/core/src/main/java/hudson/scm/SubversionSCM.java +++ b/core/src/main/java/hudson/scm/SubversionSCM.java @@ -76,6 +76,11 @@ import java.util.regex.Pattern; /** * Subversion SCM. * + *

+ * Because this instance refers to some other classes that are not necessarily + * Java serializable (like {@link #browser}), remotable {@link FileCallable}s all + * need to be declared as static inner classes. + * * @author Kohsuke Kawaguchi */ public class SubversionSCM extends SCM implements Serializable { @@ -291,7 +296,7 @@ public class SubversionSCM extends SCM implements Serializable { // write out the revision file PrintWriter w = new PrintWriter(new FileOutputStream(getRevisionFile(build))); try { - Map revMap = buildRevisionMap(workspace,listener,externals); + Map revMap = workspace.act(new BuildRevisionMapTask(this, listener, externals)); for (Entry e : revMap.entrySet()) { w.println( e.getKey() +'/'+ e.getValue().revision ); } @@ -324,55 +329,70 @@ public class SubversionSCM extends SCM implements Serializable { * if the operation failed. Otherwise the set of local workspace paths * (relative to the workspace root) that has loaded due to svn:external. */ - private List checkout(final Date timestamp, FilePath workspace, final TaskListener listener) throws IOException, InterruptedException { - final ISVNAuthenticationProvider authProvider = getDescriptor().createAuthenticationProvider(); + private List checkout(Date timestamp, FilePath workspace, TaskListener listener) throws IOException, InterruptedException { + boolean isUpdatable = useUpdate && workspace.act(new IsUpdatableTask(this, listener)); + return workspace.act(new CheckOutTask(this, timestamp, isUpdatable, listener)); + } + private static class CheckOutTask implements FileCallable> { + private final ISVNAuthenticationProvider authProvider; + private final Date timestamp; // true to "svn update", false to "svn checkout". - final boolean update = useUpdate && isUpdatable(workspace, listener); + private final boolean update; + private final TaskListener listener; + private final ModuleLocation[] locations; - return workspace.act(new FileCallable>() { - public List invoke(File ws, VirtualChannel channel) throws IOException { - SVNUpdateClient svnuc = createSvnClientManager(authProvider).getUpdateClient(); - List externals = new ArrayList(); // store discovered externals to here - SVNRevision revision = SVNRevision.create(timestamp); - if(update) { + public CheckOutTask(SubversionSCM parent, Date timestamp, boolean update, TaskListener listener) { + this.authProvider = parent.getDescriptor().createAuthenticationProvider(); + this.timestamp = timestamp; + this.update = update; + this.listener = listener; + this.locations = parent.getLocations(); + } - for (ModuleLocation l : getLocations()) { - try { - listener.getLogger().println("Updating "+ l.remote); + public List invoke(File ws, VirtualChannel channel) throws IOException { + SVNUpdateClient svnuc = createSvnClientManager(authProvider).getUpdateClient(); + List externals = new ArrayList(); // store discovered externals to here + SVNRevision revision = SVNRevision.create(timestamp); + if(update) { - svnuc.setEventHandler(new SubversionUpdateEventHandler(listener, externals, l.local)); - svnuc.doUpdate(new File(ws, l.local).getCanonicalFile(), revision, true); + for (ModuleLocation l : locations) { + try { + listener.getLogger().println("Updating "+ l.remote); - } catch (SVNException e) { - e.printStackTrace(listener.error("Failed to update "+l.remote)); - // trouble-shooting probe for #591 - if(e.getErrorMessage().getErrorCode()==SVNErrorCode.WC_NOT_LOCKED) { - listener.getLogger().println("Polled jobs are "+SCMTrigger.DESCRIPTOR.getItemsBeingPolled()); - } - return null; - } - } - } else { - Util.deleteContentsRecursive(ws); - - for (ModuleLocation l : getLocations()) { - try { - SVNURL url = SVNURL.parseURIEncoded(l.remote); - listener.getLogger().println("Checking out "+url); - - svnuc.setEventHandler(new SubversionUpdateEventHandler(listener, externals, l.local)); - svnuc.doCheckout(url, new File(ws, l.local).getCanonicalFile(), SVNRevision.HEAD, revision, true); - - } catch (SVNException e) { - e.printStackTrace(listener.error("Failed to check out "+l.remote)); - return null; + svnuc.setEventHandler(new SubversionUpdateEventHandler(listener, externals, l.local)); + svnuc.doUpdate(new File(ws, l.local).getCanonicalFile(), revision, true); + + } catch (SVNException e) { + e.printStackTrace(listener.error("Failed to update "+l.remote)); + // trouble-shooting probe for #591 + if(e.getErrorMessage().getErrorCode()== SVNErrorCode.WC_NOT_LOCKED) { + listener.getLogger().println("Polled jobs are "+ SCMTrigger.DESCRIPTOR.getItemsBeingPolled()); } + return null; + } + } + } else { + Util.deleteContentsRecursive(ws); + + for (ModuleLocation l : locations) { + try { + SVNURL url = SVNURL.parseURIEncoded(l.remote); + listener.getLogger().println("Checking out "+url); + + svnuc.setEventHandler(new SubversionUpdateEventHandler(listener, externals, l.local)); + svnuc.doCheckout(url, new File(ws, l.local).getCanonicalFile(), SVNRevision.HEAD, revision, true); + + } catch (SVNException e) { + e.printStackTrace(listener.error("Failed to check out "+l.remote)); + return null; } } - return externals; } - }); + return externals; + } + + private static final long serialVersionUID = 1L; } /** @@ -428,9 +448,8 @@ public class SubversionSCM extends SCM implements Serializable { SvnInfo svnInfo = (SvnInfo) o; if (revision != svnInfo.revision) return false; - if (!url.equals(svnInfo.url)) return false; + return url.equals(svnInfo.url); - return true; } public int hashCode() { @@ -453,7 +472,7 @@ public class SubversionSCM extends SCM implements Serializable { * @param workspace * The target to run "svn info". */ - private SVNInfo parseSvnInfo(File workspace, ISVNAuthenticationProvider authProvider) throws SVNException { + private static SVNInfo parseSvnInfo(File workspace, ISVNAuthenticationProvider authProvider) throws SVNException { SVNWCClient svnWc = createSvnClientManager(authProvider).getWCClient(); return svnWc.doInfo(workspace,SVNRevision.WORKING); } @@ -476,35 +495,45 @@ public class SubversionSCM extends SCM implements Serializable { * @return * null if the parsing somehow fails. Otherwise a map from the repository URL to revisions. */ - private Map buildRevisionMap(FilePath workspace, final TaskListener listener, final List externals) throws IOException, InterruptedException { - final ISVNAuthenticationProvider authProvider = getDescriptor().createAuthenticationProvider(); - return workspace.act(new FileCallable>() { - public Map invoke(File ws, VirtualChannel channel) throws IOException { - Map revisions = new HashMap(); + private static class BuildRevisionMapTask implements FileCallable> { + private final ISVNAuthenticationProvider authProvider; + private final TaskListener listener; + private final List externals; + private final ModuleLocation[] locations; - SVNWCClient svnWc = createSvnClientManager(authProvider).getWCClient(); - // invoke the "svn info" - for( ModuleLocation module : getLocations() ) { - try { - SvnInfo info = new SvnInfo(svnWc.doInfo(new File(ws,module.local),SVNRevision.WORKING)); - revisions.put(info.url,info); - } catch (SVNException e) { - e.printStackTrace(listener.error("Failed to parse svn info for "+module.remote)); - } + public BuildRevisionMapTask(SubversionSCM parent, TaskListener listener, List externals) { + this.authProvider = parent.getDescriptor().createAuthenticationProvider(); + this.listener = listener; + this.externals = externals; + this.locations = parent.getLocations(); + } + + public Map invoke(File ws, VirtualChannel channel) throws IOException { + Map revisions = new HashMap(); + + SVNWCClient svnWc = createSvnClientManager(authProvider).getWCClient(); + // invoke the "svn info" + for( ModuleLocation module : locations ) { + try { + SvnInfo info = new SvnInfo(svnWc.doInfo(new File(ws,module.local), SVNRevision.WORKING)); + revisions.put(info.url,info); + } catch (SVNException e) { + e.printStackTrace(listener.error("Failed to parse svn info for "+module.remote)); } - for(String local : externals){ - try { - SvnInfo info = new SvnInfo(svnWc.doInfo(new File(ws, local),SVNRevision.WORKING)); - revisions.put(info.url,info); - } catch (SVNException e) { - e.printStackTrace(listener.error("Failed to parse svn info for external "+local)); - } - - } - - return revisions; } - }); + for(String local : externals){ + try { + SvnInfo info = new SvnInfo(svnWc.doInfo(new File(ws, local),SVNRevision.WORKING)); + revisions.put(info.url,info); + } catch (SVNException e) { + e.printStackTrace(listener.error("Failed to parse svn info for external "+local)); + } + + } + + return revisions; + } + private static final long serialVersionUID = 1L; } /** @@ -524,36 +553,43 @@ public class SubversionSCM extends SCM implements Serializable { /** * Returns true if we can use "svn update" instead of "svn checkout" */ - private boolean isUpdatable(FilePath workspace, final TaskListener listener) throws IOException, InterruptedException { - final ISVNAuthenticationProvider authProvider = getDescriptor().createAuthenticationProvider(); + private static class IsUpdatableTask implements FileCallable { + private final TaskListener listener; + private final ISVNAuthenticationProvider authProvider; + private final ModuleLocation[] locations; - return workspace.act(new FileCallable() { - public Boolean invoke(File ws, VirtualChannel channel) throws IOException { - for (ModuleLocation l : getLocations()) { - String url = l.remote; - String moduleName = l.local; - File module = new File(ws,moduleName).getCanonicalFile(); // canonicalize to remove ".." and ".". See #474 + IsUpdatableTask(SubversionSCM parent,TaskListener listener) { + this.authProvider = parent.getDescriptor().createAuthenticationProvider(); + this.listener = listener; + this.locations = parent.getLocations(); + } - if(!module.exists()) { - listener.getLogger().println("Checking out a fresh workspace because "+module+" doesn't exist"); - return false; - } + public Boolean invoke(File ws, VirtualChannel channel) throws IOException { + for (ModuleLocation l : locations) { + String url = l.remote; + String moduleName = l.local; + File module = new File(ws,moduleName).getCanonicalFile(); // canonicalize to remove ".." and ".". See #474 - try { - SvnInfo svnInfo = new SvnInfo(parseSvnInfo(module,authProvider)); - if(!svnInfo.url.equals(url)) { - listener.getLogger().println("Checking out a fresh workspace because the workspace is not "+url); - return false; - } - } catch (SVNException e) { - listener.getLogger().println("Checking out a fresh workspace because Hudson failed to detect the current workspace "+module); - e.printStackTrace(listener.error(e.getMessage())); - return false; - } + if(!module.exists()) { + listener.getLogger().println("Checking out a fresh workspace because "+module+" doesn't exist"); + return false; + } + + try { + SvnInfo svnInfo = new SvnInfo(parseSvnInfo(module, authProvider)); + if(!svnInfo.url.equals(url)) { + listener.getLogger().println("Checking out a fresh workspace because the workspace is not "+url); + return false; + } + } catch (SVNException e) { + listener.getLogger().println("Checking out a fresh workspace because Hudson failed to detect the current workspace "+module); + e.printStackTrace(listener.error(e.getMessage())); + return false; } - return true; } - }); + return true; + } + private static final long serialVersionUID = 1L; } public boolean pollChanges(AbstractProject project, Launcher launcher, FilePath workspace, TaskListener listener) throws IOException, InterruptedException { @@ -562,7 +598,7 @@ public class SubversionSCM extends SCM implements Serializable { List externals = parseExternalsFile(project); // current workspace revision - Map wsRev = buildRevisionMap(workspace,listener,externals); + Map wsRev = workspace.act(new BuildRevisionMapTask(this, listener, externals)); ISVNAuthenticationProvider authProvider = getDescriptor().createAuthenticationProvider();