diff options
author | Marko Živković <[email protected]> | 2015-02-27 20:52:47 +0000 |
---|---|---|
committer | Marko Živković <[email protected]> | 2015-02-27 20:52:47 +0000 |
commit | cff1ce418df126fc68bf4046c3becd9fadea6872 (patch) | |
tree | 174bf4c171c5502b1ea79f765af6df0ff41046d9 | |
parent | ceed52165ce9d406bc139358936fcf0950db0fe4 (diff) |
Refactor Launcher "Lanceur" and add json library
git-svn-id: https://svn.code.sf.net/p/xlogo4schools/svn/trunk@31 3b0d7934-f7ef-4143-9606-b51f2e2281fd
-rw-r--r-- | logo/build.xml | 2 | ||||
-rw-r--r-- | logo/manifest_xlogo | 2 | ||||
-rw-r--r-- | logo/src/Lanceur.java | 474 |
3 files changed, 230 insertions, 248 deletions
diff --git a/logo/build.xml b/logo/build.xml index ffaea7d..884478e 100644 --- a/logo/build.xml +++ b/logo/build.xml @@ -35,7 +35,7 @@ <!-- Version for this Build --> - <property name="version" value="0.9.05beta-2014-12-22"/> + <property name="version" value="0.9.06beta-2015-02-27"/> <echo message="Using Java version ${ant.java.version}."/> diff --git a/logo/manifest_xlogo b/logo/manifest_xlogo index c54efab..abf2fc8 100644 --- a/logo/manifest_xlogo +++ b/logo/manifest_xlogo @@ -1,2 +1,2 @@ Main-Class: xlogo.Logo -Class-Path: jh.jar vecmath.jar j3dcore.jar j3dutils.jar jl1.0.1.jar log4j-api-2.1.jar log4j-core-2.1.jar
\ No newline at end of file +Class-Path: jh.jar vecmath.jar j3dcore.jar j3dutils.jar jl1.0.1.jar log4j-api-2.1.jar log4j-core-2.1.jar org.json-20131017.jar
\ No newline at end of file diff --git a/logo/src/Lanceur.java b/logo/src/Lanceur.java index e45c397..3e26c16 100644 --- a/logo/src/Lanceur.java +++ b/logo/src/Lanceur.java @@ -1,38 +1,31 @@ -/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Loic Le Coq +/* + * XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Loic Le Coq * Copyright (C) 2013 Marko Zivkovic - * * Contact Information: marko88zivkovic at gmail dot com - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the Free - * Software Foundation; either version 2 of the License, or (at your option) - * any later version. This program is distributed in the hope that it will be - * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General - * Public License for more details. You should have received a copy of the - * GNU General Public License along with this program; if not, write to the Free - * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. This program is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. You should have received a copy of the + * GNU General Public License along with this program; if not, write to the Free + * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. - * - * * This Java source code belongs to XLogo4Schools, written by Marko Zivkovic * during his Bachelor thesis at the computer science department of ETH Zurich, * in the year 2013 and/or during future work. - * * It is a reengineered version of XLogo written by Loic Le Coq, published * under the GPL License at http://xlogo.tuxfamily.org/ - * * Contents of this file were initially written by Loic Le Coq, - * modifications, extensions, refactorings might have been applied by Marko Zivkovic + * modifications, extensions, refactorings might have been applied by Marko Zivkovic */ -/** - * Title : XLogo +/** Title : XLogo * Description : XLogo is an interpreter for the Logo * programming language * - * @author Loïc Le Coq - */ + * @author Loïc Le Coq */ import java.io.*; import java.util.StringTokenizer; @@ -55,20 +48,17 @@ import java.util.Calendar; * Command line arguments are ignored, because this application is GUI-based and for * children. I see no reason for a GUI application to have command line arguments. */ -public class Lanceur -{ - private static String PROPERTIES_PREFIX = "ch.ethz.abz.xlogo4schools"; - private static int DEFAULT_MEMORY_ALLOC = 128; +public class Lanceur { + private static int DEFAULT_MEMORY_ALLOC = 128; /** * The process which contains the XLogo application */ - private Process p; + private Process p; /** * The temporary folder which contains all files to start XLogo */ - private File tmpFolder = null; - private File[] files = new File[12]; + private File tmpFolder = null; /** * Main method @@ -76,13 +66,11 @@ public class Lanceur * @param args * The path toward "lgo" files */ - public static void main(String[] args) - { + public static void main(String[] args) { new Lanceur(); } - Lanceur() - { + Lanceur() { // Look for old files from XLogo crash cleanTmp(); // Look for the memory that should be allocated to the JVM heap size @@ -97,10 +85,8 @@ public class Lanceur deleteTmpFiles(); } - private void startApplicationProcess(int memoire) - { - try - { + private void startApplicationProcess(int memoire) { + try { // Add the tmp to the path String newPath = tmpFolder.getAbsolutePath(); @@ -113,12 +99,11 @@ public class Lanceur commande[1] = "-jar"; commande[2] = "-Xmx" + memoire + "m"; commande[3] = "-Djava.library.path=" + javaLibraryPath; - commande[4] = files[0].getAbsolutePath(); + commande[4] = Library.TMP_XLOGO.getFile(tmpFolder).getAbsolutePath(); System.out.println("<----- Starting XLogo ---->"); String cmd = ""; - for (int i = 0; i < commande.length; i++) - { + for (int i = 0; i < commande.length; i++) { cmd += commande[i] + " "; } System.out.println(cmd + "\n\n"); @@ -128,31 +113,17 @@ public class Lanceur startStreamForward(p.getErrorStream()); p.waitFor(); } - catch (Exception e) - { + catch (Exception e) { System.out.println(e); } } - private void deleteTmpFiles() - { - System.out.println("Closing XLogo. Cleaning tmp file"); - for (int i = 0; i < files.length; i++) - { - if (null != files[i]) - files[i].delete(); - } - tmpFolder.delete(); - } - - private void restorePath() - { + private void restorePath() { String pathToFolder = tmpFolder.getAbsolutePath(); String path = System.getProperty("java.library.path"); StringTokenizer st = new StringTokenizer(path, File.pathSeparator); String newPath = ""; - while (st.hasMoreTokens()) - { + while (st.hasMoreTokens()) { if (!newPath.equals("")) newPath += File.pathSeparator; String element = st.nextToken(); @@ -166,32 +137,25 @@ public class Lanceur * Used to catch application streams and write them to System.out * @param stream */ - private void startStreamForward(final InputStream stream) - { + private void startStreamForward(final InputStream stream) { new Thread(){ - public void run() - { + public void run() { BufferedReader reader = null; - try - { + try { reader = new BufferedReader(new InputStreamReader(stream)); String line = ""; while ((line = reader.readLine()) != null) System.out.println(line); } - catch (IOException e) - { + catch (IOException e) { System.out.println(e.toString()); } - finally - { + finally { if (reader != null) - try - { + try { reader.close(); } - catch (IOException e) - {} + catch (IOException e) {} } } }.start(); @@ -202,24 +166,18 @@ public class Lanceur * If it found files older than 24 hours with the prefix tmp_xlogo, these * files are deleted. */ - private void cleanTmp() - { + private void cleanTmp() { String path = System.getProperty("java.io.tmpdir"); File f = new File(path); File[] files = f.listFiles(); - if (null != files) - { - for (int i = 0; i < files.length; i++) - { - try - { - if (files[i].getName().startsWith("tmp_xlogo")) - { + if (null != files) { + for (int i = 0; i < files.length; i++) { + try { + if (files[i].getName().startsWith("tmp_xlogo")) { long fileTime = files[i].lastModified(); long time = Calendar.getInstance().getTimeInMillis(); // Delete file if it's more than 24 hours old - if (time - fileTime > 24 * 3600 * 1000) - { + if (time - fileTime > 24 * 3600 * 1000) { if (files[i].isDirectory()) deleteDirectory(files[i]); files[i].delete(); @@ -227,8 +185,7 @@ public class Lanceur } } } - catch (Exception e) - { + catch (Exception e) { e.printStackTrace(); } } @@ -239,149 +196,62 @@ public class Lanceur * This method extracts the file tmp_xlogo.jar from the archive and copy it * into the temporary directory. */ - private void extractApplication() - { + private void extractApplication() { + initTmpFolder(); + + for (Library lib : Library.values()) { + if (lib.getPath() == null){ + copy2Tmp(lib); + } + } + // extract the native driver for java 3d in this folder + String osName = System.getProperty("os.name").toLowerCase(); + String arch = System.getProperty("os.arch"); + System.out.println("Operating system: " + osName); + System.out.println("Architecture: " + arch); + + // Linux + //InputStream lib; + OS os = OS.valueOf(osName, arch); + + for (Library lib : os.getLibraries()) { + copy2Tmp(lib); + } + + } + + private void initTmpFolder() { // Create in the "java.io.tmpdir" a directory called tmp_xlogo int i = 0; String tmpPath = System.getProperty("java.io.tmpdir") + File.separator + "tmp_xlogo"; - while (true) - { + while (true) { tmpFolder = new File(tmpPath + i); if (!tmpFolder.exists()) break; else i++; } - boolean b = tmpFolder.mkdir(); - System.out.println("Creating tmp_xlogo directory - success: " + b); - - // extract the file tmp_xlogo.jar in this folder - InputStream src = Lanceur.class.getResourceAsStream("tmp_xlogo.jar"); - files[0] = new File(tmpFolder.getAbsolutePath() + File.separator + "tmp_xlogo.jar"); - b = copier(src, files[0]); - System.out.println("Copying tmp_xlogo.jar - success: " + b); - - // extract the file jh.jar in this folder - src = Lanceur.class.getResourceAsStream("jh.jar"); - files[1] = new File(tmpFolder.getAbsolutePath() + File.separator + "jh.jar"); - b = copier(src, files[1]); - System.out.println("Copying jh.jar - success: " + b); - - // extract the file vecmath.jar in this folder - src = Lanceur.class.getResourceAsStream("vecmath.jar"); - files[2] = new File(tmpFolder.getAbsolutePath() + File.separator + "vecmath.jar"); - b = copier(src, files[2]); - System.out.println("Copying vecmath.jar - success: " + b); - - // extract the file j3dcore.jar in this folder - src = Lanceur.class.getResourceAsStream("j3dcore.jar"); - files[3] = new File(tmpFolder.getAbsolutePath() + File.separator + "j3dcore.jar"); - b = copier(src, files[3]); - System.out.println("Copying j3dcore.jar - success: " + b); - - // extract the file j3dutils.jar in this folder - src = Lanceur.class.getResourceAsStream("j3dutils.jar"); - files[4] = new File(tmpFolder.getAbsolutePath() + File.separator + "j3dutils.jar"); - b = copier(src, files[4]); - System.out.println("Copying j3dutils.jar - success: " + b); - // extract the file jl1.0.1 in this folder (JLayer library for mp3 playing) - src = Lanceur.class.getResourceAsStream("jl1.0.1.jar"); - files[5] = new File(tmpFolder.getAbsolutePath() + File.separator + "jl1.0.1.jar"); - b = copier(src, files[5]); - System.out.println("Copying jl1.0.1.jar - success: " + b); - - // extract the file jl1.0.1 in this folder (JLayer library for mp3 playing) - src = Lanceur.class.getResourceAsStream("log4j-api-2.1.jar"); - files[6] = new File(tmpFolder.getAbsolutePath() + File.separator + "log4j-api-2.1.jar"); - b = copier(src, files[6]); - System.out.println("Copying log4j-api-2.1.jar - success: " + b); - - // extract the file jl1.0.1 in this folder (JLayer library for mp3 playing) - src = Lanceur.class.getResourceAsStream("log4j-core-2.1.jar"); - files[7] = new File(tmpFolder.getAbsolutePath() + File.separator + "log4j-core-2.1.jar"); - b = copier(src, files[7]); - System.out.println("Copying log4j-core-2.1.jar - success: " + b); - - // extract the native driver for java 3d in this folder - String os = System.getProperty("os.name").toLowerCase(); - String arch = System.getProperty("os.arch"); - System.out.println("Operating system: " + os); - System.out.println("Architecture: " + arch); + System.out.println("Creating tmp_xlogo directory - success: " + tmpFolder.mkdir()); + } - // Linux - if (os.indexOf("linux") != -1) - { - if (arch.indexOf("86") != -1) - { - InputStream lib = Lanceur.class.getResourceAsStream("linux/x86/libj3dcore-ogl.so"); - files[8] = new File(tmpFolder.getAbsolutePath() + File.separator + "libj3dcore-ogl.so"); - copier(lib, files[8]); - lib = Lanceur.class.getResourceAsStream("linux/x86/libj3dcore-ogl-cg.so"); - files[9] = new File(tmpFolder.getAbsolutePath() + File.separator + "libj3dcore-ogl-cg.so"); - copier(lib, files[9]); - } - else - { - InputStream lib = Lanceur.class.getResourceAsStream("linux/amd64/libj3dcore-ogl.so"); - files[8] = new File(tmpFolder.getAbsolutePath() + File.separator + "libj3dcore-ogl.so"); - copier(lib, files[8]); - } - } - // windows - else if (os.indexOf("windows") != -1) - { - if (arch.indexOf("86") != -1) - { - InputStream lib = Lanceur.class.getResourceAsStream("windows/x86/j3dcore-d3d.dll"); - files[8] = new File(tmpFolder.getAbsolutePath() + File.separator + "j3dcore-d3d.dll"); - b = copier(lib, files[8]); - System.out.println("Copying library 1 - success: " + b); - lib = Lanceur.class.getResourceAsStream("windows/x86/j3dcore-ogl.dll"); - files[9] = new File(tmpFolder.getAbsolutePath() + File.separator + "j3dcore-ogl.dll"); - b = copier(lib, files[9]); - System.out.println("Copying library 2 - success: " + b); - lib = Lanceur.class.getResourceAsStream("windows/x86/j3dcore-ogl-cg.dll"); - files[10] = new File(tmpFolder.getAbsolutePath() + File.separator + "j3dcore-ogl-cg.dll"); - b = copier(lib, files[10]); - System.out.println("Copying library 3 - success: " + b); - lib = Lanceur.class.getResourceAsStream("windows/x86/j3dcore-ogl-chk.dll"); - files[11] = new File(tmpFolder.getAbsolutePath() + File.separator + "j3dcore-ogl-chk.dll"); - b = copier(lib, files[11]); - System.out.println("Copying library 4 - success: " + b); - } - else - { - InputStream lib = Lanceur.class.getResourceAsStream("windows/amd64/j3dcore-ogl.dll"); - files[8] = new File(tmpFolder.getAbsolutePath() + File.separator + "j3dcore-ogl.dll"); - b = copier(lib, files[8]); - System.out.println("Copying library 1 - success: " + b); - } - } - // Mac os - else if (os.indexOf("mac") != -1) - { - - } - // solaris - else if (os.indexOf("sunos") != -1) - { - if (arch.indexOf("86") != -1) - { - InputStream lib = Lanceur.class.getResourceAsStream("solaris/i386/libj3dcore-ogl.so"); - files[8] = new File(tmpFolder.getAbsolutePath() + File.separator + "libj3dcore-ogl.so"); - b = copier(lib, files[8]); - System.out.println("Copying library 1 - success: " + b); - } - else if (arch.indexOf("amd64") != -1) - { - InputStream lib = Lanceur.class.getResourceAsStream("solaris/amd64/libj3dcore-ogl.so"); - files[8] = new File(tmpFolder.getAbsolutePath() + File.separator + "libj3dcore-ogl.so"); - b = copier(lib, files[8]); - System.out.println("Copying library 1 - success: " + b); + private void copy2Tmp(Library lib) { + // extract the jar file in this folder + InputStream src = Lanceur.class.getResourceAsStream(lib.getResourcePath()); + File file = lib.getFile(tmpFolder); + System.out.println("Get library " + lib.getResourcePath()); + System.out.println("Copying " + lib.getLibraryName() + " - success: " + copy(src, file)); + } + + private void deleteTmpFiles() { + System.out.println("Closing XLogo. Cleaning tmp file"); + for (Library lib : Library.values()) { + File file = lib.getFile(tmpFolder); + if (file.exists()) { + file.delete(); } } - + tmpFolder.delete(); } /** @@ -392,13 +262,11 @@ public class Lanceur * The output file * @return true if success, false otherwise */ - private boolean copier(InputStream src, File destination) - { + private boolean copy(InputStream src, File destination) { boolean resultat = false; // Declaration des flux java.io.FileOutputStream destinationFile = null; - try - { + try { // Création du fichier : destination.createNewFile(); // Ouverture des flux @@ -407,32 +275,24 @@ public class Lanceur // Lecture par segment de 0.5Mo byte buffer[] = new byte[512 * 1024]; int nbLecture; - while ((nbLecture = src.read(buffer)) != -1) - { + while ((nbLecture = src.read(buffer)) != -1) { destinationFile.write(buffer, 0, nbLecture); } // Copie réussie resultat = true; } - catch (java.io.FileNotFoundException f) - {} - catch (java.io.IOException e) - {} - finally - { + catch (java.io.FileNotFoundException f) {} + catch (java.io.IOException e) {} + finally { // Quoi qu'il arrive, on ferme les flux - try - { + try { src.close(); } - catch (Exception e) - {} - try - { + catch (Exception e) {} + try { destinationFile.close(); } - catch (Exception e) - {} + catch (Exception e) {} } return (resultat); } @@ -444,22 +304,16 @@ public class Lanceur * The Directory path * @return true if success */ - private boolean deleteDirectory(File path) - { + private boolean deleteDirectory(File path) { boolean resultat = true; - if (path.exists()) - { + if (path.exists()) { File[] files = path.listFiles(); - if (null != files) - { - for (int i = 0; i < files.length; i++) - { - if (files[i].isDirectory()) - { + if (null != files) { + for (int i = 0; i < files.length; i++) { + if (files[i].isDirectory()) { resultat &= deleteDirectory(files[i]); } - else - { + else { resultat &= files[i].delete(); } } @@ -468,4 +322,132 @@ public class Lanceur resultat &= path.delete(); return (resultat); } + + /** + * These represent libraries that should be packed into the new temporary jar. + * @author Marko + * + */ + enum Library { + TMP_XLOGO("tmp_xlogo.jar"), + + JH("jh.jar"), + VECMATH("vecmath.jar"), + J3D_CORE("j3dcore.jar"), + J3D_UTILS("j3dutils.jar"), + JLAYER("jl1.0.1.jar"), + LOG4J_API("log4j-api-2.1.jar"), + LOG4J_CORE("log4j-core-2.1.jar"), + ORG_JSON("org.json-20131017.jar"), + + LIN_AMD64_J3D_CORE("linux/amd64/", "libj3dcore-ogl.so"), + LIN_X86_J3D_CORE_OGL("linux/x86/", "libj3dcore-ogl.so"), + LIN_X86_J3D_CORE_OGL_CG("linux/x86/", "libj3dcore-ogl-cg.so"), + + WIN_AMD64_J3D_CORE("windows/amd64/", "j3dcore-ogl.dll"), + WIN_X86_J3D_CORE_D3D("windows/x86/", "j3dcore-d3d.dll"), + WIN_X86_J3D_CORE_OGL("windows/x86/", "j3dcore-ogl.dll"), + WIN_X86_J3D_CORE_CG("windows/x86/", "j3dcore-ogl-cg.dll"), + WIN_X86_J3D_CORE_OGL_CHK("windows/x86/", "j3dcore-ogl-chk.dll"), + WIN_AMD64_J3D_CORE_OGL("windows/amd64/", "j3dcore-ogl.dll"), + + SOL_X86_LIB_J3D_CORE("solaris/i386/", "libj3dcore-ogl.so"), + SOL_AMD64_LIB_J3D_CORE("solaris/amd64/","libj3dcore-ogl.so"), ; + + private String path; + private String libName; + + Library(String jarName) { + this.libName = jarName; + } + + Library(String path, String libName) { + this.path = path; + this.libName = libName; + } + + public String getPath() { + return path; + } + + public String getLibraryName() { + return libName; + } + + public String getResourcePath() { + if (path != null){ + return path + libName; + } else { + return libName; + } + + } + + public File getFile(File location) { + return new File(location.getAbsolutePath() + File.separator + libName); + } + } + + /** + * These represent different operating systems and architectures. They provide system specific {@link Library}s + * @author Marko + * + */ + enum OS { + WINDOWS_AMD64( + Library.WIN_AMD64_J3D_CORE, + Library.WIN_AMD64_J3D_CORE_OGL), + WINDOWS_X86( + Library.WIN_X86_J3D_CORE_D3D, + Library.WIN_X86_J3D_CORE_OGL, + Library.WIN_X86_J3D_CORE_CG, + Library.WIN_X86_J3D_CORE_OGL_CHK), + LINUX_AMD64( + Library.LIN_AMD64_J3D_CORE), + LINUX_86( + Library.LIN_X86_J3D_CORE_OGL, + Library.LIN_X86_J3D_CORE_OGL_CG), + SOLARIS_86( + Library.SOL_X86_LIB_J3D_CORE), + SOLARIS_AMD64( + Library.SOL_AMD64_LIB_J3D_CORE), + MAC, + UNKNOWN; + + private Library[] osLibs = new Library[0]; + + OS(Library... osLibs) { + this.osLibs = osLibs; + } + + public Library[] getLibraries() { + return osLibs; + } + + public static OS valueOf(String os, String arch) { + if (os.indexOf("linux") != -1) { + return (arch.indexOf("86") != -1) ? LINUX_86 : LINUX_AMD64; + } + else if (os.indexOf("windows") != -1) { + return (arch.indexOf("86") != -1) ? WINDOWS_X86 : WINDOWS_AMD64; + } + else if (os.indexOf("mac") != -1) { + return MAC; + } + else if (os.indexOf("sunos") != -1) { + if (arch.indexOf("86") != -1) { + return SOLARIS_86; + } + else if (arch.indexOf("amd64") != -1) { + return SOLARIS_AMD64; + } + else { + return UNKNOWN; + } + } + else { + return UNKNOWN; + } + } + } } |