Fixing Clear Index action, deleting unnecessary Cache class.
authorJan Lahoda <jlahoda@netbeans.org>
Tue, 18 Sep 2012 08:00:09 +0200
changeset 87432f5a10feaf5
parent 873 2f027c57bf15
child 875 d890cdbd7bec
Fixing Clear Index action, deleting unnecessary Cache class.
remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/Cache.java
remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/ClearIndexProperty.java
remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java
     1.1 --- a/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/Cache.java	Mon Sep 17 20:41:23 2012 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,206 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
     1.8 - *
     1.9 - * The contents of this file are subject to the terms of either the GNU
    1.10 - * General Public License Version 2 only ("GPL") or the Common
    1.11 - * Development and Distribution License("CDDL") (collectively, the
    1.12 - * "License"). You may not use this file except in compliance with the
    1.13 - * License. You can obtain a copy of the License at
    1.14 - * http://www.netbeans.org/cddl-gplv2.html
    1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.16 - * specific language governing permissions and limitations under the
    1.17 - * License.  When distributing the software, include this License Header
    1.18 - * Notice in each file and include the License file at
    1.19 - * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.20 - * particular file as subject to the "Classpath" exception as provided
    1.21 - * by Sun in the GPL Version 2 section of the License file that
    1.22 - * accompanied this code. If applicable, add the following below the
    1.23 - * License Header, with the fields enclosed by brackets [] replaced by
    1.24 - * your own identifying information:
    1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
    1.26 - *
    1.27 - * If you wish your version of this file to be governed by only the CDDL
    1.28 - * or only the GPL Version 2, indicate your decision by adding
    1.29 - * "[Contributor] elects to include this software in this distribution
    1.30 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.31 - * single choice of license, a recipient has the option to distribute
    1.32 - * your version of this file under either the CDDL, the GPL Version 2 or
    1.33 - * to extend the choice of license to its licensees as provided above.
    1.34 - * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.35 - * Version 2 license, then the option applies only if the new code is
    1.36 - * made subject to such option by the copyright holder.
    1.37 - *
    1.38 - * Contributor(s):
    1.39 - *
    1.40 - * Portions Copyrighted 1997-2009 Sun Microsystems, Inc.
    1.41 - */
    1.42 -package org.netbeans.modules.jackpot30.hudson;
    1.43 -
    1.44 -import java.io.File;
    1.45 -import java.io.FileInputStream;
    1.46 -import java.io.FileOutputStream;
    1.47 -import java.io.IOException;
    1.48 -import java.io.InputStream;
    1.49 -import java.io.OutputStream;
    1.50 -import java.net.URI;
    1.51 -import java.net.URISyntaxException;
    1.52 -import java.net.URL;
    1.53 -import java.util.HashMap;
    1.54 -import java.util.LinkedList;
    1.55 -import java.util.List;
    1.56 -import java.util.Map;
    1.57 -import java.util.Map.Entry;
    1.58 -import java.util.Properties;
    1.59 -import java.util.logging.Level;
    1.60 -import java.util.logging.Logger;
    1.61 -
    1.62 -/**
    1.63 - *
    1.64 - * @author lahvac
    1.65 - */
    1.66 -public class Cache {
    1.67 -
    1.68 -    public static final int VERSION = 1;
    1.69 -
    1.70 -    private static File standaloneCacheRoot;
    1.71 -    private static Map<String, Cache> name2Cache = new HashMap<String, Cache>();
    1.72 -
    1.73 -    public static Cache findCache(String indexName, int version) {
    1.74 -        Cache cache = name2Cache.get(indexName);
    1.75 -
    1.76 -        if (cache == null) {
    1.77 -            name2Cache.put(indexName, cache = new Cache(indexName, version));
    1.78 -        }
    1.79 -
    1.80 -        return cache;
    1.81 -    }
    1.82 -
    1.83 -    private final String name;
    1.84 -    private final int version;
    1.85 -
    1.86 -    private Cache(String name, int version) {
    1.87 -        this.name = name;
    1.88 -        this.version = version;
    1.89 -    }
    1.90 -    
    1.91 -    public File findCacheRoot(URL sourceRoot) throws IOException {
    1.92 -        try {
    1.93 -            sourceRoot = sourceRoot.toURI().normalize().toURL();
    1.94 -        } catch (URISyntaxException ex) {
    1.95 -            Logger.getLogger(Cache.class.getName()).log(Level.SEVERE, null, ex);
    1.96 -        }
    1.97 -        if (standaloneCacheRoot != null) {
    1.98 -            return getDataFolder(sourceRoot, name, version);
    1.99 -        } else {
   1.100 -            throw new IllegalStateException();
   1.101 -        }
   1.102 -    }
   1.103 -
   1.104 -    public static void setStandaloneCacheRoot(File standaloneCacheRoot) {
   1.105 -        Cache.standaloneCacheRoot = standaloneCacheRoot;
   1.106 -    }
   1.107 -
   1.108 -
   1.109 -    private static Properties segments;
   1.110 -    private static long lastSegmentsTimeStamp = -1;
   1.111 -    private static Map<String, String> invertedSegments;
   1.112 -    private static int index = 0;
   1.113 -
   1.114 -    private static final String SEGMENTS_FILE = "segments";      //NOI18N
   1.115 -    private static final String SLICE_PREFIX = "s";              //NOI18N
   1.116 -
   1.117 -    private static void loadSegments () throws IOException {
   1.118 -        final File folder = standaloneCacheRoot;
   1.119 -        assert folder != null;
   1.120 -        final File segmentsFile = new File(folder, SEGMENTS_FILE);
   1.121 -
   1.122 -        if (lastSegmentsTimeStamp != segmentsFile.lastModified()) {
   1.123 -            lastSegmentsTimeStamp = segmentsFile.lastModified();
   1.124 -            segments = null;
   1.125 -        }
   1.126 -
   1.127 -        if (segments == null) {
   1.128 -            segments = new Properties ();
   1.129 -            invertedSegments = new HashMap<String,String> ();
   1.130 -            if (segmentsFile.canRead()) {
   1.131 -                final InputStream in = new FileInputStream(segmentsFile);
   1.132 -                try {
   1.133 -                    segments.load (in);
   1.134 -                } finally {
   1.135 -                    in.close();
   1.136 -                }
   1.137 -            }
   1.138 -            for (Entry<Object, Object> entry : segments.entrySet()) {
   1.139 -                String segment = (String) entry.getKey();
   1.140 -                String root = (String) entry.getValue();
   1.141 -                invertedSegments.put(root,segment);
   1.142 -                try {
   1.143 -                    index = Math.max (index,Integer.parseInt(segment.substring(SLICE_PREFIX.length())));
   1.144 -                } catch (NumberFormatException nfe) {
   1.145 -                    throw new IllegalStateException(nfe); //TODO: maybe just log the exception?
   1.146 -                }
   1.147 -            }
   1.148 -        }
   1.149 -    }
   1.150 -
   1.151 -    public static synchronized Iterable<? extends String> knownSourceRoots() throws IOException {
   1.152 -        loadSegments();
   1.153 -
   1.154 -        List<String> known = new LinkedList<String>();
   1.155 -        
   1.156 -        for (String segment : segments.stringPropertyNames()) {
   1.157 -            known.add(segment);
   1.158 -        }
   1.159 -
   1.160 -        return known;
   1.161 -    }
   1.162 -
   1.163 -    public static synchronized File sourceRootForKey(String segment) throws IOException {
   1.164 -        loadSegments();
   1.165 -
   1.166 -        try {
   1.167 -            return new File(new File(new URI(segments.getProperty(segment))).getAbsolutePath());
   1.168 -        } catch (URISyntaxException ex) {
   1.169 -            throw new IOException(ex);
   1.170 -        }
   1.171 -    }
   1.172 -
   1.173 -
   1.174 -    private static void storeSegments () throws IOException {
   1.175 -        final File folder = standaloneCacheRoot;
   1.176 -        assert folder != null;
   1.177 -        final File segmentsFile = new File(folder, SEGMENTS_FILE);
   1.178 -        segmentsFile.getParentFile().mkdirs();
   1.179 -        final OutputStream out = new FileOutputStream(segmentsFile);
   1.180 -        try {
   1.181 -            segments.store(out,null);
   1.182 -        } finally {
   1.183 -            out.close();
   1.184 -        }
   1.185 -    }
   1.186 -
   1.187 -    private static synchronized File getDataFolder (final URL root, String name, int version) throws IOException {
   1.188 -        loadSegments ();
   1.189 -        final String rootName = root.toExternalForm();
   1.190 -        String slice = invertedSegments.get (rootName);
   1.191 -        if ( slice == null) {
   1.192 -            slice = SLICE_PREFIX + (++index);
   1.193 -            while (segments.getProperty(slice) != null) {
   1.194 -                slice = SLICE_PREFIX + (++index);
   1.195 -            }
   1.196 -            segments.put (slice,rootName);
   1.197 -            invertedSegments.put(rootName, slice);
   1.198 -            storeSegments ();
   1.199 -        }
   1.200 -        final File folder = standaloneCacheRoot;
   1.201 -        return new File(new File(new File(folder, slice), name), Integer.toString(version));
   1.202 -    }
   1.203 -
   1.204 -    private static synchronized File getCacheFolder () {
   1.205 -        return standaloneCacheRoot;
   1.206 -    }
   1.207 -
   1.208 -
   1.209 -}
     2.1 --- a/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/ClearIndexProperty.java	Mon Sep 17 20:41:23 2012 +0200
     2.2 +++ b/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/ClearIndexProperty.java	Tue Sep 18 08:00:09 2012 +0200
     2.3 @@ -54,6 +54,7 @@
     2.4  import net.sf.json.JSONObject;
     2.5  import org.kohsuke.stapler.StaplerRequest;
     2.6  import org.kohsuke.stapler.StaplerResponse;
     2.7 +import org.netbeans.modules.jackpot30.hudson.IndexingBuilder.DescriptorImpl;
     2.8  
     2.9  /**
    2.10   *
    2.11 @@ -79,7 +80,7 @@
    2.12          }
    2.13  
    2.14          public String getDisplayName() {
    2.15 -            return "Clear Indices";
    2.16 +            return "Clear Job Index";
    2.17          }
    2.18  
    2.19          public String getUrlName() {
    2.20 @@ -87,11 +88,10 @@
    2.21          }
    2.22  
    2.23          public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, InterruptedException {
    2.24 -            String jobName = job.getName();
    2.25 -            Project<?, ?> prj = Hudson.getInstance().getItemByFullName(jobName, Project.class);
    2.26 -            File cacheRoot = Cache.findCache("clear-workspace", 0).findCacheRoot(prj.getSomeWorkspace().toURI().toURL()).getParentFile().getParentFile();
    2.27 +            File cacheDir = ((DescriptorImpl) DescriptorImpl.find(DescriptorImpl.class.getName())).getCacheDir();
    2.28 +            File jobCacheDir = new File(cacheDir, IndexingBuilder.codeNameForJob(job));
    2.29  
    2.30 -            deleteRecursivelly(cacheRoot);
    2.31 +            deleteRecursivelly(jobCacheDir);
    2.32              
    2.33              rsp.forwardToPreviousPage(req);
    2.34          }
     3.1 --- a/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java	Mon Sep 17 20:41:23 2012 +0200
     3.2 +++ b/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java	Tue Sep 18 08:00:09 2012 +0200
     3.3 @@ -50,6 +50,7 @@
     3.4  import hudson.model.Descriptor;
     3.5  import hudson.model.Descriptor.FormException;
     3.6  import hudson.model.Hudson;
     3.7 +import hudson.model.Job;
     3.8  import hudson.remoting.VirtualChannel;
     3.9  import hudson.tasks.Builder;
    3.10  import hudson.util.ArgumentListBuilder;
    3.11 @@ -152,7 +153,7 @@
    3.12  
    3.13          listener.getLogger().println("Running: " + toolName + " on projects: " + res);
    3.14  
    3.15 -        String codeName = build.getParent().getName();
    3.16 +        String codeName = codeNameForJob(build.getParent());
    3.17          ArgumentListBuilder args = new ArgumentListBuilder();
    3.18          FilePath targetZip = build.getBuiltOn().getRootPath().createTempFile(codeName, "zip");
    3.19  
    3.20 @@ -241,6 +242,10 @@
    3.21          }
    3.22      }
    3.23  
    3.24 +    public static String codeNameForJob(Job<?, ?> job) {
    3.25 +        return job.getName();
    3.26 +    }
    3.27 +
    3.28      private static final class RemoteResult implements Serializable {
    3.29          private final Collection<String> foundProjects;
    3.30          private final String root;
    3.31 @@ -256,7 +261,7 @@
    3.32          private File cacheDir;
    3.33  
    3.34          public DescriptorImpl() {
    3.35 -            Cache.setStandaloneCacheRoot(cacheDir = new File(Hudson.getInstance().getRootDir(), "index").getAbsoluteFile());
    3.36 +            cacheDir = new File(Hudson.getInstance().getRootDir(), "index").getAbsoluteFile();
    3.37              load();
    3.38          }
    3.39