1
1
/*
2
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
25
25
import java .io .File ;
26
26
import java .io .IOException ;
27
27
import java .io .InputStreamReader ;
28
+ import java .io .OutputStream ;
29
+ import java .io .PrintStream ;
30
+ import java .io .UncheckedIOException ;
28
31
import java .net .URL ;
29
32
import java .net .URLClassLoader ;
30
33
import java .nio .file .Files ;
@@ -107,6 +110,7 @@ public class Proc {
107
110
private List <String > args = new ArrayList <>();
108
111
private Map <String ,String > env = new HashMap <>();
109
112
private Map <String ,String > prop = new HashMap ();
113
+ private Map <String ,String > secprop = new HashMap ();
110
114
private boolean inheritIO = false ;
111
115
private boolean noDump = false ;
112
116
@@ -167,6 +171,11 @@ public Proc prop(String a, String b) {
167
171
prop .put (a , b );
168
172
return this ;
169
173
}
174
+ // Specifies a security property. Can be called multiple times.
175
+ public Proc secprop (String a , String b ) {
176
+ secprop .put (a , b );
177
+ return this ;
178
+ }
170
179
// Adds a perm to policy. Can be called multiple times. In order to make it
171
180
// effective, please also call prop("java.security.manager", "").
172
181
public Proc perm (Permission p ) {
@@ -191,6 +200,17 @@ public Proc start() throws IOException {
191
200
cp .append (url .getFile ());
192
201
}
193
202
cmd .add (cp .toString ());
203
+ if (!secprop .isEmpty ()) {
204
+ Path p = Paths .get (getId ("security" ));
205
+ try (OutputStream fos = Files .newOutputStream (p );
206
+ PrintStream ps = new PrintStream (fos )) {
207
+ secprop .forEach ((k ,v ) -> ps .println (k + "=" + v ));
208
+ } catch (IOException e ) {
209
+ throw new UncheckedIOException (e );
210
+ }
211
+ prop .put ("java.security.properties" , p .toString ());
212
+ }
213
+
194
214
for (Entry <String ,String > e : prop .entrySet ()) {
195
215
cmd .add ("-D" + e .getKey () + "=" + e .getValue ());
196
216
}
@@ -289,6 +309,12 @@ public int waitFor() throws Exception {
289
309
}
290
310
return p .waitFor ();
291
311
}
312
+ // Wait for process end with expected exit code
313
+ public void waitFor (int expected ) throws Exception {
314
+ if (p .waitFor () != expected ) {
315
+ throw new RuntimeException ("Exit code not " + expected );
316
+ }
317
+ }
292
318
293
319
// The following methods are used inside a proc
294
320
0 commit comments