Skip to content

Commit

Permalink
Merge pull request #5 from TheItivitist/master
Browse files Browse the repository at this point in the history
Optimize startup time
  • Loading branch information
benoitdesire authored Sep 6, 2023
2 parents da0824f + dcd5a4b commit f9fe866
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 76 deletions.
27 changes: 1 addition & 26 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ subprojects {
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

group = "co.paralleluniverse"
version = "1.0.4-ullink5"
version = "1.0.4-ullink6"
status = "integration"
description = "Simple Java deployment"
ext.url = "https://github.com/puniverse/capsule"
Expand All @@ -48,10 +48,6 @@ subprojects {
ext.sonatypePassword = ""
}

configurations {
javancss
}

configurations {
[compile, runtime]*.resolutionStrategy {
failOnVersionConflict()
Expand All @@ -63,7 +59,6 @@ subprojects {
testImplementation 'org.truth0:truth:0.23'
testImplementation 'com.google.jimfs:jimfs:1.1'
testImplementation 'org.jooq:joor:0.9.6'
javancss 'org.codehaus.javancss:javancss:33.54'
}

test {
Expand Down Expand Up @@ -133,26 +128,6 @@ subprojects {
// options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
// }

task javancss(dependsOn: 'classes') {
doLast {
ant {
logging.level = LogLevel.INFO
taskdef(name: 'javancss', classname: 'javancss.JavancssAntTask', classpath: configurations.javancss.asPath)
javancss(
srcdir: 'src/main/java',
abortOnFail: 'true',
generateReport: 'true',
packageMetrics: 'true',
classMetrics: 'false',
functionMetrics: 'false'
// outputFile: reportName,
)
}
}
}

assemble.dependsOn javancss

///////// Publish Artifacts
apply plugin: 'maven'
apply plugin: 'signing'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
*/
package co.paralleluniverse.capsule;

import com.google.common.jimfs.Jimfs;
import static com.google.common.truth.Truth.assert_;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -22,7 +24,7 @@
import java.util.Properties;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;
import com.google.common.jimfs.Jimfs;

/**
*
Expand Down Expand Up @@ -50,7 +52,7 @@ public void testSimpleExtract() throws Exception {
.addEntry("lib/a.jar", emptyInputStream())
.addEntry("lib/b.class", emptyInputStream())
.addEntry("q/w/x.txt", emptyInputStream())
.addEntry("d\\f\\y.txt", emptyInputStream()) // test with Windows path
.addEntry("d/f/y.txt", emptyInputStream())
.addEntry("META-INF/x.txt", emptyInputStream());

List<String> args = list("hi", "there");
Expand Down
89 changes: 68 additions & 21 deletions capsule/src/main/java/Capsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
* http://www.eclipse.org/legal/epl-v10.html
*/

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableSet;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.EOFException;
Expand Down Expand Up @@ -42,6 +52,7 @@
import java.nio.channels.FileLock;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
Expand All @@ -59,40 +70,40 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Properties;
import java.util.Random;
import java.util.RandomAccess;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicReference;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.Properties;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.RandomAccess;
import java.util.concurrent.atomic.AtomicReference;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import static java.util.Collections.*;
import static java.util.Arrays.asList;

/**
* An application capsule.
* <p>
Expand Down Expand Up @@ -3693,20 +3704,39 @@ private static boolean shouldExtractFile(String fileName) {
return true;
}

private List<Path> listJar(Path jar, String glob, boolean regular) {
final long start = clock();
final List<Path> res = new ArrayList<>();
final Pattern p = Pattern.compile(globToRegex(glob));
try (ZipInputStream zis = openJarInputStream(jar)) {
for (ZipEntry entry; (entry = zis.getNextEntry()) != null;) {
if ((!regular || !entry.isDirectory()) && p.matcher(entry.getName()).matches())
res.add(path(entry.getName())); // new URL("jar", "", jar + "!/" + entry.getName())
}
} catch (IOException e) {
private List<Path> listJar(Path jar, String glob, boolean regular)
{
long start = clock();
try (FileSystem fileSystem = FileSystems.newFileSystem(jar, null))
{
Matcher matcher = Pattern.compile(globToRegex(glob)).matcher("");
return StreamSupport.stream(fileSystem.getRootDirectories().spliterator(), false)
.flatMap(rootDir -> {
try
{
return Files.walk(rootDir);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
})
.filter(p -> !regular || Files.isRegularFile(p))
.map(Path::toString)
.map(p -> p.startsWith("/") ? p.substring(1) : p)
.filter(p -> matcher.reset(p).matches())
.map(this::path)
.sorted()
.collect(Collectors.toList());
}
catch (IOException e)
{
throw rethrow(e);
}
time("listJar", start);
return res;
finally
{
time("listJar", start);
}
}

private Path mergeCapsule(Path wrapperCapsule, Path wrappedCapsule, Path outCapsule) throws IOException {
Expand Down Expand Up @@ -5431,7 +5461,8 @@ static Path createPathingJar(Path dir, List<Path> cp) {
try {
final Path absolutePath = dir.toAbsolutePath();
final List<String> paths = createPathingClassPath(absolutePath, cp);
final Path pathingJar = Files.createTempFile(absolutePath, "capsule_pathing_jar", ".jar");

final Path pathingJar = createTempFile(absolutePath, "capsule_pathing_jar", ".jar");
final Manifest man = new Manifest();
man.getMainAttributes().putValue(ATTR_MANIFEST_VERSION, "1.0");
man.getMainAttributes().putValue(ATTR_CLASS_PATH, join(paths, " "));
Expand All @@ -5443,6 +5474,22 @@ static Path createPathingJar(Path dir, List<Path> cp) {
}
}

private static Path createTempFile(Path dir, String prefix, String suffix) throws IOException
{
for (;;)
{
try
{
String rand = Long.toUnsignedString(new Random().nextLong(), 16);
return Files.createFile(dir.resolve(prefix + rand + suffix));
}
catch (FileAlreadyExistsException ex)
{
// ignore
}
}
}

private static List<String> createPathingClassPath(Path dir, List<Path> cp) {
boolean allPathsHaveSameRoot = true;
for (Path p : cp) {
Expand Down
19 changes: 9 additions & 10 deletions capsule/src/test/java/CapsuleAgentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
* of the Eclipse Public License v1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
import capsule.test.Pair;
import co.paralleluniverse.capsule.Jar;
import co.paralleluniverse.capsule.test.CapsuleTestUtils;
import co.paralleluniverse.common.FlexibleClassLoader;
import co.paralleluniverse.common.JarClassLoader;
import co.paralleluniverse.common.PathClassLoader;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import capsule.test.Pair;
import co.paralleluniverse.capsule.Jar;
import co.paralleluniverse.capsule.test.CapsuleTestUtils;
import co.paralleluniverse.common.FlexibleClassLoader;
import co.paralleluniverse.common.JarClassLoader;
import co.paralleluniverse.common.PathClassLoader;

/**
* @author circlespainter
Expand Down
40 changes: 25 additions & 15 deletions capsule/src/test/java/CapsuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@
* http://www.eclipse.org/legal/epl-v10.html
*/

import co.paralleluniverse.capsule.Jar;
import co.paralleluniverse.capsule.test.CapsuleTestUtils;
import co.paralleluniverse.capsule.test.CapsuleTestUtils.StringPrintStream;
import static co.paralleluniverse.capsule.test.CapsuleTestUtils.*;
import static co.paralleluniverse.capsule.test.CapsuleTestUtils.DEVNULL;
import static co.paralleluniverse.capsule.test.CapsuleTestUtils.isCI;
import static co.paralleluniverse.capsule.test.CapsuleTestUtils.resetOutputStreams;
import static co.paralleluniverse.capsule.test.CapsuleTestUtils.rethrow;
import static co.paralleluniverse.capsule.test.CapsuleTestUtils.setCacheDir;
import static co.paralleluniverse.capsule.test.CapsuleTestUtils.setProperties;
import static co.paralleluniverse.capsule.test.CapsuleTestUtils.setSTDERR;
import static co.paralleluniverse.capsule.test.CapsuleTestUtils.setSTDOUT;
import static com.google.common.truth.Truth.assert_;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

import co.paralleluniverse.common.ZipFS;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import java.io.IOException;
import java.io.InputStream;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.nio.file.FileSystem;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
Expand All @@ -35,14 +43,16 @@
import java.util.Properties;
import java.util.Set;
import java.util.jar.JarInputStream;
import org.joor.Reflect;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import org.junit.Before;
import static com.google.common.truth.Truth.*;
import java.nio.file.Paths;
import org.joor.Reflect;
import org.junit.Test;
import co.paralleluniverse.capsule.Jar;
import co.paralleluniverse.capsule.test.CapsuleTestUtils;
import co.paralleluniverse.capsule.test.CapsuleTestUtils.StringPrintStream;
import co.paralleluniverse.common.ZipFS;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
//import static org.mockito.Mockito.*;

public class CapsuleTest {
Expand Down Expand Up @@ -86,7 +96,7 @@ public void testSimpleExtract() throws Exception {
.addEntry("lib/a.jar", emptyInputStream())
.addEntry("lib/b.class", emptyInputStream())
.addEntry("q/w/x.txt", emptyInputStream())
.addEntry("d\\f\\y.txt", emptyInputStream()) // test with Windows path
.addEntry("d/f/y.txt", emptyInputStream()) // test with Windows path
.addEntry("META-INF/x.txt", emptyInputStream());

List<String> args = list("hi", "there");
Expand Down

0 comments on commit f9fe866

Please sign in to comment.