You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by nt...@apache.org on 2021/02/24 10:26:32 UTC
[cayenne] 02/03: CAY-2698 EventSubject.getSubject() is not thread
safe
This is an automated email from the ASF dual-hosted git repository.
ntimofeev pushed a commit to branch STABLE-4.1
in repository https://gitbox.apache.org/repos/asf/cayenne.git
commit 8a5498880badf6f24b8e367291e8e424a76346d1
Author: Nikita Timofeev <st...@gmail.com>
AuthorDate: Wed Feb 24 13:25:03 2021 +0300
CAY-2698 EventSubject.getSubject() is not thread safe
---
.../org/apache/cayenne/event/EventSubject.java | 34 ++++++++++------------
1 file changed, 15 insertions(+), 19 deletions(-)
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/event/EventSubject.java b/cayenne-server/src/main/java/org/apache/cayenne/event/EventSubject.java
index 99c8b43..7cc0fdc 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/event/EventSubject.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/event/EventSubject.java
@@ -40,8 +40,7 @@ import org.apache.cayenne.util.WeakValueMap;
public class EventSubject implements Serializable {
// a Map that will allow the values to be GC'ed
- @SuppressWarnings("unchecked")
- private static Map<String, EventSubject> _registeredSubjects = new WeakValueMap<>();
+ private static final Map<String, EventSubject> _registeredSubjects = new WeakValueMap<>();
// Subject identifier in the form "com.foo.bar/SubjectName"
private String _fullyQualifiedSubjectName;
@@ -64,10 +63,14 @@ public class EventSubject implements Serializable {
}
String fullSubjectName = subjectOwner.getName() + "/" + subjectName;
- EventSubject newSubject = _registeredSubjects.get(fullSubjectName);
- if (newSubject == null) {
- newSubject = new EventSubject(fullSubjectName);
- _registeredSubjects.put(newSubject.getSubjectName(), newSubject);
+
+ EventSubject newSubject;
+ synchronized (_registeredSubjects) {
+ newSubject = _registeredSubjects.get(fullSubjectName);
+ if(newSubject == null) {
+ newSubject = new EventSubject(fullSubjectName);
+ _registeredSubjects.put(fullSubjectName, newSubject);
+ }
}
return newSubject;
@@ -92,8 +95,7 @@ public class EventSubject implements Serializable {
@Override
public boolean equals(Object obj) {
if (obj instanceof EventSubject) {
- return _fullyQualifiedSubjectName.equals(((EventSubject) obj)
- .getSubjectName());
+ return _fullyQualifiedSubjectName.equals(((EventSubject) obj).getSubjectName());
}
return false;
@@ -101,7 +103,9 @@ public class EventSubject implements Serializable {
@Override
public int hashCode() {
- return new HashCodeBuilder(17, 3).append(_fullyQualifiedSubjectName).toHashCode();
+ return new HashCodeBuilder(17, 3)
+ .append(_fullyQualifiedSubjectName)
+ .toHashCode();
}
public String getSubjectName() {
@@ -114,15 +118,7 @@ public class EventSubject implements Serializable {
*/
@Override
public String toString() {
- StringBuilder buf = new StringBuilder(64);
-
- buf.append("<");
- buf.append(this.getClass().getName());
- buf.append(" 0x");
- buf.append(Integer.toHexString(System.identityHashCode(this)));
- buf.append("> ");
- buf.append(_fullyQualifiedSubjectName);
-
- return buf.toString();
+ return "<" + this.getClass().getName() + " 0x" + Integer.toHexString(System.identityHashCode(this)) + "> "
+ + _fullyQualifiedSubjectName;
}
}