Created
January 1, 2014 05:28
-
-
Save mosheeshel/8205347 to your computer and use it in GitHub Desktop.
Locates a file inside your resources whether they are in a "normal" resource directory or inside a jar - untested, for reference only
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.acme.example; | |
import com.sun.javaws.Launcher; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.io.File; | |
import java.io.IOException; | |
import java.net.URISyntaxException; | |
import java.net.URL; | |
import java.util.Enumeration; | |
import java.util.jar.JarEntry; | |
import java.util.jar.JarFile; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: moshee | |
* Date: 31/12/13 | |
* Time: 18:36 | |
*/ | |
public class ResourceFileResolver { | |
private final Logger logger = LoggerFactory.getLogger(JavascriptResourceFileResolver.class); | |
public boolean checkIfResourceFileExists(String path, String fileInternalPath) { | |
try { | |
final File jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath()); | |
if(jarFile.isFile()) { // Run with JAR file | |
try { | |
final JarFile jar = new JarFile(jarFile); | |
final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar | |
while(entries.hasMoreElements()) { | |
final String name = entries.nextElement().getName(); | |
if (name.startsWith(path + "/") && name.endsWith(fileInternalPath)) { //filter according to the path | |
return true; | |
} | |
} | |
jar.close(); | |
} catch (IOException exp) { | |
logger.error("Problem with requested js file: " + path, exp); | |
} | |
} else { // Run with IDE | |
final URL url = Launcher.class.getResource("/" + path + "/" + fileInternalPath); | |
if (url != null) { | |
try { | |
final File apps = new File(url.toURI()); | |
return true; | |
} catch (URISyntaxException ex) { | |
// never happens | |
} | |
} | |
} | |
} catch (Exception exp) { | |
logger.error("Problem with requested js file: " + fileInternalPath, exp); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment