PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/hudson/hudson
Java | 84 lines | 45 code | 10 blank | 29 comment | 6 complexity | 10e83ad34d0b9f6463ad1dbdaa800726 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.model.Hudson;
27import hudson.Extension;
28import hudson.model.Item;
29import hudson.model.Items;
30import hudson.model.Job;
31import hudson.model.TopLevelItem;
32import hudson.util.IOUtils;
33import java.io.File;
34import java.io.IOException;
35import org.kohsuke.args4j.Argument;
36
37/**
38 * Updates or creates a job by reading stdin as a configuration XML file.
39 * 
40 * @author Henrik Lynggaard Hansen
41 */
42@Extension
43public class UpdateJobCommand extends CLICommand {
44
45    @Override
46    public String getShortDescription() {
47        return "Updates and potentionally creates a job by reading stdin as a configuration XML file.";
48    }
49    
50    @Argument(metaVar = "NAME", usage = "Name of the job to update", required = true)
51    public String name;
52    @Argument(metaVar = "CREATE", usage = "Create the job if needed", index = 1, required = true)
53    public Boolean create;
54
55    protected int run() throws Exception {
56        Hudson h = Hudson.getInstance();
57
58        TopLevelItem item = h.getItem(name);
59
60        if (item == null && !create) {
61            stderr.println("Job '" + name + "' does not exist and create is set to false");
62            return -1;
63        }
64
65        if (item == null) {
66            h.checkPermission(Item.CREATE);
67            h.createProjectFromXML(name, stdin);
68        } else {
69            try {               
70                h.checkPermission(Job.CONFIGURE);
71                               
72                File rootDirOfJob = new File(new File(h.getRootDir(), "jobs"), name);
73                // place it as config.xml
74                File configXml = Items.getConfigFile(rootDirOfJob).getFile();
75                IOUtils.copy(stdin, configXml);
76                
77                item = h.reloadProjectFromDisk(configXml.getParentFile());                               
78            } catch (IOException e) {
79                throw e;
80            }
81        }
82        return 0;
83    }
84}