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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, InfraDNA, 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
25 package hudson.util;
26
27 import hudson.FilePath;
28 import hudson.Launcher;
29 import hudson.Launcher.ProcStarter;
30
31 import java.io.File;
32 import java.io.Serializable;
33 import java.util.Map;
34 import java.util.TreeMap;
35
36 /**
37 * Used to build up launch parameters for a Java virtual machine.
38 *
39 * @author Kohsuke Kawaguchi
40 * @since 1.361
41 */
42 public class JVMBuilder implements Serializable {
43 private final ClasspathBuilder classpath = new ClasspathBuilder();
44 private final Map<String,String> systemProperties = new TreeMap<String,String>();
45 private final ArgumentListBuilder args = new ArgumentListBuilder();
46 private final ArgumentListBuilder vmopts = new ArgumentListBuilder();
47 private FilePath pwd;
48
49 private String mainClass;
50
51 /**
52 * Returns a builder object for creating classpath arguments.
53 */
54 public ClasspathBuilder classpath() {
55 return classpath;
56 }
57
58 public JVMBuilder systemProperty(String key, String value) {
59 this.systemProperties.put(key,value);
60 return this;
61 }
62
63 public Map<String,String> systemProperties() {
64 return this.systemProperties;
65 }
66
67 public JVMBuilder systemProperties(Map<String,String> props) {
68 if (props!=null) this.systemProperties.putAll(props);
69 return this;
70 }
71
72 /**
73 * Arguments to the main class.
74 */
75 public ArgumentListBuilder args() {
76 return args;
77 }
78
79 /**
80 * JVM options.
81 */
82 public ArgumentListBuilder vmopts() {
83 return vmopts;
84 }
85
86 /**
87 * Sets the current directory for the new JVM.
88 */
89 public JVMBuilder pwd(FilePath pwd) {
90 this.pwd = pwd;
91 return this;
92 }
93
94 /**
95 * Enables the debugger support on the given port.
96 */
97 public JVMBuilder debug(int port) {
98 vmopts.add("-Xrunjdwp:transport=dt_socket,server=y,address="+port);
99 return this;
100 }
101
102 /**
103 * Sets the current directory for the new JVM.
104 * This overloaded version only makes sense when you are launching JVM locally.
105 */
106 public JVMBuilder pwd(File pwd) {
107 return pwd(new FilePath(pwd));
108 }
109
110 public JVMBuilder mainClass(String fullyQualifiedClassName) {
111 this.mainClass = fullyQualifiedClassName;
112 return this;
113 }
114
115 public JVMBuilder mainClass(Class mainClass) {
116 return mainClass(mainClass.getName());
117 }
118
119 public ArgumentListBuilder toFullArguments() {
120 ArgumentListBuilder args = new ArgumentListBuilder();
121 args.add(new File(System.getProperty("java.home"),"bin/java")); // TODO: if we are to support a remote launch, JVM would be on a different path.
122 args.addKeyValuePairs("-D",systemProperties);
123 args.add("-cp").add(classpath.toString());
124 args.add(this.vmopts.toCommandArray());
125 args.add(mainClass);
126 args.add(this.args.toCommandArray());
127 return args;
128 }
129
130 /**
131 * Fills a {@link ProcStarter} with all the parameters configured by this builder.
132 */
133 public ProcStarter launch(Launcher launcher) {
134 return launcher.launch().cmds(toFullArguments()).pwd(pwd);
135 }
136
137
138 private static final long serialVersionUID = 1L;
139 }
140