This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new d0372dd  Config API modernization.
d0372dd is described below

commit d0372dd09d7bf469c9e896ea5b9a9464a9866497
Author: JamesBognar <[email protected]>
AuthorDate: Sun Dec 26 15:05:00 2021 -0500

    Config API modernization.
---
 .../apache/juneau/config/internal/ConfigMap.java   | 64 ++++------------------
 .../src/main/java/org/apache/juneau/ClassMeta.java | 11 +---
 .../org/apache/juneau/internal/SimpleLock.java     | 38 +++++++++++++
 .../juneau/internal/SimpleReadWriteLock.java       | 57 +++++++++++++++++++
 4 files changed, 108 insertions(+), 62 deletions(-)

diff --git 
a/juneau-core/juneau-config/src/main/java/org/apache/juneau/config/internal/ConfigMap.java
 
b/juneau-core/juneau-config/src/main/java/org/apache/juneau/config/internal/ConfigMap.java
index ce41755..b00eb69 100644
--- 
a/juneau-core/juneau-config/src/main/java/org/apache/juneau/config/internal/ConfigMap.java
+++ 
b/juneau-core/juneau-config/src/main/java/org/apache/juneau/config/internal/ConfigMap.java
@@ -19,7 +19,6 @@ import static 
org.apache.juneau.config.event.ConfigEventType.*;
 import java.io.*;
 import java.util.*;
 import java.util.concurrent.*;
-import java.util.concurrent.locks.*;
 
 import org.apache.juneau.*;
 import org.apache.juneau.collections.*;
