Index: ErrorManager.java =================================================================== RCS file: /cvs/openide/src/org/openide/ErrorManager.java,v retrieving revision 1.13 diff -c -r1.13 ErrorManager.java *** ErrorManager.java 17 Jan 2002 17:54:56 -0000 1.13 --- ErrorManager.java 12 Feb 2002 16:09:33 -0000 *************** *** 13,18 **** --- 13,29 ---- package org.openide; + import java.util.Set; + import java.util.Collection; + import java.util.HashSet; + import java.util.Iterator; + + import org.openide.util.Lookup; + import org.openide.util.LookupListener; + import org.openide.util.LookupEvent; + import org.openide.util.WeakSet; + import org.openide.util.RequestProcessor; + /** A system of managing, annotating, and classifying errors and log messages. * Rather than printing raw exceptions to the console, or popping up dialog boxes * with exceptions, or implementing custom debug or logging facililities, code may *************** *** 63,80 **** /** Serious problem, application may be crippled. */ public static final int ERROR = 0x00010000; /** Getter for the default version of error manager. * @return the error manager installed in the system * @since 2.1 */ public static ErrorManager getDefault () { ! ErrorManager em = (ErrorManager)org.openide.util.Lookup.getDefault ().lookup (ErrorManager.class); ! if (em == null) { ! em = simple (); ! } ! return em; } /** Associates annotations with an exception. * * @param t the exception --- 74,99 ---- /** Serious problem, application may be crippled. */ public static final int ERROR = 0x00010000; + /** We keep a reference to our proxy ErrorManager here. */ + private static DelegatingErrorManager current; + /** Getter for the default version of error manager. * @return the error manager installed in the system * @since 2.1 */ public static ErrorManager getDefault () { ! return getDefaultDelegate(); } + private static synchronized DelegatingErrorManager getDefaultDelegate() { + if (current != null) { + return current; + } + current = new DelegatingErrorManager(""); // NOI18N + RequestProcessor.postRequest(current); + return current; + } + /** Associates annotations with an exception. * * @param t the exception *************** *** 230,272 **** } ! /* Simple version of error manager used in case no better is found. ! */ ! private static ErrorManager simple; ! private static synchronized ErrorManager simple () { ! if (simple != null) { ! return simple; } ! simple = new ErrorManager () { ! public ErrorManager getInstance(String name) { return this; } ! public Throwable attachAnnotations (Throwable t, Annotation[] arr) { ! return t; } ! public Annotation[] findAnnotations (Throwable t) { ! return new Annotation[0]; } ! public Throwable annotate ( ! Throwable t, int severity, ! String message, String localizedMessage, ! Throwable stackTrace, java.util.Date date ! ) { ! return t; } ! public void notify (int severity, Throwable t) { t.printStackTrace(); } ! public void log(int severity, String s) { System.err.println ("Log: " + severity + " msg: " + s); // NOI18N } ! ! }; - return simple; } /** Annotation that can be attached to an error. */ --- 249,387 ---- } ! /** ! * Implementation of ErrorManager that delegates to the ones found by ! * lookup. ! */ ! private static class DelegatingErrorManager extends ErrorManager ! implements LookupListener, Runnable { ! ! private String name = null; ! ! /** ! * The set of instances we delegate to. Elements type is ErrorManager. ! */ ! private Set delegates = new HashSet(); ! ! /** ! * A set that has to be updated when the list of delegates ! * changes. All instances created by getInstance are held here. ! * It is a set of DelagatingErrorManager. ! */ ! private WeakSet createdByMe = new WeakSet(); ! ! private Lookup.Result r; ! ! public DelegatingErrorManager(String name) { ! this.name = name; } ! public ErrorManager getInstance(String name) { ! if ((name == null) || ("".equals(name))) { // NOI18N return this; } ! DelegatingErrorManager dem = new DelegatingErrorManager(name); ! attachNewDelegates(dem, name); ! createdByMe.add(dem); ! return dem; ! } ! ! public Throwable attachAnnotations (Throwable t, Annotation[] arr) { ! for (Iterator i = delegates.iterator(); i.hasNext(); ) { ! ErrorManager em = (ErrorManager)i.next(); ! em.attachAnnotations(t, arr); } ! return t; ! } ! public Annotation[] findAnnotations (Throwable t) { ! for (Iterator i = delegates.iterator(); i.hasNext(); ) { ! ErrorManager em = (ErrorManager)i.next(); ! Annotation[] res = em.findAnnotations(t); ! if ((res != null) && (res.length > 0)) { ! return res; ! } } + return new Annotation[0]; + } ! public Throwable annotate ( ! Throwable t, int severity, ! String message, String localizedMessage, ! Throwable stackTrace, java.util.Date date ! ) { ! for (Iterator i = delegates.iterator(); i.hasNext(); ) { ! ErrorManager em = (ErrorManager)i.next(); ! em.annotate(t, severity, message, localizedMessage, stackTrace, date); } ! return t; ! } ! public void notify (int severity, Throwable t) { ! if (delegates.isEmpty()) { t.printStackTrace(); } ! for (Iterator i = delegates.iterator(); i.hasNext(); ) { ! ErrorManager em = (ErrorManager)i.next(); ! em.notify(severity, t); ! } ! } ! public void log(int severity, String s) { ! if (delegates.isEmpty()) { System.err.println ("Log: " + severity + " msg: " + s); // NOI18N } ! for (Iterator i = delegates.iterator(); i.hasNext(); ) { ! ErrorManager em = (ErrorManager)i.next(); ! em.log(severity, s); ! } ! } ! ! /** ! * Updates the list of delegates. Also updates all instances created ! * by ourselves. ! */ ! public void setDelegates(Collection newDelegates) { ! delegates = new HashSet(newDelegates); ! for (Iterator i = createdByMe.iterator(); i.hasNext(); ) { ! DelegatingErrorManager dem = (DelegatingErrorManager)i.next(); ! attachNewDelegates(dem, dem.getName()); ! } ! } ! ! public String getName() { ! return name; ! } ! ! /** ! * Takes all our delegates, asks them for an instance identified by ! * name and adds those results as new delegates for dem. ! * @param String name ! * @param DelagatingErrorManager d the instance to which we will attach ! */ ! private void attachNewDelegates(DelegatingErrorManager dem, String name) { ! Set newDelegatesForDem = new HashSet(); ! for (Iterator j = delegates.iterator(); j.hasNext(); ) { ! ErrorManager e = (ErrorManager)j.next(); ! newDelegatesForDem.add(e.getInstance(name)); ! } ! dem.setDelegates(newDelegatesForDem); ! } ! ! public void run() { ! r = Lookup.getDefault().lookup( ! new Lookup.Template(ErrorManager.class)); ! Collection instances = r.allInstances(); ! setDelegates(instances); ! r.addLookupListener(this); ! } ! ! public void resultChanged (LookupEvent ev) { ! if (r != null) { ! Collection instances = r.allInstances(); ! setDelegates(instances); ! } ! } } + /** Annotation that can be attached to an error. */