Skip to content

Commit 40a13ee

Browse files
Reuse the temp file name for the same project when shorten the classpath (#298)
* Reuse the temp file name for the same project when shorten the classpath Signed-off-by: Jinbo Wang <[email protected]> * make checkstyle happy Signed-off-by: Jinbo Wang <[email protected]>
1 parent 95c6e3e commit 40a13ee

File tree

1 file changed

+57
-4
lines changed
  • com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter

1 file changed

+57
-4
lines changed

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/AdapterUtils.java

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017 Microsoft Corporation and others.
2+
* Copyright (c) 2017-2019 Microsoft Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -15,6 +15,7 @@
1515
import java.io.FileOutputStream;
1616
import java.io.IOException;
1717
import java.io.UnsupportedEncodingException;
18+
import java.math.BigInteger;
1819
import java.net.MalformedURLException;
1920
import java.net.URI;
2021
import java.net.URISyntaxException;
@@ -45,6 +46,8 @@
4546
import com.microsoft.java.debug.core.protocol.Responses;
4647
import com.microsoft.java.debug.core.protocol.Types;
4748

49+
import sun.security.action.GetPropertyAction;
50+
4851
public class AdapterUtils {
4952
private static final String OS_NAME = System.getProperty("os.name", "").toLowerCase();
5053
private static final Pattern ENCLOSING_CLASS_REGEX = Pattern.compile("^([^\\$]*)");
@@ -312,8 +315,9 @@ public static Path generateClasspathJar(String[] classPaths) throws IOException
312315
Attributes attributes = manifest.getMainAttributes();
313316
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
314317
// In jar manifest, the absolute path C:\a.jar should be converted to the url style file:///C:/a.jar
315-
attributes.put(Attributes.Name.CLASS_PATH, String.join(" ", classpathUrls));
316-
Path tempfile = Files.createTempFile("classpath_", ".jar");
318+
String classpathValue = String.join(" ", classpathUrls);
319+
attributes.put(Attributes.Name.CLASS_PATH, classpathValue);
320+
Path tempfile = createTempFile("cp_" + getMd5(classpathValue), ".jar");
317321
JarOutputStream jar = new JarOutputStream(new FileOutputStream(tempfile.toFile()), manifest);
318322
jar.close();
319323

@@ -338,9 +342,58 @@ public static Path generateArgfile(String[] classPaths, String[] modulePaths) th
338342
}
339343

340344
argfile = argfile.replace("\\", "\\\\");
341-
Path tempfile = Files.createTempFile("java_", ".argfile");
345+
Path tempfile = createTempFile("cp_" + getMd5(argfile), ".argfile");
342346
Files.write(tempfile, argfile.getBytes());
343347

344348
return tempfile;
345349
}
350+
351+
private static Path tmpdir = null;
352+
353+
private static synchronized Path getTmpDir() throws IOException {
354+
if (tmpdir == null) {
355+
try {
356+
tmpdir = Paths.get(java.security.AccessController.doPrivileged(new GetPropertyAction("java.io.tmpdir")));
357+
} catch (NullPointerException | InvalidPathException e) {
358+
Path tmpfile = Files.createTempFile("", ".tmp");
359+
tmpdir = tmpfile.getParent();
360+
try {
361+
Files.deleteIfExists(tmpfile);
362+
} catch (Exception ex) {
363+
// do nothing
364+
}
365+
}
366+
}
367+
368+
return tmpdir;
369+
}
370+
371+
private static Path createTempFile(String baseName, String suffix) throws IOException {
372+
// loop until the temp file can be created
373+
SecurityManager sm = System.getSecurityManager();
374+
for (int i = 0; ; i++) {
375+
Path tempFile = getTmpDir().resolve(baseName + (i == 0 ? "" : i) + suffix);
376+
try {
377+
// delete the old temp file
378+
Files.deleteIfExists(tempFile);
379+
} catch (Exception e) {
380+
// do nothing
381+
}
382+
383+
if (!Files.exists(tempFile)) {
384+
return Files.createFile(tempFile);
385+
}
386+
}
387+
}
388+
389+
private static String getMd5(String input) {
390+
try {
391+
MessageDigest md = MessageDigest.getInstance("MD5");
392+
byte[] messageDigest = md.digest(input.getBytes());
393+
BigInteger md5 = new BigInteger(1, messageDigest);
394+
return md5.toString(Character.MAX_RADIX);
395+
} catch (NoSuchAlgorithmException e) {
396+
return Integer.toString(input.hashCode(), Character.MAX_RADIX);
397+
}
398+
}
346399
}

0 commit comments

Comments
 (0)