chibenwa commented on a change in pull request #911:
URL: https://github.com/apache/james-project/pull/911#discussion_r823364273



##########
File path: 
server/container/lifecycle-api/src/main/java/org/apache/james/lifecycle/api/Disposable.java
##########
@@ -29,4 +38,134 @@
      * Dispose the object
      */
     void dispose();
+
+    abstract class LeakAware<T extends LeakAware.Resource> implements 
Disposable {
+        public static class Resource implements Disposable {
+            private final AtomicBoolean isDisposed = new AtomicBoolean(false);
+            private final Disposable cleanup;
+
+            public Resource(Disposable cleanup) {
+                this.cleanup = cleanup;
+            }
+
+            public boolean isDisposed() {
+                return isDisposed.get();
+            }
+
+            @Override
+            public void dispose() {
+                isDisposed.set(true);
+                cleanup.dispose();
+            }
+        }
+
+        public static class LeakDetectorException extends RuntimeException {
+            public LeakDetectorException() {
+                super();
+            }
+        }
+
+        public enum Level {
+            NONE,
+            SIMPLE,
+            ADVANCED,
+            TESTING;
+
+            static Level parse(String input) {
+                for (Level level : values()) {
+                    if (level.name().equalsIgnoreCase(input)) {
+                        return level;
+                    }
+                }
+                throw new IllegalArgumentException(String.format("Unknown 
level `%s`", input));
+            }
+        }
+
+        public static final ReferenceQueue<LeakAware> REFERENCE_QUEUE = new 
ReferenceQueue<>();
+        public static final ConcurrentHashMap<LeakAwareFinalizer, Boolean> 
REFERENCES_IN_USE = new ConcurrentHashMap<>();
+        public static final Level LEVEL = Level.SIMPLE;
+
+        public static void track() {
+            if (leakDetectorEnabled()) {
+                Reference<?> referenceFromQueue;
+                while ((referenceFromQueue = REFERENCE_QUEUE.poll()) != null) {
+                    ((LeakAwareFinalizer) referenceFromQueue).detectLeak();
+                    referenceFromQueue.clear();
+                }
+            }
+        }
+
+        private static boolean leakDetectorEnabled() {
+            return LEVEL != Level.NONE;
+        }
+
+        private final T resource;
+        private final LeakAwareFinalizer finalizer;
+
+        protected LeakAware(T resource) {
+            this.resource = resource;
+            this.finalizer = new LeakAwareFinalizer(this, resource, 
REFERENCE_QUEUE);
+            REFERENCES_IN_USE.put(finalizer, true);
+        }
+
+        @Override
+        public void dispose() {
+            REFERENCES_IN_USE.remove(finalizer);
+            resource.dispose();
+        }
+
+        public T getResource() {
+            return resource;
+        }
+    }
+
+    class LeakAwareFinalizer extends PhantomReference<LeakAware> {
+        private static final Logger LOGGER = 
LoggerFactory.getLogger(LeakAwareFinalizer.class);
+
+        private final LeakAware.Resource resource;
+
+        public LeakAwareFinalizer(LeakAware referent, LeakAware.Resource 
resource, ReferenceQueue<? super LeakAware> q) {
+            super(referent, q);
+            this.resource = resource;

Review comment:
       No that's itended, leaks on MailImpl is notan issue, we don't need 
detection for it as it do not hold resources in itself.




-- 
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]

Reply via email to