1 package net.sf.panoptes.model.node.log4j;
2
3 import java.net.InetAddress;
4 import java.util.Collections;
5 import java.util.Enumeration;
6 import java.util.Hashtable;
7 import java.util.Set;
8 import java.util.TreeSet;
9 import java.util.Vector;
10
11 import net.sf.panoptes.model.component.log4j.LoggerComponent;
12 import net.sf.panoptes.model.component.registry.ComponentAlreadyExistsException;
13 import net.sf.panoptes.model.component.registry.ComponentName;
14 import net.sf.panoptes.model.component.registry.ComponentRegistry;
15 import net.sf.panoptes.model.component.registry.MalformedComponentNameException;
16 import net.sf.panoptes.model.node.ComponentGroupNode;
17 import net.sf.panoptes.model.node.NodeDescriptor;
18 import net.sf.panoptes.model.node.INode;
19
20 import org.apache.log4j.spi.LoggingEvent;
21
22 /***
23 * LoggerRepository.java
24 *
25 * Controller class for all logger locations. Should probably be instanciated
26 * on a per-client-basis in the near future.
27 *
28 * @author Dag Liodden
29 * @version 0.1
30 */
31 public class LoggerRepository
32 extends ComponentGroupNode {
33
34 private ComponentRegistry componentRegistry;
35 Hashtable locations = new Hashtable();
36 Vector allEvents = new Vector();
37 private Vector listeners = new Vector();
38
39 /***
40 * Since we have a refresh interval and a worker-thread dispatching batches of
41 * update-events, we need to keep track of which locations that are added or
42 * updated.
43 */
44 Set locationsAdded = Collections.synchronizedSortedSet(new TreeSet());
45 Set locationsChanged = Collections.synchronizedSortedSet(new TreeSet());
46
47 /***
48 * Constructor.
49 * @param host
50 * @param className
51 * @param isPackage
52 * @param parent
53 */
54 public LoggerRepository(ComponentRegistry componentRegistry) {
55 super("Logger repository", "Logs");
56 this.componentRegistry = componentRegistry;
57 setAcceptsChildren(false);
58
59 /***
60 * The worker-thread to update listeners to this repository
61 */
62 Runnable r = new Runnable() {
63 public void run() {
64 while (true) {
65 if ((locationsAdded.size() > 0) || (locationsChanged.size() > 0))
66 notifyListeners();
67 try {
68 Thread.sleep(2000);
69 } catch (InterruptedException e) {
70 }
71
72 }
73 }
74 };
75 Thread worker = new Thread(r);
76 worker.start();
77
78 SocketAppenderListener s = new SocketAppenderListener(1980, this); // TODO: Configure Log4J Listener port
79 s.startListening();
80 }
81
82
83 /***
84 * Add an event to this repository. Listeners are notified on the next batch update
85 *
86 * @param e the event to add
87 * @param host the source host (not used quite yet)
88 */
89 public void addEvent(LoggingEvent e, InetAddress host) {
90 String className = e.getLocationInformation().getClassName();
91
92 boolean newLocation = false;
93 LoggerComponent loc = (LoggerComponent) locations.get(className);
94 if (loc == null) {
95 loc = new LoggerComponent(host.getHostName(), className, false, null);
96 newLocation = true;
97 addChild(loc);
98 locations.put(className, loc);
99 ComponentName componentName;
100 try {
101 componentName =
102 new ComponentName(ComponentName.DOMAIN_LOGGER, "host=" + host.toString() + ", class=" + className);
103 componentRegistry.registerComponent(loc, componentName);
104 System.out.println(componentName.toString());
105 } catch (MalformedComponentNameException e1) {
106 e1.printStackTrace();
107 } catch (ComponentAlreadyExistsException e2) {
108 e2.printStackTrace();
109 }
110 }
111 loc.addEvent(e);
112 allEvents.insertElementAt(e, 0);
113
114 if (newLocation && !locationsAdded.contains(loc))
115 locationsAdded.add(loc);
116 if (!locationsChanged.contains(loc))
117 locationsChanged.add(loc);
118 }
119
120 /***
121 * Called by the refresh thread
122 */
123 private void notifyListeners() {
124 Set added = new TreeSet();
125 Set changed = new TreeSet();
126
127 synchronized (locationsChanged) {
128 changed.addAll(locationsChanged);
129 locationsChanged.clear();
130 }
131
132 synchronized (locationsAdded) {
133 added.addAll(locationsAdded);
134 locationsAdded.clear();
135 }
136 RepositoryUpdateEvent e = new RepositoryUpdateEvent(changed, added);
137
138 for (Enumeration en = listeners.elements(); en.hasMoreElements();) {
139 ((RepositoryListener) en.nextElement()).repositoryUpdated(e);
140 }
141 }
142
143 /***
144 * Lists all LoggerLocations in this repository
145 *
146 * @return LoggerComponent[] the locations
147 */
148 public LoggerComponent[] listLocations() {
149 return (LoggerComponent[]) locations.values().toArray(
150 new LoggerComponent[locations.size()]);
151 }
152
153 /***
154 * Lists all event in this repository
155 *
156 * @return LoggingEvent[] the events
157 */
158 public LoggingEvent[] listEvents() {
159 return (LoggingEvent[]) allEvents.toArray(
160 new LoggingEvent[allEvents.size()]);
161 }
162
163 /***
164 * Adds a <code>RepositoryListener</code> to this repository
165 *
166 * @param listener the listener to add
167 * @see RepositoryListener
168 */
169 public void addListener(RepositoryListener listener) {
170 listeners.add(listener);
171 }
172
173 /***
174 * @see com.glt.troodon.console.INode#getConfigDescriptor()
175 */
176 public NodeDescriptor getConfigDescriptor() {
177 return new NodeDescriptor("Logs", "Logs");
178 }
179
180 /***
181 * @see com.glt.troodon.console.INode#getChildren()
182 */
183 public INode[] getChildren() {
184 return listLocations();
185 }
186
187 /***
188 * @see com.glt.troodon.console.INode#getIconName()
189 */
190 public String getIconName() {
191 return INode.ICON_LOG_FOLDER;
192 }
193
194
195 }
This page was automatically generated by Maven