Source Code Cross Referenced for EventMulticaster.java in  » Net » Terracotta » com » tc » util » event » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Terracotta » com.tc.util.event 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003:         * notice. All rights reserved.
004:         */
005:        package com.tc.util.event;
006:
007:        import java.io.Serializable;
008:        import java.util.ArrayList;
009:        import java.util.Collections;
010:        import java.util.Iterator;
011:        import java.util.LinkedList;
012:        import java.util.List;
013:
014:        /**
015:         * Generic Event/Listener implementation.
016:         * <p>
017:         * USAGE:
018:         * </p>
019:         * 
020:         * <pre>
021:         *  class Worker implements Runnable {
022:         *  
023:         *  private final EventMulticaster updateObserver;
024:         *  
025:         *  private void doWork() {
026:         *    if (stateChanged()) updateObserver.fireUpdateEvent();
027:         *  
028:         *  ...
029:         *  
030:         *  public void addWorkEventListener(UpdateEventListener listener) {
031:         *    updateObserver.addListener(listener);
032:         *  
033:         *  ...
034:         *  
035:         *  class Master {
036:         *  
037:         *  private void delegateTask() {
038:         *    Worker worker = new Worker();
039:         *    worker.addWorkEventListener(new UpdateEventListener() {
040:         *      public void handleUpdate(Object data) {
041:         *        System.out.println(data);       
042:         *      }
043:         *    });
044:         *  
045:         *  ...
046:         * </pre>
047:         */
048:        public final class EventMulticaster implements  Serializable {
049:
050:            private transient UpdateEventListener eventListener;
051:            private transient EventQueue queue;
052:            private transient Thread dispatchThread;
053:            private volatile boolean dispatchInterrupted;
054:
055:            public synchronized void enableDispatchThread() {
056:                this .eventListener = null;
057:                this .queue = new EventQueue();
058:                this .dispatchThread = new Thread() {
059:                    public void run() {
060:                        QueueEvent event = null;
061:                        try {
062:                            while (true) {
063:                                event = queue.take();
064:                                event.listener.handleUpdate(event.data);
065:                            }
066:                        } catch (InterruptedException e) {
067:                            dispatchInterrupted = true;
068:                        }
069:                    }
070:                };
071:                dispatchThread.setDaemon(true);
072:                dispatchThread.start();
073:            }
074:
075:            public void fireUpdateEvent() {
076:                fireUpdateEvent(null);
077:            }
078:
079:            public synchronized void fireUpdateEvent(UpdateEvent data) {
080:                if (dispatchInterrupted)
081:                    throw new IllegalStateException();
082:                if (eventListener != null
083:                        && (data == null || eventListener != data.source)) {
084:                    if (queue == null)
085:                        eventListener.handleUpdate(data);
086:                    else {
087:                        QueueEvent event = new QueueEvent();
088:                        event.listener = eventListener;
089:                        event.data = data;
090:                        queue.offer(event);
091:                    }
092:                }
093:            }
094:
095:            public synchronized void addListener(UpdateEventListener listener) {
096:                if (eventListener == null) {
097:                    eventListener = listener;
098:                } else if (eventListener instanceof  BroadcastListener) {
099:                    ((BroadcastListener) eventListener).add(listener);
100:                } else {
101:                    BroadcastListener broadcast = new BroadcastListener();
102:                    broadcast.add(eventListener);
103:                    broadcast.add(listener);
104:                    eventListener = broadcast;
105:                }
106:            }
107:
108:            public synchronized boolean removeListener(
109:                    UpdateEventListener listener) {
110:                if (eventListener instanceof  EventMulticaster.BroadcastListener) {
111:                    BroadcastListener broadcast = (BroadcastListener) eventListener;
112:                    if (!broadcast.remove(listener))
113:                        return false;
114:                    if (broadcast.size() == 1)
115:                        eventListener = broadcast.pop();
116:                    return true;
117:                } else {
118:                    if (eventListener != listener)
119:                        return false;
120:                    eventListener = null;
121:                    return true;
122:                }
123:            }
124:
125:            // --------------------------------------------------------------------------------
126:
127:            private class BroadcastListener implements  UpdateEventListener,
128:                    Serializable {
129:
130:                private List listeners = Collections
131:                        .synchronizedList(new ArrayList());
132:
133:                public void handleUpdate(UpdateEvent data) {
134:                    for (Iterator iter = listeners.iterator(); iter.hasNext();) {
135:                        UpdateEventListener listener = (UpdateEventListener) iter
136:                                .next();
137:                        if (listener == data.source)
138:                            continue;
139:                        if (queue == null)
140:                            listener.handleUpdate(data);
141:                        else {
142:                            QueueEvent event = new QueueEvent();
143:                            event.listener = listener;
144:                            event.data = data;
145:                            queue.offer(event);
146:                        }
147:                    }
148:                }
149:
150:                private void add(UpdateEventListener listener) {
151:                    listeners.add(listener);
152:                }
153:
154:                private boolean remove(UpdateEventListener listener) {
155:                    return listeners.remove(listener);
156:                }
157:
158:                private int size() {
159:                    return listeners.size();
160:                }
161:
162:                private UpdateEventListener pop() {
163:                    return (UpdateEventListener) listeners.iterator().next();
164:                }
165:            }
166:
167:            // --------------------------------------------------------------------------------
168:
169:            private class QueueEvent implements  Serializable {
170:                private UpdateEventListener listener;
171:                private UpdateEvent data;
172:            }
173:
174:            // --------------------------------------------------------------------------------
175:
176:            private class EventQueue implements  Serializable {
177:
178:                private final List list = new LinkedList();
179:
180:                private void offer(QueueEvent val) {
181:                    if (val == null)
182:                        throw new NullPointerException();
183:                    synchronized (list) {
184:                        list.add(val);
185:                        list.notify();
186:                    }
187:                }
188:
189:                private QueueEvent take() throws InterruptedException {
190:                    synchronized (list) {
191:                        while (list.size() == 0) {
192:                            list.wait();
193:                        }
194:                        return (QueueEvent) list.remove(0);
195:                    }
196:                }
197:            }
198:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.