use of lucee.runtime.exp.PageException 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;
}
use of lucee.runtime.exp.PageException in project Lucee by lucee.
the class IKHandlerDatasource method loadData.
@Override
public IKStorageValue loadData(PageContext pc, String appName, String name, String strType, int type, Log log) throws PageException {
ConfigImpl config = (ConfigImpl) ThreadLocalPageContext.getConfig(pc);
DatasourceConnectionPool pool = config.getDatasourceConnectionPool();
DatasourceConnection dc = pool.getDatasourceConnection(config, pc.getDataSource(name), null, null);
SQLExecutor executor = SQLExecutionFactory.getInstance(dc);
Query query;
try {
if (!dc.getDatasource().isStorage())
throw new ApplicationException("storage usage for this datasource is disabled, you can enable this in the Lucee administrator.");
query = executor.select(config, pc.getCFID(), pc.getApplicationContext().getName(), dc, type, log, true);
} catch (SQLException se) {
throw Caster.toPageException(se);
} finally {
if (dc != null)
pool.releaseDatasourceConnection(dc);
}
if (query != null && config.debug()) {
boolean debugUsage = DebuggerUtil.debugQueryUsage(pc, query);
pc.getDebugger().addQuery(debugUsage ? query : null, name, "", query.getSql(), query.getRecordcount(), ((PageContextImpl) pc).getCurrentPageSource(null), query.getExecutionTime());
}
boolean _isNew = query.getRecordcount() == 0;
if (_isNew) {
ScopeContext.info(log, "create new " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID() + " in datasource [" + name + "]");
return null;
}
String str = Caster.toString(query.getAt(KeyConstants._data, 1));
if (str.startsWith("struct:"))
return null;
try {
IKStorageValue data = (IKStorageValue) JavaConverter.deserialize(str);
ScopeContext.info(log, "load existing data from [" + name + "." + PREFIX + "_" + strType + "_data] to create " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID());
return data;
} catch (Exception e) {
ScopeContext.error(log, e);
return null;
// throw Caster.toPageException(e);
}
}
use of lucee.runtime.exp.PageException in project Lucee by lucee.
the class IKStorageValue method serialize.
static byte[] serialize(MapPro<Collection.Key, IKStorageScopeItem> data) throws PageException {
if (data == null)
return EMPTY;
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(os);
oos.writeObject(data);
} catch (Exception e) {
throw Caster.toPageException(e);
} finally {
IOUtil.closeEL(oos);
}
return os.toByteArray();
}
use of lucee.runtime.exp.PageException in project Lucee by lucee.
the class IKStorageValue method deserialize.
public static MapPro<Collection.Key, IKStorageScopeItem> deserialize(byte[] barr) throws PageException {
if (barr == null || barr.length == 0)
return null;
ObjectInputStream ois = null;
MapPro<Collection.Key, IKStorageScopeItem> data = null;
try {
ois = new ObjectInputStream(new ByteArrayInputStream(barr));
data = (MapPro<Collection.Key, IKStorageScopeItem>) ois.readObject();
} catch (Exception e) {
throw Caster.toPageException(e);
} finally {
IOUtil.closeEL(ois);
}
return data;
}
use of lucee.runtime.exp.PageException in project Lucee by lucee.
the class StorageScopeDatasource method _loadData.
protected static Struct _loadData(PageContext pc, String datasourceName, String strType, int type, Log log, boolean mxStyle) throws PageException {
ConfigImpl config = (ConfigImpl) ThreadLocalPageContext.getConfig(pc);
DatasourceConnectionPool pool = config.getDatasourceConnectionPool();
DatasourceConnection dc = pool.getDatasourceConnection(config, pc.getDataSource(datasourceName), null, null);
SQLExecutor executor = SQLExecutionFactory.getInstance(dc);
Query query;
try {
if (!dc.getDatasource().isStorage())
throw new ApplicationException("storage usage for this datasource is disabled, you can enable this in the Lucee administrator.");
query = executor.select(config, pc.getCFID(), pc.getApplicationContext().getName(), dc, type, log, true);
} catch (SQLException se) {
throw Caster.toPageException(se);
} finally {
if (dc != null)
pool.releaseDatasourceConnection(dc);
}
if (query != null && config.debug()) {
boolean debugUsage = DebuggerUtil.debugQueryUsage(pc, query);
pc.getDebugger().addQuery(debugUsage ? query : null, datasourceName, "", query.getSql(), query.getRecordcount(), ((PageContextImpl) pc).getCurrentPageSource(null), query.getExecutionTime());
}
boolean _isNew = query.getRecordcount() == 0;
if (_isNew) {
ScopeContext.info(log, "create new " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID() + " in datasource [" + datasourceName + "]");
return null;
}
String str = Caster.toString(query.get(KeyConstants._data));
if (str != null && str.startsWith("struct:"))
str = str.substring(7);
if (mxStyle)
return null;
try {
Struct s = (Struct) pc.evaluate(str);
ScopeContext.info(log, "load existing data from [" + datasourceName + "." + PREFIX + "_" + strType + "_data] to create " + strType + " scope for " + pc.getApplicationContext().getName() + "/" + pc.getCFID());
return s;
} catch (Exception e) {
ScopeContext.error(log, e);
return null;
}
}
Aggregations