/** * Return the directory to be used for application specific temp files. * @return the application temp directory */ public File getDir() { if (this.dir == null) { synchronized (this) { byte[] hash = generateHash(this.sourceClass); this.dir = new File(getTempDirectory(), toHexString(hash)); this.dir.mkdirs(); Assert.state(this.dir.exists(), () -> "Unable to create temp directory " + this.dir); } } return this.dir; }
@Override public String toString() { return getDir().getAbsolutePath(); }
private void update(MessageDigest digest, Object source) { if (source != null) { digest.update(getUpdateSourceBytes(source)); } }
public File getValidDirectory(boolean mkdirs) { File dir = getDirectory(); if (dir == null) { return new ApplicationTemp().getDir("servlet-sessions"); } if (!dir.isAbsolute()) { dir = new File(new ApplicationHome().getDir(), dir.getPath()); } if (!dir.exists() && mkdirs) { dir.mkdirs(); } assertDirectory(mkdirs, dir); return dir; }
private byte[] generateHash(Class<?> sourceClass) { ApplicationHome home = new ApplicationHome(sourceClass); MessageDigest digest; try { digest = MessageDigest.getInstance("SHA-1"); update(digest, home.getSource()); update(digest, home.getDir()); update(digest, System.getProperty("user.dir")); update(digest, System.getProperty("java.home")); update(digest, System.getProperty("java.class.path")); update(digest, System.getProperty("sun.java.command")); update(digest, System.getProperty("sun.boot.class.path")); return digest.digest(); } catch (Exception ex) { throw new IllegalStateException(ex); } }
/** * Return a sub-directory of the application temp. * @param subDir the sub-directory name * @return a sub-directory */ public File getDir(String subDir) { File dir = new File(getDir(), subDir); dir.mkdirs(); return dir; }
private byte[] getUpdateSourceBytes(Object source) { if (source instanceof File) { return getUpdateSourceBytes(((File) source).getAbsolutePath()); } return source.toString().getBytes(); }