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;
}
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]);
}
}
}
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;
}
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());
}
}
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;
}
Aggregations