JavaRelease>>inDirectory refactored to not hard-code relative paths to java executable within JRE/JDK.
authorJan Vrany <jan.vrany@fit.cvut.cz>
Tue, 08 Apr 2014 14:59:37 +0200
changeset 3058 ccf0bb9b983b
parent 3057 cec6d766280e
child 3059 824804a0ae03
JavaRelease>>inDirectory refactored to not hard-code relative paths to java executable within JRE/JDK. Instead, let the individual release to convert java executable path to java home and other way around.
JavaRelease.st
--- a/JavaRelease.st	Tue Apr 08 09:34:44 2014 +0200
+++ b/JavaRelease.st	Tue Apr 08 14:59:37 2014 +0200
@@ -355,10 +355,31 @@
     "Created: / 11-11-2013 / 14:07:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
+forExecutable: javaExecutable
+    "Returns an instance of JavaRelease for given java executable 
+     (java.exe on Windows, java on UNIX). Return nil if javaExecutable
+     does not exits, not executable or version of that Java version is
+     not recognized/suppported."
+
+    javaExecutable notNil ifTrue:[
+        | releases |
+        releases := self allSubclasses reject:[:each | each isAbstract ] thenCollect:[:each | each new ].
+        releases sort:[:a :b | a priority > b priority ].
+        releases do:[:release |
+            (release validateJavaExecutable: javaExecutable) ifTrue:[
+                release javaExecutable: javaExecutable.
+                ^ release.
+            ].
+        ]
+    ].
+    ^ nil.
+
+    "Created: / 08-04-2014 / 14:12:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 inDirectory:javaHome
     "Returns an instance of JavaRelease for given javaHome or nil if
-     given javaHome does not point to Java installation directory"
-
+     given javaHome does not point to Java installation directory."
 
     javaHome notNil ifTrue:[
         | releases |
@@ -374,6 +395,7 @@
     ^ nil.
 
     "Created: / 11-11-2013 / 14:53:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (comment): / 08-04-2014 / 14:13:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 system
@@ -393,26 +415,7 @@
         javaExecutable isSymbolicLink ifTrue:[
             javaExecutable := javaExecutable physicalFilename asAbsoluteFilename.
         ].
-
-        javaHome :=  nil.
-        OperatingSystem isMSWINDOWSlike ifTrue:[
-            (javaExecutable pathName endsWith: 'jre\bin\java.exe') ifTrue:[
-                javaHome := javaExecutable pathName copyTo: javaExecutable pathName size - 'jre\bin\java.exe' size - 1.
-            ] ifFalse:[
-                (javaExecutable pathName endsWith: 'bin\java.exe') ifTrue:[
-                   javaHome := javaExecutable pathName copyTo: javaExecutable pathName size - 'bin\java.exe' size - 1.
-                ]
-            ].
-        ] ifFalse:[
-            (javaExecutable pathName endsWith: 'jre/bin/java') ifTrue:[
-                javaHome := javaExecutable pathName copyTo: javaExecutable pathName size - 'jre/bin/java' size - 1.
-            ] ifFalse:[
-                (javaExecutable pathName endsWith: 'bin/java') ifTrue:[
-                   javaHome := javaExecutable pathName copyTo: javaExecutable pathName size - 'bin/java' size - 1.
-                ]
-            ].
-        ].
-        System := self inDirectory: javaHome
+        System := self forExecutable: javaHome
     ].
     ^ System
 
@@ -421,7 +424,7 @@
     "
 
     "Created: / 11-11-2013 / 14:18:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 11-11-2013 / 16:03:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 08-04-2014 / 14:53:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaRelease class methodsFor:'instance creation-private'!
@@ -534,6 +537,43 @@
     "Created: / 12-02-2013 / 14:56:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
