Search in sources :

Example 1 with JDBCDriver

use of lucee.runtime.db.JDBCDriver in project Lucee by lucee.

the class XMLConfigWebFactory method loadDataSources.

/**
 * loads datasource settings from XMl DOM
 *
 * @param configServer
 * @param config
 * @param doc
 * @throws BundleException
 * @throws ClassNotFoundException
 */
private static void loadDataSources(ConfigServerImpl configServer, ConfigImpl config, Document doc, Log log) {
    // load JDBC Driver defintion
    {
        Element jdbc = getChildByName(doc.getDocumentElement(), "jdbc");
        Element[] drivers = getChildren(jdbc, "driver");
        Map<String, JDBCDriver> map = new HashMap<String, JDBCDriver>();
        // first add the server drivers, so they can be overwritten
        if (configServer != null) {
            JDBCDriver[] sds = configServer.getJDBCDrivers();
            for (JDBCDriver sd : sds) {
                map.put(sd.cd.toString(), sd);
            }
        }
        ClassDefinition cd;
        String label;
        for (Element driver : drivers) {
            cd = getClassDefinition(driver, "", config.getIdentification());
            label = getAttr(driver, "label");
            // check if label exists
            if (StringUtil.isEmpty(label)) {
                log.error("Datasource", "missing label for jdbc driver [" + cd.getClassName() + "]");
                continue;
            }
            // check if it is a bundle
            if (!cd.isBundle()) {
                log.error("Datasource", "jdbc driver [" + label + "] does not describe a bundle");
                continue;
            }
            map.put(cd.toString(), new JDBCDriver(label, cd));
        }
        config.setJDBCDrivers(map.values().toArray(new JDBCDriver[map.size()]));
    }
    // When set to true, makes JDBC use a representation for DATE data that
    // is compatible with the Oracle8i database.
    System.setProperty("oracle.jdbc.V8Compatible", "true");
    boolean hasCS = configServer != null;
    Map<String, DataSource> datasources = new HashMap<String, DataSource>();
    // Copy Parent datasources as readOnly
    if (hasCS) {
        Map<String, DataSource> ds = configServer.getDataSourcesAsMap();
        Iterator<Entry<String, DataSource>> it = ds.entrySet().iterator();
        Entry<String, DataSource> entry;
        while (it.hasNext()) {
            entry = it.next();
            if (!entry.getKey().equals(QOQ_DATASOURCE_NAME))
                datasources.put(entry.getKey(), entry.getValue().cloneReadOnly());
        }
    }
    // Default query of query DB
    try {
        setDatasource(config, datasources, QOQ_DATASOURCE_NAME, new ClassDefinitionImpl("org.hsqldb.jdbcDriver", "hsqldb", "1.8.0", config.getIdentification()), "hypersonic-hsqldb", "", -1, "jdbc:hsqldb:.", "sa", "", DEFAULT_MAX_CONNECTION, -1, 60000, true, true, DataSource.ALLOW_ALL, false, false, null, new StructImpl(), "", ParamSyntax.DEFAULT, false, false);
    } catch (Exception e) {
        log.error("Datasource", e);
    }
    SecurityManager sm = config.getSecurityManager();
    short access = sm.getAccess(SecurityManager.TYPE_DATASOURCE);
    int accessCount = -1;
    if (access == SecurityManager.VALUE_YES)
        accessCount = -1;
    else if (access == SecurityManager.VALUE_NO)
        accessCount = 0;
    else if (access >= SecurityManager.VALUE_1 && access <= SecurityManager.VALUE_10) {
        accessCount = access - SecurityManager.NUMBER_OFFSET;
    }
    // Databases
    Element databases = getChildByName(doc.getDocumentElement(), "data-sources");
    // if(databases==null)databases=doc.createElement("data-sources");
    // PSQ
    String strPSQ = getAttr(databases, "psq");
    if (StringUtil.isEmpty(strPSQ)) {
        // prior version was buggy, was the opposite
        strPSQ = getAttr(databases, "preserve-single-quote");
        if (!StringUtil.isEmpty(strPSQ)) {
            Boolean b = Caster.toBoolean(strPSQ, null);
            if (b != null)
                strPSQ = b.booleanValue() ? "false" : "true";
        }
    }
    if (access != SecurityManager.VALUE_NO && !StringUtil.isEmpty(strPSQ)) {
        config.setPSQL(toBoolean(strPSQ, true));
    } else if (hasCS)
        config.setPSQL(configServer.getPSQL());
    // Data Sources
    Element[] dataSources = getChildren(databases, "data-source");
    if (accessCount == -1)
        accessCount = dataSources.length;
    if (dataSources.length < accessCount)
        accessCount = dataSources.length;
    // if(hasAccess) {
    ClassDefinition cd;
    for (int i = 0; i < accessCount; i++) {
        Element dataSource = dataSources[i];
        if (dataSource.hasAttribute("database")) {
            try {
                cd = getClassDefinition(dataSource, "", config.getIdentification());
                if (!cd.isBundle()) {
                    JDBCDriver jdbc = config.getJDBCDriverByClassName(cd.getClassName(), null);
                    if (jdbc != null)
                        cd = jdbc.cd;
                }
                setDatasource(config, datasources, getAttr(dataSource, "name"), cd, getAttr(dataSource, "host"), getAttr(dataSource, "database"), Caster.toIntValue(getAttr(dataSource, "port"), -1), getAttr(dataSource, "dsn"), getAttr(dataSource, "username"), ConfigWebUtil.decrypt(getAttr(dataSource, "password")), Caster.toIntValue(getAttr(dataSource, "connectionLimit"), DEFAULT_MAX_CONNECTION), Caster.toIntValue(getAttr(dataSource, "connectionTimeout"), -1), Caster.toLongValue(getAttr(dataSource, "metaCacheTimeout"), 60000), toBoolean(getAttr(dataSource, "blob"), true), toBoolean(getAttr(dataSource, "clob"), true), Caster.toIntValue(getAttr(dataSource, "allow"), DataSource.ALLOW_ALL), toBoolean(getAttr(dataSource, "validate"), false), toBoolean(getAttr(dataSource, "storage"), false), getAttr(dataSource, "timezone"), toStruct(getAttr(dataSource, "custom")), getAttr(dataSource, "dbdriver"), ParamSyntax.toParamSyntax(dataSource, ParamSyntax.DEFAULT), toBoolean(getAttr(dataSource, "literal-timestamp-with-tsoffset"), false), toBoolean(getAttr(dataSource, "always-set-timeout"), false));
            } catch (Exception e) {
                log.error("Datasource", e);
            }
        }
    }
    // }
    config.setDataSources(datasources);
}
Also used : SecurityManager(lucee.runtime.security.SecurityManager) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) ClassDefinition(lucee.runtime.db.ClassDefinition) FunctionLibException(lucee.transformer.library.function.FunctionLibException) PageException(lucee.runtime.exp.PageException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SecurityException(lucee.runtime.exp.SecurityException) TagLibException(lucee.transformer.library.tag.TagLibException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SQLException(java.sql.SQLException) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) SAXException(org.xml.sax.SAXException) ClassException(lucee.commons.lang.ClassException) MalformedURLException(java.net.MalformedURLException) ExpressionException(lucee.runtime.exp.ExpressionException) ApplicationException(lucee.runtime.exp.ApplicationException) lucee.aprint(lucee.aprint) DataSource(lucee.runtime.db.DataSource) DumpWriterEntry(lucee.runtime.dump.DumpWriterEntry) Entry(java.util.Map.Entry) GatewayEntry(lucee.runtime.gateway.GatewayEntry) ClassDefinitionImpl(lucee.transformer.library.ClassDefinitionImpl) StructImpl(lucee.runtime.type.StructImpl) JDBCDriver(lucee.runtime.db.JDBCDriver) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 2 with JDBCDriver

use of lucee.runtime.db.JDBCDriver in project Lucee by lucee.

the class Admin method doGetJDBCDrivers.

private void doGetJDBCDrivers() throws PageException {
    JDBCDriver[] drivers = config.getJDBCDrivers();
    lucee.runtime.type.Query qry = new QueryImpl(new Key[] { KeyConstants._label, KeyConstants._class, KeyConstants._bundleName, KeyConstants._bundleVersion }, drivers.length, "jdbc");
    JDBCDriver driver;
    for (int row = 0; row < drivers.length; row++) {
        driver = drivers[row];
        qry.setAt(KeyConstants._label, row, driver.label);
        qry.setAt(KeyConstants._class, row, driver.cd.getClassName());
        qry.setAt(KeyConstants._bundleName, row, driver.cd.getName());
        qry.setAt(KeyConstants._bundleVersion, row, driver.cd.getVersion().toString());
    }
    pageContext.setVariable(getString("admin", action, "returnVariable"), qry);
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) JDBCDriver(lucee.runtime.db.JDBCDriver) Query(lucee.runtime.type.Query)

Aggregations

JDBCDriver (lucee.runtime.db.JDBCDriver)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 lucee.aprint (lucee.aprint)1 ClassException (lucee.commons.lang.ClassException)1 ClassDefinition (lucee.runtime.db.ClassDefinition)1 DataSource (lucee.runtime.db.DataSource)1 DumpWriterEntry (lucee.runtime.dump.DumpWriterEntry)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 ExpressionException (lucee.runtime.exp.ExpressionException)1 PageException (lucee.runtime.exp.PageException)1 SecurityException (lucee.runtime.exp.SecurityException)1 GatewayEntry (lucee.runtime.gateway.GatewayEntry)1