From 83265a8ae1d0bbc854538c769061d5ec23c650c3 Mon Sep 17 00:00:00 2001 From: Thisal Tennakoon Date: Mon, 7 Aug 2023 12:08:13 +0530 Subject: [PATCH] Initial commit --- .../synapse/config/SynapseConfiguration.java | 102 ++++++++++-------- 1 file changed, 60 insertions(+), 42 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java b/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java index 47ed64af38..341242d5fc 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java +++ b/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java @@ -194,6 +194,8 @@ public class SynapseConfiguration implements ManagedLifecycle, SynapseArtifact { */ private Map messageProcessors = new ConcurrentHashMap(); + private Map locks = new ConcurrentHashMap(); + /** * Endpoint templates to create actual endpoints */ @@ -1147,6 +1149,20 @@ public Map getDefinedEndpoints() { return definedEndpoints; } + public Object acquireLock(String key) { + Object lock = locks.get(key); + if (lock == null) { + synchronized (this) { + lock = locks.get(key); + if (lock == null) { + lock = new Object(); + locks.put(key, lock); + } + } + } + return lock; + } + /** * Get the definition of the endpoint with the given key * @@ -1155,58 +1171,60 @@ public Map getDefinedEndpoints() { */ public Endpoint getEndpoint(String key) { - Object o = getEntry(key); - if (o != null && o instanceof Endpoint) { - return (Endpoint) o; - } + synchronized (acquireLock(key)) { + Object o = getEntry(key); + if (o != null && o instanceof Endpoint) { + return (Endpoint) o; + } - Entry entry = null; - if (o == null) { - entry = new Entry(key); - entry.setType(Entry.REMOTE_ENTRY); - } else { - Object object = localRegistry.get(key); - if (object instanceof Entry) { - entry = (Entry) object; + Entry entry = null; + if (o == null) { + entry = new Entry(key); + entry.setType(Entry.REMOTE_ENTRY); + } else { + Object object = localRegistry.get(key); + if (object instanceof Entry) { + entry = (Entry) object; + } } - } - assertEntryNull(entry, key); + assertEntryNull(entry, key); - //noinspection ConstantConditions - if (entry.getMapper() == null) { - entry.setMapper(XMLToEndpointMapper.getInstance()); - } + //noinspection ConstantConditions + if (entry.getMapper() == null) { + entry.setMapper(XMLToEndpointMapper.getInstance()); + } - if (entry.getType() == Entry.REMOTE_ENTRY) { - if (registry != null) { - o = registry.getResource(entry, getProperties()); - if (o != null && o instanceof Endpoint) { - localRegistry.put(key, entry); - return (Endpoint) o; - } else if (o instanceof OMNode) { - properties.put(SynapseConstants.SYNAPSE_CONFIGURATION, this); - Endpoint e = (Endpoint) XMLToEndpointMapper.getInstance(). - getObjectFromOMNode((OMNode) o, properties); - if (e != null) { - entry.setValue(e); - return e; + if (entry.getType() == Entry.REMOTE_ENTRY) { + if (registry != null) { + o = registry.getResource(entry, getProperties()); + if (o != null && o instanceof Endpoint) { + localRegistry.put(key, entry); + return (Endpoint) o; + } else if (o instanceof OMNode) { + properties.put(SynapseConstants.SYNAPSE_CONFIGURATION, this); + Endpoint e = (Endpoint) XMLToEndpointMapper.getInstance(). + getObjectFromOMNode((OMNode) o, properties); + if (e != null) { + entry.setValue(e); + return e; + } } } - } - } else { - Object value = entry.getValue(); - if (value instanceof OMNode) { - properties.put(SynapseConstants.SYNAPSE_CONFIGURATION, this); - Object object = entry.getMapper().getObjectFromOMNode( - (OMNode) value, getProperties()); - if (object instanceof Endpoint) { - entry.setValue(object); - return (Endpoint) object; + } else { + Object value = entry.getValue(); + if (value instanceof OMNode) { + properties.put(SynapseConstants.SYNAPSE_CONFIGURATION, this); + Object object = entry.getMapper().getObjectFromOMNode( + (OMNode) value, getProperties()); + if (object instanceof Endpoint) { + entry.setValue(object); + return (Endpoint) object; + } } } + return null; } - return null; } /**