java/sql-vyuka/src/java/cz/frantovo/sql/vyuka/dao/VyukaSuperDAO.java
author František Kučera <franta-hg@frantovo.cz>
Wed Feb 08 13:15:23 2012 +0100 (2012-02-08)
changeset 78 3b4abb1ec5a3
parent 16 9acb74ac7346
child 86 a51bbc91a4cb
permissions -rw-r--r--
Limit počtu řádků (10000) a doby provádění SQL dotazu (3 vteřiny) v pískovišti,
nedokonalá ochrana proti DoS útoku (kartézský součin, náročný dotaz).
franta-hg@16
     1
package cz.frantovo.sql.vyuka.dao;
franta-hg@16
     2
franta-hg@16
     3
import cz.frantovo.superDAO.SuperDAO;
franta-hg@16
     4
import java.sql.Connection;
franta-hg@16
     5
import java.sql.SQLException;
franta-hg@16
     6
import java.util.logging.Level;
franta-hg@16
     7
import javax.naming.InitialContext;
franta-hg@16
     8
import javax.naming.NamingException;
franta-hg@16
     9
import javax.sql.DataSource;
franta-hg@16
    10
franta-hg@16
    11
/**
franta-hg@16
    12
 *
franta-hg@16
    13
 * @author fiki
franta-hg@16
    14
 */
franta-hg@16
    15
public class VyukaSuperDAO extends SuperDAO {
franta-hg@16
    16
franta-hg@16
    17
    protected InitialContext kontext = null;
franta-hg@16
    18
franta-hg@16
    19
    protected enum DATABAZE {
franta-hg@16
    20
franta-hg@16
    21
        /** Databáze aplikace – historie, nastavení. */
franta-hg@16
    22
        APLIKACE,
franta-hg@16
    23
        /** Databáze, na které se spouštějí příkazy uživatelů. */
franta-hg@16
    24
        PISKOVISTE
franta-hg@16
    25
    }
franta-hg@16
    26
franta-hg@16
    27
    public VyukaSuperDAO() {
franta-hg@16
    28
        try {
franta-hg@16
    29
            kontext = new InitialContext();
franta-hg@16
    30
        } catch (NamingException ex) {
franta-hg@16
    31
            log.log(Level.SEVERE, "Chyba při inicializaci kontextu", ex);
franta-hg@16
    32
        }
franta-hg@16
    33
    }
franta-hg@16
    34
franta-hg@16
    35
    /**
franta-hg@16
    36
     * @param databaze Která databáze (aplikace = historie a nastavení | pískoviště = příkazy uživatele)
franta-hg@16
    37
     * @return Databázové spojení
franta-hg@16
    38
     */
franta-hg@16
    39
    protected Connection getSpojeni(DATABAZE databaze) {
franta-hg@16
    40
        if (databaze == null) {
franta-hg@16
    41
            databaze = DATABAZE.PISKOVISTE;
franta-hg@16
    42
        }
franta-hg@33
    43
        String jndi = orizni(getVlastnost(databaze, VyukaSuperDAO.class));
franta-hg@16
    44
        DataSource zdroj = null;
franta-hg@16
    45
        try {
franta-hg@16
    46
            zdroj = (DataSource) kontext.lookup(jndi);
franta-hg@16
    47
            return zdroj.getConnection();
franta-hg@16
    48
        } catch (NamingException ex) {
franta-hg@16
    49
            log.log(Level.SEVERE, "getSpojeni: lookup", ex);
franta-hg@16
    50
            return null;
franta-hg@16
    51
        } catch (SQLException ex) {
franta-hg@16
    52
            log.log(Level.SEVERE, "getSpojeni: sql", ex);
franta-hg@16
    53
            return null;
franta-hg@16
    54
        }
franta-hg@16
    55
    }
franta-hg@16
    56
}