diff --git a/src/main/java/org/jboss/fuse/mvnd/daemon/Client.java b/src/main/java/org/jboss/fuse/mvnd/daemon/Client.java index 702b6b24..362010d5 100644 --- a/src/main/java/org/jboss/fuse/mvnd/daemon/Client.java +++ b/src/main/java/org/jboss/fuse/mvnd/daemon/Client.java @@ -188,16 +188,8 @@ public class Client { Path workingDir = Layout.userDir(); String command = ""; try { - String classpath = - Stream.concat( - Stream.concat(Files.list(mavenHome.resolve("lib/ext")), - Files.list(mavenHome.resolve("lib"))) - .filter(p -> p.getFileName().toString().endsWith(".jar")) - .filter(Files::isRegularFile), - Stream.of(mavenHome.resolve("conf"), mavenHome.resolve("conf/logging"))) - .map(Path::normalize) - .map(Path::toString) - .collect(Collectors.joining(":")); + String url = ServerMain.class.getClassLoader().getResource(Server.class.getName().replace('.', '/') + ".class").toString(); + String classpath = url.substring("file:jar:".length(), url.indexOf('!')); String java = ScriptUtils.isWindows() ? "bin\\java.exe" : "bin/java"; List args = new ArrayList<>(); args.add("\"" + javaHome.resolve(java) + "\""); @@ -214,7 +206,7 @@ public class Client { args.add("-D" + Server.DAEMON_IDLE_TIMEOUT + "=" + System.getProperty(Server.DAEMON_IDLE_TIMEOUT)); } - args.add(Server.class.getName()); + args.add(ServerMain.class.getName()); command = String.join(" ", args); LOGGER.debug("Starting daemon process: uid = {}, workingDir = {}, daemonArgs: {}", uid, workingDir, command); diff --git a/src/main/java/org/jboss/fuse/mvnd/daemon/Server.java b/src/main/java/org/jboss/fuse/mvnd/daemon/Server.java index dab0fc65..ca2cd512 100644 --- a/src/main/java/org/jboss/fuse/mvnd/daemon/Server.java +++ b/src/main/java/org/jboss/fuse/mvnd/daemon/Server.java @@ -51,7 +51,7 @@ import static org.jboss.fuse.mvnd.daemon.DaemonState.Busy; import static org.jboss.fuse.mvnd.daemon.DaemonState.StopRequested; import static org.jboss.fuse.mvnd.daemon.DaemonState.Stopped; -public class Server implements AutoCloseable { +public class Server implements AutoCloseable, Runnable { public static final String DAEMON_IDLE_TIMEOUT = "daemon.idleTimeout"; @@ -74,19 +74,7 @@ public class Server implements AutoCloseable { private final Lock stateLock = new ReentrantLock(); private final Condition condition = stateLock.newCondition(); - public static void main(String[] args) throws Exception { - String uid = System.getProperty("daemon.uid"); - String mavenHome = System.getProperty("maven.home"); - if (uid == null || mavenHome == null) { - throw new IllegalStateException("The system properties 'daemon.uid' and 'maven.home' must be valid"); - } - - try (Server server = new Server(uid)) { - server.run(); - } - } - - private Server(String uid) throws IOException { + public Server(String uid) throws IOException { this.uid = uid; try { cli = new DaemonMavenCli(); diff --git a/src/main/java/org/jboss/fuse/mvnd/daemon/ServerMain.java b/src/main/java/org/jboss/fuse/mvnd/daemon/ServerMain.java new file mode 100644 index 00000000..070e9fdc --- /dev/null +++ b/src/main/java/org/jboss/fuse/mvnd/daemon/ServerMain.java @@ -0,0 +1,70 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed 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.jboss.fuse.mvnd.daemon; + +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.stream.Stream; + +public class ServerMain { + + public static void main(String[] args) throws Exception { + String uidStr = System.getProperty("daemon.uid"); + String mavenHomeStr = System.getProperty("maven.home"); + if (uidStr == null || mavenHomeStr == null) { + throw new IllegalStateException("The system properties 'daemon.uid' and 'maven.home' must be valid"); + } + + Path mavenHome = Paths.get(mavenHomeStr); + URL[] classpath = + Stream.concat( + Stream.concat(Files.list(mavenHome.resolve("lib/ext")), + Files.list(mavenHome.resolve("lib"))) + .filter(p -> p.getFileName().toString().endsWith(".jar")) + .filter(Files::isRegularFile), + Stream.of(mavenHome.resolve("conf"), mavenHome.resolve("conf/logging"))) + .map(Path::normalize) + .map(Path::toUri) + .map(uri -> { + try { + return uri.toURL(); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + }) + .toArray(URL[]::new); + ClassLoader loader = new URLClassLoader(classpath, null) { + @Override + protected Class findClass(String name) throws ClassNotFoundException { + try { + return super.findClass(name); + } catch (ClassNotFoundException e) { + return ServerMain.class.getClassLoader().loadClass(name); + } + } + }; + Thread.currentThread().setContextClassLoader(loader); + Class clazz = loader.loadClass("org.jboss.fuse.mvnd.daemon.Server"); + try (AutoCloseable server = (AutoCloseable) clazz.getConstructor(String.class).newInstance(uidStr)) { + ((Runnable) server).run(); + } + } + +}