Search in sources :

Example 21 with DataSource

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

the class ScopeContext method getCFSessionScope.

/**
 * return cf session scope
 *
 * @param pc PageContext
 * @param isNew
 * @return cf session matching the context
 * @throws PageException
 */
private Session getCFSessionScope(PageContext pc, RefBoolean isNew) throws PageException {
    ApplicationContext appContext = pc.getApplicationContext();
    // get Context
    Map<String, Scope> context = getSubMap(cfSessionContexts, appContext.getName());
    // get Session
    boolean isMemory = false;
    String storage = appContext.getSessionstorage();
    if (StringUtil.isEmpty(storage, true)) {
        storage = ConfigImpl.DEFAULT_STORAGE_SESSION;
        isMemory = true;
    } else if ("ram".equalsIgnoreCase(storage)) {
        storage = "memory";
        isMemory = true;
    } else if ("registry".equalsIgnoreCase(storage)) {
        storage = "file";
    } else {
        storage = storage.toLowerCase();
        if ("memory".equals(storage))
            isMemory = true;
    }
    Session existing = (Session) context.get(pc.getCFID());
    if (existing != null && (existing.isExpired() || !(existing instanceof StorageScope)))
        // second should not happen
        existing = null;
    Session session = appContext.getSessionCluster() ? null : existing;
    if (session == null || !(session instanceof StorageScope) || !((StorageScope) session).getStorage().equalsIgnoreCase(storage)) {
        // not necessary to check session in the same way, because it is overwritten anyway
        if (isMemory) {
            if (existing != null)
                session = existing;
            else
                session = SessionMemory.getInstance(pc, isNew, getLog());
        } else if ("file".equals(storage)) {
            session = SessionFile.getInstance(appContext.getName(), pc, getLog());
        } else if ("cookie".equals(storage))
            session = SessionCookie.getInstance(appContext.getName(), pc, getLog());
        else {
            DataSource ds = pc.getDataSource(storage, null);
            if (ds != null && ds.isStorage()) {
                if (INVIDUAL_STORAGE_KEYS) {
                    try {
                        session = (Session) IKStorageScopeSupport.getInstance(Scope.SCOPE_SESSION, new IKHandlerDatasource(), appContext.getName(), storage, pc, existing, getLog());
                    } catch (PageException pe) {
                        session = SessionDatasource.getInstance(storage, pc, getLog(), null);
                    }
                } else
                    session = SessionDatasource.getInstance(storage, pc, getLog(), null);
            } else {
                if (INVIDUAL_STORAGE_KEYS) {
                    try {
                        session = (Session) IKStorageScopeSupport.getInstance(Scope.SCOPE_SESSION, new IKHandlerCache(), appContext.getName(), storage, pc, existing, getLog());
                    } catch (PageException pe) {
                        session = SessionCache.getInstance(storage, appContext.getName(), pc, existing, getLog(), null);
                    }
                } else
                    session = SessionCache.getInstance(storage, appContext.getName(), pc, existing, getLog(), null);
            }
            if (session == null) {
                // datasource not enabled for storage
                if (ds != null)
                    throw new ApplicationException("datasource [" + storage + "] is not enabled to be used as session/client storage, " + "you have to enable it in the Lucee administrator or define key \"storage=true\" for datasources defined in the application event handler.");
                CacheConnection cc = CacheUtil.getCacheConnection(pc, storage, null);
                if (cc != null)
                    throw new ApplicationException("cache [" + storage + "] is not enabled to be used  as a session/client storage, you have to enable it in the Lucee administrator.");
                throw new ApplicationException("there is no cache or datasource with name [" + storage + "] defined.");
            }
        }
        if (session instanceof StorageScope)
            ((StorageScope) session).setStorage(storage);
        context.put(pc.getCFID(), session);
        isNew.setValue(true);
    } else {
        getLog().log(Log.LEVEL_INFO, "scope-context", "use existing session scope for " + appContext.getName() + "/" + pc.getCFID() + " from storage " + storage);
    }
    session.touchBeforeRequest(pc);
    return session;
}
Also used : StorageScope(lucee.runtime.type.scope.storage.StorageScope) PageException(lucee.runtime.exp.PageException) DataSource(lucee.runtime.db.DataSource) IKHandlerDatasource(lucee.runtime.type.scope.storage.IKHandlerDatasource) ApplicationContext(lucee.runtime.listener.ApplicationContext) ApplicationException(lucee.runtime.exp.ApplicationException) StorageScope(lucee.runtime.type.scope.storage.StorageScope) MemoryScope(lucee.runtime.type.scope.storage.MemoryScope) CacheConnection(lucee.runtime.cache.CacheConnection) IKHandlerCache(lucee.runtime.type.scope.storage.IKHandlerCache) HttpSession(javax.servlet.http.HttpSession)

