From 7a6b16315899b958d775e093bb56d42cb7a558c3 Mon Sep 17 00:00:00 2001 From: ideal <125553253@qq.com> Date: Thu, 23 Aug 2018 10:44:54 +0800 Subject: [PATCH 1/4] add jvmLaunchers --- .../apache/commons/lang3/vm/JVMException.java | 28 +++ .../apache/commons/lang3/vm/JVMLauncher.java | 161 ++++++++++++++++++ .../apache/commons/lang3/vm/JVMLaunchers.java | 79 +++++++++ .../lang3/vm/ObjectInputStreamProxy.java | 110 ++++++++++++ .../commons/lang3/vm/Serializables.java | 55 ++++++ .../apache/commons/lang3/vm/VmCallable.java | 25 +++ .../org/apache/commons/lang3/vm/VmFuture.java | 57 +++++++ .../commons/lang3/vm/JVMLaunchersTest.java | 34 ++++ 8 files changed, 549 insertions(+) create mode 100644 src/main/java/org/apache/commons/lang3/vm/JVMException.java create mode 100644 src/main/java/org/apache/commons/lang3/vm/JVMLauncher.java create mode 100644 src/main/java/org/apache/commons/lang3/vm/JVMLaunchers.java create mode 100644 src/main/java/org/apache/commons/lang3/vm/ObjectInputStreamProxy.java create mode 100644 src/main/java/org/apache/commons/lang3/vm/Serializables.java create mode 100644 src/main/java/org/apache/commons/lang3/vm/VmCallable.java create mode 100644 src/main/java/org/apache/commons/lang3/vm/VmFuture.java create mode 100644 src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java diff --git a/src/main/java/org/apache/commons/lang3/vm/JVMException.java b/src/main/java/org/apache/commons/lang3/vm/JVMException.java new file mode 100644 index 00000000000..3fa3399bfb7 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/vm/JVMException.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3.vm; + +public class JVMException + extends Exception +{ + private static final long serialVersionUID = -1L; + + public JVMException(String message) + { + super(message); + } +} diff --git a/src/main/java/org/apache/commons/lang3/vm/JVMLauncher.java b/src/main/java/org/apache/commons/lang3/vm/JVMLauncher.java new file mode 100644 index 00000000000..6bea41b54c5 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/vm/JVMLauncher.java @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3.vm; + +import org.apache.commons.lang3.exception.ExceptionUtils; + +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.Serializable; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import static java.nio.charset.StandardCharsets.UTF_8; + +public final class JVMLauncher +{ + private VmCallable callable; + private Process process; + private Collection userJars; + private Consumer consoleHandler; + + public JVMLauncher(VmCallable callable, Consumer consoleHandler, Collection userJars) + { + this.callable = callable; + this.userJars = userJars; + this.consoleHandler = consoleHandler; + } + + public VmFuture startAndGet() + throws IOException, ClassNotFoundException, JVMException + { + return startAndGet(null); + } + + public VmFuture startAndGet(ClassLoader classLoader) + throws IOException, ClassNotFoundException, JVMException + { + byte[] bytes = startAndGetByte(); + VmFuture vmFuture = (VmFuture) Serializables.byteToObject(bytes, classLoader); + if (!vmFuture.get().isPresent()) { + throw new JVMException(vmFuture.getOnFailure()); + } + return vmFuture; + } + + private byte[] startAndGetByte() + throws IOException + { + try (ServerSocket sock = new ServerSocket()) { + sock.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); + ProcessBuilder builder = new ProcessBuilder(buildMainArg(sock.getLocalPort())) + .redirectErrorStream(true); + + this.process = builder.start(); + try (OutputStream os = new BufferedOutputStream(process.getOutputStream())) { + os.write(Serializables.serialize(callable)); //send callable to newJVM + } + try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), UTF_8))) { + String line; + while ((line = reader.readLine()) != null) { + consoleHandler.accept(line); + } + } + + try (Socket client = sock.accept()) { + try (InputStream input = client.getInputStream()) { + byte[] byt = new byte[input.available()]; + input.read(byt); + return byt; + } + } + } + } + + private String getUserAddClasspath() + { + return userJars.stream() + .map(URL::getPath) + .collect(Collectors.joining(File.pathSeparator)); + } + + private List buildMainArg(int port) + { + File java = new File(new File(System.getProperty("java.home"), "bin"), "java"); + ArrayList ops = new ArrayList<>(); + ops.add(java.toString()); + ops.add("-classpath"); + String userSdkJars = getUserAddClasspath(); //building depend ext jars + ops.add(System.getProperty("java.class.path") + ":" + userSdkJars); + + String javaLibPath = System.getProperty("java.library.path"); + if (javaLibPath != null) { + ops.add("-Djava.library.path=" + javaLibPath); + } + ops.add(JVMLauncher.class.getCanonicalName()); //newJVM main(args) class + ops.add(Integer.toString(port)); + return ops; + } + + public static void main(String[] args) + throws Exception + { + System.out.println("vm start ok ..."); + VmFuture future; + + try (ObjectInputStreamProxy ois = new ObjectInputStreamProxy(System.in)) { + VmCallable callable = (VmCallable) ois.readObject(); + System.out.println("vm start init ok ..."); + future = new VmFuture<>(callable.call()); + } + catch (Throwable e) { + future = new VmFuture<>(ExceptionUtils.getStackTrace(e)); + } + + try (OutputStream out = chooseOutputStream(args)) { + out.write(Serializables.serialize(future)); + System.out.println("vm exiting ok ..."); + } + } + + private static OutputStream chooseOutputStream(String[] args) + throws IOException + { + if (args.length > 0) { + int port = Integer.parseInt(args[0]); + Socket sock = new Socket(); + sock.connect(new InetSocketAddress(InetAddress.getLocalHost(), port)); + return sock.getOutputStream(); + } + else { + return System.out; + } + } +} diff --git a/src/main/java/org/apache/commons/lang3/vm/JVMLaunchers.java b/src/main/java/org/apache/commons/lang3/vm/JVMLaunchers.java new file mode 100644 index 00000000000..8fe7a3049f4 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/vm/JVMLaunchers.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3.vm; + +import java.io.Serializable; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.function.Consumer; + +import static java.util.Objects.requireNonNull; + +public class JVMLaunchers +{ + private JVMLaunchers() {} + + public static class VmBuilder + { + private VmCallable callable; + private Consumer consoleHandler; + private final List tmpJars = new ArrayList<>(); + + public VmBuilder setCallable(VmCallable callable) + { + this.callable = requireNonNull(callable, "callable is null"); + return this; + } + + public VmBuilder setConsole(Consumer consoleHandler) + { + this.consoleHandler = requireNonNull(consoleHandler, "consoleHandler is null"); + return this; + } + + public VmBuilder addUserURLClassLoader(URLClassLoader vmClassLoader) + { + ClassLoader classLoader = vmClassLoader; + while (classLoader instanceof URLClassLoader) { + Collections.addAll(tmpJars, ((URLClassLoader) classLoader).getURLs()); + classLoader = classLoader.getParent(); + } + return this; + } + + public VmBuilder addUserjars(Collection jars) + { + tmpJars.addAll(jars); + return this; + } + + public JVMLauncher build() + { + requireNonNull(consoleHandler, "setConsole(Consumer consoleHandler) not setting"); + return new JVMLauncher(callable, consoleHandler, tmpJars); + } + } + + public static VmBuilder newJvm() + { + return new VmBuilder(); + } +} diff --git a/src/main/java/org/apache/commons/lang3/vm/ObjectInputStreamProxy.java b/src/main/java/org/apache/commons/lang3/vm/ObjectInputStreamProxy.java new file mode 100644 index 00000000000..11478380f85 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/vm/ObjectInputStreamProxy.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3.vm; + +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectStreamClass; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map; + +public class ObjectInputStreamProxy + extends java.io.ObjectInputStream +{ + private ClassLoader classLoader; + + public ObjectInputStreamProxy(InputStream in) + throws IOException + { + super(in); + } + + /** + * ObjectInputStreamProxy used by user classLoader + *

+ * + * @param classLoader used by loadObject + */ + public ObjectInputStreamProxy(InputStream in, ClassLoader classLoader) + throws IOException + { + super(in); + this.classLoader = classLoader; + } + + /** + * get Method LatestUserDefinedLoader with java.io.ObjectInputStreamProxy + * with jdk.internal.misc.VM.latestUserDefinedLoader() + */ + public static ClassLoader getLatestUserDefinedLoader() + { + //super.latestUserDefinedLoader(); + Class class1 = java.io.ObjectInputStream.class; + try { + Method method = class1.getDeclaredMethod("latestUserDefinedLoader"); + method.setAccessible(true); + return (ClassLoader) method.invoke(null); + } + catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException("Not compatible with java version"); + } + } + + /** + * get field primClasses with java.io.ObjectInputStreamProxy + */ + private static Map> getPrimClasses() + { + Class class1 = java.io.ObjectInputStream.class; + Map> primClasses = null; + try { + Field field = class1.getDeclaredField("primClasses"); + field.setAccessible(true); + primClasses = (Map>) field.get(class1); + return primClasses; + } + catch (NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException("Not compatible with java version"); + } + } + + @Override + protected Class resolveClass(ObjectStreamClass desc) + throws IOException, ClassNotFoundException + { + if (classLoader == null) { + return super.resolveClass(desc); + } + + //return super.resolveClass(desc); + String name = desc.getName(); + try { + return Class.forName(name, false, classLoader); + } + catch (ClassNotFoundException ex) { + Class cl = getPrimClasses().get(name); + if (cl != null) { + return cl; + } + else { + throw ex; + } + } + } +} diff --git a/src/main/java/org/apache/commons/lang3/vm/Serializables.java b/src/main/java/org/apache/commons/lang3/vm/Serializables.java new file mode 100644 index 00000000000..3a81c4cb27b --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/vm/Serializables.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3.vm; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +public class Serializables +{ + private Serializables() {} + + public static byte[] serialize(Serializable serializable) + throws IOException + { + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream os = new ObjectOutputStream(bos) + ) { + os.writeObject(serializable); + return bos.toByteArray(); + } + } + + public static Object byteToObject(byte[] bytes) + throws IOException, ClassNotFoundException + { + return byteToObject(bytes, null); + } + + public static Object byteToObject(byte[] bytes, ClassLoader classLoader) + throws IOException, ClassNotFoundException + { + try (ByteArrayInputStream bi = new ByteArrayInputStream(bytes); + ObjectInputStreamProxy oi = new ObjectInputStreamProxy(bi, classLoader) + ) { + return oi.readObject(); + } + } +} diff --git a/src/main/java/org/apache/commons/lang3/vm/VmCallable.java b/src/main/java/org/apache/commons/lang3/vm/VmCallable.java new file mode 100644 index 00000000000..c4ff18e968f --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/vm/VmCallable.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3.vm; + +import java.io.Serializable; +import java.util.concurrent.Callable; + +public interface VmCallable + extends Callable, Serializable +{ +} diff --git a/src/main/java/org/apache/commons/lang3/vm/VmFuture.java b/src/main/java/org/apache/commons/lang3/vm/VmFuture.java new file mode 100644 index 00000000000..d776d6e9539 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/vm/VmFuture.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3.vm; + +import java.io.Serializable; +import java.util.Optional; + +public class VmFuture + implements Serializable +{ + private V result; + private String errorMessage; + + public Optional get() + { + return Optional.ofNullable(result); + } + + public String getOnFailure() + { + return errorMessage; + } + + public VmFuture(Serializable result) + { + this.result = (V) result; + } + + public VmFuture(String errorMessage) + { + this.errorMessage = errorMessage; + } + + public VmFuture(Serializable result, String errorMessage) + { + this.errorMessage = errorMessage; + } + + static VmFuture make(Serializable result, String errorMessage) + { + return new VmFuture<>(result, errorMessage); + } +} diff --git a/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java b/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java new file mode 100644 index 00000000000..757481d84d5 --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java @@ -0,0 +1,34 @@ +package org.apache.commons.lang3.vm; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.util.Collections; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.*; + +public class JVMLaunchersTest +{ + + @Test + public void newJvm() + throws JVMException, IOException, ClassNotFoundException + { + System.out.println("--- vm test ---"); + JVMLauncher launcher = JVMLaunchers.newJvm() + .setCallable(() -> { + System.out.println("************ Compile start ***************"); + TimeUnit.SECONDS.sleep(1); + System.out.println("************ Compile stop ***************"); + return 1; + }) + .addUserjars(Collections.emptyList()) + .setConsole((msg) -> System.err.println(msg)) + .build(); + + VmFuture out = launcher.startAndGet(); + Assert.assertEquals(out.get().get().intValue(), 1); + } +} \ No newline at end of file From 62f3a0d97c4eb3d4df376805463a6e61533ef06f Mon Sep 17 00:00:00 2001 From: ideal <125553253@qq.com> Date: Thu, 23 Aug 2018 13:08:45 +0800 Subject: [PATCH 2/4] Compatible with java7 --- .../org/apache/commons/lang3/vm/Consumer.java | 6 ++++ .../apache/commons/lang3/vm/JVMLauncher.java | 13 ++++---- .../apache/commons/lang3/vm/JVMLaunchers.java | 2 +- .../org/apache/commons/lang3/vm/VmFuture.java | 5 ++- .../commons/lang3/vm/JVMLaunchersTest.java | 33 +++++++++++++------ 5 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 src/main/java/org/apache/commons/lang3/vm/Consumer.java diff --git a/src/main/java/org/apache/commons/lang3/vm/Consumer.java b/src/main/java/org/apache/commons/lang3/vm/Consumer.java new file mode 100644 index 00000000000..fa15ec91d90 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/vm/Consumer.java @@ -0,0 +1,6 @@ +package org.apache.commons.lang3.vm; + +public interface Consumer +{ + void accept(T t); +} diff --git a/src/main/java/org/apache/commons/lang3/vm/JVMLauncher.java b/src/main/java/org/apache/commons/lang3/vm/JVMLauncher.java index 6bea41b54c5..e1a0886aaa6 100644 --- a/src/main/java/org/apache/commons/lang3/vm/JVMLauncher.java +++ b/src/main/java/org/apache/commons/lang3/vm/JVMLauncher.java @@ -16,6 +16,7 @@ */ package org.apache.commons.lang3.vm; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import java.io.BufferedOutputStream; @@ -34,8 +35,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.function.Consumer; -import java.util.stream.Collectors; import static java.nio.charset.StandardCharsets.UTF_8; @@ -64,7 +63,7 @@ public VmFuture startAndGet(ClassLoader classLoader) { byte[] bytes = startAndGetByte(); VmFuture vmFuture = (VmFuture) Serializables.byteToObject(bytes, classLoader); - if (!vmFuture.get().isPresent()) { + if (vmFuture.get() == null) { throw new JVMException(vmFuture.getOnFailure()); } return vmFuture; @@ -101,9 +100,11 @@ private byte[] startAndGetByte() private String getUserAddClasspath() { - return userJars.stream() - .map(URL::getPath) - .collect(Collectors.joining(File.pathSeparator)); + List builder = new ArrayList<>(); + for(URL url: userJars){ + builder.add(url.getPath()); + } + return StringUtils.join(builder, File.pathSeparator); } private List buildMainArg(int port) diff --git a/src/main/java/org/apache/commons/lang3/vm/JVMLaunchers.java b/src/main/java/org/apache/commons/lang3/vm/JVMLaunchers.java index 8fe7a3049f4..0257e6657bc 100644 --- a/src/main/java/org/apache/commons/lang3/vm/JVMLaunchers.java +++ b/src/main/java/org/apache/commons/lang3/vm/JVMLaunchers.java @@ -23,7 +23,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.function.Consumer; + import static java.util.Objects.requireNonNull; diff --git a/src/main/java/org/apache/commons/lang3/vm/VmFuture.java b/src/main/java/org/apache/commons/lang3/vm/VmFuture.java index d776d6e9539..40cf7050ceb 100644 --- a/src/main/java/org/apache/commons/lang3/vm/VmFuture.java +++ b/src/main/java/org/apache/commons/lang3/vm/VmFuture.java @@ -17,7 +17,6 @@ package org.apache.commons.lang3.vm; import java.io.Serializable; -import java.util.Optional; public class VmFuture implements Serializable @@ -25,9 +24,9 @@ public class VmFuture private V result; private String errorMessage; - public Optional get() + public V get() { - return Optional.ofNullable(result); + return result; } public String getOnFailure() diff --git a/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java b/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java index 757481d84d5..8f3d76c33bd 100644 --- a/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java +++ b/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java @@ -4,31 +4,44 @@ import org.junit.Test; import java.io.IOException; +import java.io.Serializable; +import java.net.URL; +import java.util.Arrays; import java.util.Collections; import java.util.concurrent.TimeUnit; import static org.junit.Assert.*; -public class JVMLaunchersTest +public class JVMLaunchersTest implements Serializable { - @Test public void newJvm() throws JVMException, IOException, ClassNotFoundException { System.out.println("--- vm test ---"); JVMLauncher launcher = JVMLaunchers.newJvm() - .setCallable(() -> { - System.out.println("************ Compile start ***************"); - TimeUnit.SECONDS.sleep(1); - System.out.println("************ Compile stop ***************"); - return 1; + .setCallable(new VmCallable(){ + @Override + public Integer call() + throws Exception + { + System.out.println("************ Compile start ***************"); + TimeUnit.SECONDS.sleep(1); + System.out.println("************ Compile stop ***************"); + return 1; + } + }) + .addUserjars(Arrays.asList()) + .setConsole(new Consumer() { + @Override + public void accept(String msg) + { + System.err.println(msg); + } }) - .addUserjars(Collections.emptyList()) - .setConsole((msg) -> System.err.println(msg)) .build(); VmFuture out = launcher.startAndGet(); - Assert.assertEquals(out.get().get().intValue(), 1); + Assert.assertEquals(out.get().intValue(), 1); } } \ No newline at end of file From d25f5e3422c4eb5517521f8e625b9392f5979b45 Mon Sep 17 00:00:00 2001 From: ideal <125553253@qq.com> Date: Thu, 23 Aug 2018 13:09:08 +0800 Subject: [PATCH 3/4] Compatible with java7 --- src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java b/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java index 8f3d76c33bd..4762cbd5fe8 100644 --- a/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java +++ b/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java @@ -31,7 +31,7 @@ public Integer call() return 1; } }) - .addUserjars(Arrays.asList()) + .addUserjars(Collections.emptyList()) .setConsole(new Consumer() { @Override public void accept(String msg) From 7f778a5711bb6f3a5a68fb5e2a0e2b982446fa45 Mon Sep 17 00:00:00 2001 From: ideal <125553253@qq.com> Date: Thu, 23 Aug 2018 13:20:04 +0800 Subject: [PATCH 4/4] add license --- .../org/apache/commons/lang3/vm/Consumer.java | 16 ++++++++++++++++ .../commons/lang3/vm/JVMLaunchersTest.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/vm/Consumer.java b/src/main/java/org/apache/commons/lang3/vm/Consumer.java index fa15ec91d90..e31110aa108 100644 --- a/src/main/java/org/apache/commons/lang3/vm/Consumer.java +++ b/src/main/java/org/apache/commons/lang3/vm/Consumer.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.commons.lang3.vm; public interface Consumer diff --git a/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java b/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java index 4762cbd5fe8..7ecb6742f98 100644 --- a/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java +++ b/src/test/java/org/apache/commons/lang3/vm/JVMLaunchersTest.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.commons.lang3.vm; import org.junit.Assert;