File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\cli\InstallPluginCommand.java.html

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 */
24 package hudson.cli;
25
26 import hudson.Extension;
27 import hudson.FilePath;
28 import hudson.PluginManager;
29 import hudson.util.IOException2;
30 import jenkins.model.Jenkins;
31 import hudson.model.UpdateSite;
32 import hudson.model.UpdateSite.Data;
33 import hudson.util.EditDistance;
34 import org.kohsuke.args4j.Argument;
35 import org.kohsuke.args4j.Option;
36
37 import java.io.File;
38 import java.net.URL;
39 import java.net.MalformedURLException;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.ArrayList;
43 import java.util.Set;
44
45 /**
46 * Installs a plugin either from a file, an URL, or from update center.
47 *
48 * @author Kohsuke Kawaguchi
49 * @since 1.331
50 */
51 @Extension
52 public class InstallPluginCommand extends CLICommand {
53 public String getShortDescription() {
54 return Messages.InstallPluginCommand_ShortDescription();
55 }
56
57 @Argument(metaVar="SOURCE",required=true,usage="If this points to a local file, that file will be installed. " +
58 "If this is an URL, Jenkins downloads the URL and installs that as a plugin." +
59 "Otherwise the name is assumed to be the short name of the plugin in the existing update center (like \"findbugs\")," +
60 "and the plugin will be installed from the update center")
61 public List<String> sources = new ArrayList<String>();
62
63 @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.)")
64 public String name;
65
66 @Option(name="-restart",usage="Restart Jenkins upon successful installation")
67 public boolean restart;
68
69 @Option(name="-deploy",usage="Deploy plugins right away without postponing them until the reboot.")
70 public boolean dynamicLoad;
71
72 protected int run() throws Exception {
73 Jenkins h = Jenkins.getInstance();
74 h.checkPermission(PluginManager.UPLOAD_PLUGINS);
75 PluginManager pm = h.getPluginManager();
76
77 for (String source : sources) {
78 // is this a file?
79 if (channel!=null) {
80 FilePath f = new FilePath(channel, source);
81 if (f.exists()) {
82 stdout.println(Messages.InstallPluginCommand_InstallingPluginFromLocalFile(f));
83 if (name==null)
84 name = f.getBaseName();
85 f.copyTo(getTargetFilePath());
86 if (dynamicLoad)
87 pm.dynamicLoad(getTargetFile());
88 continue;
89 }
90 }
91
92 // is this an URL?
93 try {
94 URL u = new URL(source);
95 stdout.println(Messages.InstallPluginCommand_InstallingPluginFromUrl(u));
96 if (name==null) {
97 name = u.getPath();
98 name = name.substring(name.lastIndexOf('/')+1);
99 name = name.substring(name.lastIndexOf('\\')+1);
100 int idx = name.lastIndexOf('.');
101 if (idx>0) name = name.substring(0,idx);
102 }
103 getTargetFilePath().copyFrom(u);
104 if (dynamicLoad)
105 pm.dynamicLoad(getTargetFile());
106 continue;
107 } catch (MalformedURLException e) {
108 // not an URL
109 }
110
111 // is this a plugin the update center?
112 UpdateSite.Plugin p = h.getUpdateCenter().getPlugin(source);
113 if (p!=null) {
114 stdout.println(Messages.InstallPluginCommand_InstallingFromUpdateCenter(source));
115 Throwable e = p.deploy(dynamicLoad).get().getError();
116 if (e!=null)
117 throw new IOException2("Failed to install plugin "+source,e);
118 continue;
119 }
120
121 stdout.println(Messages.InstallPluginCommand_NotAValidSourceName(source));
122
123 if (!source.contains(".") && !source.contains(":") && !source.contains("/") && !source.contains("\\")) {
124 // looks like a short plugin name. Why did we fail to find it in the update center?
125 if (h.getUpdateCenter().getSites().isEmpty()) {
126 stdout.println(Messages.InstallPluginCommand_NoUpdateCenterDefined());
127 } else {
128 Set<String> candidates = new HashSet<String>();
129 for (UpdateSite s : h.getUpdateCenter().getSites()) {
130 Data dt = s.getData();
131 if (dt==null)
132 stdout.println(Messages.InstallPluginCommand_NoUpdateDataRetrieved(s.getUrl()));
133 else
134 candidates.addAll(dt.plugins.keySet());
135 }
136 stdout.println(Messages.InstallPluginCommand_DidYouMean(source,EditDistance.findNearest(source,candidates)));
137 }
138 }
139
140 return 1;
141 }
142
143 if (restart)
144 h.restart();
145 return 0; // all success
146 }
147
148 private FilePath getTargetFilePath() {
149 return new FilePath(getTargetFile());
150 }
151
152 private File getTargetFile() {
153 return new File(Jenkins.getInstance().getPluginManager().rootDir,name+".jpi");
154 }
155 }
156