Example 22 with DataSource

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

the class StorageScopeDatasource method store.

@Override
public void store(PageContext pc) {
    DatasourceConnection dc = null;
    ConfigImpl ci = (ConfigImpl) ThreadLocalPageContext.getConfig(pc);
    DatasourceConnectionPool pool = ci.getDatasourceConnectionPool();
    Log log = ci.getLog("scope");
    try {
        // FUTURE change method interface
        pc = ThreadLocalPageContext.get(pc);
        DataSource ds;
        if (pc != null)
            ds = pc.getDataSource(datasourceName);
        else
            ds = ci.getDataSource(datasourceName);
        dc = pool.getDatasourceConnection(null, ds, null, null);
        SQLExecutor executor = SQLExecutionFactory.getInstance(dc);
        executor.update(ci, cfid, appName, dc, getType(), sct, getTimeSpan(), log);
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        ScopeContext.error(log, t);
    } finally {
        if (dc != null)
            pool.releaseDatasourceConnection(dc);
    }
}
Also used : DatasourceConnection(lucee.runtime.db.DatasourceConnection) DatasourceConnectionPool(lucee.runtime.db.DatasourceConnectionPool) Log(lucee.commons.io.log.Log) SQLExecutor(lucee.runtime.type.scope.storage.db.SQLExecutor) ConfigImpl(lucee.runtime.config.ConfigImpl) DataSource(lucee.runtime.db.DataSource)

Example 23 with DataSource

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

the class Admin method doUpdateDatasource.

/**
 * @throws PageException
 */