@@ -57,7 +56,7 @@ public class ConfigMap implements ConfigStoreListener {
        // Import statements in this config.
        final List<Import> imports = new CopyOnWriteArrayList<>();
 
-       private final ReadWriteLock lock = new ReentrantReadWriteLock();
+       private final SimpleReadWriteLock lock = new SimpleReadWriteLock();
 
        /**
         * Constructor.
@@ -225,8 +224,7 @@ public class ConfigMap implements ConfigStoreListener {
        public ConfigMapEntry getEntry(String section, String key) {
                checkSectionName(section);
                checkKeyName(key);
-               readLock();
-               try {
+               try (SimpleLock x = lock.read()) {
                        ConfigSection cs = entries.get(section);
                        ConfigMapEntry ce = cs == null ? null : 
cs.entries.get(key);
 
@@ -239,8 +237,6 @@ public class ConfigMap implements ConfigStoreListener {
                        }
 
                        return ce;
-               } finally {
-                       readUnlock();
                }
        }
 
@@ -259,12 +255,9 @@ public class ConfigMap implements ConfigStoreListener {
         */
        public List<String> getPreLines(String section) {
                checkSectionName(section);
-               readLock();
-               try {
+               try (SimpleLock x = lock.read()) {
                        ConfigSection cs = entries.get(section);
                        return cs == null ? null : cs.preLines;
-               } finally {
-                       readUnlock();
                }
        }
 
@@ -457,8 +450,7 @@ public class ConfigMap implements ConfigStoreListener {
        private ConfigMap applyChange(boolean addToChangeList, ConfigEvent ce) {
                if (ce == null)
                        return this;
-               writeLock();
-               try {
+               try (SimpleLock x = lock.write()) {
                        String section = ce.getSection();
                        ConfigSection cs = entries.get(section);
                        if (ce.getType() == SET_ENTRY) {
@@ -492,8 +484,6 @@ public class ConfigMap implements ConfigStoreListener {
                        }
                        if (addToChangeList)
                                changes.add(ce);
-               } finally {
-                       writeUnlock();
                }
                return this;
        }
@@ -549,8 +539,7 @@ public class ConfigMap implements ConfigStoreListener {
         * @throws IOException Thrown by underlying stream.
         */
        public ConfigMap commit() throws IOException {
-               writeLock();
-               try {
+               try (SimpleLock x = lock.write()) {
                        String newContents = asString();
                        for (int i = 0; i <= 10; i++) {
                                if (i == 10)
@@ -561,8 +550,6 @@ public class ConfigMap implements ConfigStoreListener {
                                onChange(currentContents);
                        }
                        this.changes.clear();
-               } finally {
-                       writeUnlock();
                }
                return this;
        }
@@ -616,8 +603,7 @@ public class ConfigMap implements ConfigStoreListener {
        @Override /* ConfigStoreListener */
        public void onChange(String newContents) {
                ConfigEvents changes = null;
-               writeLock();
-               try {
+               try (SimpleLock x = lock.write()) {
                        if (ne(contents, newContents)) {
                                changes = findDiffs(newContents);
                                load(newContents);
@@ -628,8 +614,6 @@ public class ConfigMap implements ConfigStoreListener {
                        }
                } catch (IOException e) {
                        throw runtimeException(e);
-               } finally {
-                       writeUnlock();
                }
                if (changes != null && ! changes.isEmpty())
                        signal(changes);
@@ -637,11 +621,8 @@ public class ConfigMap implements ConfigStoreListener {
 
        @Override /* Object */
        public String toString() {
-               readLock();
-               try {
+               try (SimpleLock x = lock.read()) {
                        return asString();
-               } finally {
-                       readUnlock();
                }
        }
 
@@ -658,8 +639,7 @@ public class ConfigMap implements ConfigStoreListener {
         */
        public OMap asMap() {
                OMap m = new OMap();
-               readLock();
-               try {
+               try (SimpleLock x = lock.read()) {
                        for (Import i : imports)
                                m.putAll(i.getConfigMap().asMap());
                        for (ConfigSection cs : entries.values()) {
@@ -668,8 +648,6 @@ public class ConfigMap implements ConfigStoreListener {
                                        m2.put(ce.key, ce.value);
                                m.put(cs.name, m2);
                        }
-               } finally {
-                       readUnlock();
                }
                return m;
        }
@@ -682,12 +660,9 @@ public class ConfigMap implements ConfigStoreListener {
         * @throws IOException Thrown by underlying stream.
         */
        public Writer writeTo(Writer w) throws IOException {
-               readLock();
-               try {
+               try (SimpleLock x = lock.read()) {
                        for (ConfigSection cs : entries.values())
                                cs.writeTo(w);
-               } finally {
-                       readUnlock();
                }
                return w;
        }
@@ -699,14 +674,11 @@ public class ConfigMap implements ConfigStoreListener {
         */
        public ConfigMap rollback() {
                if (changes.size() > 0) {
-                       writeLock();
-                       try {
+                       try (SimpleLock x = lock.write()) {
                                changes.clear();
                                load(contents);
                        } catch (IOException e) {
                                throw runtimeException(e);
-                       } finally {
-                               writeUnlock();
                        }
                }
                return this;
@@ -717,22 +689,6 @@ public class ConfigMap implements ConfigStoreListener {
        // Private methods
        
//-----------------------------------------------------------------------------------------------------------------
 
-       private void readLock() {
-               lock.readLock().lock();
-       }
-
-       private void readUnlock() {
-               lock.readLock().unlock();
-       }
-
-       private void writeLock() {
-               lock.writeLock().lock();
-       }
-
-       private void writeUnlock() {
-               lock.writeLock().unlock();
-       }
-
        private void checkSectionName(String s) {
                if (! ("".equals(s) || isValidNewSectionName(s)))
                        throw illegalArgumentException("Invalid section name: 
''{0}''", s);
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
index bcd5a32..f5368e1 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
@@ -28,7 +28,6 @@ import java.time.temporal.*;
 import java.util.*;
 import java.util.Date;
 import java.util.concurrent.*;
-import java.util.concurrent.locks.*;
 import java.util.function.*;
 
 import org.apache.juneau.annotation.*;
@@ -122,8 +121,7 @@ public final class ClassMeta<T> implements Type {
        private final Map<Class<?>,Optional<?>> annotationLastMap = new 
ConcurrentHashMap<>();
        private final Map<String,Optional<?>> properties = new 
ConcurrentHashMap<>();
 
-       private final ReadWriteLock lock = new ReentrantReadWriteLock(false);
-       private final Lock rLock = lock.readLock(), wLock = lock.writeLock();
+       private final SimpleReadWriteLock lock = new SimpleReadWriteLock(false);
 
        /**
         * Construct a new {@code ClassMeta} based on the specified {@link 
Class}.
@@ -151,8 +149,7 @@ public final class ClassMeta<T> implements Type {
                this.beanContext = beanContext;
                String notABeanReason = null;
 
-               wLock.lock();
-               try {
+               try (SimpleLock x = lock.write()) {
                        // We always immediately add this class meta to the 
bean context cache so that we can resolve recursive references.
                        if (beanContext != null && beanContext.cmCache != null 
&& isCacheable(innerClass))
                                beanContext.cmCache.put(innerClass, this);
@@ -196,7 +193,6 @@ public final class ClassMeta<T> implements Type {
                        throw e;
                } finally {
                        this.notABeanReason = notABeanReason;
-                       wLock.unlock();
                }
        }
 
@@ -217,8 +213,7 @@ public final class ClassMeta<T> implements Type {
         * Causes thread to wait until constructor has exited.
         */
        final void waitForInit() {
-               rLock.lock();
-               rLock.unlock();
+               try (SimpleLock x = lock.read()) {}
        }
 
        /**
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/SimpleLock.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/SimpleLock.java
new file mode 100644
index 0000000..610b885
--- /dev/null
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/SimpleLock.java
@@ -0,0 +1,38 @@
+// 
***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright 
ownership.  The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not 
use this file except in compliance            *
+// * with the License.  You may obtain a copy of the License at                
                                              *
+// *                                                                           
                                              *
+// *  http://www.apache.org/licenses/LICENSE-2.0                               
                                              *
+// *                                                                           
                                              *
+// * Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
express or implied.  See the License for the        *
+// * specific language governing permissions and limitations under the 
License.                                              *
+// 
***************************************************************************************************************************
+package org.apache.juneau.internal;
+
+import java.util.concurrent.locks.Lock;
+
+/**
+ * A simple auto-closeable wrapper around a lock.
+ */
+public class SimpleLock implements AutoCloseable {
+
+       private final Lock lock;
+
+       /**
+        * Constructor.
+        *
+        * @param lock The lock being wrapped.
+        */
+       public SimpleLock(Lock lock) {
+               this.lock = lock;
+               this.lock.lock();
+       }
+
+       @Override
+       public void close() {
+               this.lock.unlock();
+       }
+}
diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/SimpleReadWriteLock.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/SimpleReadWriteLock.java
new file mode 100644
index 0000000..8bc61d6
--- /dev/null
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/SimpleReadWriteLock.java
@@ -0,0 +1,57 @@
+// 
***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright 
ownership.  The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not 
use this file except in compliance            *
+// * with the License.  You may obtain a copy of the License at                
                                              *
+// *                                                                           
                                              *
+// *  http://www.apache.org/licenses/LICENSE-2.0                               
                                              *
+// *                                                                           
                                              *
+// * Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
express or implied.  See the License for the        *
+// * specific language governing permissions and limitations under the 
License.                                              *
+// 
***************************************************************************************************************************
+package org.apache.juneau.internal;
+
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ * An extension of {@link ReentrantReadWriteLock} with convenience methods for 
creating
+ * auto-closeable locks.
+ */
+public class SimpleReadWriteLock extends ReentrantReadWriteLock {
+       private static final long serialVersionUID = 1L;
+
+       /**
+        * Constructor.
+        */
+       public SimpleReadWriteLock() {
+               super();
+       }
+
+       /**
+        * Constructor
+        *
+        * @param fair <jk>true</jk> if this lock should use a fair ordering 
policy.
+        */
+       public SimpleReadWriteLock(boolean fair) {
+               super(fair);
+       }
+
+       /**
+        * Construct a write lock.
+        *
+        * @return A new closeable write lock.
+        */
+       public SimpleLock write(){
+               return new SimpleLock(writeLock());
+       }
+
+       /**
+        * Construct a read lock.
+        *
+        * @return A new closeable read lock.
+        */
+       public SimpleLock read(){
+               return new SimpleLock(readLock());
+       }
+}

Reply via email to