PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/cli/InstallPluginCommand.java

http://github.com/hudson/hudson
Java | 138 lines | 93 code | 12 blank | 33 comment | 10 complexity | c1670ec4d86a9f3775c2d5bfd007447b MD5 | raw file
  1/*
  2 * The MIT License
  3 *
  4 * Copyright (c) 2004-2010, Sun Microsystems, Inc.
  5 *
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 *
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 *
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24package hudson.cli;
 25
 26import hudson.Extension;
 27import hudson.FilePath;
 28import hudson.model.Hudson;
 29import hudson.model.UpdateSite;
 30import hudson.model.UpdateSite.Data;
 31import hudson.util.EditDistance;
 32import org.kohsuke.args4j.Argument;
 33import org.kohsuke.args4j.Option;
 34
 35import java.io.File;
 36import java.net.URL;
 37import java.net.MalformedURLException;
 38import java.util.HashSet;
 39import java.util.List;
 40import java.util.ArrayList;
 41import java.util.Set;
 42
 43/**
 44 * Installs a plugin either from a file, an URL, or from update center.
 45 * 
 46 * @author Kohsuke Kawaguchi
 47 * @since 1.331
 48 */
 49@Extension
 50public class InstallPluginCommand extends CLICommand {
 51    public String getShortDescription() {
 52        return "Installs a plugin either from a file, an URL, or from update center";
 53    }
 54
 55    @Argument(metaVar="SOURCE",required=true,usage="If this points to a local file, that file will be installed. " +
 56            "If this is an URL, Hudson downloads the URL and installs that as a plugin." +
 57            "Otherwise the name is assumed to be the short name of the plugin in the existing update center (like \"findbugs\")," +
 58            "and the plugin will be installed from the update center")
 59    public List<String> sources = new ArrayList<String>();
 60
 61    @Option(name="-name",usage="If specified, the plugin will be installed as this short name (whereas normally the name is inferred from the source name automatically.)")
 62    public String name;
 63
 64    @Option(name="-restart",usage="Restart Hudson upon successful installation")
 65    public boolean restart;
 66
 67    protected int run() throws Exception {
 68        Hudson h = Hudson.getInstance();
 69        h.checkPermission(Hudson.ADMINISTER);
 70
 71        for (String source : sources) {
 72            // is this a file?
 73            FilePath f = new FilePath(channel, source);
 74            if (f.exists()) {
 75                stdout.println(Messages.InstallPluginCommand_InstallingPluginFromLocalFile(f));
 76                if (name==null)
 77                    name = f.getBaseName();
 78                f.copyTo(getTargetFile());
 79                continue;
 80            }
 81
 82            // is this an URL?
 83            try {
 84                URL u = new URL(source);
 85                stdout.println(Messages.InstallPluginCommand_InstallingPluginFromUrl(u));
 86                if (name==null) {
 87                    name = u.getPath();
 88                    name = name.substring(name.indexOf('/')+1);
 89                    name = name.substring(name.indexOf('\\')+1);
 90                    int idx = name.lastIndexOf('.');
 91                    if (idx>0)  name = name.substring(0,idx);
 92                }
 93                getTargetFile().copyFrom(u);
 94                continue;
 95            } catch (MalformedURLException e) {
 96                // not an URL
 97            }
 98
 99            // is this a plugin the update center?
100            UpdateSite.Plugin p = h.getUpdateCenter().getPlugin(source);
101            if (p!=null) {
102                stdout.println(Messages.InstallPluginCommand_InstallingFromUpdateCenter(source));
103                p.deploy().get();
104                continue;
105            }
106
107            stdout.println(Messages.InstallPluginCommand_NotAValidSourceName(source));
108
109            if (!source.contains(".") && !source.contains(":") && !source.contains("/") && !source.contains("\\")) {
110                // looks like a short plugin name. Why did we fail to find it in the update center?
111                if (h.getUpdateCenter().getSites().isEmpty()) {
112                    stdout.println(Messages.InstallPluginCommand_NoUpdateCenterDefined());
113                } else {
114                    Set<String> candidates = new HashSet<String>();
115                    for (UpdateSite s : h.getUpdateCenter().getSites()) {
116                        Data dt = s.getData();
117                        if (dt==null) {
118                            stdout.println(Messages.InstallPluginCommand_NoUpdateDataRetrieved(s.getUrl()));
119                        } else {
120                            candidates.addAll(dt.plugins.keySet());
121                        }
122                    }
123                    stdout.println(Messages.InstallPluginCommand_DidYouMean(source,EditDistance.findNearest(source,candidates)));
124                }
125            }
126
127            return 1;
128        }
129
130        if (restart)
131            h.restart();
132        return 0; // all success
133    }
134
135    private FilePath getTargetFile() {
136        return new FilePath(new File(Hudson.getInstance().getPluginManager().rootDir,name+".hpi"));
137    }
138}