private void doUpdateDatasource() throws PageException {
    int allow = (getBoolV("allowed_select", false) ? DataSource.ALLOW_SELECT : 0) + (getBoolV("allowed_insert", false) ? DataSource.ALLOW_INSERT : 0) + (getBoolV("allowed_update", false) ? DataSource.ALLOW_UPDATE : 0) + (getBoolV("allowed_delete", false) ? DataSource.ALLOW_DELETE : 0) + (getBoolV("allowed_alter", false) ? DataSource.ALLOW_ALTER : 0) + (getBoolV("allowed_drop", false) ? DataSource.ALLOW_DROP : 0) + (getBoolV("allowed_revoke", false) ? DataSource.ALLOW_REVOKE : 0) + (getBoolV("allowed_grant", false) ? DataSource.ALLOW_GRANT : 0) + (getBoolV("allowed_create", false) ? DataSource.ALLOW_CREATE : 0);
    if (allow == 0)
        allow = DataSource.ALLOW_ALL;
    String cn = getString("admin", action, "classname");
    if ("com.microsoft.jdbc.sqlserver.SQLServerDriver".equals(cn)) {
        cn = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    }
    ClassDefinition cd = new ClassDefinitionImpl(cn, getString("bundleName", null), getString("bundleVersion", null), config.getIdentification());
    // customParameterSyntax
    Struct sct = getStruct("customParameterSyntax", null);
    ParamSyntax ps = (sct != null && sct.containsKey("delimiter") && sct.containsKey("separator")) ? ParamSyntax.toParamSyntax(sct) : ParamSyntax.DEFAULT;
    // 
    boolean literalTimestampWithTSOffset = getBoolV("literalTimestampWithTSOffset", false);
    boolean alwaysSetTimeout = getBoolV("alwaysSetTimeout", false);
    String dsn = getString("admin", action, "dsn");
    String name = getString("admin", action, "name");
    String newName = getString("admin", action, "newName");
    String username = getString("admin", action, "dbusername");
    String password = getString("admin", action, "dbpassword");
    String host = getString("host", "");
    String timezone = getString("timezone", "");
    String database = getString("database", "");
    int port = getInt("port", -1);
    int connLimit = getInt("connectionLimit", -1);
    int connTimeout = getInt("connectionTimeout", -1);
    long metaCacheTimeout = getLong("metaCacheTimeout", 60000);
    boolean blob = getBoolV("blob", false);
    boolean clob = getBoolV("clob", false);
    boolean validate = getBoolV("validate", false);
    boolean storage = getBoolV("storage", false);
    boolean verify = getBoolV("verify", true);
    Struct custom = getStruct("custom", new StructImpl());
    String dbdriver = getString("dbdriver", "");
    // config.getDatasourceConnectionPool().remove(name);
    DataSource ds = null;
    try {
        ds = new DataSourceImpl(config, null, name, cd, host, dsn, database, port, username, password, connLimit, connTimeout, metaCacheTimeout, blob, clob, allow, custom, false, validate, storage, null, dbdriver, ps, literalTimestampWithTSOffset, alwaysSetTimeout, config.getLog("application"));
    } catch (Exception e) {
        throw Caster.toPageException(e);
    }
    if (verify)
        _doVerifyDatasource(ds, username, password);
    // print.out("limit:"+connLimit);
    admin.updateDataSource(name, newName, cd, dsn, username, password, host, database, port, connLimit, connTimeout, metaCacheTimeout, blob, clob, allow, validate, storage, timezone, custom, dbdriver, ps, literalTimestampWithTSOffset, alwaysSetTimeout);
    store();
    adminSync.broadcast(attributes, config);
}
Also used : ClassDefinitionImpl(lucee.transformer.library.ClassDefinitionImpl) StructImpl(lucee.runtime.type.StructImpl) DataSourceImpl(lucee.runtime.db.DataSourceImpl) ClassDefinition(lucee.runtime.db.ClassDefinition) ParamSyntax(lucee.runtime.db.ParamSyntax) PageException(lucee.runtime.exp.PageException) SecurityException(lucee.runtime.exp.SecurityException) IOException(java.io.IOException) DeprecatedException(lucee.runtime.exp.DeprecatedException) BundleException(org.osgi.framework.BundleException) MalformedURLException(java.net.MalformedURLException) SMTPException(lucee.runtime.net.mail.SMTPException) ApplicationException(lucee.runtime.exp.ApplicationException) Struct(lucee.runtime.type.Struct) DataSource(lucee.runtime.db.DataSource)

Example 24 with DataSource

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

the class Admin method doGetDatasources.

/**
 * @throws PageException
 */
