// Chromis POS - The New Face of Open Source POS // Copyright (c) (c) 2015-2016 // http://www.chromis.co.uk // // This file is part of Chromis POS // // Chromis POS is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Chromis POS is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Chromis POS. If not, see <http://www.gnu.org/licenses/>. package uk.chromis.pos.forms; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import uk.chromis.basic.BasicException; import uk.chromis.data.loader.Session; import uk.chromis.pos.util.AltEncrypter; /** * * @author adrianromero */ public class AppViewConnection { /** Creates a new instance of AppViewConnection */ private AppViewConnection() { } /** * * @param props * @return * @throws BasicException */ public static Session createSession(AppProperties props) throws BasicException { return createSession(); } public static Session createSession() throws BasicException { try{ if (isJavaWebStart()) { Class.forName(AppConfig.getInstance().getProperty("db.driver"), true, Thread.currentThread().getContextClassLoader()); } else { ClassLoader cloader = new URLClassLoader(new URL[] {new File(AppConfig.getInstance().getProperty("db.driverlib")).toURI().toURL()}); DriverManager.registerDriver(new DriverWrapper((Driver) Class.forName(AppConfig.getInstance().getProperty("db.driver"), true, cloader).newInstance())); } String sDBUser = AppConfig.getInstance().getProperty("db.user"); String sDBPassword = AppConfig.getInstance().getProperty("db.password"); if (sDBUser != null && sDBPassword != null && sDBPassword.startsWith("crypt:")) { // the password is encrypted AltEncrypter cypher = new AltEncrypter("cypherkey" + sDBUser); sDBPassword = cypher.decrypt(sDBPassword.substring(6)); } return new Session(AppConfig.getInstance().getProperty("db.URL"), sDBUser,sDBPassword); } catch (InstantiationException | IllegalAccessException | MalformedURLException | ClassNotFoundException e) { throw new BasicException(AppLocal.getIntString("message.databasedrivererror"), e); } catch (SQLException eSQL) { throw new BasicException(AppLocal.getIntString("message.databaseconnectionerror"), eSQL); } } private static boolean isJavaWebStart() { try { Class.forName("javax.jnlp.ServiceManager"); return true; } catch (ClassNotFoundException ue) { return false; } } }