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

tv pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/turbine-core.git

commit 473d2b16f7208811616a274b433aea125e3a99c9
Author: Thomas Vandahl <[email protected]>
AuthorDate: Wed Jan 15 15:32:26 2025 +0100

    Try to fix pool object lifecycle
---
 .../apache/turbine/services/intake/IntakeTool.java | 66 ++++++++++++++--------
 .../turbine/services/intake/IntakeToolTest.java    | 12 ++++
 2 files changed, 56 insertions(+), 22 deletions(-)

diff --git a/src/java/org/apache/turbine/services/intake/IntakeTool.java 
b/src/java/org/apache/turbine/services/intake/IntakeTool.java
index 4a19f831..4ff098fc 100644
--- a/src/java/org/apache/turbine/services/intake/IntakeTool.java
+++ b/src/java/org/apache/turbine/services/intake/IntakeTool.java
@@ -1,6 +1,5 @@
 package org.apache.turbine.services.intake;
 
-
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -20,7 +19,7 @@ package org.apache.turbine.services.intake;
  * under the License.
  */
 
-
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -47,7 +46,7 @@ import org.apache.turbine.util.RunData;
  * @author <a href="mailto:[email protected]";>Henning P. Schmiedehausen</a>
  * @author <a href="mailto:[email protected]";>Quinton McCombs</a>
  * @author <a href="mailto:[email protected]";>Eric Pugh</a>
- * @version $Id$
+ * @version $Id: IntakeTool.java 1886259 2021-02-06 16:54:03Z tv $
  */
 public class IntakeTool
         implements ApplicationTool, Recyclable
@@ -129,10 +128,12 @@ public class IntakeTool
 
         for (int i = groupNames.length - 1; i >= 0; i--)
         {
+            Group foundGroup = null;
+
             try
             {
-                List<Group> foundGroups = intakeService.getGroup(groupNames[i])
-                    .getObjects(pp);
+                foundGroup = intakeService.getGroup(groupNames[i]);
+                List<Group> foundGroups = foundGroup.getObjects(pp);
 
                 if (foundGroups != null)
                 {
@@ -144,6 +145,20 @@ public class IntakeTool
             {
                 log.error(e);
             }
+            finally
+            {
+                if (foundGroup != null)
+                {
+                    try
+                    {
+                        intakeService.releaseGroup(foundGroup);
+                    }
+                    catch (IntakeException intakeException)
+                    {
+                        log.error(intakeException, intakeException);
+                    }
+                }
+            }
         }
     }
 
@@ -227,10 +242,7 @@ public class IntakeTool
     public void newForm()
     {
         declaredGroups.clear();
-        for (Group group : groups.values())
-        {
-            group.resetDeclared();
-        }
+        groups.values().forEach(Group::resetDeclared);
     }
 
     /**
@@ -300,6 +312,24 @@ public class IntakeTool
             Group g = null;
 
             String inputKey = intakeService.getGroupKey(groupName) + key;
+//            g = groups.computeIfAbsent(inputKey, k -> {
+//                if (create)
+//                {
+//                    try
+//                    {
+//                        Group gg = intakeService.getGroup(groupName);
+//                        gg.init(key, pp);
+//                        return gg;
+//                    }
+//                    catch (IntakeException e)
+//                    {
+//                        // TODO Auto-generated catch block
+//                        e.printStackTrace();
+//                    }
+//                }
+//
+//                return null;
+//            });
             if (groups.containsKey(inputKey))
             {
                 g = groups.get(inputKey);
@@ -381,6 +411,7 @@ public class IntakeTool
     public boolean isAllValid()
     {
         boolean allValid = true;
+        // TODO: fail fast?
         for (Group group : groups.values())
         {
             allValid &= group.isAllValid();
@@ -445,13 +476,9 @@ public class IntakeTool
 
                        if (groupKeys != null)
                        {
-                       for (String groupKey : groupKeys)
-                {
-                           if (!groupKey.equals(group.getGID()))
-                           {
-                                pp.add(INTAKE_GRP, groupKey);
-                           }
-                }
+                           Arrays.stream(groupKeys)
+                               .filter(groupKey -> 
!groupKey.equals(group.getGID()))
+                               .forEach(groupKey -> pp.add(INTAKE_GRP, 
groupKey));
                    }
 
             try
@@ -472,12 +499,7 @@ public class IntakeTool
      */
     public void removeAll()
     {
-        Object[] allGroups = groups.values().toArray();
-        for (int i = allGroups.length - 1; i >= 0; i--)
-        {
-            Group group = (Group) allGroups[i];
-            remove(group);
-        }
+        groups.values().forEach(this::remove);
     }
 
     /**
diff --git a/src/test/org/apache/turbine/services/intake/IntakeToolTest.java 
b/src/test/org/apache/turbine/services/intake/IntakeToolTest.java
index cc445f26..6dbfab4b 100644
--- a/src/test/org/apache/turbine/services/intake/IntakeToolTest.java
+++ b/src/test/org/apache/turbine/services/intake/IntakeToolTest.java
@@ -74,6 +74,18 @@ public class IntakeToolTest extends BaseTestCase
         assertEquals("LoginGroup", group.getIntakeGroupName());
     }
 
+    @Test
+    public void testRemove() throws Exception
+    {
+        File file = new File("./target/appData.ser");
+        assertTrue("Make sure serialized data file exists:" + file, 
file.exists());
+        Group group = intakeTool.get("LoginGroup", "loginGroupKey");
+        assertNotNull(group);
+        assertEquals(1, intakeTool.groups.size());
+        intakeTool.remove(group);
+        assertTrue(intakeTool.groups.isEmpty());
+    }
+
     /**
      * Make sure refresh DOESN'T do anything
      *

Reply via email to