package textbender.g.beans; // Copyright 2006, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.beans.*; import java.lang.reflect.UndeclaredThrowableException; import java.rmi.*; import textbender.g.lang.*; import textbender.g.util.EventListenerR; import textbender.g.util.logging.LoggerX; /** A remote listener for property change events. The remote equivalent of * java.beans.{@linkplain PropertyChangeListener PropertyChangeListener}. */ public interface PropertyChangeListenerR extends EventListenerR, Remote { // - P r o p e r t y - C h a n g e - L i s t e n e r - R ------------------------------ /** Called when a bound property value is changed, * per {@linkplain PropertyChangeListener#propertyChange PropertyChangeListener}. * A remote event's source may be null; because, in PropertyChangeEvent, * the {@linkplain PropertyChangeEvent#source source field} is transient. */ public @ThreadSafe void propertyChange( PropertyChangeEvent e ) throws RemoteException; // ==================================================================================== /** For property event dispatchers, * a catcher that treats exception-throwing listeners as disabled. */ public static class DispatchCatcher extends Catcher0 { /** Constructs a DispatchCatcher. */ public DispatchCatcher( Registry registry ) { this.registry = registry; } private final Registry registry; /** Notifies the registry that the listener * is {@linkplain Registry#disabledPropertyChangeListener disabled}. */ public @Override void catchException( PropertyChangeListenerR listener, Exception x ) { final java.util.logging.Logger logger = LoggerX.i( getClass() ); // can later dot-append registry.getClass(), to allow finer config logger.info( "disabling listener, it threw: " + x ); logger.log( LoggerX.FINEST, /*message*/"", x ); try{ registry.disabledPropertyChangeListener( listener ); } catch( RemoteException xR ) { logger.log( LoggerX.WARNING, /*message*/"", xR ); } } } // ==================================================================================== /** For property event dispatchers, by name, * a catcher that treats exception-throwing listeners as disabled. */ public static class DispatchCatcherN extends Catcher0 { /** Constructs a DispatchCatcherN. */ public DispatchCatcherN( RegistryN registry, String propertyName ) { this.registry = registry; this.propertyName = propertyName; } private final RegistryN registry; final String propertyName; /** Notifies the registry that the listener * is {@linkplain Registry#disabledPropertyChangeListener disabled}. */ public @Override void catchException( PropertyChangeListenerR listener, Exception x ) { // LoggerX.i(registry.getClass()).info( "disabling listener, it threw: " + x ); // LoggerX.i(registry.getClass()).log( LoggerX.FINEST, /*message*/"", x ); final java.util.logging.Logger logger = LoggerX.i( getClass() ); // can later dot-append registry.getClass(), to allow finer config logger.info( "disabling listener, it threw: " + x ); logger.log( LoggerX.FINEST, /*message*/"", x ); try{ registry.disabledPropertyChangeListener( propertyName, listener ); } // catch( RemoteException xR ) { LoggerX.i(registry.getClass()).log( LoggerX.WARNING, /*message*/"", xR ); } catch( RemoteException xR ) { logger.log( LoggerX.WARNING, /*message*/"", xR ); } } } // ==================================================================================== /** Where remote listeners can register for property change events, * per {@linkplain PropertyChangeListenerX.Registry PropertyChangeListenerX.Registry}. */ public interface Registry extends Remote { // - P r o p e r t y - C h a n g e - L i s t e n e r - R . R e g i s t r y ------------ /** Registers a listener to receive property change events. * The effect is immediate; the listener receives events * for each subsequent property change. */ public @ThreadSafe void addPropertyChangeListener( PropertyChangeListenerR listener ) throws RemoteException; /** Signals that a previously registered listener is no longer needed. * In response, the registry will eventually unregister it. * But it must expect to receive stray events, meantime. */ public @ThreadSafe void disabledPropertyChangeListener( PropertyChangeListenerR listener ) throws RemoteException; } // ==================================================================================== /** Where remote listeners can register for property change events, by property name, * per {@linkplain PropertyChangeListenerX.RegistryN PropertyChangeListenerX.RegistryN}. */ public interface RegistryN extends PropertyChangeListenerR.Registry { // - P r o p e r t y - C h a n g e - L i s t e n e r - R . R e g i s t r y - N -------- /** Registers a listener to receive property change events, by property name. * The effect is immediate; the listener receives events * for each subsequent property change. */ public @ThreadSafe void addPropertyChangeListener( String propertyName, PropertyChangeListenerR listener ) throws RemoteException; /** Signals that a previously registered listener is no longer needed. * In response, the registry will eventually unregister it. * But it must expect to receive stray events, meantime. */ public @ThreadSafe void disabledPropertyChangeListener( String propertyName, PropertyChangeListenerR listener ) throws RemoteException; } // ==================================================================================== /** A wrapper of PropertyChangeListenerR * that conforms it to PropertyChangeListener. */ public static @ThreadSafe final class WrapperL extends EventListenerR.WrapperL implements PropertyChangeListener { /** Constructs a WrapperL. * * @param remoteListener underlying remote listener, * per {@linkplain textbender.g.util.EventListenerR.WrapperL#remoteListener() remoteListener}() * @param catcher for any errors or exceptions that occur * during event dispatch */ public WrapperL( PropertyChangeListenerR remoteListener, Catcher catcher ) { super( remoteListener ); if( catcher == null ) throw new NullPointerException(); this.catcher = catcher; } private final Catcher catcher; // - P r o p e r t y - C h a n g e - L i s t e n e r ------------------------------ /** Relays the event to the underlying remote listener. */ public void propertyChange( PropertyChangeEvent e ) { try{ remoteListener.propertyChange( e ); } catch( Error r ) { catcher.catchError( remoteListener, r ); } catch( Exception x ) { catcher.catchException( remoteListener, x ); } } } }