|
| 1 | +/******************************************************************************* |
| 2 | +* Copyright (c) 2021 Microsoft Corporation and others. |
| 3 | +* All rights reserved. This program and the accompanying materials |
| 4 | +* are made available under the terms of the Eclipse Public License v1.0 |
| 5 | +* which accompanies this distribution, and is available at |
| 6 | +* http://www.eclipse.org/legal/epl-v10.html |
| 7 | +* |
| 8 | +* Contributors: |
| 9 | +* Microsoft Corporation - initial API and implementation |
| 10 | +*******************************************************************************/ |
| 11 | + |
| 12 | +package com.microsoft.java.debug.core.adapter.handler; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.io.FileOutputStream; |
| 16 | +import java.io.IOException; |
| 17 | +import java.math.BigInteger; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.InvalidPathException; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.nio.file.Paths; |
| 22 | +import java.security.MessageDigest; |
| 23 | +import java.security.NoSuchAlgorithmException; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.jar.Attributes; |
| 29 | +import java.util.jar.JarOutputStream; |
| 30 | +import java.util.jar.Manifest; |
| 31 | + |
| 32 | +import com.microsoft.java.debug.core.adapter.AdapterUtils; |
| 33 | + |
| 34 | +import org.apache.commons.lang3.ArrayUtils; |
| 35 | + |
| 36 | +import sun.security.action.GetPropertyAction; |
| 37 | + |
| 38 | +public class LaunchUtils { |
| 39 | + private static Set<Path> tempFilesInUse = new HashSet<>(); |
| 40 | + |
| 41 | + /** |
| 42 | + * Generate the classpath parameters to a temporary classpath.jar. |
| 43 | + * @param classPaths - the classpath parameters |
| 44 | + * @return the file path of the generate classpath.jar |
| 45 | + * @throws IOException Some errors occur during generating the classpath.jar |
| 46 | + */ |
| 47 | + public static synchronized Path generateClasspathJar(String[] classPaths) throws IOException { |
| 48 | + List<String> classpathUrls = new ArrayList<>(); |
| 49 | + for (String classpath : classPaths) { |
| 50 | + classpathUrls.add(AdapterUtils.toUrl(classpath)); |
| 51 | + } |
| 52 | + |
| 53 | + Manifest manifest = new Manifest(); |
| 54 | + Attributes attributes = manifest.getMainAttributes(); |
| 55 | + attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); |
| 56 | + // In jar manifest, the absolute path C:\a.jar should be converted to the url style file:///C:/a.jar |
| 57 | + String classpathValue = String.join(" ", classpathUrls); |
| 58 | + attributes.put(Attributes.Name.CLASS_PATH, classpathValue); |
| 59 | + String baseName = "cp_" + getMd5(classpathValue); |
| 60 | + cleanupTempFiles(baseName, ".jar"); |
| 61 | + Path tempfile = createTempFile(baseName, ".jar"); |
| 62 | + JarOutputStream jar = new JarOutputStream(new FileOutputStream(tempfile.toFile()), manifest); |
| 63 | + jar.close(); |
| 64 | + lockTempLaunchFile(tempfile); |
| 65 | + |
| 66 | + return tempfile; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Generate the classpath parameters to a temporary argfile file. |
| 71 | + * @param classPaths - the classpath parameters |
| 72 | + * @param modulePaths - the modulepath parameters |
| 73 | + * @return the file path of the generated argfile |
| 74 | + * @throws IOException Some errors occur during generating the argfile |
| 75 | + */ |
| 76 | + public static synchronized Path generateArgfile(String[] classPaths, String[] modulePaths) throws IOException { |
| 77 | + String argfile = ""; |
| 78 | + if (ArrayUtils.isNotEmpty(classPaths)) { |
| 79 | + argfile = "-classpath \"" + String.join(File.pathSeparator, classPaths) + "\""; |
| 80 | + } |
| 81 | + |
| 82 | + if (ArrayUtils.isNotEmpty(modulePaths)) { |
| 83 | + argfile = " --module-path \"" + String.join(File.pathSeparator, modulePaths) + "\""; |
| 84 | + } |
| 85 | + |
| 86 | + argfile = argfile.replace("\\", "\\\\"); |
| 87 | + String baseName = "cp_" + getMd5(argfile); |
| 88 | + cleanupTempFiles(baseName, ".argfile"); |
| 89 | + Path tempfile = createTempFile(baseName, ".argfile"); |
| 90 | + Files.write(tempfile, argfile.getBytes()); |
| 91 | + lockTempLaunchFile(tempfile); |
| 92 | + |
| 93 | + return tempfile; |
| 94 | + } |
| 95 | + |
| 96 | + public static void lockTempLaunchFile(Path tempFile) { |
| 97 | + if (tempFile != null) { |
| 98 | + tempFilesInUse.add(tempFile); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public static void releaseTempLaunchFile(Path tempFile) { |
| 103 | + if (tempFile != null) { |
| 104 | + tempFilesInUse.remove(tempFile); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static Path tmpdir = null; |
| 109 | + |
| 110 | + private static synchronized Path getTmpDir() throws IOException { |
| 111 | + if (tmpdir == null) { |
| 112 | + try { |
| 113 | + tmpdir = Paths.get(java.security.AccessController.doPrivileged(new GetPropertyAction("java.io.tmpdir"))); |
| 114 | + } catch (NullPointerException | InvalidPathException e) { |
| 115 | + Path tmpfile = Files.createTempFile("", ".tmp"); |
| 116 | + tmpdir = tmpfile.getParent(); |
| 117 | + try { |
| 118 | + Files.deleteIfExists(tmpfile); |
| 119 | + } catch (Exception ex) { |
| 120 | + // do nothing |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return tmpdir; |
| 126 | + } |
| 127 | + |
| 128 | + private static void cleanupTempFiles(String baseName, String suffix) throws IOException { |
| 129 | + for (int i = 0; ; i++) { |
| 130 | + Path tempFile = getTmpDir().resolve(baseName + (i == 0 ? "" : i) + suffix); |
| 131 | + if (tempFilesInUse.contains(tempFile)) { |
| 132 | + continue; |
| 133 | + } else if (!Files.exists(tempFile)) { |
| 134 | + break; |
| 135 | + } else { |
| 136 | + try { |
| 137 | + // delete the old temp file |
| 138 | + Files.deleteIfExists(tempFile); |
| 139 | + } catch (Exception e) { |
| 140 | + // do nothing |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private static Path createTempFile(String baseName, String suffix) throws IOException { |
| 147 | + // loop until the temp file can be created |
| 148 | + for (int i = 0; ; i++) { |
| 149 | + Path tempFile = getTmpDir().resolve(baseName + (i == 0 ? "" : i) + suffix); |
| 150 | + if (!Files.exists(tempFile)) { |
| 151 | + return Files.createFile(tempFile); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private static String getMd5(String input) { |
| 157 | + try { |
| 158 | + MessageDigest md = MessageDigest.getInstance("MD5"); |
| 159 | + byte[] messageDigest = md.digest(input.getBytes()); |
| 160 | + BigInteger md5 = new BigInteger(1, messageDigest); |
| 161 | + return md5.toString(Character.MAX_RADIX); |
| 162 | + } catch (NoSuchAlgorithmException e) { |
| 163 | + return Integer.toString(input.hashCode(), Character.MAX_RADIX); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments