[hibernate-commits] Hibernate SVN: r19769 - in core/branches/gradle2/buildSrc/src/main: java and 4 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Jun 18 19:22:21 EDT 2010


Author: steve.ebersole at jboss.com
Date: 2010-06-18 19:22:21 -0400 (Fri, 18 Jun 2010)
New Revision: 19769

Added:
   core/branches/gradle2/buildSrc/src/main/java/
   core/branches/gradle2/buildSrc/src/main/java/org/
   core/branches/gradle2/buildSrc/src/main/java/org/hibernate/
   core/branches/gradle2/buildSrc/src/main/java/org/hibernate/gradle/
   core/branches/gradle2/buildSrc/src/main/java/org/hibernate/gradle/util/
   core/branches/gradle2/buildSrc/src/main/java/org/hibernate/gradle/util/JavaVersion.java
   core/branches/gradle2/buildSrc/src/main/java/org/hibernate/gradle/util/Jdk.java
Log:
helpers to handle mixed jdk versions

Added: core/branches/gradle2/buildSrc/src/main/java/org/hibernate/gradle/util/JavaVersion.java
===================================================================
--- core/branches/gradle2/buildSrc/src/main/java/org/hibernate/gradle/util/JavaVersion.java	                        (rev 0)
+++ core/branches/gradle2/buildSrc/src/main/java/org/hibernate/gradle/util/JavaVersion.java	2010-06-18 23:22:21 UTC (rev 19769)
@@ -0,0 +1,41 @@
+package org.hibernate.gradle.util;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class JavaVersion {
+	public static enum Family {
+		JAVA6( 6 ),
+		JAVA5( 5 );
+
+		private final int code;
+
+		private Family(int code) {
+			this.code = code;
+		}
+	}
+
+	private final String fullVersionString;
+	private final Family family;
+
+	public JavaVersion(String javaVersionString) {
+		this.fullVersionString = javaVersionString;
+		family = fullVersionString.startsWith( "1.6" )
+				? Family.JAVA6
+				: Family.JAVA5;
+	}
+
+	public String getFullVersionString() {
+		return fullVersionString;
+	}
+
+	public Family getFamily() {
+		return family;
+	}
+
+	public boolean isAtLeast(Family family) {
+		return getFamily().code >= family.code;
+	}
+}

Added: core/branches/gradle2/buildSrc/src/main/java/org/hibernate/gradle/util/Jdk.java
===================================================================
--- core/branches/gradle2/buildSrc/src/main/java/org/hibernate/gradle/util/Jdk.java	                        (rev 0)
+++ core/branches/gradle2/buildSrc/src/main/java/org/hibernate/gradle/util/Jdk.java	2010-06-18 23:22:21 UTC (rev 19769)
@@ -0,0 +1,138 @@
+package org.hibernate.gradle.util;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+
+import org.apache.tools.ant.taskdefs.condition.Os;
+import org.apache.tools.ant.util.FileUtils;
+import org.gradle.process.internal.DefaultExecHandle;
+import org.gradle.process.internal.ExecHandle;
+import org.gradle.process.internal.ExecHandleBuilder;
+
+/**
+ * Models path information for a particular JDK install.
+ * <p/>
+ * Copied largely from {@link org.gradle.util.Jvm} and {@link org.apache.tools.ant.util.JavaEnvUtils}.  The main
+ * difference is that those classes are static, based solely on the reported "java.home" sys prop.  Also, Ant's
+ * JavaEnvUtils allows for use of either a JRE or JDK; we do not care about allowing for a JRE-only set up here.
+ *
+ *
+ * @author Steve Ebersole
+ */
+public class Jdk {
+	private static final boolean IS_DOS = Os.isFamily( "dos" );
+	private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
+
+	private final File jdkHome;
+	private final JavaVersion version;
+
+	public Jdk(File jdkHome) {
+		this.jdkHome = jdkHome;
+		if ( !jdkHome.exists() ) {
+			throw new IllegalArgumentException( "Invalid path specified for JDK home; " + jdkHome.getAbsolutePath() + " did not exist" );
+		}
+		this.version = determineJdkVersion();
+	}
+
+	public Jdk(String jdkHomePath) {
+		this( new File( jdkHomePath ) );
+	}
+
+    public File getJavaExecutable() {
+        return new File( getJdkExecutable( "java" ) );
+    }
+
+	public File getJavacExecutable() {
+		return new File( getJdkExecutable( "javac" ) );
+	}
+
+    public File getJavadocExecutable() {
+        return new File( getJdkExecutable( "javadoc" ) );
+    }
+
+	protected String getJdkExecutable(String command) {
+		File executable = findInDir( jdkHome + "/bin", command );
+
+		if ( executable == null ) {
+			executable = findInDir( jdkHome + "/../bin", command );
+		}
+
+		if ( executable != null ) {
+			return executable.getAbsolutePath();
+		}
+		else {
+			// Unfortunately on Windows java.home doesn't always refer
+			// to the correct location, so we need to fall back to
+			// assuming java is somewhere on the PATH.
+			return addExtension( command );
+		}
+	}
+
+	private static File findInDir(String dirName, String commandName) {
+		File dir = FILE_UTILS.normalize(dirName);
+		File executable = null;
+		if (dir.exists()) {
+			executable = new File(dir, addExtension(commandName));
+			if (!executable.exists()) {
+				executable = null;
+			}
+		}
+		return executable;
+	}
+
+	private static String addExtension(String command) {
+		// This is the most common extension case - exe for windows and OS/2,
+		// nothing for *nix.
+		return command + (IS_DOS ? ".exe" : "");
+	}
+
+	private JavaVersion determineJdkVersion() {
+		String javaVersionString = extractFromSunJdk();
+		if ( javaVersionString == null ) {
+			throw new RuntimeException( "Could not determine Java version" );
+		}
+		return new JavaVersion( javaVersionString );
+	}
+
+	private String extractFromSunJdk() {
+		String version = null;
+		final String key = "java version \"";
+		try {
+			File javaCommand = getJavaExecutable();
+			ExecHandleBuilder execHandleBuilder = new ExecHandleBuilder();
+			execHandleBuilder.commandLine( javaCommand.getAbsolutePath(), "-version" );
+			ExecHandle execHandle = execHandleBuilder.build();
+			execHandle.start();
+//			Process javaProcess = Runtime.getRuntime().exec( new String[] { javaCommand.getAbsolutePath(), "-version" } );
+			Process javaProcess = Runtime.getRuntime().exec( javaCommand.getAbsolutePath() + " -version" );
+
+			try {
+				BufferedReader br = new BufferedReader( new InputStreamReader( javaProcess.getErrorStream() ) );
+				String line;
+				while ( (line = br.readLine()) != null) {
+					if ( version == null && line.startsWith( key ) ) {
+						version = line.substring( key.length(), line.length() - 1 );
+					}
+				}
+
+				br = new BufferedReader( new InputStreamReader( javaProcess.getInputStream() ) );
+				while ( (line = br.readLine()) != null) {
+					if ( version == null && line.startsWith( key ) ) {
+						version = line.substring( key.length(), line.length() - 1 );
+					}
+				}
+			}
+			finally {
+				javaProcess.destroy();
+			}
+		}
+		catch ( IOException e ) {
+			throw new RuntimeException( "Unable to determine Java version", e );
+		}
+		return version;
+	}
+}



More information about the hibernate-commits mailing list