Skip to content

Commit

Permalink
App should create its own temp dir (system temps can be periodiclly c…
Browse files Browse the repository at this point in the history
…leared).
  • Loading branch information
zvoneg authored and TFaga committed Dec 18, 2017
1 parent 0ca5258 commit 1e913de
Showing 1 changed file with 31 additions and 14 deletions.
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

0 comments on commit 1e913de

Please sign in to comment.