Skip to content

Commit

Permalink
SystemInfo is now in the System package.
Browse files Browse the repository at this point in the history
  • Loading branch information
igr committed Jun 16, 2018
1 parent d9ec2f1 commit 71c4b65
Show file tree
Hide file tree
Showing 37 changed files with 1,109 additions and 577 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

package jodd.typeconverter;

import jodd.system.SystemUtil;
import jodd.typeconverter.impl.URLConverter;
import jodd.util.SystemUtil;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -69,8 +69,8 @@ private static Collection<Arguments> testdata_testConvert_with_non_null_input()
final List<Arguments> params = new ArrayList<>();

params.add(Arguments.of(new URL("http://jodd.org/")));
params.add(Arguments.of(new File(SystemUtil.tempDir(), "jodd.txt")));
params.add(Arguments.of(new File(SystemUtil.tempDir(), "jodd.txt").toURI()));
params.add(Arguments.of(new File(SystemUtil.info().getTempDir(), "jodd.txt")));
params.add(Arguments.of(new File(SystemUtil.info().getTempDir(), "jodd.txt").toURI()));
params.add(Arguments.of("http://jodd.org/"));

return params;
Expand Down
1 change: 1 addition & 0 deletions jodd-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
testCompile lib.junit5_params
testCompile 'org.webjars:jquery:2.1.1'
testCompile 'commons-codec:commons-codec:1.11'
testCompile project(':jodd-bean')

perfCompile 'net.openhft:smoothie-map:1.3'
}
Expand Down
6 changes: 3 additions & 3 deletions jodd-core/src/main/java/jodd/io/FileNameUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

package jodd.io;

import jodd.system.SystemUtil;
import jodd.util.StringPool;
import jodd.util.SystemUtil;

import java.io.File;

