Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/rohanclan/ashpool/Ashpool.java


1   /*
2    * Ashpool - XML Database
3    * Copyright (C) 2003 Rob Rohan
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the
6    * Free Software Foundation; either version 2 of the License, or (at your
7    * option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful, but
10   * WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License along
15   * with this program; if not, write to the Free Software Foundation, Inc.,
16   * 675 Mass Ave, Cambridge, MA 02139, USA.
17   *
18   * Ashpool.java
19   *
20   * Created on February 1, 2003, 10:56 AM
21   */
22  
23  package com.rohanclan.ashpool;
24  
25  import com.rohanclan.ashpool.core.*;
26  import com.rohanclan.ashpool.*;
27  
28  /**
29   *
30   * @author  rob
31   */
32  public class Ashpool {
33      //private final int MAXCOMMANDS = 10;
34      private static final String p1 = "Ashpool# ";
35      private static final String p2 = "Ashpool> ";
36      private ConnectionManager connMan;
37      private boolean running = false;
38      
39      private Runtime r = Runtime.getRuntime();
40      
41      private String currentcmd="";
42      //private Commands commands[];
43      
44      /** Creates a new instance of Ashpool */
45      public Ashpool(String datauri) {
46          
47          try{
48              System.out.println("Datastore: [" + datauri + "]");
49              java.io.File source = new java.io.File(datauri);
50              connMan = new ConnectionManager(source);
51              
52              //System.out.println("Building command list.");
53              buildCommandList();
54              
55              //System.out.println("Starting main program loop.");
56              running = true;
57              startMainProgramLoop();
58              
59          }catch(Exception e){
60              System.err.println("Startup Error: " + e.toString());
61              e.printStackTrace(System.err);
62          }
63      }
64          
65      private void buildCommandList(){
66          //commands = new Commands[MAXCOMMANDS];
67          //commands[0] = new Commands("quit","com.rohanclan.ashpool.Ashpool$Quit");
68          //commands[1] = new Commands("list","com.rohanclan.ashpool.Ashpool$ListTables");
69      }
70      
71      public void startMainProgramLoop(){
72          StringBuffer commandbuff;
73          String command;
74          
75          int in = 0;
76          System.out.println(" <   Welcome to Ashpool  >");
77          System.out.println("<< Type 'help;' for help >>");
78          System.out.println("");
79          System.out.print(p1);
80          
81          try{
82              while(running){
83                  commandbuff = new StringBuffer();
84                  
85                  while((char)in != ';'){
86                      in = System.in.read();
87                      commandbuff.append((char)in);
88                      if(in == '\n'){
89                          System.out.print(p1);
90                      }
91                  }
92                  //clear out the enter
93                  command = commandbuff.substring(0,commandbuff.length() -1);
94                  in = 0;
95                  
96                  /////////////temp///////////////////////////////////////////////
97                  if(command.toString().trim().toLowerCase().equals("quit") 
98                      || command.toString().trim().toLowerCase().equals("exit")){
99                      new Ashpool.Quit();
100                 }else if(command.toString().trim().toLowerCase().equals("help")){
101                     new Ashpool.Help();
102                 }else if(command.toString().trim().toLowerCase().equals("sys")){
103                     new Ashpool.Memory();
104                 }else if(command.toString().trim().toLowerCase().equals("gc")){
105                     System.out.print("Garbage Collecting...");
106                     r.gc();
107                     System.out.println("[ Done ]");
108                 //execute a system command
109                 }else if(command.toString().trim().startsWith("!")){
110                     r.exec(command.toString().trim().substring(1));
111                 }else{
112                     currentcmd = command.toString().trim();
113                     new Ashpool.RunSQL();
114                 }
115                 ////////////////////////////////////////////////////////////////
116             }
117         }catch(Exception e){
118             System.err.println("Main loop: " + e.toString());
119             e.printStackTrace(System.err);
120         }
121     }
122     
123     /** repeats a string x times */
124     private void repeat(String pattern, int length){
125         for(int x=0; x<length; x++){
126             System.out.print(pattern);
127         }   
128     }
129     
130     /** draws a "line" */
131     private void drawLine(String pattern, int length){
132         repeat(pattern, length);
133         System.out.println("");
134     }
135     
136     /** cause I am lazy */
137     private void print(String msg){
138         System.out.print(msg);
139     }
140     
141     /** cause I am lazy */
142     private void println(String msg){
143         System.out.println(msg);
144     }
145     
146     ///////////////////////////////////////////////////////////////////////////
147     class Commands {
148         String command;
149         String _command;
150         
151         public Commands(String outname, String innername){
152             this.command = outname;
153             this._command = innername;
154         }
155         
156         public void runCommand(){
157             try{
158                 Class cl = Class.forName(_command);
159                 
160             }catch(Exception e){
161                 System.err.println("Problem exec command " + command + ": " + e.toString());
162                 e.printStackTrace(System.err);
163             }
164         }
165     }
166     ///////////////////////////////////////////////////////////////////////////
167     interface AshpoolCmd{
168         public void doAction();
169     }
170     
171     class Help implements AshpoolCmd{
172         public Help(){ doAction(); }
173         
174         public void doAction(){
175                         print("+"); repeat("=",52); println("+");
176             println("| * Basic Help:                                      |");
177             println("| End SQL commands with a ';' to run them.           |");
178             println("| type 'quit;' or 'exit;' to exit                    |");
179                         print("+"); repeat("=",52); println("+");
180             println("| * System Commands:                                 |");
181             println("| select test;            = simple result            |");
182             println("| select tables;          = table listing            |");
183             println("| select types;           = supported types          |");
184             println("| select columns <table>; = show a tables columns    |");
185             println("| sys;                    = see system environment   |");
186             println("| gc;                     = force garbage collection |");
187             println("| !<command>;             = execute system command   |");
188                         print("+"); repeat("=",52); println("+");
189         }
190     }
191     
192     class Memory implements AshpoolCmd{
193         public Memory(){ doAction(); }
194         
195         public void doAction() {
196             long freeMemory  = r.freeMemory();
197             long totalMemory = r.totalMemory();
198             long maxMemory   = r.maxMemory();
199             
200             println("Available Processors: " + r.availableProcessors());
201             println("Total: " + totalMemory 
202                     + " (" + (int)(totalMemory/1024) + "K " 
203                     + " " + (int)(totalMemory/1024/1024) + "MB)"
204                    ); 
205             println("Free : " + freeMemory
206                     + " (" + (int)(freeMemory/1024) + "K "
207                     + " " + (int)(freeMemory/1024/1024) + "MB)"
208                    );
209             println("Max  : " + maxMemory
210                     + " (" + (int)(maxMemory/1024) + "K "
211                     + " " + (int)(maxMemory/1024/1024) + "MB)"
212                    );
213              print("+"); repeat("=",51); println("+");
214             println("SAX Factory      : " 
215                 + System.getProperty("javax.xml.parsers.SAXParserFactory")
216             );
217             
218             println("Transform Factory: " 
219                 + System.getProperty("javax.xml.transform.TransformerFactory")
220             );
221             
222             println("Runtime Version  : " 
223                 + System.getProperty("java.runtime.version")
224             );
225             println("OS Name          : " 
226                 + System.getProperty("os.name")
227             );
228             println("Processor Type   : " 
229                 + System.getProperty("os.arch")
230             );
231             println("File Encoding    : " 
232                 + System.getProperty("file.encoding")
233             );
234             println("User Language    : " 
235                 + System.getProperty("user.language")
236             );
237         }        
238     }
239     
240     class RunSQL implements AshpoolCmd{
241         public RunSQL(){ doAction(); }
242         public void doAction(){
243             try{
244                 AResultSet qresults = connMan.executeStatement(currentcmd);
245                 if(qresults != null && ((AResultSetMetaData)qresults.getMetaData()).getRecordCount() > 0){
246                     
247                     drawLine("=",80);
248                     for(int c=1; c<= qresults.getMetaData().getColumnCount(); c++){
249                         System.out.print(qresults.getMetaData().getColumnName(c) + " | ");
250                     }
251                     println("");
252                     drawLine("-",80);
253                     while(qresults.next()){
254                         for(int c=1; c<= qresults.getMetaData().getColumnCount(); c++){
255                             System.out.print(qresults.getString(c) + " | ");
256                         }
257                         System.out.println("");
258                     }
259                     drawLine("=",80);
260                 }else{
261                     System.out.println("Empty Results.");
262                 }
263             }catch(Exception e){
264                 System.out.println("Unknown command? " + currentcmd);
265                 e.printStackTrace(System.err);
266             }
267         }
268     }
269         
270     class Quit implements AshpoolCmd{
271         public Quit(){ doAction();}
272         public void doAction(){
273             running = false;   
274         }
275     }
276     
277     /////////////////////////////////////////////////////////////////////////
278     public static void main(String args[]){
279         if(args.length < 1){
280             System.out.println("Usage: Ashpool <datastore>");
281             System.exit(1);
282         }
283         System.out.println("+===============================+");
284         System.out.println("|    Ashpool  \u00ABXML Database\u00BB    |");
285         System.out.println("|   Copyright 2003  Rob Rohan   |");
286         System.out.println("+===============================+");
287         
288         Ashpool boot = new Ashpool(args[0]);
289     }
290 }