~ubuntu-branches/ubuntu/wily/ntop/wily-proposed

« back to all changes in this revision

Viewing changes to ntop/database/MySQLServer.java

  • Committer: Bazaar Package Importer
  • Author(s): Dennis Schoen
  • Date: 2002-04-12 11:38:47 UTC
  • Revision ID: james.westby@ubuntu.com-20020412113847-4k4yydw0pzybc6g8
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* NTOP JDBC interface for mySQL
 
2
*
 
3
*     Command line invocation requires a text file parameter to store SQL commands into for debug:
 
4
*
 
5
*         java MySQLServer [ntopSQL.txt]
 
6
*
 
7
*         The SQL capture file write may be commented out.
 
8
*         SQL time stamps are generated for the SQL capture file only.
 
9
*
 
10
*         This routine checks for and corrects the invalid "DELETE *" command generated by NTOP
 
11
*         It does NOT generate the missing "FIRSTSEEN" values.
 
12
*         Both of the delete and firstseen problems should be fixed in the NTOP C-code.
 
13
*
 
14
*  Author Dave Moore <davem@mitre.org>  
 
15
*  (Based upon Java ODBC sample for MS Access) (Don't forget to set you userID & password.)
 
16
*  March 2001
 
17
*/
 
18
import java.util.*;
 
19
import java.text.*;
 
20
import java.io.*;
 
21
import java.net.*;
 
22
import java.sql.*;
 
23
import java.lang.System;
 
24
 
 
25
public class MySQLServer {
 
26
  private Connection connection;
 
27
 
 
28
  private boolean checkForWarning (SQLWarning warn) {
 
29
    boolean rc = false;
 
30
 
 
31
    // If an SQLWarning object was given, display the
 
32
    // warning messages.  Note that there could be
 
33
    // multiple warnings chained together
 
34
    try {
 
35
      if (warn != null) {
 
36
        System.out.println ("\n *** Warning ***\n");
 
37
        rc = true;
 
38
        while (warn != null) {
 
39
          System.out.println ("SQLState: " +
 
40
                              warn.getSQLState ());
 
41
          System.out.println ("Message:  " +
 
42
                              warn.getMessage ());
 
43
          System.out.println ("");
 
44
          warn = warn.getNextWarning ();
 
45
        }
 
46
      }
 
47
      return rc;
 
48
    } catch(Exception e) {
 
49
      return(true);
 
50
    }
 
51
  }
 
52
 
 
53
  public MySQLServer(String dbName) {
 
54
    try {
 
55
      Class.forName("org.gjt.mm.mysql.Driver");
 
56
      connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ntop?user=myid&password=mypass");
 
57
      checkForWarning(connection.getWarnings());
 
58
      //connection.setAutoCommit(true); // Automatic transaction commitment
 
59
      connection.setAutoCommit(true); // Automatic transaction commitment
 
60
    } catch (Exception e) {
 
61
      e.printStackTrace();
 
62
      System.exit(-1);
 
63
    }
 
64
  }
 
65
 
 
66
 
 
67
  public void executeStatement(String statement) {
 
68
       PreparedStatement stmt;
 
69
       int rc;
 
70
 
 
71
        try {
 
72
          stmt = connection.prepareStatement(statement); // Insert record in the HostInfo table
 
73
          rc = stmt.executeUpdate();
 
74
          stmt.close(); // Close the statement
 
75
        } catch(Exception ex) {
 
76
          ex.printStackTrace();
 
77
        }
 
78
  }
 
79
 
 
80
    private String replace_substring(String original, String delimiter, String replacement) {
 
81
    // Given an "original" input string, find all values of the "delimiter" string
 
82
    // and replace them with the "replacement" string.
 
83
    String result = "";
 
84
    int delimiter_length = delimiter.length();
 
85
    int replacement_length = replacement.length();
 
86
    int index_1 = 0;
 
87
    int index_2 = 0;
 
88
    if (original != null) {
 
89
      while (index_2 >= 0) {
 
90
        index_2 = original.indexOf(delimiter, index_1);  // next delimiter position from previous
 
91
        if (index_2 >= 0) {
 
92
          if (index_2 == index_1) {         //very next character ?
 
93
            result = result + replacement;
 
94
            }
 
95
            else {
 
96
              result = result + original.substring(index_1, index_2) + replacement;
 
97
             //log("found " + delimiter + " replacing with " + replacement + ".");
 
98
            }
 
99
          index_1 = index_2 + delimiter_length;  //skip over delimiter.
 
100
        }
 
101
      }
 
102
      result = result + original.substring(index_1, original.length());  //pick up tail end.
 
103
    }
 
104
    return result;
 
105
  }
 
106
 
 
107
 
 
108
// only argument is a text file to echo SQL commands to for debug.
 
109
  public static void main( String args[] ) throws IOException
 
110
  {
 
111
        byte buffer[];
 
112
        int bufLength = 1514;
 
113
        String cmndline;
 
114
        MySQLServer jdbcServer;
 
115
        DatagramSocket socket = null;
 
116
        DatagramPacket pkt;
 
117
 
 
118
//       SimpleDateFormat DateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss:SSS zzz");
 
119
        SimpleDateFormat DateFormatter = new SimpleDateFormat("EEE, HH:mm:ss.SSS");
 
120
        java.util.Date currentTime_1;
 
121
 
 
122
        System.out.println("Starting up...");
 
123
 
 
124
        try {
 
125
            socket = new DatagramSocket(4000);
 
126
        } catch (Exception e) {
 
127
          e.printStackTrace();
 
128
          System.exit(-1);
 
129
        }
 
130
 
 
131
        if (args.length != 1)
 
132
        {
 
133
          System.out.println("SQL capture file name parameter is required");
 
134
          System.exit(-1);
 
135
        }
 
136
 
 
137
//     debug SQL commands out to listout
 
138
       PrintWriter listout = new PrintWriter(new FileWriter(args[0]), true);  //autoflush set to true
 
139
 
 
140
        buffer = new byte[bufLength];
 
141
        pkt = new DatagramPacket(buffer, buffer.length);
 
142
 
 
143
//        comment out to stop database access
 
144
          jdbcServer = new MySQLServer("NTOP");
 
145
 
 
146
        while(true) {
 
147
            try {
 
148
               socket.receive(pkt);
 
149
               String statement = new String(buffer, 0, pkt.getLength());
 
150
 
 
151
               //Fix SQL syntax(1): Change "DELETE *" to just "DELETE".
 
152
               statement = jdbcServer.replace_substring(statement, "DELETE *", "DELETE");
 
153
 
 
154
               currentTime_1 = new java.util.Date();
 
155
               String dateString = DateFormatter.format(currentTime_1);
 
156
               cmndline = "["+pkt.getLength()+"] '"+statement+"'";
 
157
 
 
158
//             comment out to eliminate debug dump file
 
159
              listout.println(dateString + " : " + cmndline);
 
160
 
 
161
//             comment out to block database access
 
162
               jdbcServer.executeStatement(statement);
 
163
 
 
164
               pkt.setLength(bufLength);
 
165
            } catch (Exception e) {
 
166
              e.printStackTrace();
 
167
            }
 
168
        }
 
169
 
 
170
        /*
 
171
        socket.close();
 
172
        System.out.println("Done.");
 
173
        */
 
174
  }
 
175
 
 
176
}