KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > ChangeNotificationController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.util;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
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     //The singleton
39
private static ChangeNotificationController instance = null;
40
41     //TEST
42
private static class ThreadLocalNotifications extends ThreadLocal JavaDoc implements NotificationListener
43     {
44         public Object JavaDoc initialValue()
45         {
46             return new ArrayList JavaDoc();
47         }
48
49         public List JavaDoc getList()
50         {
51             return (List JavaDoc) super.get();
52         }
53
54         public void setContextParameters(Map JavaDoc 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         //System.out.println("Adding notificationMessage:" + notificationMessage);
69
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 JavaDoc internalMessageList = new ArrayList JavaDoc();
78         List JavaDoc publicMessageList = new ArrayList JavaDoc();
79
80         Iterator JavaDoc iterator = list.getList().iterator();
81         while(iterator.hasNext())
82         {
83             NotificationMessage notificationMessage = (NotificationMessage)iterator.next();
84             //System.out.println("notificationMessage:" + notificationMessage.getClassName() + notificationMessage.getType());
85

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 JavaDoc internalMessage = null;
104         Hashtable JavaDoc publicMessage = null;
105         
106         if(internalMessageList.size() > 0)
107         {
108             internalMessage = new Hashtable JavaDoc();
109             Iterator JavaDoc 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 JavaDoc();
125             Iterator JavaDoc publicMessageListIterator = publicMessageList.iterator();
126             int i = 0;
127             while(publicMessageListIterator.hasNext())
128             {
129                 NotificationMessage notificationMessage = (NotificationMessage)publicMessageListIterator.next();
130
131                 //For mixed env where we want to do cms upgrade first - remove when we release a new version
132
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 JavaDoc e)
153         {
154             e.printStackTrace();
155         }
156         
157     }
158     //TEST
159

160     /**
161      * A factory method that makes sure we operate on a singeton.
162      * We assign a couple of standard listeners like a logger and a transactionHistoryLogger.
163      */

164     
165     public static ChangeNotificationController getInstance()
166     {
167         if(instance == null)
168         {
169             instance = new ChangeNotificationController();
170             //instance.registerListener(new FileLogger());
171

172             String JavaDoc logTransactions = CmsPropertyHandler.getLogTransactions();
173             if(logTransactions == null || !logTransactions.equalsIgnoreCase("false"))
174                 instance.registerListener(new TransactionHistoryWriter());
175             
176             //instance.registerListener(new RemoteCacheUpdater());
177
instance.registerListener(list);
178             //instance.registerListener(new WorkflowEngine());
179
}
180         
181         return instance;
182     }
183
184 //-------------------- The object stuff ---------------------//
185

186     //List of all listeners.
187
private List JavaDoc listeners = new ArrayList JavaDoc();
188     
189     //List of all listeners that shall be unregistered
190
//(to avoid concurrent modification exceptions, and deadlocks)
191
private List JavaDoc unregisteredlisteners = new ArrayList JavaDoc();
192     
193     /**
194      * The standard constructor is private to force use of factory-method.
195      */

196     
197     private ChangeNotificationController()
198     {
199     }
200
201     /**
202      * This method registers a new listener to be notified when a new notifiation is available.
203      */

204     
205     public void registerListener(NotificationListener notificationListener)
206     {
207         this.listeners.add(notificationListener);
208     }
209
210     /**
211      * This method unregisters an existing listener.
212      */

213     
214     public void unregisterListener(NotificationListener notificationListener)
215     {
216         this.unregisteredlisteners.add(notificationListener);
217     }
218     
219     /**
220      * This method gets called when a new notification has come.
221      * It then iterates through the listeners and notifies them.
222      */

223     public void addNotificationMessage(NotificationMessage notificationMessage)
224     {
225         logger.info("Got a new notification:" + notificationMessage.getName() + ":" + notificationMessage.getType() + ":" + notificationMessage.getObjectId() + ":" + notificationMessage.getObjectName());
226         Iterator JavaDoc 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 JavaDoc 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