1 package net.sf.panoptes.module.log4j;
2
3 import java.net.InetAddress;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Enumeration;
7 import java.util.List;
8 import java.util.Vector;
9
10 import net.sf.panoptes.model.component.registry.ComponentRegistry;
11 import net.sf.panoptes.model.node.Node;
12 import net.sf.panoptes.model.node.NodeDescriptor;
13
14 import org.apache.commons.collections.CollectionUtils;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.apache.log4j.spi.LoggingEvent;
18 import org.eclipse.swt.graphics.Image;
19
20 /***
21 * LoggerRepository.java
22 *
23 * Controller class for all logger locations. Should probably be instanciated
24 * on a per-client-basis in the near future.
25 *
26 * @author Dag Liodden
27 * @version 0.1
28 */
29 public class LoggerRepository {
30
31 private ComponentRegistry componentRegistry;
32 Vector events = new Vector();
33 private Vector listeners = new Vector();
34 private Log log = LogFactory.getLog(getClass());
35
36 /***
37 * Since we have a refresh interval and a worker-thread dispatching batches of
38 * update-events, we need to keep track of which locations that are added or
39 * updated.
40 */
41 Vector newEvents = new Vector();
42
43 /***
44 * Constructor.
45 * @param host
46 * @param className
47 * @param isPackage
48 * @param parent
49 */
50 public LoggerRepository(ComponentRegistry componentRegistry) {
51 this.componentRegistry = componentRegistry;
52
53 /***
54 * The worker-thread to update listeners to this repository
55 */
56 Runnable r = new Runnable() {
57 public void run() {
58 while (true) {
59 if (newEvents.size() > 0)
60 notifyListeners();
61 try {
62 Thread.sleep(100);
63 } catch (InterruptedException e) {
64 }
65
66 }
67 }
68 };
69 Thread worker = new Thread(r);
70 worker.start();
71
72 SocketAppenderListener s = new SocketAppenderListener(4445, this); // TODO: Configure Log4J Listener port
73 s.startListening();
74 }
75
76 /***
77 * Add an event to this repository. Listeners are notified on the next batch update
78 *
79 * @param e the event to add
80 * @param host the source host (not used quite yet)
81 */
82 public void addEvent(LoggingEvent e, InetAddress host) {
83 LoggingEventInfo ei = new LoggingEventInfo(this, e, host.getCanonicalHostName());
84 events.add(ei);
85 newEvents.add(ei);
86 }
87
88 /***
89 * Called by the refresh thread
90 */
91 private void notifyListeners() {
92 ArrayList newEventsClone;
93
94 /***
95 * Lock newEvents and clone it to avoid the LoggerListener to slow down by waiting for the UI updates
96 */
97 synchronized (newEvents) {
98 newEventsClone = new ArrayList(newEvents);
99 newEvents.clear();
100 }
101 for (Enumeration e = listeners.elements(); e.hasMoreElements();) {
102 LoggerComponent logger = (LoggerComponent) e.nextElement();
103 Collection filtered = CollectionUtils.select(newEventsClone, logger.getFilter());
104 if (filtered.size() > 0)
105 logger.repositoryUpdated(filtered);
106 }
107 }
108
109 /***
110 * Lists all event in this repository
111 *
112 * @return LoggingEvent[] the events
113 */
114 public LoggingEventInfo[] listEvents() {
115 return (LoggingEventInfo[]) events.toArray(new LoggingEventInfo[events.size()]);
116 }
117
118 /***
119 * Lists a number of events in this repository.
120 *
121 * @param maxCount the maximum number of events to return
122 * @return LoggingEvent[] the events
123 */
124 public LoggingEventInfo[] listEvents(LogFilter filter, int maxCount) {
125 List filtered = new ArrayList(CollectionUtils.select(events, filter));
126 int start = 0;
127 int count = filtered.size();
128 int end = count;
129 if (count > maxCount) {
130 start = count - maxCount - 1;
131 end = start + maxCount - 1;
132 filtered = filtered.subList(start, end);
133 }
134 return (LoggingEventInfo[]) filtered.toArray(new LoggingEventInfo[filtered.size()]);
135 }
136
137 /***
138 * Lists all event in this repository
139 *
140 * @return LoggingEvent[] the events
141 */
142 public LoggingEventInfo[] listEvents(LogFilter filter) {
143 synchronized (events) {
144 Collection filtered = CollectionUtils.select(events, filter);
145 return (LoggingEventInfo[]) filtered.toArray(new LoggingEventInfo[filtered.size()]);
146 }
147 }
148
149 /***
150 * Adds a <code>LoggerComponent</code> to this repository so it can be notified
151 * when the repository changes.
152 *
153 * @param listener the listener to add
154 * @see RepositoryListener
155 */
156 public void addLoggerCompnent(LoggerComponent component) {
157 listeners.add(component);
158 }
159
160 /***
161 * @see com.glt.troodon.console.Node#getConfigDescriptor()
162 */
163 public NodeDescriptor getConfigDescriptor() {
164 return new NodeDescriptor("Logs", "Logs");
165 }
166
167 /***
168 * @see com.glt.troodon.console.Node#getChildren()
169 */
170 public Node[] getChildren() {
171 return new Node[0];
172 }
173
174 /***
175 * @see com.glt.troodon.console.Node#getIconName()
176 */
177 public Image getIcon() {
178 return Node.ICON_LOG_FOLDER;
179 }
180
181 }
This page was automatically generated by Maven