Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classloader temp directory for classpath initialization #79

Merged
merged 1 commit into from
Dec 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions tools/loader/src/main/java/com/kumuluz/ee/loader/EeClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* out of or in connection with the software or the use or other dealings in the
* software. See the License for the specific language governing permissions and
* limitations under the License.
*/
*/
package com.kumuluz.ee.loader;

import com.kumuluz.ee.loader.exception.EeClassLoaderException;
Expand All @@ -27,6 +27,8 @@
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.security.CodeSource;
Expand All @@ -35,7 +37,6 @@
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Logger;

/**
* @author Benjamin Kastelic
Expand All @@ -46,12 +47,9 @@ public class EeClassLoader extends ClassLoader {
/**
* Directory name for temporary files.
*/
private static final String TMP_DIRECTORY = "EeClassLoader";

private Boolean DEBUG = false;

private static final String TMP_DIRECTORY = "tmp/EeClassLoader";
private final Thread mainThread = Thread.currentThread();

private Boolean DEBUG = false;
private File tempDir;
private List<JarFileInfo> jarFiles;
private Set<File> deleteOnExit;
Expand Down Expand Up @@ -141,16 +139,29 @@ public EeClassLoader(ClassLoader parent) {
debug(String.format("Initialised KumuluzEE classloader @%dms", System.currentTimeMillis() - startTime));
}

private File createTempFile(JarEntryInfo jarEntryInfo) throws EeClassLoaderException {
// Temp files directory:
// WinXP: C:/Documents and Settings/username/Local Settings/Temp/EeClassLoader
// Unix: /var/tmp/EeClassLoader
// Win7+: C:/Users/username/AppData/Local/Temp/EeClassLoader
private File createTempFile(JarEntryInfo jarEntryInfo) throws EeClassLoaderException, URISyntaxException, IOException {
// Create temp directory for classpath initialization
if (tempDir == null) {
File dir = new File(System.getProperty("java.io.tmpdir"), TMP_DIRECTORY);

ProtectionDomain protectionDomain = getClass().getProtectionDomain();
CodeSource codeSource = protectionDomain.getCodeSource();
URI location = (codeSource == null ? null : codeSource.getLocation().toURI());
String path = (location == null ? null : location.getSchemeSpecificPart());
if (path == null) {
throw new IllegalStateException("Unable to determine code source archive");
}

File jarDir = new File(path);
String jarFolder = jarDir.getParentFile().getPath();

File dir = new File(jarFolder, TMP_DIRECTORY);

if (!dir.exists()) {
dir.mkdir();
dir.mkdirs();
}

dir.deleteOnExit();

chmod777(dir); // Unix - allow temp directory RW access to all users.
if (!dir.exists() || !dir.isDirectory()) {
throw new EeClassLoaderException("Cannot create temp directory " + dir.getAbsolutePath());
Expand Down Expand Up @@ -206,6 +217,8 @@ private void loadJar(final JarFileInfo jarFileInfo) {
throw new RuntimeException(String.format("Cannot load jar entries from jar %s", je.getName().toLowerCase()), e);
} catch (EeClassLoaderException e) {
throw new RuntimeException("ERROR on loading inner JAR: " + e.getMessageAll());
} catch (URISyntaxException e) {
throw new RuntimeException(String.format("Invalid path for %s", je.getName().toLowerCase()), e);
}
});
}
Expand Down Expand Up @@ -567,6 +580,10 @@ protected String findLibrary(String name) {
} catch (EeClassLoaderException e) {

debug(String.format("Failure to load native library %s: %s", name, e.toString()));
} catch (URISyntaxException e) {
throw new RuntimeException(String.format("Invalid path for %s", name), e);
} catch (IOException e) {
throw new RuntimeException("Unable to create temp directory: " + e.getMessage());
}
}
return null;
Expand Down