Search in sources :

Example 1 with StorageScope

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

the class ScopeContext method getCount.

private int getCount(Map<String, Scope> context) {
    Iterator<Entry<String, Scope>> it = context.entrySet().iterator();
    Entry<String, Scope> entry;
    int count = 0;
    StorageScope s;
    while (it.hasNext()) {
        entry = it.next();
        if (entry.getValue() instanceof StorageScope) {
            s = (StorageScope) entry.getValue();
            if (!s.isExpired())
                count++;
        }
    }
    return count;
}
Also used : StorageScope(lucee.runtime.type.scope.storage.StorageScope) Entry(java.util.Map.Entry) StorageScope(lucee.runtime.type.scope.storage.StorageScope) MemoryScope(lucee.runtime.type.scope.storage.MemoryScope)

Example 2 with StorageScope

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

the class ScopeContext method storeUnusedStorageScope.

private void storeUnusedStorageScope(CFMLFactoryImpl cfmlFactory, int type) {
    Map<String, Map<String, Scope>> contexts = type == Scope.SCOPE_CLIENT ? cfClientContexts : cfSessionContexts;
    long timespan = type == Scope.SCOPE_CLIENT ? CLIENT_MEMORY_TIMESPAN : SESSION_MEMORY_TIMESPAN;
    String strType = VariableInterpreter.scopeInt2String(type);
    if (contexts.size() == 0)
        return;
    long now = System.currentTimeMillis();
    Object[] arrContexts = contexts.keySet().toArray();
    Object applicationName, cfid, o;
    Map<String, Scope> fhm;
    for (int i = 0; i < arrContexts.length; i++) {
        applicationName = arrContexts[i];
        fhm = contexts.get(applicationName);
        if (fhm.size() > 0) {
            Object[] arrClients = fhm.keySet().toArray();
            int count = arrClients.length;
            for (int y = 0; y < arrClients.length; y++) {
                cfid = arrClients[y];
                o = fhm.get(cfid);
                if (!(o instanceof StorageScope))
                    continue;
                StorageScope scope = (StorageScope) o;
                if (scope.lastVisit() + timespan < now && !(scope instanceof MemoryScope)) {
                    getLog().log(Log.LEVEL_INFO, "scope-context", "remove from memory " + strType + " scope for " + applicationName + "/" + cfid + " from storage " + scope.getStorage());
                    fhm.remove(arrClients[y]);
                    count--;
                }
            }
            if (count == 0)
                contexts.remove(arrContexts[i]);
        }
    }
}
Also used : StorageScope(lucee.runtime.type.scope.storage.StorageScope) StorageScope(lucee.runtime.type.scope.storage.StorageScope) MemoryScope(lucee.runtime.type.scope.storage.MemoryScope) MemoryScope(lucee.runtime.type.scope.storage.MemoryScope) Map(java.util.Map)

Example 3 with StorageScope

use of lucee.runtime.type.scope.storage.StorageScope 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 4 with StorageScope

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

the class ScopeContext method removeCFSessionScope.

public void removeCFSessionScope(PageContext pc) throws PageException {
    Session sess = getCFSessionScope(pc, new RefBooleanImpl());
    ApplicationContext appContext = pc.getApplicationContext();
    Map<String, Scope> context = getSubMap(cfSessionContexts, appContext.getName());
    if (context != null) {
        context.remove(pc.getCFID());
        if (sess instanceof StorageScope)
            ((StorageScope) sess).unstore(pc.getConfig());
    }
}
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) RefBooleanImpl(lucee.commons.lang.types.RefBooleanImpl) HttpSession(javax.servlet.http.HttpSession)

Example 5 with StorageScope

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

MemoryScope (lucee.runtime.type.scope.storage.MemoryScope)5 StorageScope (lucee.runtime.type.scope.storage.StorageScope)5 HttpSession (javax.servlet.http.HttpSession)3 ApplicationContext (lucee.runtime.listener.ApplicationContext)3 DataSource (lucee.runtime.db.DataSource)2 IKHandlerCache (lucee.runtime.type.scope.storage.IKHandlerCache)2 IKHandlerDatasource (lucee.runtime.type.scope.storage.IKHandlerDatasource)2 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 RefBooleanImpl (lucee.commons.lang.types.RefBooleanImpl)1 CacheConnection (lucee.runtime.cache.CacheConnection)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 PageException (lucee.runtime.exp.PageException)1