+javaExecutableForJavaHome: javaHomeArg
+    "For given java home (path to JRE root if JRE, or JDK root if part of full JDK) return
+     path to java executable (java.exe on Windows, java on UNIX). If no executable is found
+     in given root, return nil."
+
+    | javaHomeDirectory javaExecutableName javaExecutable |
+
+    "/ By default, java executable is either in <jdk root>/jre/bin or
+    "/ <jre root>/bin. So far only AppleJDK is different...
+
+    javaHomeArg isNil ifTrue:[ ^ nil ].
+    javaHomeDirectory := javaHomeArg asFilename asAbsoluteFilename.
+    javaHomeDirectory exists ifFalse:[ ^ nil ].
+    javaHomeDirectory isDirectory ifFalse:[ ^ nil ].
+    javaExecutableName := OperatingSystem isMSWINDOWSlike ifTrue:[ 'java.exe' ] ifFalse:[ 'java' ].
+
+    "/ Try JDK...
+    javaExecutable := javaHomeDirectory / 'jre' / 'bin' / javaExecutableName.
+    javaExecutable exists ifTrue:[ ^ javaExecutable ].
+
+    "/ Try JRE...
+    javaExecutable := javaHomeDirectory / 'bin' / javaExecutableName.
+    javaExecutable exists ifTrue:[ ^ javaExecutable ].
+
+    ^ nil
+                 
+    "
+    JavaRelease basicNew javaExecutableForJavaHome: (OperatingSystem pathOfCommand:'java') asFilename directory directory.
+    JavaRelease basicNew javaExecutableForJavaHome: '/usr/lib/jvm/java-7-openjdk-amd64/jre'
+    JavaRelease basicNew javaExecutableForJavaHome: '/usr/lib/jvm/java-7-openjdk-amd64'
+    JavaRelease basicNew javaExecutableForJavaHome: 'xxxx'
+    JavaRelease basicNew javaExecutableForJavaHome: '/usr/bin'
+    "
+
+    "Created: / 08-04-2014 / 14:44:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 javaExtDirs
 
     "Returns a default value of java.ext.dirs property"
@@ -602,6 +642,36 @@
     "Modified: / 11-02-2013 / 02:58:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
+javaHomeForJavaExecutable: javaExecutable
+    "For given java executable (java.exe on Windows, java on UNIX) return
+     a Java home (path to JRE root (if JRE) or JDK root (if part of full JDK).
+     Upon failure (executable does not seem to be a part of JRE nor JDK), return
+     nil."
+
+    | javaExecutableFilename dir |
+
+    "/ By default, java executable is either in <jdk root>/jre/bin or
+    "/ <jre root>/bin. So far only AppleJDK is different...
+
+    javaExecutable isNil ifTrue:[ ^ nil ].
+    javaExecutableFilename := javaExecutable asFilename asAbsoluteFilename.
+    javaExecutableFilename exists ifFalse:[ ^ nil ].
+    javaExecutableFilename isExecutable ifFalse:[ ^ nil ].
+    dir := javaExecutableFilename directory.
+    dir baseName ~= 'bin' ifTrue:[ ^ nil ].
+    dir := dir directory.
+    ^ dir baseName = 'jre' ifTrue:[ dir directory ] ifFalse:[ dir ]
+
+    "
+    JavaRelease basicNew javaHomeForJavaExecutable: (OperatingSystem pathOfCommand:'java')
+    JavaRelease basicNew javaHomeForJavaExecutable: '/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java'
+    JavaRelease basicNew javaHomeForJavaExecutable: 'xxxx'
+    JavaRelease basicNew javaHomeForJavaExecutable: '/usr'
+    "
+
+    "Created: / 08-04-2014 / 14:21:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 javaHomeOrNil
 
     "Answer a Java home"
@@ -1030,6 +1100,16 @@
 
 !JavaRelease methodsFor:'validating'!
 
+validateJavaExecutable: javaExecutable
+    "Validates given java executable (java.exe on Windows, java on UNIX). 
+     Return true, if given executable represents same version as receiver,
+     false otherwise."
+
+    ^ self validateJavaHome: (self javaHomeForJavaExecutable: javaExecutable)
+
+    "Created: / 08-04-2014 / 14:13:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 validateJavaHome1: home
 
     home isNil ifTrue:[ ^ false ].
@@ -1057,16 +1137,11 @@
 !
 
 validateJavaHome: home
-    | jreHome javaBinary javaVersionStream javaVersionString |
+    | javaBinary javaVersionStream javaVersionString |
 
     (self validateJavaHome1: home) ifFalse:[ ^ false ].
 
-    jreHome := home asFilename / 'jre'.
-    jreHome exists ifFalse:[ 
-        jreHome := home asFilename.      
-    ].
-
-    javaBinary := jreHome / 'bin' / (OperatingSystem isMSWINDOWSlike ifTrue:[ 'java.exe' ] ifFalse:[ 'java' ]).
+    javaBinary := self javaExecutableForJavaHome: home.
     javaBinary isExecutable ifFalse:[ ^ false ].
     javaVersionStream := String new writeStream.
     OperatingSystem executeCommand: '"' , javaBinary pathName , '" -version' outputTo: javaVersionStream.
@@ -1086,7 +1161,7 @@
     "
 
     "Created: / 27-10-2010 / 19:14:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 03-04-2014 / 16:48:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 08-04-2014 / 14:50:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 validateJavaVersionString:aVersionString