/** * Return if this version is equal to or newer than a given version. * @param version the version to compare * @return {@code true} if this version is equal to or newer than {@code version} */ public boolean isEqualOrNewerThan(JavaVersion version) { return compareTo(version) >= 0; }
/** * Create a new {@link ApplicationHome} instance for the specified source class. * @param sourceClass the source class or {@code null} */ public ApplicationHome(Class<?> sourceClass) { this.source = findSource((sourceClass != null) ? sourceClass : getStartClass()); this.dir = findHomeDir(this.source); }
@Override public String getValue(SpringApplicationEvent event) { return SystemProperties.get(this.properties); }
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); } }
private String getPid() { return getValue(" with PID ", () -> new ApplicationPid().toString()); }
@Override public String toString() { return getDir().toString(); }
private void writePidFile(SpringApplicationEvent event) throws IOException { File pidFile = this.file; String override = getProperty(event, FILE_PROPERTIES); if (override != null) { pidFile = new File(override); } new ApplicationPid().write(pidFile); pidFile.deleteOnExit(); }
@Override public String toString() { return getDir().getAbsolutePath(); }
public ApplicationPid() { this.pid = getPid(); }
private void update(MessageDigest digest, Object source) { if (source != null) { digest.update(getUpdateSourceBytes(source)); } }
private File findHomeDir(File source) { File homeDir = source; homeDir = (homeDir != null) ? homeDir : findDefaultHomeDir(); if (homeDir.isFile()) { homeDir = homeDir.getParentFile(); } homeDir = homeDir.exists() ? homeDir : new File("."); return homeDir.getAbsoluteFile(); }
/** * Returns the {@link JavaVersion} of the current runtime. * @return the {@link JavaVersion} */ public static JavaVersion getJavaVersion() { List<JavaVersion> candidates = Arrays.asList(JavaVersion.values()); Collections.reverse(candidates); for (JavaVersion candidate : candidates) { if (candidate.available) { return candidate; } } return EIGHT; }
private void assertCanOverwrite(File file) throws IOException { if (!file.canWrite() || !canWritePosixFile(file)) { throw new FileNotFoundException(file.toString() + " (permission denied)"); } }
private File findSource(URL location) throws IOException { URLConnection connection = location.openConnection(); if (connection instanceof JarURLConnection) { return getRootJarFile(((JarURLConnection) connection).getJarFile()); } return new File(location.getPath()); }
private Class<?> getStartClass() { try { ClassLoader classLoader = getClass().getClassLoader(); return getStartClass(classLoader.getResources("META-INF/MANIFEST.MF")); } catch (Exception ex) { return null; } }
/** * Create a new {@link WebServerPortFileWriter} instance with a specified file. * @param file the file containing port */ public WebServerPortFileWriter(File file) { Assert.notNull(file, "File must not be null"); String override = SystemProperties.get(PROPERTY_VARIABLES); if (override != null) { this.file = new File(override); } else { this.file = file; } }
public void apply(LogFile logFile) { PropertyResolver resolver = getPropertyResolver(); setSystemProperty(resolver, EXCEPTION_CONVERSION_WORD, "exception-conversion-word"); setSystemProperty(PID_KEY, new ApplicationPid().toString()); setSystemProperty(resolver, CONSOLE_LOG_PATTERN, "pattern.console"); setSystemProperty(resolver, FILE_LOG_PATTERN, "pattern.file"); setSystemProperty(resolver, FILE_MAX_HISTORY, "file.max-history"); setSystemProperty(resolver, FILE_MAX_SIZE, "file.max-size"); setSystemProperty(resolver, LOG_LEVEL_PATTERN, "pattern.level"); setSystemProperty(resolver, LOG_DATEFORMAT_PATTERN, "pattern.dateformat"); if (logFile != null) { logFile.applyToSystemProperties(); } }
/** * Return if this version is older than a given version. * @param version the version to compare * @return {@code true} if this version is older than {@code version} */ public boolean isOlderThan(JavaVersion version) { return compareTo(version) < 0; }
/** * 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; }