use of lucee.runtime.cache.CacheConnection in project Lucee by lucee.
the class Admin method doGetCacheDefaultConnection.
private void doGetCacheDefaultConnection() throws PageException {
int type;
String strType = getString("admin", "GetCacheDefaultConnection", "cacheType");
strType = strType.toLowerCase().trim();
if (strType.equals("object"))
type = ConfigImpl.CACHE_TYPE_OBJECT;
else if (strType.equals("template"))
type = ConfigImpl.CACHE_TYPE_TEMPLATE;
else if (strType.equals("query"))
type = ConfigImpl.CACHE_TYPE_QUERY;
else if (strType.equals("resource"))
type = ConfigImpl.CACHE_TYPE_RESOURCE;
else if (strType.equals("function"))
type = ConfigImpl.CACHE_TYPE_FUNCTION;
else if (strType.equals("include"))
type = ConfigImpl.CACHE_TYPE_INCLUDE;
else if (strType.equals("http"))
type = ConfigImpl.CACHE_TYPE_HTTP;
else if (strType.equals("file"))
type = ConfigImpl.CACHE_TYPE_FILE;
else if (strType.equals("webservice"))
type = ConfigImpl.CACHE_TYPE_WEBSERVICE;
else
throw new ApplicationException("inv,query,resourcealid type defintion, valid values are [object,template,query,resource,function,include]");
CacheConnection cc = config.getCacheDefaultConnection(type);
if (cc != null) {
Struct sct = new StructImpl();
sct.setEL(KeyConstants._name, cc.getName());
sct.setEL(KeyConstants._class, cc.getClassDefinition().getClassName());
sct.setEL(KeyConstants._bundleName, cc.getClassDefinition().getName());
sct.setEL(KeyConstants._bundleVersion, cc.getClassDefinition().getVersionAsString());
sct.setEL(KeyConstants._custom, cc.getCustom());
sct.setEL(KeyConstants._default, Caster.toBoolean(true));
sct.setEL(KeyConstants._readonly, Caster.toBoolean(cc.isReadOnly()));
pageContext.setVariable(getString("admin", action, "returnVariable"), sct);
} else
throw new ApplicationException("there is no cache default connection");
}
use of lucee.runtime.cache.CacheConnection in project Lucee by lucee.
the class XMLConfigAdmin method validateStorage.
private String validateStorage(String storage) throws ApplicationException {
storage = storage.trim().toLowerCase();
// empty
if (StringUtil.isEmpty(storage, true))
return "";
// standard storages
if ("cookie".equals(storage) || "memory".equals(storage) || "file".equals(storage))
return storage;
// aliases
if ("ram".equals(storage))
return "memory";
if ("registry".equals(storage))
return "file";
// datasource
DataSource ds = config.getDataSource(storage, null);
if (ds != null) {
if (ds.isStorage())
return storage;
throw new ApplicationException("datasource [" + storage + "] is not enabled to be used as session/client storage");
}
// cache
CacheConnection cc = CacheUtil.getCacheConnection(ThreadLocalPageContext.get(config), storage, null);
if (cc != null) {
if (cc.isStorage())
return storage;
throw new ApplicationException("cache [" + storage + "] is not enabled to be used as session/client storage");
}
String sdx = StringUtil.soundex(storage);
// check if a datasource has a similar name
DataSource[] sources = config.getDataSources();
for (int i = 0; i < sources.length; i++) {
if (StringUtil.soundex(sources[i].getName()).equals(sdx))
throw new ApplicationException("no matching storage for [" + storage + "] found, did you mean [" + sources[i].getName() + "]");
}
// check if a cache has a similar name
Iterator<String> it = config.getCacheConnections().keySet().iterator();
String name;
while (it.hasNext()) {
name = it.next();
if (StringUtil.soundex(name).equals(sdx))
throw new ApplicationException("no matching storage for [" + storage + "] found, did you mean [" + name + "]");
}
throw new ApplicationException("no matching storage for [" + storage + "] found");
}
use of lucee.runtime.cache.CacheConnection in project Lucee by lucee.
the class XMLConfigAdmin method removeCacheConnection.
public void removeCacheConnection(String name) throws ExpressionException, SecurityException {
checkWriteAccess();
boolean hasAccess = ConfigWebUtil.hasAccess(config, SecurityManagerImpl.TYPE_CACHE);
if (!hasAccess)
throw new SecurityException("no access to remove cache connection");
// check parameters
if (StringUtil.isEmpty(name))
throw new ExpressionException("name for Cache Connection can not be an empty value");
Element parent = _getRootElement("cache");
// remove default flag
if (name.equalsIgnoreCase(parent.getAttribute("default-object")))
parent.removeAttribute("default-object");
if (name.equalsIgnoreCase(parent.getAttribute("default-template")))
parent.removeAttribute("default-template");
if (name.equalsIgnoreCase(parent.getAttribute("default-query")))
parent.removeAttribute("default-query");
if (name.equalsIgnoreCase(parent.getAttribute("default-resource")))
parent.removeAttribute("default-resource");
// remove element
Element[] children = XMLConfigWebFactory.getChildren(parent, "connection");
for (int i = 0; i < children.length; i++) {
String n = children[i].getAttribute("name");
if (n != null && n.equalsIgnoreCase(name)) {
Map<String, CacheConnection> conns = config.getCacheConnections();
CacheConnection cc = conns.get(n.toLowerCase());
if (cc != null) {
CacheUtil.releaseEL(cc);
// CacheUtil.removeEL( config instanceof ConfigWeb ? (ConfigWeb) config : null, cc );
}
parent.removeChild(children[i]);
}
}
}
use of lucee.runtime.cache.CacheConnection in project Lucee by lucee.
the class XMLConfigWebFactory method loadCache.
/*private static ClassDefinition matchJDBCBundle(Config config, ClassDefinition cd) {
if(!cd.isBundle()) {
if("org.hsqldb.jdbcDriver".equals(cd.getClassName()))
return new ClassDefinitionImpl<>(cd.getClassName(), "hypersonic.hsqldb", null, config.getIdentification());
if("org.gjt.mm.mysql.Driver".equals(cd.getClassName()))
return new ClassDefinitionImpl<>(cd.getClassName(), "com.mysql.jdbc", null, config.getIdentification());
if("org.h2.Driver".equals(cd.getClassName()))
return new ClassDefinitionImpl<>(cd.getClassName(), "org.h2", null, config.getIdentification());
if("net.sourceforge.jtds.jdbc.Driver".equals(cd.getClassName()))
return new ClassDefinitionImpl<>(cd.getClassName(), "jtds", null, config.getIdentification());
if("com.microsoft.jdbc.sqlserver.SQLServerDriver".equals(cd.getClassName()))
return new ClassDefinitionImpl<>(cd.getClassName(), "microsoft.sqljdbc", null, config.getIdentification());
}
return cd;
}*/
/**
* @param configServer
* @param config
* @param doc
*/
/**
* @param configServer
* @param config
* @param doc
*/
private static void loadCache(ConfigServerImpl configServer, ConfigImpl config, Document doc, Log log) {
boolean hasCS = configServer != null;
// load Cache info
{
Element parent = getChildByName(doc.getDocumentElement(), "caches");
Element[] children = getChildren(parent, "cache");
Map<String, ClassDefinition> map = new HashMap<String, ClassDefinition>();
// first add the server drivers, so they can be overwritten
if (configServer != null) {
Iterator<ClassDefinition> it = configServer.getCacheDefinitions().values().iterator();
ClassDefinition cd;
while (it.hasNext()) {
cd = it.next();
map.put(cd.getClassName(), cd);
}
}
ClassDefinition cd;
String label;
for (Element child : children) {
cd = getClassDefinition(child, "", config.getIdentification());
// check if it is a bundle
if (!cd.isBundle()) {
log.error("Datasource", "[" + cd + "] does not have bundle info");
continue;
}
map.put(cd.getClassName(), cd);
}
config.setCacheDefinitions(map);
}
Map<String, CacheConnection> caches = new HashMap<String, CacheConnection>();
boolean hasAccess = ConfigWebUtil.hasAccess(config, SecurityManagerImpl.TYPE_CACHE);
// print.o("LOAD CACHE:"+hasAccess+":"+hasCS);
Element eCache = getChildByName(doc.getDocumentElement(), "cache");
// has changes
String md5 = getMD5(eCache, hasCS ? configServer.getCacheMD5() : "");
if (md5.equals(config.getCacheMD5()))
return;
config.setCacheMD5(md5);
String[] typeNames = new String[] { "resource", "function", "include", "query", "template", "object", "file", "http", "webservice" };
int[] types = new int[] { ConfigImpl.CACHE_TYPE_RESOURCE, ConfigImpl.CACHE_TYPE_FUNCTION, ConfigImpl.CACHE_TYPE_INCLUDE, ConfigImpl.CACHE_TYPE_QUERY, ConfigImpl.CACHE_TYPE_TEMPLATE, ConfigImpl.CACHE_TYPE_OBJECT, ConfigImpl.CACHE_TYPE_FILE, ConfigImpl.CACHE_TYPE_HTTP, ConfigImpl.CACHE_TYPE_WEBSERVICE };
// default cache
for (int i = 0; i < types.length; i++) {
String def = getAttr(eCache, "default-" + typeNames[i]);
if (hasAccess && !StringUtil.isEmpty(def)) {
config.setCacheDefaultConnectionName(types[i], def);
} else if (hasCS) {
if (eCache.hasAttribute("default-" + typeNames[i]))
config.setCacheDefaultConnectionName(types[i], "");
else
config.setCacheDefaultConnectionName(types[i], configServer.getCacheDefaultConnectionName(types[i]));
} else
config.setCacheDefaultConnectionName(+types[i], "");
}
// cache connections
Element[] eConnections = getChildren(eCache, "connection");
// if(hasAccess) {
ClassDefinition cd;
String name;
CacheConnection cc;
// caches
if (hasAccess)
for (int i = 0; i < eConnections.length; i++) {
Element eConnection = eConnections[i];
name = getAttr(eConnection, "name");
cd = getClassDefinition(eConnection, "", config.getIdentification());
if (!cd.isBundle()) {
ClassDefinition _cd = config.getCacheDefinition(cd.getClassName());
if (_cd != null)
cd = _cd;
}
try {
Struct custom = toStruct(getAttr(eConnection, "custom"));
// Workaround for old EHCache class defintions
if (cd.getClassName() != null && cd.getClassName().endsWith(".EHCacheLite")) {
cd = new ClassDefinitionImpl("org.lucee.extension.cache.eh.EHCache");
if (!custom.containsKey("distributed"))
custom.setEL("distributed", "off");
if (!custom.containsKey("asynchronousReplicationIntervalMillis"))
custom.setEL("asynchronousReplicationIntervalMillis", "1000");
if (!custom.containsKey("maximumChunkSizeBytes"))
custom.setEL("maximumChunkSizeBytes", "5000000");
} else //
if (cd.getClassName() != null && (cd.getClassName().endsWith(".extension.io.cache.eh.EHCache") || cd.getClassName().endsWith("lucee.runtime.cache.eh.EHCache")))
cd = new ClassDefinitionImpl("org.lucee.extension.cache.eh.EHCache");
// else cacheClazz = cd.getClazz();
cc = new CacheConnectionImpl(config, name, cd, custom, Caster.toBooleanValue(getAttr(eConnection, "read-only"), false), Caster.toBooleanValue(getAttr(eConnection, "storage"), false));
if (!StringUtil.isEmpty(name)) {
caches.put(name.toLowerCase(), cc);
} else
SystemOut.print(config.getErrWriter(), "missing cache name");
} catch (ClassException ce) {
log.error("Cache", ce);
// SystemOut.print(config.getErrWriter(), ExceptionUtil.getStacktrace(ce, true));
} catch (BundleException be) {
log.error("Cache", be);
// SystemOut.print(config.getErrWriter(), ExceptionUtil.getStacktrace(be, true));
} catch (IOException e) {
log.error("Cache", e);
// SystemOut.print(config.getErrWriter(), ExceptionUtil.getStacktrace(e, true));
}
}
// }
// call static init once per driver
{
// group by classes
final Map<ClassDefinition, List<CacheConnection>> _caches = new HashMap<ClassDefinition, List<CacheConnection>>();
{
Iterator<Entry<String, CacheConnection>> it = caches.entrySet().iterator();
Entry<String, CacheConnection> entry;
List<CacheConnection> list;
while (it.hasNext()) {
entry = it.next();
cc = entry.getValue();
// Jira 3196 ?!
if (cc == null)
continue;
list = _caches.get(cc.getClassDefinition());
if (list == null) {
list = new ArrayList<CacheConnection>();
_caches.put(cc.getClassDefinition(), list);
}
list.add(cc);
}
}
// call
Iterator<Entry<ClassDefinition, List<CacheConnection>>> it = _caches.entrySet().iterator();
Entry<ClassDefinition, List<CacheConnection>> entry;
List<CacheConnection> list;
ClassDefinition _cd;
while (it.hasNext()) {
entry = it.next();
list = entry.getValue();
_cd = entry.getKey();
try {
Method m = _cd.getClazz().getMethod("init", new Class[] { Config.class, String[].class, Struct[].class });
if (Modifier.isStatic(m.getModifiers()))
m.invoke(null, new Object[] { config, _toCacheNames(list), _toArguments(list) });
else
SystemOut.print(config.getErrWriter(), "method [init(Config,String[],Struct[]):void] for class [" + _cd.toString() + "] is not static");
} catch (InvocationTargetException e) {
log.error("Cache", e.getTargetException());
} catch (RuntimeException e) {
log.error("Cache", e);
} catch (NoSuchMethodException e) {
log.error("Cache", "missing method [public static init(Config,String[],Struct[]):void] for class [" + _cd.toString() + "] ");
// SystemOut.print(config.getErrWriter(), "missing method [public static init(Config,String[],Struct[]):void] for class [" + _cd.toString() + "] ");
} catch (Throwable e) {
ExceptionUtil.rethrowIfNecessary(e);
log.error("Cache", e);
}
}
}
// Copy Parent caches as readOnly
if (hasCS) {
Map<String, CacheConnection> ds = configServer.getCacheConnections();
Iterator<Entry<String, CacheConnection>> it = ds.entrySet().iterator();
Entry<String, CacheConnection> entry;
while (it.hasNext()) {
entry = it.next();
cc = entry.getValue();
if (!caches.containsKey(entry.getKey()))
caches.put(entry.getKey(), new ServerCacheConnection(configServer, cc));
}
}
config.setCaches(caches);
}
use of lucee.runtime.cache.CacheConnection in project Lucee by lucee.
the class PageContextImpl method getCacheConnection.
public CacheConnection getCacheConnection(String cacheName, CacheConnection defaultValue) {
cacheName = cacheName.toLowerCase().trim();
CacheConnection cc = null;
if (getApplicationContext() != null)
cc = ((ApplicationContextSupport) getApplicationContext()).getCacheConnection(cacheName, null);
if (cc == null)
cc = config.getCacheConnections().get(cacheName);
if (cc == null)
return defaultValue;
return cc;
}
Aggregations