Ability to upload index from an external source.
authorJan Lahoda <jlahoda@netbeans.org>
Sat, 10 Dec 2011 13:21:17 +0100
changeset 7147bb1a471ce94
parent 713 41876aab0159
child 715 d26cba8e1283
Ability to upload index from an external source.
remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java
remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/UploadIndex.java
     1.1 --- a/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java	Tue Nov 15 13:28:23 2011 +0100
     1.2 +++ b/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java	Sat Dec 10 13:21:17 2011 +0100
     1.3 @@ -55,10 +55,10 @@
     1.4  import java.io.File;
     1.5  import java.io.FileOutputStream;
     1.6  import java.io.IOException;
     1.7 +import java.io.InputStream;
     1.8  import java.io.OutputStreamWriter;
     1.9  import java.io.Serializable;
    1.10  import java.io.Writer;
    1.11 -import java.net.URL;
    1.12  import java.util.Arrays;
    1.13  import java.util.Collection;
    1.14  import java.util.Comparator;
    1.15 @@ -154,21 +154,14 @@
    1.16  
    1.17          indexer.join();
    1.18  
    1.19 -        File oldCacheDir = new File(cacheDir, codeName + ".old");
    1.20 -        File segCacheDir = new File(cacheDir, codeName);
    1.21 -        File newCacheDir = new File(cacheDir, codeName + ".new");
    1.22 +        InputStream ins = targetZip.read();
    1.23  
    1.24 -        targetZip.unzip(new FilePath(newCacheDir));
    1.25 -        targetZip.delete();
    1.26 -
    1.27 -        segCacheDir.renameTo(oldCacheDir);
    1.28 -
    1.29 -        new File(newCacheDir, codeName).renameTo(segCacheDir);
    1.30 -
    1.31 -        new URL("http://localhost:9998/index/internal/indexUpdated").openStream().close();
    1.32 -
    1.33 -        new FilePath(newCacheDir).deleteRecursive();
    1.34 -        new FilePath(oldCacheDir).deleteRecursive();
    1.35 +        try {
    1.36 +            UploadIndex.uploadIndex(codeName, ins);
    1.37 +        } finally {
    1.38 +            ins.close();
    1.39 +            targetZip.delete();
    1.40 +        }
    1.41  
    1.42          return true;
    1.43      }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/UploadIndex.java	Sat Dec 10 13:21:17 2011 +0100
     2.3 @@ -0,0 +1,128 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2011 Sun Microsystems, Inc. All rights reserved.
     2.8 + *
     2.9 + * The contents of this file are subject to the terms of either the GNU
    2.10 + * General Public License Version 2 only ("GPL") or the Common
    2.11 + * Development and Distribution License("CDDL") (collectively, the
    2.12 + * "License"). You may not use this file except in compliance with the
    2.13 + * License. You can obtain a copy of the License at
    2.14 + * http://www.netbeans.org/cddl-gplv2.html
    2.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.16 + * specific language governing permissions and limitations under the
    2.17 + * License.  When distributing the software, include this License Header
    2.18 + * Notice in each file and include the License file at
    2.19 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    2.20 + * particular file as subject to the "Classpath" exception as provided
    2.21 + * by Sun in the GPL Version 2 section of the License file that
    2.22 + * accompanied this code. If applicable, add the following below the
    2.23 + * License Header, with the fields enclosed by brackets [] replaced by
    2.24 + * your own identifying information:
    2.25 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.26 + *
    2.27 + * If you wish your version of this file to be governed by only the CDDL
    2.28 + * or only the GPL Version 2, indicate your decision by adding
    2.29 + * "[Contributor] elects to include this software in this distribution
    2.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.31 + * single choice of license, a recipient has the option to distribute
    2.32 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.33 + * to extend the choice of license to its licensees as provided above.
    2.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.35 + * Version 2 license, then the option applies only if the new code is
    2.36 + * made subject to such option by the copyright holder.
    2.37 + *
    2.38 + * Contributor(s):
    2.39 + *
    2.40 + * Portions Copyrighted 2011 Sun Microsystems, Inc.
    2.41 + */
    2.42 +
    2.43 +package org.netbeans.modules.jackpot30.hudson;
    2.44 +
    2.45 +import hudson.Extension;
    2.46 +import hudson.FilePath;
    2.47 +import hudson.cli.CLICommand;
    2.48 +import hudson.model.RootAction;
    2.49 +import java.io.File;
    2.50 +import java.io.IOException;
    2.51 +import java.io.InputStream;
    2.52 +import java.net.URL;
    2.53 +import javax.servlet.ServletException;
    2.54 +import org.apache.commons.fileupload.FileItem;
    2.55 +import org.kohsuke.args4j.Argument;
    2.56 +import org.kohsuke.stapler.StaplerRequest;
    2.57 +import org.kohsuke.stapler.StaplerResponse;
    2.58 +import org.netbeans.modules.jackpot30.hudson.IndexingBuilder.DescriptorImpl;
    2.59 +
    2.60 +/**
    2.61 + *
    2.62 + * @author lahvac
    2.63 + */
    2.64 +public class UploadIndex {
    2.65 +
    2.66 +    public static void uploadIndex(String codeName, InputStream ins) throws IOException, InterruptedException {
    2.67 +        File cacheDir = ((DescriptorImpl) DescriptorImpl.find(DescriptorImpl.class.getName())).getCacheDir();
    2.68 +        File oldCacheDir = new File(cacheDir, codeName + ".old");
    2.69 +        File segCacheDir = new File(cacheDir, codeName);
    2.70 +        File newCacheDir = new File(cacheDir, codeName + ".new");
    2.71 +
    2.72 +        new FilePath(newCacheDir).unzipFrom(ins);
    2.73 +
    2.74 +        segCacheDir.renameTo(oldCacheDir);
    2.75 +
    2.76 +        new File(newCacheDir, codeName).renameTo(segCacheDir);
    2.77 +
    2.78 +        new URL("http://localhost:9998/index/internal/indexUpdated").openStream().close();
    2.79 +
    2.80 +        new FilePath(newCacheDir).deleteRecursive();
    2.81 +        new FilePath(oldCacheDir).deleteRecursive();
    2.82 +    }
    2.83 +
    2.84 +    @Extension
    2.85 +    public static final class UploadIndexCommand extends CLICommand {
    2.86 +
    2.87 +        @Argument(metaVar="<jobname>", usage="Job name")
    2.88 +        public String job;
    2.89 +
    2.90 +        @Override
    2.91 +        public String getShortDescription() {
    2.92 +            return "Upload indexing zip";
    2.93 +        }
    2.94 +
    2.95 +        @Override
    2.96 +        protected int run() throws Exception {
    2.97 +            uploadIndex(job, stdin);
    2.98 +            stdin.close();
    2.99 +            return 0;
   2.100 +        }
   2.101 +
   2.102 +    }
   2.103 +
   2.104 +    @Extension
   2.105 +    public static class IndexGlobalAction implements RootAction {
   2.106 +
   2.107 +        public String getIconFileName() {
   2.108 +            return null;
   2.109 +        }
   2.110 +
   2.111 +        public String getDisplayName() {
   2.112 +            return "Jackpot 3.0 Index Upload";
   2.113 +        }
   2.114 +
   2.115 +        public String getUrlName() {
   2.116 +            return "index-upload";
   2.117 +        }
   2.118 +
   2.119 +        public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, InterruptedException {
   2.120 +            FileItem indexFile = req.getFileItem("index");
   2.121 +            String codeName = req.getHeader("codeName");
   2.122 +            InputStream ins = indexFile.getInputStream();
   2.123 +
   2.124 +            try {
   2.125 +                uploadIndex(codeName, ins);
   2.126 +            } finally {
   2.127 +                ins.close();
   2.128 +            }
   2.129 +        }
   2.130 +    }
   2.131 +}