franta-hg@0: package cz.frantovo.superPostak; franta-hg@0: franta-hg@0: import java.security.MessageDigest; franta-hg@0: import java.security.NoSuchAlgorithmException; franta-hg@0: import java.sql.Connection; franta-hg@0: import java.sql.DriverManager; franta-hg@0: import java.sql.SQLException; franta-hg@0: import java.util.Arrays; franta-hg@0: import java.util.logging.Level; franta-hg@0: import java.util.logging.Logger; franta-hg@0: franta-hg@0: /** franta-hg@0: * Vrstva pro připojení k databázi franta-hg@0: * @author fiki franta-hg@0: */ franta-hg@0: public class Data { franta-hg@0: franta-hg@13: private static final Logger log = Logger.getLogger(Data.class.getName()); franta-hg@0: private static Connection spojeni; franta-hg@0: private static byte[] hash; franta-hg@0: private static MessageDigest sha; franta-hg@0: franta-hg@0: public static synchronized DataSQL getSpojeniSQL(String url, String jmeno, char[] heslo) throws SQLException { franta-hg@0: return new DataSQL(getSpojeni(url, jmeno, heslo)); franta-hg@0: } franta-hg@0: franta-hg@0: private static synchronized Connection getSpojeni(String url, String jmeno, char[] heslo) throws SQLException { franta-hg@0: if (Arrays.equals(hash, hashuj(url, jmeno, heslo)) && spojeni != null && !spojeni.isClosed()) { franta-hg@0: log.log(Level.INFO, "Znovupoužívám DB spojení."); franta-hg@0: } else { franta-hg@0: spojeni = getNoveSpojeni(url, jmeno, heslo); franta-hg@0: } franta-hg@0: franta-hg@0: return spojeni; franta-hg@0: } franta-hg@0: franta-hg@0: private static Connection getNoveSpojeni( franta-hg@0: String url, String jmeno, char[] heslo) throws SQLException { franta-hg@0: /** Pokusíme se uzavřít staré spojení */ franta-hg@0: if (spojeni != null && !spojeni.isClosed()) { franta-hg@0: try { franta-hg@0: spojeni.close(); franta-hg@0: } catch (Exception e) { franta-hg@0: log.log(Level.INFO, "Nepodařilo se uzavřít spojení", e); franta-hg@0: } franta-hg@0: } franta-hg@0: franta-hg@0: /** Vrátíme nové spojení */ franta-hg@0: hash = hashuj(url, jmeno, heslo); franta-hg@0: spojeni = DriverManager.getConnection(url, jmeno, String.valueOf(heslo)); franta-hg@13: log.log(Level.INFO, "Otevřeno nové spoejní spojení k databázi: {0}", url); franta-hg@0: return spojeni; franta-hg@0: } franta-hg@0: franta-hg@0: private static byte[] hashuj(String url, String jmeno, char[] heslo) { franta-hg@0: try { franta-hg@0: if (sha == null) { franta-hg@0: sha = MessageDigest.getInstance("SHA-256"); franta-hg@0: } franta-hg@0: String text = url + "/" + jmeno + "/" + String.valueOf(heslo); franta-hg@0: return sha.digest(text.getBytes()); franta-hg@0: franta-hg@0: } catch (NoSuchAlgorithmException e) { franta-hg@0: /** Tohle by nemělo nikdy nastat. V nouzovém případě použijeme součet hodnot místo hashe. */ franta-hg@0: log.log(Level.SEVERE, "Neexistující hashovací algoritmus", e); franta-hg@0: String soucet = url + "/" + jmeno + String.valueOf(heslo).length(); franta-hg@0: return soucet.getBytes(); franta-hg@0: } franta-hg@0: } franta-hg@0: } franta-hg@0: franta-hg@0: