Skip to content

Commit

Permalink
Gradle: Self-extract required contents from Uber Jar (#1015)
Browse files Browse the repository at this point in the history
Closes #1007
  • Loading branch information
yukiisbored authored and mariobehling committed Jan 19, 2017
1 parent 7a48fb3 commit bc4463a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
16 changes: 16 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ jar {

shadowJar {
mergeServiceFiles()

from ('conf') {
into 'conf'
}

from ('html') {
into 'html'
}

from ('installation') {
into 'installation'
}

from ('ssi') {
into 'ssi'
}
}

repositories {
Expand Down
52 changes: 51 additions & 1 deletion src/org/loklak/LoklakServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;

import javax.servlet.MultipartConfigElement;
Expand Down Expand Up @@ -136,7 +140,9 @@


public class LoklakServer {


private final static String[] FOLDER_TO_EXTRACT = { "conf", "html", "installation", "ssi"};

public final static Set<String> blacklistedHosts = new ConcurrentHashSet<>();

private static Server server = null;
Expand Down Expand Up @@ -193,6 +199,9 @@ public static String getServerURI() {

public static void main(String[] args) throws Exception {
System.setProperty("java.awt.headless", "true"); // no awt used here so we can switch off that stuff

// extract contents
extractContents();

// init config, log and elasticsearch
Path data = FileSystems.getDefault().getPath("data");
Expand Down Expand Up @@ -341,6 +350,47 @@ public void run() {
// The main termination line is therefore inside the shutdown hook.
}

private static void extractContents() {
// Check if we're running inside a JAR file
if(!LoklakServer.class.getResource("LoklakServer.class").getPath().startsWith("file:"))
return;

try {
JarFile jar =
new JarFile(LoklakServer.class.getProtectionDomain().getCodeSource().getLocation().getPath());
Enumeration entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry file = (JarEntry) entries.nextElement();

for (String s : FOLDER_TO_EXTRACT) {
if (file.getName().startsWith(s + File.separator)) {
extract(jar, file);
continue;
}
}
}
} catch (IOException e) {
Log.getLog().warn("An exception has occurred while extracting contents from JAR File", e);
Log.getLog().info("Exiting due to fatal error ...");
System.exit(1);
}
}

private static void extract(JarFile jar, JarEntry file) throws IOException {
Path workingDirectory = Paths.get("").toAbsolutePath();
File target = new File(workingDirectory.toString() + File.separator + file.getName());

if (target.exists())
return;

if (file.isDirectory()) {
target.mkdir();
return;
}

Files.copy(jar.getInputStream(file), target.toPath());
}

// initialize harvester
private static void initializeHarvester() {
String type = DAO.getConfig("harvester.type", "classic");
Expand Down

0 comments on commit bc4463a

Please sign in to comment.