Search in sources :

Example 1 with IKHandlerDatasource

use of lucee.runtime.type.scope.storage.IKHandlerDatasource in project Lucee by lucee.

the class ScopeContext method getClientScope.

public Client getClientScope(PageContext pc) throws PageException {
    ApplicationContext appContext = pc.getApplicationContext();
    // get Context
    Map<String, Scope> context = getSubMap(cfClientContexts, appContext.getName());
    // get Client
    boolean isMemory = false;
    String storage = appContext.getClientstorage();
    if (StringUtil.isEmpty(storage, true)) {
        storage = ConfigImpl.DEFAULT_STORAGE_CLIENT;
    } 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;
    }
    Client existing = (Client) context.get(pc.getCFID());
    Client client = appContext.getClientCluster() ? null : existing;
    // client=doMemory?(Client) context.get(pc.getCFID()):null;
    if (client == null || client.isExpired() || !client.getStorage().equalsIgnoreCase(storage)) {
        if ("file".equals(storage)) {
            client = ClientFile.getInstance(appContext.getName(), pc, getLog());
        } else if ("cookie".equals(storage))
            client = ClientCookie.getInstance(appContext.getName(), pc, getLog());
        else if ("memory".equals(storage)) {
            if (existing != null)
                client = existing;
            client = ClientMemory.getInstance(pc, getLog());
        } else {
            DataSource ds = pc.getDataSource(storage, null);
            if (ds != null) {
                if (INVIDUAL_STORAGE_KEYS) {
                    try {
                        client = (Client) IKStorageScopeSupport.getInstance(Scope.SCOPE_CLIENT, new IKHandlerDatasource(), appContext.getName(), storage, pc, existing, getLog());
                    } catch (PageException pe) {
                        // code above could fail when an old scope is loaded, remember client scope can be easy be
                        // 180 days old
                        client = ClientDatasource.getInstance(storage, pc, getLog());
                    }
                } else
                    client = ClientDatasource.getInstance(storage, pc, getLog());
            } else {
                if (INVIDUAL_STORAGE_KEYS) {
                    try {
                        client = (Client) IKStorageScopeSupport.getInstance(Scope.SCOPE_CLIENT, new IKHandlerCache(), appContext.getName(), storage, pc, existing, getLog());
                    } catch (PageException pe) {
                        // code above could fail when an old scope is loaded, remember client scope can be easy be
                        // 180 days old
                        client = ClientCache.getInstance(storage, appContext.getName(), pc, existing, getLog(), null);
                    }
                } else
                    client = ClientCache.getInstance(storage, appContext.getName(), pc, existing, getLog(), null);
            }
            if (client == 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.");
                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.");
            }
        }
        client.setStorage(storage);
        context.put(pc.getCFID(), client);
    } else
        getLog().log(Log.LEVEL_INFO, "scope-context", "use existing client scope for " + appContext.getName() + "/" + pc.getCFID() + " from storage " + storage);
    client.touchBeforeRequest(pc);
    return client;
}
Also used : PageException(lucee.runtime.exp.PageException) 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) DataSource(lucee.runtime.db.DataSource) IKHandlerDatasource(lucee.runtime.type.scope.storage.IKHandlerDatasource)

Example 2 with IKHandlerDatasource

use of lucee.runtime.type.scope.storage.IKHandlerDatasource in project Lucee by lucee.

the class ScopeContext method hasExistingCFSessionScope.

private boolean hasExistingCFSessionScope(PageContext pc) {
    ApplicationContext appContext = pc.getApplicationContext();
    // get Context
    Map<String, Scope> context = getSubMap(cfSessionContexts, appContext.getName());
    // get Session
    String storage = appContext.getSessionstorage();
    if (StringUtil.isEmpty(storage, true))
        storage = "memory";
    else if ("ram".equalsIgnoreCase(storage))
        storage = "memory";
    else if ("registry".equalsIgnoreCase(storage))
        storage = "file";
    else
        storage = storage.toLowerCase();
    Session session = (Session) context.get(pc.getCFID());
    if (!(session instanceof StorageScope) || session.isExpired() || !((StorageScope) session).getStorage().equalsIgnoreCase(storage)) {
        if ("memory".equals(storage))
            return false;
        else if ("file".equals(storage))
            return SessionFile.hasInstance(appContext.getName(), pc);
        else if ("cookie".equals(storage))
            return SessionCookie.hasInstance(appContext.getName(), pc);
        else {
            DataSource ds = pc.getConfig().getDataSource(storage, null);
            if (ds != null && ds.isStorage()) {
                if (INVIDUAL_STORAGE_KEYS) {
                    return IKStorageScopeSupport.hasInstance(Scope.SCOPE_SESSION, new IKHandlerDatasource(), appContext.getName(), storage, pc);
                } else {
                    if (SessionDatasource.hasInstance(storage, pc))
                        return true;
                }
            }
            if (INVIDUAL_STORAGE_KEYS)
                return IKStorageScopeSupport.hasInstance(Scope.SCOPE_SESSION, new IKHandlerCache(), appContext.getName(), storage, pc);
            return SessionCache.hasInstance(storage, appContext.getName(), pc);
        }
    }
    return true;
}
Also used : StorageScope(lucee.runtime.type.scope.storage.StorageScope) ApplicationContext(lucee.runtime.listener.ApplicationContext) StorageScope(lucee.runtime.type.scope.storage.StorageScope) MemoryScope(lucee.runtime.type.scope.storage.MemoryScope) IKHandlerCache(lucee.runtime.type.scope.storage.IKHandlerCache) HttpSession(javax.servlet.http.HttpSession) DataSource(lucee.runtime.db.DataSource) IKHandlerDatasource(lucee.runtime.type.scope.storage.IKHandlerDatasource)

Example 3 with IKHandlerDatasource

use of lucee.runtime.type.scope.storage.IKHandlerDatasource 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)

Aggregations

DataSource (lucee.runtime.db.DataSource)3 ApplicationContext (lucee.runtime.listener.ApplicationContext)3 IKHandlerCache (lucee.runtime.type.scope.storage.IKHandlerCache)3 IKHandlerDatasource (lucee.runtime.type.scope.storage.IKHandlerDatasource)3 MemoryScope (lucee.runtime.type.scope.storage.MemoryScope)3 StorageScope (lucee.runtime.type.scope.storage.StorageScope)3 HttpSession (javax.servlet.http.HttpSession)2 CacheConnection (lucee.runtime.cache.CacheConnection)2 ApplicationException (lucee.runtime.exp.ApplicationException)2 PageException (lucee.runtime.exp.PageException)2