Expand Down Expand Up @@ -984,13 +984,13 @@ public static String[] split(final String filename) {
public static String resolveHome(final String path) {
if (path.length() == 1) {
if (path.charAt(0) == '~') {
return SystemUtil.userHome();
return SystemUtil.info().getHomeDir();
}
return path;
}
if (path.length() >= 2) {
if ((path.charAt(0) == '~') && (path.charAt(1) == File.separatorChar)) {
return SystemUtil.userHome() + path.substring(1);
return SystemUtil.info().getHomeDir() + path.substring(1);
}
}
return path;
Expand Down
4 changes: 2 additions & 2 deletions jodd-core/src/main/java/jodd/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import jodd.core.JoddCore;
import jodd.crypt.DigestEngine;
import jodd.net.URLDecoder;
import jodd.system.SystemUtil;
import jodd.util.StringPool;
import jodd.util.StringUtil;
import jodd.util.SystemUtil;

import java.io.BufferedReader;
import java.io.BufferedWriter;
Expand Down Expand Up @@ -70,7 +70,7 @@ public class FileUtil {
* Simple factory for {@link File} objects but with home resolving.
*/
public static File file(String fileName) {
fileName = StringUtil.replace(fileName, USER_HOME, SystemUtil.userHome());
fileName = StringUtil.replace(fileName, USER_HOME, SystemUtil.info().getHomeDir());
return new File(fileName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import jodd.io.upload.FileUpload;
import jodd.io.upload.FileUploadFactory;
import jodd.io.upload.MultipartRequestInputStream;
import jodd.util.SystemUtil;
import jodd.system.SystemUtil;

import java.io.File;
import java.io.IOException;
Expand All @@ -43,7 +43,7 @@ public class DiskFileUploadFactory implements FileUploadFactory {
protected int maxFileSize = 102400;

public DiskFileUploadFactory() throws IOException {
this(SystemUtil.tempDir());
this(SystemUtil.info().getTempDir());
}

public DiskFileUploadFactory(final String destFolder) throws IOException {
Expand All @@ -59,7 +59,7 @@ public DiskFileUploadFactory(final String destFolder, final int maxFileSize) thr

public DiskFileUploadFactory setUploadDir(String destFolder) throws IOException {
if (destFolder == null) {
destFolder = SystemUtil.tempDir();
destFolder = SystemUtil.info().getTempDir();
}
File destination = new File(destFolder);
if (!destination.exists()) {
Expand Down
90 changes: 90 additions & 0 deletions jodd-core/src/main/java/jodd/system/HostInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2003-present, Jodd Team (http://jodd.org)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package jodd.system;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* Host information.
*/
abstract class HostInfo {

private final String HOST_NAME;
private final String HOST_ADDRESS;

public HostInfo() {
String hostName;
String hostAddress;

try {
final InetAddress localhost = InetAddress.getLocalHost();

hostName = localhost.getHostName();
hostAddress = localhost.getHostAddress();
}
catch (UnknownHostException uhex) {
hostName = "localhost";
hostAddress = "127.0.0.1";
}

this.HOST_NAME = hostName;
this.HOST_ADDRESS = hostAddress;
}

/**
* Returns host name.
*/
public final String getHostName() {
return HOST_NAME;
}

/**
* Returns host IP address.
*/
public final String getHostAddress() {
return HOST_ADDRESS;
}

// ---------------------------------------------------------------- util

protected String nosep(final String in) {
if (in.endsWith(File.separator)) {
return in.substring(0, in.length() - 1);
}
return in;
}

// ---------------------------------------------------------------- toString

@Override
public String toString() {
return "\nHost name: " + getHostName() +
"\nHost address: " + getHostAddress();
}

}
189 changes: 189 additions & 0 deletions jodd-core/src/main/java/jodd/system/JavaInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Copyright (c) 2003-present, Jodd Team (http://jodd.org)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package jodd.system;


import java.util.ArrayList;

abstract class JavaInfo extends HostInfo {

private final String JAVA_VERSION = SystemUtil.get("java.version");
private final int JAVA_VERSION_NUMBER = detectJavaVersionNumber();
private final String JAVA_VENDOR = SystemUtil.get("java.vendor");
private final String JAVA_VENDOR_URL = SystemUtil.get("java.vendor.url");
private final String JAVA_SPECIFICATION_VERSION = SystemUtil.get("java.specification.version");
private final String JAVA_SPECIFICATION_NAME = SystemUtil.get("java.specification.name");
private final String JAVA_SPECIFICATION_VENDOR = SystemUtil.get("java.specification.vendor");
private final String[] JRE_PACKAGES = buildJrePackages(JAVA_VERSION_NUMBER);

/**
* Returns Java version string, as specified in system property.
* Returned string contain major version, minor version and revision.
*/
public String getJavaVersion() {
return JAVA_VERSION;
}

/**
* Returns unified Java version as an integer.
*/
public int getJavaVersionNumber() {
return JAVA_VERSION_NUMBER;
}

/**
* Returns Java vendor.
*/
public String getJavaVendor() {
return JAVA_VENDOR;
}

/**
* Returns Java vendor URL.
*/
public String getJavaVendorURL() {
return JAVA_VENDOR_URL;
}

/**
* Retrieves the version of the currently running JVM.
*/
public String getJavaSpecificationVersion() {
return JAVA_SPECIFICATION_VERSION;
}

public final String getJavaSpecificationName() {
return JAVA_SPECIFICATION_NAME;
}

public final String getJavaSpecificationVendor() {
return JAVA_SPECIFICATION_VENDOR;
}

// ---------------------------------------------------------------- packages

/**
* Returns list of packages, build into runtime jars.
*/
public String[] getJrePackages() {
return JRE_PACKAGES;
}

/**
* Builds a set of java core packages.
*/
private String[] buildJrePackages(final int javaVersionNumber) {
final ArrayList<String> packages = new ArrayList<>();

switch (javaVersionNumber) {
case 9:
case 8:
case 7:
case 6:
case 5:
// in Java1.5, the apache stuff moved
packages.add("com.sun.org.apache");
// fall through...
case 4:
if (javaVersionNumber == 4) {
packages.add("org.apache.crimson");
packages.add("org.apache.xalan");
packages.add("org.apache.xml");
packages.add("org.apache.xpath");
}
packages.add("org.ietf.jgss");
packages.add("org.w3c.dom");
packages.add("org.xml.sax");
// fall through...
case 3:
packages.add("org.omg");
packages.add("com.sun.corba");
packages.add("com.sun.jndi");
packages.add("com.sun.media");
packages.add("com.sun.naming");
packages.add("com.sun.org.omg");
packages.add("com.sun.rmi");
packages.add("sunw.io");
packages.add("sunw.util");
// fall through...
case 2:
packages.add("com.sun.java");
packages.add("com.sun.image");
// fall through...
case 1:
default:
// core stuff
packages.add("sun");
packages.add("java");
packages.add("javax");
break;
}

return packages.toArray(new String[0]);
}



// ---------------------------------------------------------------- java checks

private int detectJavaVersionNumber() {
if (JAVA_VERSION.startsWith("1.")) {
// up to java 8
final int index = JAVA_VERSION.indexOf('.', 2);
return Integer.parseInt(JAVA_VERSION.substring(2, index));
} else {
final int index = JAVA_VERSION.indexOf('.');
return Integer.parseInt(JAVA_VERSION.substring(0, index));
}
}

/**
* Checks if the currently running JVM is at least compliant
* with provided JDK version.
*/
public boolean isAtLeastJavaVersion(final int version) {
return JAVA_VERSION_NUMBER >= version;
}

/**
* Checks if the currently running JVM is equal to provided version.
*/
public boolean isJavaVersion(final int version) {
return JAVA_VERSION_NUMBER == version;
}

@Override
public String toString() {
return
super.toString() +
"\nJava Version: " + getJavaVersion() +
"\nJava Vendor: " + getJavaVendor() +
"\nJava Vendor URL: " + getJavaVendorURL() +
"\nJava Spec. Name: " + getJavaSpecificationName() +
"\nJava Spec. Version: " + getJavaSpecificationVersion() +
"\nJava Spec. Vendor: " + getJavaSpecificationVendor();
}
}
Loading

0 comments on commit 71c4b65

Please sign in to comment.