001/* 002 * JGrapes Event Driven Framework 003 * Copyright (C) 2016-2026 Michael N. Lipp 004 * 005 * This program is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU Affero General Public License as published by 007 * the Free Software Foundation; either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, but 011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public 013 * License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program; if not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jgrapes.core.internal; 020 021import java.util.Collections; 022import java.util.IdentityHashMap; 023import java.util.Map; 024import java.util.logging.Level; 025import java.util.logging.Logger; 026import org.jgrapes.core.ComponentType; 027 028/** 029 * A registry for generators. Used to track generators and determine 030 * whether the application has stopped. 031 */ 032@SuppressWarnings({ "PMD.ClassWithOnlyPrivateConstructorsShouldBeFinal", 033 "PMD.AvoidSynchronizedStatement" }) 034public class GeneratorRegistry { 035 036 @SuppressWarnings("PMD.FieldNamingConventions") 037 private static final Logger generatorTracking 038 = Logger.getLogger(ComponentType.class.getPackage().getName() 039 + ".generatorTracking"); 040 041 private long running; 042 private Thread keepAlive; 043 private Map<Object, Object> generators; 044 045 /** 046 * Holds a generator instance. 047 */ 048 private static final class InstanceHolder { 049 private static final GeneratorRegistry INSTANCE 050 = new GeneratorRegistry(); 051 } 052 053 private GeneratorRegistry() { 054 if (generatorTracking.isLoggable(Level.FINE)) { 055 generators = Collections.synchronizedMap(new IdentityHashMap<>()); 056 } 057 } 058 059 /** 060 * Returns the singleton instance of the registry. 061 * 062 * @return the generator registry 063 */ 064 public static GeneratorRegistry instance() { 065 return InstanceHolder.INSTANCE; 066 } 067 068 /** 069 * Adds a generator. 070 * 071 * @param obj the obj 072 */ 073 public void add(Object obj) { 074 synchronized (this) { 075 running += 1; 076 if (generators != null) { 077 generators.put(obj, null); 078 generatorTracking.finest(() -> "Added generator " + obj 079 + ", " + generators.size() + " generators registered: " 080 + generators.keySet()); 081 } 082 if (running == 1) { // NOPMD, no, not using a constant for this. 083 keepAlive = new Thread("GeneratorRegistry") { 084 @Override 085 @SuppressWarnings({ "PMD.EmptyCatchBlock", 086 "PMD.OverridingThreadRun" }) 087 public void run() { 088 try { 089 while (true) { 090 sleep(Long.MAX_VALUE); 091 } 092 } catch (InterruptedException e) { 093 // Okay, then stop 094 } 095 } 096 }; 097 keepAlive.start(); 098 } 099 } 100 } 101 102 /** 103 * Removes the generator. 104 * 105 * @param obj the generator 106 */ 107 public void remove(Object obj) { 108 synchronized (this) { 109 running -= 1; 110 if (generators != null) { 111 generators.remove(obj); 112 generatorTracking.finest(() -> "Removed generator " + obj 113 + ", " + generators.size() + " generators registered: " 114 + generators.keySet()); 115 } 116 if (running == 0) { 117 generatorTracking 118 .finest(() -> "Zero generators, notifying all."); 119 keepAlive.interrupt(); 120 notifyAll(); 121 } 122 } 123 } 124 125 /** 126 * Checks if is exhausted (no generators left) 127 * 128 * @return true, if is exhausted 129 */ 130 public boolean isExhausted() { 131 return running == 0; 132 } 133 134 /** 135 * Await exhaustion. 136 * 137 * @throws InterruptedException the interrupted exception 138 */ 139 @SuppressWarnings({ "PMD.GuardLogStatement" }) 140 public void awaitExhaustion() throws InterruptedException { 141 if (generators != null) { 142 synchronized (this) { 143 if (running != generators.size()) { 144 generatorTracking 145 .severe(() -> "Generator count doesn't match tracked."); 146 } 147 } 148 } 149 while (running > 0) { 150 // generators.keySet() may call EventProcessor.toString() 151 // which locks on the EventProcessor which may want a lock 152 // on the registry (deadlock). So keep this out of the 153 // synchronized. 154 if (generators != null) { 155 generatorTracking 156 .fine(() -> "Thread " + Thread.currentThread().getName() 157 + " is waiting, " + generators.size() 158 + " generators registered: " 159 + generators.keySet()); 160 } 161 synchronized (this) { 162 if (running > 0) { 163 wait(); 164 } 165 } 166 } 167 generatorTracking 168 .finest("Thread " + Thread.currentThread().getName() 169 + " continues."); 170 } 171 172 /** 173 * Await exhaustion with a timeout. 174 * 175 * @param timeout the timeout 176 * @return true, if successful 177 * @throws InterruptedException the interrupted exception 178 */ 179 @SuppressWarnings({ "PMD.CollapsibleIfStatements" }) 180 public boolean awaitExhaustion(long timeout) 181 throws InterruptedException { 182 synchronized (this) { 183 if (generators != null) { 184 if (running != generators.size()) { 185 generatorTracking.severe( 186 "Generator count doesn't match tracked."); 187 } 188 } 189 if (isExhausted()) { 190 return true; 191 } 192 if (generators != null) { 193 generatorTracking 194 .fine(() -> "Waiting, generators: " + generators.keySet()); 195 } 196 wait(timeout); 197 if (generators != null) { 198 generatorTracking 199 .fine(() -> "Waited, generators: " + generators.keySet()); 200 } 201 return isExhausted(); 202 } 203 } 204}