pepness commented on code in PR #6596:
URL: https://github.com/apache/netbeans/pull/6596#discussion_r1367824928


##########
enterprise/glassfish.common/src/org/netbeans/modules/glassfish/common/LogViewMgr.java:
##########
@@ -135,14 +147,11 @@ private LogViewMgr(final String uri) {
      */
     public static LogViewMgr getInstance(String uri) {
         LogViewMgr logViewMgr;
-        synchronized (instances) {
-            WeakReference<LogViewMgr> viewRef = instances.get(uri);
-            logViewMgr = viewRef != null ? viewRef.get() : null;
-            if(logViewMgr == null) {
-                logViewMgr = new LogViewMgr(uri);
-                instances.put(uri, new WeakReference<LogViewMgr>(logViewMgr));
-            }
-        }
+        
+        logViewMgr = instances.computeIfAbsent(uri, key -> 
+                        new WeakReference<LogViewMgr>(new LogViewMgr(uri))
+                    ).get();

Review Comment:
   The supplier will be executed if the key is absent in the map.
   
   I added some logging to this method and test both versions and the behavior 
is the same, this method triggers when starting, restarting, and stopping the 
server or when selecting the option `View Domain Server Log`:
   
   ![Screenshot from 2023-10-21 
19-25-36](https://github.com/apache/netbeans/assets/9832133/ed7b6db7-b20f-43d7-8a1e-ebef966d46cd)
   
   The first time it runs it will:
   1. search the key(uri) in the map, because is the first time it runs with 
this GlassFish instance it will return `null`
   2. compute the `value` to be inserted with the provided key (uri)
   3. insert the `[key, value]` entry into the map
   4. return the `value` which is a `WeakReference`
   5. execute the `.get()` method from the returned `value`
   6. assign the `LogViewMgr` obtained in step 5
   7. return the `LogViewMgr` object
   
   ```
   INFO [org.netbeans.modules.glassfish.common.LogViewMgr]: uri: 
[/path/to/app/server/glassfish625/glassfish:/path/to/app/server/glassfish625/glassfish/domains/domain1]deployer:gfv610ee9:localhost:4848
   INFO [org.netbeans.modules.glassfish.common.LogViewMgr]: instances.get(uri): 
null
   INFO [org.netbeans.modules.glassfish.common.LogViewMgr]: 
instances.get(uri).get(): null
   INFO [org.netbeans.modules.glassfish.common.LogViewMgr]: instances.get(uri): 
java.lang.ref.WeakReference@766adea5
   INFO [org.netbeans.modules.glassfish.common.LogViewMgr]: logViewMgr: 
org.netbeans.modules.glassfish.common.LogViewMgr@766adea3
   ```
   The subsequent calls to the same GlassFish instance will log the same:
   1. search the key(uri) in the map, it exists in the map
   2. return the `value` associated to the key, a `WeakReference` object
   3. execute the `.get()` method from the returned `value`
   4. assign the `LogViewMgr` obtained in step 3
   5. return the `LogViewMgr` object
   
   ```
   INFO [org.netbeans.modules.glassfish.common.LogViewMgr]: uri: 
[/path/to/app/server/glassfish625/glassfish:/path/to/app/server/glassfish625/glassfish/domains/domain1]deployer:gfv610ee9:localhost:4848
   INFO [org.netbeans.modules.glassfish.common.LogViewMgr]: instances.get(uri): 
java.lang.ref.WeakReference@766adea5
   INFO [org.netbeans.modules.glassfish.common.LogViewMgr]: 
instances.get(uri).get(): 
org.netbeans.modules.glassfish.common.LogViewMgr@766adea3
   INFO [org.netbeans.modules.glassfish.common.LogViewMgr]: instances.get(uri): 
java.lang.ref.WeakReference@766adea5
   INFO [org.netbeans.modules.glassfish.common.LogViewMgr]: logViewMgr: 
org.netbeans.modules.glassfish.common.LogViewMgr@766adea3
   ```
   
   I tested with two different GlassFish instances (version 6 and 7).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to