1 package com.daffodilwoods.tools.shell; 2 3 import java.io.*; 4 import java.util.*; 5 6 public class ConnectInformation { 7 String userName = "Daffodil", 8 password = "daffodil"; 9 String hostName = "localhost"; 10 String databaseName = "school"; 11 String databasePath = ""; 12 int port = 3456; 13 boolean readOnly; 14 private Properties connectionProperties; 15 16 21 22 public ConnectInformation(BufferedReader rd) throws Exception { 23 connectionProperties = new Properties(); 24 connectionProperties.setProperty("create", "true"); 25 try { 26 File propFile; 27 String propURL = System.getProperty("shell.properties"); 28 29 if (propURL != null) { 30 propFile = new File(propURL); 31 if (!propFile.exists()) { 32 throw new InternalError ("Property file does not exists ... "); 33 } 34 readProp(propFile); 35 return; 36 } 37 fromConsole(rd); 38 } 39 catch (NullPointerException ex1) { 40 DisplayOutput.displayLn("\n\n No proper Inforamation"); 41 System.exit(0); 42 } 43 catch (Exception ex) { 44 DisplayOutput.printError("", ex); 45 DisplayOutput.displayLn("\nNot Connected"); 46 } 47 } 48 49 54 55 private void readProp(File propertyFile) throws Exception { 56 connectionProperties = new Properties(); 57 connectionProperties.load(new FileInputStream(propertyFile)); 58 userName = connectionProperties.getProperty("Username", "daffodil"); 59 password = connectionProperties.getProperty("Password", "daffodil"); 60 hostName = connectionProperties.getProperty("Host", "localhost"); 61 port = Integer.parseInt(connectionProperties.getProperty("Port", "3456")); 62 databaseName = connectionProperties.getProperty("Databasename","school"); 63 DaffodilDBShell.trace = connectionProperties.getProperty("stacktrace", 64 "false").toLowerCase().equals("true"); 65 DaffodilDBShell.isRMI = connectionProperties.getProperty("Mode", "").trim(). 66 equalsIgnoreCase("r"); 67 } 68 69 74 75 private void fromConsole(BufferedReader rd) throws Exception { 76 DisplayOutput.display("Username: [default : daffodil]: "); 77 String input = rd.readLine(); 78 userName = input.equals("") ? "daffodil" : input; 79 DisplayOutput.display("Password : [default : daffodil]: "); 80 input = rd.readLine(); 81 password = input.equals("") ? "daffodil" : input; 82 83 connectionProperties.setProperty("user", userName); 84 connectionProperties.setProperty("password", password); 85 86 if (DaffodilDBShell.isRMI) { 87 readRmi(rd); 88 } 89 else { 90 readEmbeded(rd); 91 } 92 DisplayOutput.display("Database name : [default: school]:"); 93 input = rd.readLine(); 94 databaseName = input.equals("") ? "school" : input; 95 DisplayOutput.display("StackTrace [true/false] : [default: false]:"); 96 input = rd.readLine(); 97 DaffodilDBShell.trace = input.trim().equalsIgnoreCase("true"); 98 DisplayOutput.display( 99 "DatabaseOption [Standard/Custom] : [default: Standard]:"); 100 input = rd.readLine(); 101 String databaseOption = input.equals("") ? "Standard" : input; 102 if (databaseOption.equalsIgnoreCase("Custom")) { 103 readCustom(rd); 104 } 105 } 106 107 110 111 private void readCustom(BufferedReader rd) { 112 try { 113 DisplayOutput.display("InitialFileSize [size] : [default: 5MB]:"); 114 String input = rd.readLine(); 115 connectionProperties.setProperty("InitialFileSize", 116 input.trim().equals("") ? "5M" : input); 117 DisplayOutput.display("MultiFileSupport [true/false] : [default: false]:"); 118 input = rd.readLine(); 119 connectionProperties.setProperty("MultiFileSupport", 120 input.equals("true") + ""); 121 DisplayOutput.display("IncrementFactor [size] : [default: 100]:"); 122 input = rd.readLine(); 123 connectionProperties.setProperty("IncrementFactor", 124 input.trim().equals("") ? "100" : input); 125 } 126 catch (Exception ex) { 127 DisplayOutput.printError("", ex); 128 } 129 } 130 131 135 136 private void readEmbeded(BufferedReader rd) throws Exception { 137 if (System.getProperty("Databasepath") == null) { 138 System.setProperty("Databasepath", 139 System.getProperty("user.home") + File.separator + 140 "databases"); 141 } 142 DisplayOutput.display("DaffodilDBHome(DataBase Path):[default: " + 143 System.getProperty("Databasepath") + "]:"); 144 String input = rd.readLine(); 145 databasePath = (input == null || input.equals("")) ? 146 System.getProperty("Databasepath") : input; 147 DisplayOutput.display("ReadOnlyMode(true/false):[default: false]:"); 148 input = rd.readLine(); 149 readOnly = ! (input == null || input.equals("")); 150 } 151 152 156 157 private void readRmi(BufferedReader rd) throws Exception { 158 DisplayOutput.display("Host :[default : localhost]: "); 159 String input = rd.readLine(); 160 hostName = input.trim().equals("") ? "localhost" : input; 161 do { 162 DisplayOutput.display("Port :[default : 3456]: "); 163 input = rd.readLine(); 164 if (input.equals("")) { 165 port = 3456; 166 } 167 else { 168 port = checkInt(input, "Port Number"); 169 } 170 if (port < 0 && port > 65535) { 171 DisplayOutput.displayLn("***Invalid port number***"); 172 } 173 } 174 while (port < 0 && port > 65535); 175 } 176 177 private int checkInt(String value, String methodName) throws Exception { 178 int val = 0; 179 try { 180 val = Integer.parseInt(value); 181 } 182 catch (NumberFormatException ex) { 183 DisplayOutput.displayLn("\n Invalid " + methodName + " \n"); 184 System.exit(0); 185 } 186 return val; 187 } 188 189 public Properties getConnectionProperties() { 190 return connectionProperties; 191 } 192 } 193 | Popular Tags |