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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
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.lifecycle;
25
26 import hudson.FilePath;
27 import hudson.Launcher.LocalLauncher;
28 import hudson.Util;
29 import jenkins.model.Jenkins;
30 import hudson.util.StreamTaskListener;
31 import hudson.util.jna.Kernel32;
32 import static hudson.util.jna.Kernel32.MOVEFILE_DELAY_UNTIL_REBOOT;
33 import static hudson.util.jna.Kernel32.MOVEFILE_REPLACE_EXISTING;
34
35 import org.apache.commons.io.FileUtils;
36 import org.apache.commons.io.output.ByteArrayOutputStream;
37
38 import java.io.File;
39 import java.io.IOException;
40 import java.io.FileWriter;
41 import java.net.URL;
42 import java.util.logging.Level;
43 import java.util.logging.Logger;
44
45 /**
46 * {@link Lifecycle} for Hudson installed as Windows service.
47 *
48 * @author Kohsuke Kawaguchi
49 * @see WindowsInstallerLink
50 */
51 public class WindowsServiceLifecycle extends Lifecycle {
52 public WindowsServiceLifecycle() {
53 updateHudsonExeIfNeeded();
54 }
55
56 /**
57 * If <tt>hudson.exe</tt> is old compared to our copy,
58 * schedule an overwrite (except that since it's currently running,
59 * we can only do it when Hudson restarts next time.)
60 */
61 private void updateHudsonExeIfNeeded() {
62 try {
63 File rootDir = Jenkins.getInstance().getRootDir();
64
65 URL exe = getClass().getResource("/windows-service/jenkins.exe");
66 String ourCopy = Util.getDigestOf(exe.openStream());
67
68 for (String name : new String[]{"hudson.exe","jenkins.exe"}) {
69 try {
70 File currentCopy = new File(rootDir,name);
71 if(!currentCopy.exists()) continue;
72 String curCopy = new FilePath(currentCopy).digest();
73
74 if(ourCopy.equals(curCopy)) continue; // identical
75
76 File stage = new File(rootDir,name+".new");
77 FileUtils.copyURLToFile(exe,stage);
78 Kernel32.INSTANCE.MoveFileExA(stage.getAbsolutePath(),currentCopy.getAbsolutePath(),MOVEFILE_DELAY_UNTIL_REBOOT|MOVEFILE_REPLACE_EXISTING);
79 LOGGER.info("Scheduled a replacement of "+name);
80 } catch (IOException e) {
81 LOGGER.log(Level.SEVERE, "Failed to replace "+name,e);
82 } catch (InterruptedException e) {
83 }
84 }
85 } catch (IOException e) {
86 LOGGER.log(Level.SEVERE, "Failed to replace hudson.exe",e);
87 }
88 }
89
90 /**
91 * On Windows, jenkins.war is locked, so we place a new version under a special name,
92 * which is picked up by the service wrapper upon restart.
93 */
94 @Override
95 public void rewriteHudsonWar(File by) throws IOException {
96 File dest = getHudsonWar();
97 // this should be impossible given the canRewriteHudsonWar method,
98 // but let's be defensive
99 if(dest==null) throw new IOException("jenkins.war location is not known.");
100
101 // backing up the old jenkins.war before its lost due to upgrading
102 // unless we are trying to rewrite jenkins.war by a backup itself
103 File bak = new File(dest.getPath() + ".bak");
104 if (!by.equals(bak))
105 FileUtils.copyFile(dest, bak);
106
107 String baseName = dest.getName();
108 baseName = baseName.substring(0,baseName.indexOf('.'));
109
110 File rootDir = Jenkins.getInstance().getRootDir();
111 File copyFiles = new File(rootDir,baseName+".copies");
112
113 FileWriter w = new FileWriter(copyFiles, true);
114 w.write(by.getAbsolutePath()+'>'+getHudsonWar().getAbsolutePath()+'\n');
115 w.close();
116 }
117
118 @Override
119 public void restart() throws IOException, InterruptedException {
120 File me = getHudsonWar();
121 File home = me.getParentFile();
122
123 ByteArrayOutputStream baos = new ByteArrayOutputStream();
124 StreamTaskListener task = new StreamTaskListener(baos);
125 task.getLogger().println("Restarting a service");
126 File executable = new File(home, "hudson.exe");
127 if (!executable.exists()) executable = new File(home, "jenkins.exe");
128
129 int r = new LocalLauncher(task).launch().cmds(executable, "restart")
130 .stdout(task).pwd(home).join();
131 if(r!=0)
132 throw new IOException(baos.toString());
133 }
134
135 private static final Logger LOGGER = Logger.getLogger(WindowsServiceLifecycle.class.getName());
136 }
137