protected boolean checkAndSet(String candidate) { if (!check(candidate)) return false; // seems okay tmpdir = candidate; log.debug("TmpDirFinder: Selected tmp dir '"+candidate+"' as the best tmp working space"); return true; }
protected synchronized boolean find() { if (isFound()) return true; String customtmp = System.getProperty(BROOKLYN_OS_TMPDIR_PROPERTY); if (customtmp!=null) { if (checkAndSet(customtmp)) return true; log.warn("TmpDirFinder: Custom tmp directory '"+customtmp+"' in "+BROOKLYN_OS_TMPDIR_PROPERTY+" is not a valid tmp dir; ignoring"); } String systmp = System.getProperty("java.io.tmpdir"); boolean systmpWeird = (systmp.contains("/var/") || systmp.startsWith("/private")); if (!systmpWeird) if (checkAndSet(systmp)) return true; if (checkAndSet(File.separator+"tmp")) return true; if (systmpWeird) if (checkAndSet(systmp)) return true; try { String hometmp = mergePaths(home(), ".tmp"); File hometmpF = new File(hometmp); hometmpF.mkdirs(); if (checkAndSet(hometmp)) return true; } catch (Exception e) { log.debug("TmpDirFinder: Cannot create tmp dir in user's home dir: "+e); } return false; }
/** returns the best tmp dir to use; see {@link TmpDirFinder} for the logic * (and the explanation why this is needed!) */ public static String tmp() { Maybe<String> tmp = tmpdir.get(); if (tmp.isPresent()) return tmp.get(); tmpdir.useWithWarning(System.getProperty("java.io.tmpdir")); return tmp.get(); }
public Maybe<String> get() { if (isFallback()) log.debug("TmpDirFinder: using fallback tmp directory "+tmpdir, new Throwable("Caller using fallback tmp dir")); if (isFound()) return Maybe.of(tmpdir); if (find()) return Maybe.of(tmpdir); return Maybe.absent(newFailure("TmpDirFinder: No valid tmp dir can be found")); }