1 23 24 package org.infoglue.cms.util; 25 26 import java.util.ArrayList ; 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import org.apache.log4j.Logger; 33 34 public class ChangeNotificationController 35 { 36 private final static Logger logger = Logger.getLogger(ChangeNotificationController.class.getName()); 37 38 private static ChangeNotificationController instance = null; 40 41 private static class ThreadLocalNotifications extends ThreadLocal implements NotificationListener 43 { 44 public Object initialValue() 45 { 46 return new ArrayList (); 47 } 48 49 public List getList() 50 { 51 return (List ) super.get(); 52 } 53 54 public void setContextParameters(Map map) 55 { 56 } 57 58 public void notify(NotificationMessage message) 59 { 60 list.getList().add(message); 61 } 62 } 63 64 private static ThreadLocalNotifications list = new ThreadLocalNotifications(); 65 66 public void put(NotificationMessage notificationMessage) 67 { 68 list.getList().add(notificationMessage); 70 } 71 72 public static void notifyListeners() 73 { 74 if(list.getList().size() > 0) 75 logger.info("Now as the transaction is done and there are items in the notification list - let's notify the deliver app..."); 76 77 List internalMessageList = new ArrayList (); 78 List publicMessageList = new ArrayList (); 79 80 Iterator iterator = list.getList().iterator(); 81 while(iterator.hasNext()) 82 { 83 NotificationMessage notificationMessage = (NotificationMessage)iterator.next(); 84 86 if(notificationMessage.getType() == NotificationMessage.PUBLISHING || notificationMessage.getType() == NotificationMessage.UNPUBLISHING || notificationMessage.getType() == NotificationMessage.SYSTEM) 87 { 88 publicMessageList.add(notificationMessage); 89 90 if(notificationMessage.getType() == NotificationMessage.SYSTEM) 91 { 92 internalMessageList.add(notificationMessage); 93 } 94 } 95 else 96 { 97 internalMessageList.add(notificationMessage); 98 } 99 100 iterator.remove(); 101 } 102 103 Hashtable internalMessage = null; 104 Hashtable publicMessage = null; 105 106 if(internalMessageList.size() > 0) 107 { 108 internalMessage = new Hashtable (); 109 Iterator internalMessageListIterator = internalMessageList.iterator(); 110 int i = 0; 111 while(internalMessageListIterator.hasNext()) 112 { 113 NotificationMessage notificationMessage = (NotificationMessage)internalMessageListIterator.next(); 114 internalMessage.put(i + ".className", notificationMessage.getClassName()); 115 internalMessage.put(i + ".objectId", notificationMessage.getObjectId()); 116 internalMessage.put(i + ".objectName", notificationMessage.getObjectName()); 117 internalMessage.put(i + ".typeId", "" + notificationMessage.getType()); 118 i++; 119 } 120 } 121 122 if(publicMessageList.size() > 0) 123 { 124 publicMessage = new Hashtable (); 125 Iterator publicMessageListIterator = publicMessageList.iterator(); 126 int i = 0; 127 while(publicMessageListIterator.hasNext()) 128 { 129 NotificationMessage notificationMessage = (NotificationMessage)publicMessageListIterator.next(); 130 131 if(i == 0) 133 { 134 publicMessage.put("className", notificationMessage.getClassName()); 135 publicMessage.put("objectId", notificationMessage.getObjectId()); 136 publicMessage.put("objectName", notificationMessage.getObjectName()); 137 publicMessage.put("typeId", "" + notificationMessage.getType()); 138 } 139 140 publicMessage.put(i + ".className", notificationMessage.getClassName()); 141 publicMessage.put(i + ".objectId", notificationMessage.getObjectId()); 142 publicMessage.put(i + ".objectName", notificationMessage.getObjectName()); 143 publicMessage.put(i + ".typeId", "" + notificationMessage.getType()); 144 i++; 145 } 146 } 147 148 try 149 { 150 new RemoteCacheUpdater().updateRemoteCaches(internalMessage, publicMessage); 151 } 152 catch (Exception e) 153 { 154 e.printStackTrace(); 155 } 156 157 } 158 160 164 165 public static ChangeNotificationController getInstance() 166 { 167 if(instance == null) 168 { 169 instance = new ChangeNotificationController(); 170 172 String logTransactions = CmsPropertyHandler.getLogTransactions(); 173 if(logTransactions == null || !logTransactions.equalsIgnoreCase("false")) 174 instance.registerListener(new TransactionHistoryWriter()); 175 176 instance.registerListener(list); 178 } 180 181 return instance; 182 } 183 184 186 private List listeners = new ArrayList (); 188 189 private List unregisteredlisteners = new ArrayList (); 192 193 196 197 private ChangeNotificationController() 198 { 199 } 200 201 204 205 public void registerListener(NotificationListener notificationListener) 206 { 207 this.listeners.add(notificationListener); 208 } 209 210 213 214 public void unregisterListener(NotificationListener notificationListener) 215 { 216 this.unregisteredlisteners.add(notificationListener); 217 } 218 219 223 public void addNotificationMessage(NotificationMessage notificationMessage) 224 { 225 logger.info("Got a new notification:" + notificationMessage.getName() + ":" + notificationMessage.getType() + ":" + notificationMessage.getObjectId() + ":" + notificationMessage.getObjectName()); 226 Iterator i = listeners.iterator(); 227 while(i.hasNext()) 228 { 229 try 230 { 231 NotificationListener nl = (NotificationListener)i.next(); 232 if(!unregisteredlisteners.contains(nl)) 233 { 234 logger.info("Notifying the listener:" + nl.getClass().getName()); 235 nl.notify(notificationMessage); 236 } 237 } 238 catch(Exception e) 239 { 240 logger.error("One of the listeners threw an exception but we carry on with the others. Error: " + e.getMessage(), e); 241 } 242 } 243 listeners.removeAll(unregisteredlisteners); 244 unregisteredlisteners.clear(); 245 } 246 247 248 } | Popular Tags |