1 9 package org.ozoneDB.tools; 10 11 import java.io.*; 12 import java.util.*; 13 import org.ozoneDB.*; 14 import org.ozoneDB.core.*; 15 16 20 public class Install extends Object { 21 22 protected static String dirName; 23 24 27 protected static PrintWriter log = new PrintWriter(System.out); 28 29 30 public static void main( String [] args ) throws Exception { 31 String dir = File.separator + "tmp" + File.separator + "db" + File.separator; 32 boolean help = false; 33 34 for (int i = 0; i < args.length; i++) { 35 if (args[i].startsWith( "-d" )) { 36 dir = args[i].substring( 2 ) + File.separator; 37 } else if (args[i].startsWith( "-h" )) { 38 help = true; 39 } else { 40 System.out.println( "illegal option: " + args[i] ); 41 help = true; 42 } 43 } 44 45 if (args.length == 0 || help) { 46 System.out.println( "usage: ozoneInst -d<dir> [-D<property>=<value>]*" ); 47 System.out.println( " -d<directory> database directory" ); 48 System.out.println( " -h shows this help" ); 50 System.out.println( "" ); 51 52 System.out.println( "where properties include:" ); 53 Setup defaults = new Setup( null ); 54 defaults.fillWithOzoneDefaults(); 55 for (Enumeration e = defaults.propertyNames(); e.hasMoreElements();) { 56 System.out.println( " " + (String )e.nextElement() ); 57 } 58 System.exit( 0 ); 59 } 60 61 try { 63 File file = new File( dir ); 64 if (file.list().length > 0) { 65 System.out.print( "Do you want to recursively delete all files in " + dir + " ? [y/N]" ); 66 67 InputStreamReader is = new InputStreamReader( System.in ); 68 69 int c = is.read(); 70 if (c != 'y') { 71 System.exit( 0 ); 72 } 73 } 74 } catch (Exception e) { 75 } 76 77 System.out.println( "installing database in " + dir + " ..." ); 78 79 Setup defaults = new Setup( null ); 80 defaults.addProperties( System.getProperties(), "ozoneDB." ); 81 createDB( dir, defaults, new PrintWriter( System.out, true ) ); 82 83 System.out.println( "Edit the file " + dir + "config.properties to change settings." ); 85 System.out.println( "Ready." ); 86 } 87 88 89 public static void createDB( String _dirName, Setup _defaults, PrintWriter _log ) throws Exception { 90 log = _log; 91 dirName = _dirName + File.separator; 92 93 mkDir(); 94 makeDataDir(); 95 makeIdTableDir(); 96 97 resetConfigFile( _defaults ); 98 resetStateFile( _defaults ); 99 } 100 101 102 105 public static void mkDir() throws Exception { 106 deleteDir( dirName, true ); 109 110 File f = new File( dirName ); 111 boolean creationSuccess = true; 112 if (!f.exists()) { 113 log.println( "making dir " + dirName + " ..." ); 114 creationSuccess = f.mkdirs(); 115 } 116 if (!creationSuccess) { 117 throw new IOException("Failed to create db dir " + f); 118 } 119 } 120 121 122 public static void resetStateFile( Setup config ) throws IOException { 123 log.println( "making " + dirName + Env.STATE_FILE + " ..." ); 124 125 Setup state = new Setup( null ); 126 long dbID = config.longProperty( Setup.DB_ID, 0 ); 127 128 state.setLongProperty( Setup.XOID, (dbID << 40) + 100 ); 130 log.println( " ID counter = " + dbID + " * 2^40" ); 131 132 OutputStream out = new PrintStream( new FileOutputStream( new File( dirName, Env.STATE_FILE ) ) ); 133 state.store( out, "Ozone Server State File.\n#Do not edit!" ); 134 out.close(); 135 } 136 137 138 public static Setup resetConfigFile( Setup _defaults ) throws IOException { 139 log.println( "making " + dirName + Env.CONFIG_FILE + " ..." ); 140 141 Setup defaults = new Setup( null ); 142 defaults.fillWithOzoneDefaults(); 143 defaults.addProperties( _defaults, "ozoneDB." ); 144 145 OutputStream out = new FileOutputStream( new File( dirName, Env.CONFIG_FILE ) ); 146 defaults.store( out, 147 "Ozone Config File.\n" + "#\n" + "# Do not use comments. This file will be overwritten,\n" 148 + "# if the config changes. See the ozone documentation\n" 149 + "# for details about the properties and their values.\n" ); 150 out.close(); 151 152 defaults.print( System.out, "", " " ); 153 return defaults; 155 } 156 157 158 161 public static void makeDataDir() throws IOException { 162 log.println( "making " + dirName + Env.DATA_DIR + " ..." ); 163 164 File f = new File( dirName + Env.DATA_DIR ); 165 boolean creationSuccess = true; 166 if (!f.exists()) { 167 creationSuccess = f.mkdir(); 168 } 169 if (!creationSuccess) { 170 throw new IOException("Failed to create data dir " + f); 171 } 172 } 173 174 175 public static void makeIdTableDir() throws IOException { 176 log.println( "making " + dirName + Env.OS_DIR + " ..." ); 177 178 File f = new File( dirName + Env.OS_DIR ); 179 boolean creationSuccess = true; 180 if (!f.exists()) { 181 creationSuccess = f.mkdir(); 182 } 183 if (!creationSuccess) { 184 throw new IOException("Failed to create IdTable dir " + f); 185 } 186 } 187 188 189 public static void deleteDir( String path, boolean recursive ) throws IOException { 190 log.println( "deleting " + path + " ..." ); 191 File f = new File( path ); 192 String [] files = f.list(); 193 if (files == null) { 194 return; 195 } 196 197 for (int i = 0; i < files.length; i++) { 198 File file = new File( path, files[i] ); 199 if (file.isDirectory()) { 200 if (recursive) { 201 deleteDir( file.getPath(), recursive ); 202 } 203 } else { 204 if (!file.delete()) { 205 log.println( "Unable to delete " + file + "." ); 206 } 207 } 208 } 209 } 210 211 216 public static boolean dbExists(String dbDir) { 217 File testFile = new File( dbDir, Env.CONFIG_FILE ); 218 return testFile.exists(); 219 } 220 } 221 | Popular Tags |