psmith 2003/09/01 23:32:06 Modified: src/java/org/apache/log4j/chainsaw LogUI.java ChainsawAppenderHandler.java Log: The beginnings of the refactoring of event reception and dispatching to internal LogPanels. LogUI now implements event batch listener interface. Eventually this will move to LogPanels implementing this interface and receiving the event batch directly, and LogUI only needing to deal with "new" panels. Revision Changes Path 1.13 +22 -128 jakarta-log4j/src/java/org/apache/log4j/chainsaw/LogUI.java Index: LogUI.java =================================================================== RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/LogUI.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- LogUI.java 2 Sep 2003 03:39:16 -0000 1.12 +++ LogUI.java 2 Sep 2003 06:32:06 -0000 1.13 @@ -183,7 +183,8 @@ * @author Paul Smith <[EMAIL PROTECTED]> * */ -public class LogUI extends JFrame implements ChainsawViewer, SettingsListener { +public class LogUI extends JFrame implements ChainsawViewer, SettingsListener, + EventBatchListener { private static final String CONFIG_FILE_TO_USE = "config.file"; private static final String USE_CYCLIC_BUFFER_PROP_NAME = "chainsaw.usecyclicbuffer"; @@ -311,13 +312,15 @@ LogUI logUI = new LogUI(); - logUI.handler = new ChainsawAppenderHandler(logUI); + logUI.handler = new ChainsawAppenderHandler(); + logUI.handler.addEventBatchListener(logUI); LogManager.getRootLogger().addAppender(logUI.handler); logUI.activateViewer(); } public void activateViewer(ChainsawAppender appender) { - handler = new ChainsawAppenderHandler(this, appender); + handler = new ChainsawAppenderHandler(appender); + handler.addEventBatchListener(this); activateViewer(); } @@ -940,128 +943,6 @@ } } - /** - * Determines an appropriate title for the Tab for the Tab Pane - * by locating a the log4jmachinename property - * @param v - * @return - */ - String getTabIdentifier(Vector v) { - int fieldIndex = - ChainsawColumns.getColumnsNames().indexOf( - ChainsawConstants.PROPERTIES_COL_NAME); - - if (fieldIndex < 0) { - return ChainsawConstants.UNKNOWN_TAB_NAME; - } - - String properties = (String) v.get(fieldIndex); - - String machinekey = ChainsawConstants.LOG4J_MACHINE_KEY + "="; - String machinename = null; - int machineposition = properties.indexOf(machinekey) + machinekey.length(); - int machinelength = properties.indexOf(",", machineposition); - - if (machinelength == -1) { - machinelength = properties.length(); - } - - if (machineposition >= machinekey.length()) { - machinename = properties.substring(machineposition, machinelength); - - int dotposition = machinename.indexOf("."); - boolean isnumeric = true; - - if (dotposition > -1) { - char[] firstdotpart = - machinename.substring(0, dotposition).toCharArray(); - - for (int i = 0; i < firstdotpart.length; i++) { - isnumeric = isnumeric && Character.isDigit(firstdotpart[i]); - } - - if (!isnumeric) { - machinename = machinename.substring(0, dotposition); - } - } - } - - String appkey = ChainsawConstants.LOG4J_APP_KEY + "="; - String appname = null; - int appposition = properties.indexOf(appkey) + appkey.length(); - - if (appposition >= appkey.length()) { - int applength = properties.indexOf(",", appposition); - - if (applength == -1) { - applength = properties.length(); - } - - appname = properties.substring(appposition, applength); - } - - StringBuffer ident = new StringBuffer(); - - if (machinename != null) { - ident.append(machinename); - } - - if (appname != null) { - ident.append("-"); - ident.append(appname); - } - - if (ident.length() == 0) { - /** - * Maybe there's a Remote Host entry? - */ - String remoteHostKey = ChainsawConstants.LOG4J_REMOTEHOST_KEY + "="; - String remoteHost = null; - int rhposition = - properties.indexOf(remoteHostKey) + remoteHostKey.length(); - - if (rhposition >= remoteHostKey.length()) { - int rhlength = properties.indexOf(":", rhposition); - - if (rhlength == -1) { - rhlength = properties.length(); - } - - remoteHost = properties.substring(rhposition, rhlength); - } - - if (remoteHost != null) { - ident.append(remoteHost); - } - } - - if (ident.length() == 0) { - ident.append(ChainsawConstants.UNKNOWN_TAB_NAME); - } - - return ident.toString(); - } - - /** - * Receives a ChainsawEventBatch object and passes each identifiers - * collection of events in sequence, to allow a Single tab - * to receive as many events in a single hit as possible. - * - * This method (actually the internal addRows method) takes these - * Collection of events and uses the value adjusting property - * to ensure table Listeners don't constantly get notified of every single event, but - * wait until the sequence is complete - * @param eventBatch - */ - void receiveEventBatch(ChainsawEventBatch eventBatch) { - for (Iterator iter = eventBatch.identifierIterator(); iter.hasNext();) { - String ident = (String) iter.next(); - - if (!pausedList.contains(ident)) { - addRows(ident, eventBatch.entrySet(ident)); - } - } - } /** * Formats the individual elements of an LoggingEvent by ensuring that @@ -1129,7 +1010,12 @@ * @param ident * @param v */ - private void addRows(final String ident, final List eventBatchEntrys) { + public void receiveEventBatch( + final String ident, final List eventBatchEntrys) { + if (eventBatchEntrys.size() == 0) { + return; + } + EventContainer tableModel; JSortTable table; ScrollToBottom scrollToBottom; @@ -1159,7 +1045,7 @@ table = (JSortTable) tableMap.get(ident); } else { final String eventType = - ((ChainsawEventBatch.Entry) eventBatchEntrys.get(0)).getEventType(); + ((ChainsawEventBatchEntry) eventBatchEntrys.get(0)).getEventType(); int bufferSize = 500; @@ -1215,7 +1101,7 @@ } for (Iterator iter = eventBatchEntrys.iterator(); iter.hasNext();) { - ChainsawEventBatch.Entry entry = (ChainsawEventBatch.Entry) iter.next(); + ChainsawEventBatchEntry entry = (ChainsawEventBatchEntry) iter.next(); Vector v = formatFields(entry.getEventVector()); final String eventType = entry.getEventType(); String level = @@ -1342,6 +1228,14 @@ } return getCurrentLogPanel().isLogTreePanelVisible(); + } + + /* (non-Javadoc) + * @see org.apache.log4j.chainsaw.EventBatchListener#getInterestedIdentifier() + */ + public String getInterestedIdentifier() { + // this instance is interested in ALL event batches, as we determine how to route things + return null; } /** 1.2 +155 -13 jakarta-log4j/src/java/org/apache/log4j/chainsaw/ChainsawAppenderHandler.java Index: ChainsawAppenderHandler.java =================================================================== RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/ChainsawAppenderHandler.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ChainsawAppenderHandler.java 25 Jun 2003 04:05:21 -0000 1.1 +++ ChainsawAppenderHandler.java 2 Sep 2003 06:32:06 -0000 1.2 @@ -63,37 +63,43 @@ import java.util.Set; import java.util.Vector; +import javax.swing.event.EventListenerList; + /** - * Integrates a ChainsawAppender and a LogUI instance by registering - * itself as a sub-appender of a ChainsawAppender (and therefore receives - * any events the ChainsawAppender receives, and queues up those events. - * - * The Queued LoggingEvents are then periodically processed/converted into - * Vectors of event information that are then forwared into the LogUI instance + * A handler class that either extends a particular appender hierarchy or can be bound + * into the Log4j appender framework, and queues events, to be later + * dispatched to registered/interested parties. * * @author Scott Deboy <[EMAIL PROTECTED]> + * @author Paul Smith <[EMAIL PROTECTED]> * */ public class ChainsawAppenderHandler extends AppenderSkeleton { private ChainsawAppender appender; - private LogUI logUI; private WorkQueue worker; private final Object mutex = new Object(); private int sleepInterval = 1000; + private EventListenerList listenerList = new EventListenerList(); - public ChainsawAppenderHandler(LogUI logUI, ChainsawAppender appender) { + public ChainsawAppenderHandler(ChainsawAppender appender) { this.appender = appender; - this.logUI = logUI; appender.addAppender(this); activateOptions(); } - public ChainsawAppenderHandler(LogUI logUI) { - this.logUI = logUI; + public ChainsawAppenderHandler() { activateOptions(); } + public void addEventBatchListener(EventBatchListener l) { + listenerList.add(EventBatchListener.class, l); + } + + public void removeEventBatchListener(EventBatchListener l) { + listenerList.remove(EventBatchListener.class, l); + } + public void append(LoggingEvent event) { worker.enqueue(event); } @@ -217,6 +223,108 @@ } /** + * Determines an appropriate title for the Tab for the Tab Pane + * by locating a the log4jmachinename property + * @param v + * @return + */ + private static String getTabIdentifier(Vector v) { + int fieldIndex = + ChainsawColumns.getColumnsNames().indexOf( + ChainsawConstants.PROPERTIES_COL_NAME); + + if (fieldIndex < 0) { + return ChainsawConstants.UNKNOWN_TAB_NAME; + } + + String properties = (String) v.get(fieldIndex); + + String machinekey = ChainsawConstants.LOG4J_MACHINE_KEY + "="; + String machinename = null; + int machineposition = properties.indexOf(machinekey) + machinekey.length(); + int machinelength = properties.indexOf(",", machineposition); + + if (machinelength == -1) { + machinelength = properties.length(); + } + + if (machineposition >= machinekey.length()) { + machinename = properties.substring(machineposition, machinelength); + + int dotposition = machinename.indexOf("."); + boolean isnumeric = true; + + if (dotposition > -1) { + char[] firstdotpart = + machinename.substring(0, dotposition).toCharArray(); + + for (int i = 0; i < firstdotpart.length; i++) { + isnumeric = isnumeric && Character.isDigit(firstdotpart[i]); + } + + if (!isnumeric) { + machinename = machinename.substring(0, dotposition); + } + } + } + + String appkey = ChainsawConstants.LOG4J_APP_KEY + "="; + String appname = null; + int appposition = properties.indexOf(appkey) + appkey.length(); + + if (appposition >= appkey.length()) { + int applength = properties.indexOf(",", appposition); + + if (applength == -1) { + applength = properties.length(); + } + + appname = properties.substring(appposition, applength); + } + + StringBuffer ident = new StringBuffer(); + + if (machinename != null) { + ident.append(machinename); + } + + if (appname != null) { + ident.append("-"); + ident.append(appname); + } + + if (ident.length() == 0) { + /** + * Maybe there's a Remote Host entry? + */ + String remoteHostKey = ChainsawConstants.LOG4J_REMOTEHOST_KEY + "="; + String remoteHost = null; + int rhposition = + properties.indexOf(remoteHostKey) + remoteHostKey.length(); + + if (rhposition >= remoteHostKey.length()) { + int rhlength = properties.indexOf(":", rhposition); + + if (rhlength == -1) { + rhlength = properties.length(); + } + + remoteHost = properties.substring(rhposition, rhlength); + } + + if (remoteHost != null) { + ident.append(remoteHost); + } + } + + if (ident.length() == 0) { + ident.append(ChainsawConstants.UNKNOWN_TAB_NAME); + } + + return ident.toString(); + } + + /** * Queue of Events are placed in here, which are picked up by an * asychronous thread. The WorkerThread looks for events once a second and * processes all events accumulated during that time.. @@ -281,12 +389,13 @@ } Vector convertedEventVector = convert(e); - String ident = logUI.getTabIdentifier(convertedEventVector); + String ident = getTabIdentifier(convertedEventVector); eventBatch.addEvent(ident, eventType, convertedEventVector); } - logUI.receiveEventBatch(eventBatch); + dispatchEventBatch(eventBatch); + // logUI.receiveEventBatch(eventBatch); innerList.clear(); } @@ -294,6 +403,39 @@ Thread.sleep(getQueueInterval()); } catch (InterruptedException ie) { } + } + } + + /** + * Dispatches the event batches contents to all the interested parties + * by iterating over each identifier and dispatching the + * ChainsawEventBatchEntry object to each listener that is interested. + * @param eventBatch + */ + private void dispatchEventBatch(ChainsawEventBatch eventBatch) { + EventBatchListener[] listeners = + (EventBatchListener[]) listenerList.getListeners( + EventBatchListener.class); + + for (Iterator iter = eventBatch.identifierIterator(); iter.hasNext();) { + String identifier = (String) iter.next(); + List eventList = null; + + for (int i = 0; i < listeners.length; i++) { + EventBatchListener listener = listeners[i]; + + if ( + (listener.getInterestedIdentifier() == null) + || listener.getInterestedIdentifier().equals(identifier)) { + if (eventList == null) { + eventList = eventBatch.entrySet(identifier); + } + + listener.receiveEventBatch(identifier, eventList); + } + } + + eventList = null; } } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]