Skip to content

Commit

Permalink
Merge branch '1.2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Oct 9, 2015
2 parents 9973147 + 888fa90 commit 8948322
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2015 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.
Expand Down Expand Up @@ -27,6 +27,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.jar.JarEntry;
import java.util.jar.Manifest;

Expand All @@ -40,6 +41,7 @@
* {@link Archive} implementation backed by a {@link JarFile}.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
public class JarFileArchive extends Archive {

Expand All @@ -53,6 +55,8 @@ public class JarFileArchive extends Archive {

private URL url;

private File tempUnpackFolder;

public JarFileArchive(File file) throws IOException {
this(file, null);
}
Expand Down Expand Up @@ -123,10 +127,25 @@ private Archive getUnpackedNestedArchive(JarEntryData data) throws IOException {
}

private File getTempUnpackFolder() {
File tempFolder = new File(System.getProperty("java.io.tmpdir"));
File unpackFolder = new File(tempFolder, "spring-boot-libs");
unpackFolder.mkdirs();
return unpackFolder;
if (this.tempUnpackFolder == null) {
File tempFolder = new File(System.getProperty("java.io.tmpdir"));
this.tempUnpackFolder = createUnpackFolder(tempFolder);
}
return this.tempUnpackFolder;
}

private File createUnpackFolder(File parent) {
int attempts = 0;
while (attempts++ < 1000) {
String fileName = new File(this.jarFile.getName()).getName();
File unpackFolder = new File(parent,
fileName + "-spring-boot-libs-" + UUID.randomUUID());
if (unpackFolder.mkdirs()) {
return unpackFolder;
}
}
throw new IllegalStateException(
"Failed to create unpack folder in directory '" + parent + "'");
}

private void unpack(JarEntryData data, File file) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,33 @@ public static void createTestJar(File file, boolean unpackNested) throws Excepti
writeDirEntry(jarOutputStream, "special/");
writeEntry(jarOutputStream, "special/\u00EB.dat", '\u00EB');

JarEntry nestedEntry = new JarEntry("nested.jar");
byte[] nestedJarData = getNestedJarData();
nestedEntry.setSize(nestedJarData.length);
nestedEntry.setCompressedSize(nestedJarData.length);
if (unpackNested) {
nestedEntry.setComment("UNPACK:0000000000000000000000000000000000000000");
}
CRC32 crc32 = new CRC32();
crc32.update(nestedJarData);
nestedEntry.setCrc(crc32.getValue());

nestedEntry.setMethod(ZipEntry.STORED);
jarOutputStream.putNextEntry(nestedEntry);
jarOutputStream.write(nestedJarData);
jarOutputStream.closeEntry();
writeNestedEntry("nested.jar", unpackNested, jarOutputStream);
writeNestedEntry("another-nested.jar", unpackNested, jarOutputStream);
}
finally {
jarOutputStream.close();
}
}

private static void writeNestedEntry(String name, boolean unpackNested,
JarOutputStream jarOutputStream) throws Exception, IOException {
JarEntry nestedEntry = new JarEntry(name);
byte[] nestedJarData = getNestedJarData();
nestedEntry.setSize(nestedJarData.length);
nestedEntry.setCompressedSize(nestedJarData.length);
if (unpackNested) {
nestedEntry.setComment("UNPACK:0000000000000000000000000000000000000000");
}
CRC32 crc32 = new CRC32();
crc32.update(nestedJarData);
nestedEntry.setCrc(crc32.getValue());

nestedEntry.setMethod(ZipEntry.STORED);
jarOutputStream.putNextEntry(nestedEntry);
jarOutputStream.write(nestedJarData);
jarOutputStream.closeEntry();
}

private static byte[] getNestedJarData() throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
JarOutputStream jarOutputStream = new JarOutputStream(byteArrayOutputStream);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2015 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.
Expand Down Expand Up @@ -31,13 +31,16 @@

import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;

/**
* Tests for {@link JarFileArchive}.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
public class JarFileArchiveTests {

Expand All @@ -58,7 +61,6 @@ public void setup() throws Exception {
private void setup(boolean unpackNested) throws Exception {
this.rootJarFile = this.temporaryFolder.newFile();
this.rootJarFileUrl = this.rootJarFile.toURI().toString();
System.out.println(this.rootJarFileUrl);
TestJarCreator.createTestJar(this.rootJarFile, unpackNested);
this.archive = new JarFileArchive(this.rootJarFile);
}
Expand All @@ -72,7 +74,7 @@ public void getManifest() throws Exception {
@Test
public void getEntries() throws Exception {
Map<String, Archive.Entry> entries = getEntriesMap(this.archive);
assertThat(entries.size(), equalTo(9));
assertThat(entries.size(), equalTo(10));
}

@Test
Expand All @@ -98,6 +100,31 @@ public void getNestedUnpackedArchive() throws Exception {
assertThat(nested.getUrl().toString(), endsWith(".jar"));
}

@Test
public void unpackedLocationsAreUniquePerArchive() throws Exception {
setup(true);
Entry entry = getEntriesMap(this.archive).get("nested.jar");
URL firstNested = this.archive.getNestedArchive(entry).getUrl();
setup(true);
entry = getEntriesMap(this.archive).get("nested.jar");
URL secondNested = this.archive.getNestedArchive(entry).getUrl();
assertThat(secondNested, is(not(equalTo(firstNested))));
}

@Test
public void unpackedLocationsFromSameArchiveShareSameParent() throws Exception {
setup(true);
File nested = new File(this.archive
.getNestedArchive(getEntriesMap(this.archive).get("nested.jar")).getUrl()
.toURI());
File anotherNested = new File(
this.archive
.getNestedArchive(
getEntriesMap(this.archive).get("another-nested.jar"))
.getUrl().toURI());
assertThat(nested.getParent(), is(equalTo(anotherNested.getParent())));
}

@Test
public void getFilteredArchive() throws Exception {
Archive filteredArchive = this.archive
Expand Down

0 comments on commit 8948322

Please sign in to comment.