Skip to content

Commit

Permalink
use centralized token creation
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Nov 7, 2023
1 parent 8c57d13 commit 34a8ac3
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 46 deletions.
13 changes: 2 additions & 11 deletions core/src/main/java/lucee/runtime/component/ComponentLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
*/
package lucee.runtime.component;

import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.jsp.tagext.BodyContent;

import lucee.commons.io.SystemUtil;
import lucee.commons.io.res.Resource;
import lucee.commons.io.res.filter.DirectoryResourceFilter;
import lucee.commons.io.res.filter.ExtensionResourceFilter;
Expand Down Expand Up @@ -65,7 +64,6 @@ public class ComponentLoader {
private static final short RETURN_TYPE_PAGE = 1;
private static final short RETURN_TYPE_INTERFACE = 2;
private static final short RETURN_TYPE_COMPONENT = 3;
private static final ConcurrentHashMap<String, String> tokens = new ConcurrentHashMap<String, String>();
private static final ResourceFilter DIR_OR_EXT = new OrResourceFilter(
new ResourceFilter[] { DirectoryResourceFilter.FILTER, new ExtensionResourceFilter(Constants.getComponentExtensions()) });
private static final ImportDefintion[] EMPTY_ID = new ImportDefintion[0];
Expand Down Expand Up @@ -103,7 +101,7 @@ public static StaticScope getStaticScope(PageContext pc, PageSource loadingLocat

// if there is no static scope stored yet, we need to load it
if (ss == null) {
synchronized (cp.getPageSource().getDisplayPath() + ":" + getToken(cp.getHash() + "")) {
synchronized (SystemUtil.createToken(cp.getPageSource().getDisplayPath(), cp.getHash() + "")) {
ss = cp.getStaticScope();
if (ss == null) {
ss = searchComponent(pc, loadingLocation, rawPath, searchLocal, searchRoot, false, false).staticScope();
Expand Down Expand Up @@ -696,11 +694,4 @@ private static CIPage toCIPage(Page p) {
return null;
}

public static String getToken(String key) {
String lock = tokens.putIfAbsent(key, key);
if (lock == null) {
lock = key;
}
return lock;
}
}
12 changes: 2 additions & 10 deletions core/src/main/java/lucee/runtime/db/DatasourceManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import lucee.commons.db.DBUtil;
import lucee.commons.digest.HashUtil;
import lucee.commons.io.IOUtil;
import lucee.commons.io.SystemUtil;
import lucee.commons.lang.Pair;
import lucee.runtime.PageContext;
import lucee.runtime.PageContextImpl;
Expand All @@ -55,7 +56,6 @@ public final class DatasourceManagerImpl implements DataSourceManager {
private int isolation = Connection.TRANSACTION_NONE;
private Map<DataSource, DatasourceConnectionPro> transConnsReg = new HashMap<DataSource, DatasourceConnectionPro>();
private Map<DataSource, ORMDatasourceConnection> transConnsORM = new HashMap<DataSource, ORMDatasourceConnection>();
private static final ConcurrentHashMap<String, String> tokens = new ConcurrentHashMap<String, String>();
private boolean inside;

private Map<String, Savepoint> savepoints = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -86,7 +86,7 @@ public DatasourceConnection getConnection(PageContext pc, DataSource ds, String

// first time that datasource is used within this transaction
if (existingDC == null) {
synchronized (getToken(ds.id())) {
synchronized (SystemUtil.createToken("DatasourceManagerImpl", ds.id())) {
existingDC = transConnsReg.get(ds);
if (existingDC == null) {
DatasourceConnectionPro newDC = (DatasourceConnectionPro) config.getDatasourceConnectionPool().getDatasourceConnection(config, ds, user, pass);
Expand Down Expand Up @@ -495,12 +495,4 @@ private void throwException(Pair<DatasourceConnection, Exception> pair) throws D
private int _size() {
return transConnsORM.size() + transConnsReg.size();
}

public static String getToken(String key) {
String lock = tokens.putIfAbsent(key, key);
if (lock == null) {
lock = key;
}
return lock;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import lucee.commons.io.SystemUtil;
import lucee.runtime.type.Collection;
import lucee.runtime.type.Collection.Key;
import lucee.runtime.type.KeyImpl;
Expand All @@ -34,7 +35,6 @@
*/
public final class SoftMethodStorage {

private final ConcurrentHashMap<String, Object> tokens = new ConcurrentHashMap<String, Object>();
private Map<Class, SoftReference<Map<Key, Map<Integer, Method[]>>>> map = new ConcurrentHashMap<Class, SoftReference<Map<Key, Map<Integer, Method[]>>>>();

/**
Expand Down Expand Up @@ -94,7 +94,7 @@ public int compare(Method l, Method r) {
* @return returns stored struct
*/
private Map<Key, Map<Integer, Method[]>> store(Class clazz) {
synchronized (getToken(clazz)) {
synchronized (SystemUtil.createToken("SoftMethodStorage", clazz.getName())) {
Method[] methods = clazz.getMethods();
Map<Key, Map<Integer, Method[]>> methodsMap = new ConcurrentHashMap<Key, Map<Integer, Method[]>>();
for (int i = 0; i < methods.length; i++) {
Expand All @@ -105,15 +105,6 @@ private Map<Key, Map<Integer, Method[]>> store(Class clazz) {
}
}

private Object getToken(Class clazz) {
Object newLock = new Object();
Object lock = tokens.putIfAbsent(clazz.getName(), newLock);
if (lock == null) {
lock = newLock;
}
return lock;
}

/**
* stores a single method
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import lucee.runtime.type.scope.ScopeContext;

public class IKHandlerCache implements IKHandler {
private static final ConcurrentHashMap<String, Object> tokens = new ConcurrentHashMap<String, Object>();

protected boolean storeEmpty = Caster.toBooleanValue(SystemUtil.getSystemPropOrEnvVar("lucee.store.empty", null), true);

Expand All @@ -33,7 +32,7 @@ public class IKHandlerCache implements IKHandler {
public IKStorageValue loadData(PageContext pc, String appName, String name, String strType, int type, Log log) throws PageException {
Cache cache = getCache(pc, name);
String key = getKey(pc.getCFID(), appName, strType);
synchronized (getToken(key)) { // sync necessary?
synchronized (SystemUtil.createToken("IKHandlerCache", key)) { // sync necessary?
Object val = cache.getValue(key, null);
if (val instanceof byte[][]) {
ScopeContext.info(log,
Expand All @@ -58,7 +57,7 @@ public void store(IKStorageScopeSupport storageScope, PageContext pc, String app
Cache cache = getCache(ThreadLocalPageContext.get(pc), name);
String key = getKey(pc.getCFID(), appName, storageScope.getTypeAsString());

synchronized (getToken(key)) {
synchronized (SystemUtil.createToken("IKHandlerCache", key)) {
Object existingVal = cache.getValue(key, null);

if (storeEmpty || storageScope.hasContent()) {
Expand Down Expand Up @@ -101,7 +100,7 @@ public void unstore(IKStorageScopeSupport storageScope, PageContext pc, String a
Cache cache = getCache(pc, name);
String key = getKey(pc.getCFID(), appName, storageScope.getTypeAsString());

synchronized (getToken(key)) {
synchronized (SystemUtil.createToken("IKHandlerCache", key)) {
cache.remove(key);
}
}
Expand Down Expand Up @@ -129,12 +128,4 @@ public static String getKey(String cfid, String appName, String type) {
return new StringBuilder("lucee-storage:").append(type).append(":").append(cfid).append(":").append(appName).toString().toUpperCase();
}

public static Object getToken(String key) {
Object newLock = new Object();
Object lock = tokens.putIfAbsent(key, newLock);
if (lock == null) {
lock = newLock;
}
return lock;
}
}
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.0.1.18-SNAPSHOT"/>
<property name="version" value="6.0.1.19-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.0.1.18-SNAPSHOT</version>
<version>6.0.1.19-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit 34a8ac3

Please sign in to comment.