| 1 | /* |
| 2 | Copyright (C) 2005 MySQL AB |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of version 2 of the GNU General Public License as |
| 6 | published by the Free Software Foundation. |
| 7 | |
| 8 | There are special exceptions to the terms and conditions of the GPL |
| 9 | as it is applied to this software. View the full text of the |
| 10 | exception in file EXCEPTIONS-CONNECTOR-J in the directory of this |
| 11 | software distribution. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | package com.mysql.jdbc.util; |
| 24 | |
| 25 | import java.io.File; |
| 26 | import java.io.FileOutputStream; |
| 27 | import java.sql.Connection; |
| 28 | import java.sql.ResultSet; |
| 29 | import java.util.Properties; |
| 30 | |
| 31 | import com.mysql.jdbc.NonRegisteringDriver; |
| 32 | |
| 33 | /** |
| 34 | * Creates output directory structure for multi-jvm, multi-url |
| 35 | * unit, regression and compliance tests. |
| 36 | */ |
| 37 | public class VersionFSHierarchyMaker { |
| 38 | |
| 39 | /** |
| 40 | * @param args |
| 41 | */ |
| 42 | public static void main(String[] args) throws Exception { |
| 43 | if (args.length < 3) { |
| 44 | usage(); |
| 45 | System.exit(1); |
| 46 | } |
| 47 | |
| 48 | String jdbcUrl = null; |
| 49 | |
| 50 | |
| 51 | jdbcUrl = System.getProperty("com.mysql.jdbc.testsuite.url"); |
| 52 | |
| 53 | |
| 54 | Connection conn = new NonRegisteringDriver().connect(jdbcUrl, null); |
| 55 | |
| 56 | ResultSet rs = conn.createStatement().executeQuery("SELECT VERSION()"); |
| 57 | rs.next(); |
| 58 | String mysqlVersion = removeWhitespaceChars(rs.getString(1)); |
| 59 | |
| 60 | String jvmVersion = removeWhitespaceChars(System.getProperty("java.version")); |
| 61 | String jvmVendor = removeWhitespaceChars(System.getProperty("java.vendor")); |
| 62 | String osName = removeWhitespaceChars(System.getProperty("os.name")); |
| 63 | String osArch = removeWhitespaceChars(System.getProperty("os.arch")); |
| 64 | String osVersion = removeWhitespaceChars(System.getProperty("os.version")); |
| 65 | |
| 66 | String jvmSubdirName = jvmVendor + "-" + jvmVersion; |
| 67 | String osSubdirName = osName + "-" + osArch + "-" + osVersion; |
| 68 | |
| 69 | File baseDir = new File(args[1]); |
| 70 | File mysqlVersionDir = new File(baseDir, mysqlVersion); |
| 71 | File osVersionDir = new File(mysqlVersionDir, osSubdirName); |
| 72 | File jvmVersionDir = new File(osVersionDir, jvmSubdirName); |
| 73 | |
| 74 | jvmVersionDir.mkdirs(); |
| 75 | |
| 76 | |
| 77 | FileOutputStream pathOut = null; |
| 78 | |
| 79 | try { |
| 80 | String propsOutputPath = args[2]; |
| 81 | pathOut = new FileOutputStream(propsOutputPath); |
| 82 | String baseDirStr = baseDir.getAbsolutePath(); |
| 83 | String jvmVersionDirStr = jvmVersionDir.getAbsolutePath(); |
| 84 | |
| 85 | if (jvmVersionDirStr.startsWith(baseDirStr)) { |
| 86 | jvmVersionDirStr = jvmVersionDirStr.substring(baseDirStr.length() + 1); |
| 87 | } |
| 88 | |
| 89 | pathOut.write(jvmVersionDirStr.getBytes()); |
| 90 | } finally { |
| 91 | if (pathOut != null) { |
| 92 | pathOut.flush(); |
| 93 | pathOut.close(); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public static String removeWhitespaceChars(String input) { |
| 99 | if (input == null) { |
| 100 | return input; |
| 101 | } |
| 102 | |
| 103 | int strLen = input.length(); |
| 104 | |
| 105 | StringBuffer output = new StringBuffer(strLen); |
| 106 | |
| 107 | for (int i = 0; i < strLen; i++) { |
| 108 | char c = input.charAt(i); |
| 109 | if (!Character.isDigit(c) && !Character.isLetter(c)) { |
| 110 | if (Character.isWhitespace(c)) { |
| 111 | output.append("_"); |
| 112 | } else { |
| 113 | output.append("."); |
| 114 | } |
| 115 | } else { |
| 116 | output.append(c); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return output.toString(); |
| 121 | } |
| 122 | |
| 123 | private static void usage() { |
| 124 | System.err.println("Creates a fs hierarchy representing MySQL version, OS version and JVM version."); |
| 125 | System.err.println("Stores the full path as 'outputDirectory' property in file 'directoryPropPath'"); |
| 126 | System.err.println(); |
| 127 | System.err.println("Usage: java VersionFSHierarchyMaker unit|compliance baseDirectory directoryPropPath"); |
| 128 | } |
| 129 | } |