KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > tools > shell > ConnectInformation


1 package com.daffodilwoods.tools.shell;
2
3 import java.io.*;
4 import java.util.*;
5
6 public class ConnectInformation {
7   String JavaDoc userName = "Daffodil",
8       password = "daffodil";
9   String JavaDoc hostName = "localhost";
10   String JavaDoc databaseName = "school";
11   String JavaDoc databasePath = "";
12   int port = 3456;
13   boolean readOnly;
14   private Properties connectionProperties;
15
16   /************************************************************************
17    * To collect information required for connection with Database
18    * @param conMode Connection Mode > Server Edition or Embedded Edition
19    * @throws java.lang.Exception
20    */

21
22   public ConnectInformation(BufferedReader rd) throws Exception JavaDoc {
23     connectionProperties = new Properties();
24     connectionProperties.setProperty("create", "true");
25     try {
26       File propFile;
27       String JavaDoc propURL = System.getProperty("shell.properties");
28
29       if (propURL != null) {
30         propFile = new File(propURL);
31         if (!propFile.exists()) {
32           throw new InternalError JavaDoc("Property file does not exists ... ");
33         }
34         readProp(propFile);
35         return;
36       }
37       fromConsole(rd);
38     }
39     catch (NullPointerException JavaDoc ex1) {
40       DisplayOutput.displayLn("\n\n No proper Inforamation");
41       System.exit(0);
42     }
43     catch (Exception JavaDoc ex) {
44       DisplayOutput.printError("", ex);
45       DisplayOutput.displayLn("\nNot Connected");
46     }
47   }
48
49   /**
50    * readProp is used to read the property file to set the properies required for
51    * connection with database
52    * @throws java.lang.Exception
53    */

54
55   private void readProp(File propertyFile) throws Exception JavaDoc {
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   /***************************************************************************
70    * fromConsole method is used to get required information from the user
71    * to connect to database
72    * @throws java.lang.Exception
73    */

74
75   private void fromConsole(BufferedReader rd) throws Exception JavaDoc {
76     DisplayOutput.display("Username: [default : daffodil]: ");
77     String JavaDoc 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 JavaDoc databaseOption = input.equals("") ? "Standard" : input;
102     if (databaseOption.equalsIgnoreCase("Custom")) {
103       readCustom(rd);
104     }
105   }
106
107   /************************************************************************
108    * readCustom method is used to collect customary information from user
109    */

110
111   private void readCustom(BufferedReader rd) {
112     try {
113       DisplayOutput.display("InitialFileSize [size] : [default: 5MB]:");
114       String JavaDoc 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 JavaDoc ex) {
127       DisplayOutput.printError("", ex);
128     }
129   }
130
131   /*****************************************************************************
132    * readEmbedded is to collect information required for Embedded Edition
133    * @throws java.lang.Exception
134    */

135
136   private void readEmbeded(BufferedReader rd) throws Exception JavaDoc {
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 JavaDoc 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   /****************************************************************************
153    * readRMI is to collect information required for Server Edition
154    * @throws java.lang.Exception
155    */

156
157   private void readRmi(BufferedReader rd) throws Exception JavaDoc {
158     DisplayOutput.display("Host :[default : localhost]: ");
159     String JavaDoc 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 JavaDoc value, String JavaDoc methodName) throws Exception JavaDoc {
178     int val = 0;
179     try {
180       val = Integer.parseInt(value);
181     }
182     catch (NumberFormatException JavaDoc 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