private void doGetDatasources() throws PageException {
    Map ds = config.getDataSourcesAsMap();
    Iterator it = ds.keySet().iterator();
    lucee.runtime.type.Query qry = new QueryImpl(new String[] { "name", "host", "classname", "bundleName", "bundleVersion", "dsn", "DsnTranslated", "database", "port", "timezone", "username", "password", "passwordEncrypted", "readonly", "grant", "drop", "create", "revoke", "alter", "select", "delete", "update", "insert", "connectionLimit", "openConnections", "connectionTimeout", "clob", "blob", "validate", "storage", "customSettings", "metaCacheTimeout" }, ds.size(), "query");
    int row = 0;
    while (it.hasNext()) {
        Object key = it.next();
        DataSource d = (DataSource) ds.get(key);
        row++;
        qry.setAt(KeyConstants._name, row, key);
        qry.setAt(KeyConstants._host, row, d.getHost());
        qry.setAt("classname", row, d.getClassDefinition().getClassName());
        qry.setAt("bundleName", row, d.getClassDefinition().getName());
        qry.setAt("bundleVersion", row, d.getClassDefinition().getVersionAsString());
        qry.setAt("dsn", row, d.getDsnOriginal());
        qry.setAt("database", row, d.getDatabase());
        qry.setAt(KeyConstants._port, row, d.getPort() < 1 ? "" : Caster.toString(d.getPort()));
        qry.setAt("dsnTranslated", row, d.getDsnTranslated());
        qry.setAt("timezone", row, toStringTimeZone(d.getTimeZone()));
        qry.setAt(KeyConstants._password, row, d.getPassword());
        qry.setAt("passwordEncrypted", row, ConfigWebUtil.encrypt(d.getPassword()));
        qry.setAt(KeyConstants._username, row, d.getUsername());
        qry.setAt(KeyConstants._readonly, row, Caster.toBoolean(d.isReadOnly()));
        qry.setAt(KeyConstants._select, row, Boolean.valueOf(d.hasAllow(DataSource.ALLOW_SELECT)));
        qry.setAt(KeyConstants._delete, row, Boolean.valueOf(d.hasAllow(DataSource.ALLOW_DELETE)));
        qry.setAt(KeyConstants._update, row, Boolean.valueOf(d.hasAllow(DataSource.ALLOW_UPDATE)));
        qry.setAt(KeyConstants._create, row, Boolean.valueOf(d.hasAllow(DataSource.ALLOW_CREATE)));
        qry.setAt(KeyConstants._insert, row, Boolean.valueOf(d.hasAllow(DataSource.ALLOW_INSERT)));
        qry.setAt(KeyConstants._drop, row, Boolean.valueOf(d.hasAllow(DataSource.ALLOW_DROP)));
        qry.setAt(KeyConstants._grant, row, Boolean.valueOf(d.hasAllow(DataSource.ALLOW_GRANT)));
        qry.setAt(KeyConstants._revoke, row, Boolean.valueOf(d.hasAllow(DataSource.ALLOW_REVOKE)));
        qry.setAt(KeyConstants._alter, row, Boolean.valueOf(d.hasAllow(DataSource.ALLOW_ALTER)));
        int oc = config.getDatasourceConnectionPool().openConnections(key.toString());
        qry.setAt("openConnections", row, oc < 0 ? 0 : oc);
        qry.setAt("connectionLimit", row, d.getConnectionLimit() < 1 ? "" : Caster.toString(d.getConnectionLimit()));
        qry.setAt("connectionTimeout", row, d.getConnectionTimeout() < 1 ? "" : Caster.toString(d.getConnectionTimeout()));
        qry.setAt("customSettings", row, d.getCustoms());
        qry.setAt("blob", row, Boolean.valueOf(d.isBlob()));
        qry.setAt("clob", row, Boolean.valueOf(d.isClob()));
        qry.setAt("validate", row, Boolean.valueOf(d.validate()));
        qry.setAt("storage", row, Boolean.valueOf(d.isStorage()));
        qry.setAt("metaCacheTimeout", row, Caster.toDouble(d.getMetaCacheTimeout()));
    }
    pageContext.setVariable(getString("admin", action, "returnVariable"), qry);
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) Iterator(java.util.Iterator) Map(java.util.Map) HashMap(java.util.HashMap) Query(lucee.runtime.type.Query) DataSource(lucee.runtime.db.DataSource)

Example 25 with DataSource

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

the class PageContextImpl method getDataSource.

@Override
public DataSource getDataSource(String datasource) throws PageException {
    DataSource ds = getApplicationContext() == null ? null : getApplicationContext().getDataSource(datasource, null);
    if (ds != null)
        return ds;
    ds = getConfig().getDataSource(datasource, null);
    if (ds != null)
        return ds;
    throw DatabaseException.notFoundException(this, datasource);
}
Also used : DataSource(lucee.runtime.db.DataSource)

Aggregations

DataSource (lucee.runtime.db.DataSource)27 PageException (lucee.runtime.exp.PageException)9 Struct (lucee.runtime.type.Struct)9 DatasourceConnection (lucee.runtime.db.DatasourceConnection)8 ApplicationException (lucee.runtime.exp.ApplicationException)8 ConfigImpl (lucee.runtime.config.ConfigImpl)7 StructImpl (lucee.runtime.type.StructImpl)7 SQLException (java.sql.SQLException)5 Map (java.util.Map)5 Log (lucee.commons.io.log.Log)5 Entry (java.util.Map.Entry)4 CacheConnection (lucee.runtime.cache.CacheConnection)4 ClassDefinition (lucee.runtime.db.ClassDefinition)4 DataSourceManager (lucee.runtime.db.DataSourceManager)4 DatasourceConnectionPool (lucee.runtime.db.DatasourceConnectionPool)4 ApplicationContext (lucee.runtime.listener.ApplicationContext)4 QueryImpl (lucee.runtime.type.QueryImpl)4 SQLExecutor (lucee.runtime.type.scope.storage.db.SQLExecutor)4 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3