Package mod.tsql

Source Code of mod.tsql.ScanTables

/*
*  This software and supporting documentation were developed by
*
*    Siemens Corporate Technology
*    Competence Center Knowledge Management and Business Transformation
*    D-81730 Munich, Germany
*
*    Authors (representing a really great team ;-) )
*            Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
*  This software is Open Source under GNU General Public License (GPL).
*  Read the text of this license in LICENSE.TXT
*  or look at www.opensource.org/licenses/
*
*  Once more we emphasize, that:
*  THIS SOFTWARE IS MADE AVAILABLE,  AS IS,  WITHOUT ANY WARRANTY
*  REGARDING  THE  SOFTWARE,  ITS  PERFORMANCE OR
*  FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
*  ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
*  PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/


// TsqlBatch

// ************ package ****************************************************
package mod.tsql;

/** Generic class for db dumps.
*  It writes the result to file in tsql dump format.
*  It runs in an endless loop sleeping the determined interval
*  in o:/KFM/config/all/TsqlBatch.properties.
*
* <usage>
* TsqlBatch tBatch = new TsqlBatch(); // make an instance
* tBatch.addDump("members", "where nick like 'tab.%'", "nick"); // add tables to dump,
* //define where conditions if necessary and order by clauses if necessary
* tBatch.execute(); // execute it
* </usage>
*
*/
// ************ imports ****************************************************
import KFM.Converter;
import java.util.*;
import java.sql.*;
import java.io.*;
import KFM.Smtp;

public class ScanTables{
                                    // With a value of -1 the number of loops will be endless (the default).
     private Properties mProps = null; // holds the properties e.g. database connection
     private String mMailHost; // mail host
     private String mAdminEmail; // administrator that received the mail
     private String mFrom; // email address from the sender
     private String mSmtpMessage = null; // message that should be sent
     private String mJdbcDriver = null;

     static final String cJdbcDriverDefault="oracle.jdbc.driver.OracleDriver";

     public ScanTables(String tPropsPath)
             throws FileNotFoundException,IOException
     {
         mProps = new Properties();
         mProps.load(new FileInputStream(tPropsPath));
     }



     protected void sendMail(int aCountResult){
         if (mSmtpMessage != null)
        {
            mSmtpMessage = Converter.replaceString("<resultcount>", Integer.toString(aCountResult), mSmtpMessage);
            mSmtpMessage = "Subject: " + mSmtpMessage + "\n\n" + mSmtpMessage + "\n";
            Smtp tMail = new Smtp(mMailHost, mAdminEmail, mFrom, mSmtpMessage, true);
        }

     }

    /**
     * Executes the program.
     * First initializes the db connection given in 'o:/KFM/config/all/TsqlBatch.properties'.
     * Second it starts an endless loop executing the dumps and then sleeping the period
     * given in property 'dumpInterval' of 'o:/KFM/config/all/TsqlBatch.properties'.
     */
    public void execute ()
    throws SQLException
    {
        ResultSet tResult = null;
        Connection tCon = null;
        Statement tSt = null;
        try{
            String tConnection = mProps.getProperty("JdbcConnection");
            String tUser = mProps.getProperty("JdbcUser");
            String tPassword = mProps.getProperty("JdbcPassword");
            mAdminEmail = mProps.getProperty("AdminEmail");
            mMailHost = mProps.getProperty("MailHost");
            mFrom = mProps.getProperty("From");
            String tQuery = mProps.getProperty("sqlquery");
            mSmtpMessage = mProps.getProperty("SmtpMessage");
            // open connection
            tCon = null;
            if (mJdbcDriver == null)
                mJdbcDriver = cJdbcDriverDefault;
            try {
                Class.forName(mJdbcDriver);
                // According to JDBC book, it is not necessary to call `DriverManager.registerDriver�.
            } catch (java.lang.ClassNotFoundException ex) {
                System.err.println("ClassNotFoundException: " + ex.getMessage());
                System.exit(1);
            }
            //SS DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            tCon = DriverManager.getConnection(tConnection, tUser, tPassword);
            // setAutoCommit
            tCon.setAutoCommit(false);
            tSt = tCon.createStatement();
            tResult = tSt.executeQuery(tQuery);
            if (tResult.next()){
                int tColumnCount = tResult.getInt(1);
                sendMail(tColumnCount);
            }
        }
        finally{
            if (tResult != null)
                tResult.close();
            if (tSt != null)
                tSt.close();
            if (tCon != null)
                tCon.close();
        }
    }


    /**
     * Here you can define the message that will be sent to you to inform you the update was
     * made correctly. It will be sent after each successfull update so if you haven't get the message
     * one day you have to make sure the process is still running.
     */
    public void setSmtpMessage(String aMessage)
    {
        mSmtpMessage = "Subject: " + aMessage + "\n\n" + aMessage + "\n";
    }


    /**
     * This is only an example to run this class.
     * In the example the tables members, desktops, gadgetdefs and gadgetroles are dumped.
     * @param args
     */
    public static void main (String [] args)
    {
        try{
            if (args == null || args.length < 1)
                System.out.println("Usage: java mod.tsql.ScanTables <path to property ScanTables.properties>");
            ScanTables tTables = new ScanTables(args[0]);
            tTables.execute();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
TOP

Related Classes of mod.tsql.ScanTables

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.