1 package org.myoodb.tools; 25 26 import java.io.*; 27 import java.util.*; 28 29 public class MyOodbInstall 30 { 31 private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(MyOodbInstall.class); 32 33 public static void main(String [] args) throws Exception 34 { 35 String dir = "db" + File.separator; 36 boolean help = false; 37 38 for (int i = 0; i < args.length; i++) 39 { 40 if (args[i].startsWith("-d")) 41 { 42 dir = args[i].substring(2) + File.separator; 43 } 44 else if (args[i].startsWith("-h")) 45 { 46 help = true; 47 } 48 else 49 { 50 LOGGER.error("illegal option: " + args[i]); 51 help = true; 52 } 53 } 54 55 if (args.length == 0 || help) 56 { 57 LOGGER.info("usage: install -d<directory>"); 58 LOGGER.info(" -d<directory> database directory"); 59 LOGGER.info(" -h shows this help"); 60 LOGGER.info(""); 61 62 System.exit(0); 63 } 64 65 createDatabase(dir); 66 } 67 68 public static void createDatabase(String dirName) throws Exception 69 { 70 LOGGER.info("Installing Database in " + dirName + " ..."); 71 72 makeDirectory(dirName); 73 makeDirectory(dirName, org.myoodb.core.MyOodbManager.ROOT_DIR); 74 makeDirectory(dirName, org.myoodb.core.MyOodbManager.OBJECT_DIR); 75 resetPropertyFiles(dirName); 76 77 LOGGER.info("Installation Complete."); 78 } 79 80 public static void makeDirectory(String dirName) throws Exception 81 { 82 org.myoodb.core.FileHelper.delete(dirName); 83 84 File dirFile = new File(dirName); 85 86 if (dirFile.exists() == false) 87 { 88 if (LOGGER.isDebugEnabled() == true) 89 { 90 LOGGER.debug("making dir " + dirName + " ..."); 91 } 92 93 if (dirFile.mkdirs() == false) 94 { 95 throw new IOException("Failed to create db dir " + dirFile); 96 } 97 } 98 } 99 100 public static void resetPropertyFiles(String dirName) throws IOException 101 { 102 if (LOGGER.isDebugEnabled() == true) 103 { 104 LOGGER.debug("making " + dirName + File.separator + org.myoodb.core.MyOodbManager.ID_FILE + " ..."); 105 LOGGER.debug("making " + dirName + File.separator + org.myoodb.core.MyOodbManager.USER_FILE + " ..."); 106 } 107 108 org.myoodb.core.Properties properties = new org.myoodb.core.Properties(null); 109 properties.setLongProperty(org.myoodb.core.IdentifierManager.XID, 0); 110 OutputStream out = new PrintStream(new FileOutputStream(new File(dirName, org.myoodb.core.MyOodbManager.ID_FILE))); 111 properties.store(out, "Identifier Property File.\n"); 112 out.close(); 113 114 properties = new org.myoodb.core.Properties(null); 115 out = new PrintStream(new FileOutputStream(new File(dirName, org.myoodb.core.MyOodbManager.USER_FILE))); 116 properties.store(out, "User Property File.\n"); 117 out.close(); 118 } 119 120 public static void makeDirectory(String dirName, String component) throws IOException 121 { 122 if (LOGGER.isDebugEnabled() == true) 123 { 124 LOGGER.debug("making " + dirName + File.separator + component + " ..."); 125 } 126 127 File dirFile = new File(dirName + File.separator + component); 128 129 if (dirFile.exists() == false) 130 { 131 if (LOGGER.isDebugEnabled() == true) 132 { 133 LOGGER.debug("making " + component + " dir " + dirName + File.separator + component + " ..."); 134 } 135 136 if (dirFile.mkdirs() == false) 137 { 138 throw new IOException("Failed to create " + component + " db dir " + dirFile); 139 } 140 } 141 } 142 143 public static boolean dbExists(String dirName) 144 { 145 File testFile = new File(dirName, org.myoodb.core.MyOodbManager.ID_FILE); 146 return testFile.exists(); 147 } 148 } 149 | Popular Tags |