001    /*
002     * DestinationValidator.java
003     *
004     * Developed for the "Rethinking CS101" project. See http://www.cs101.org, the
005     * CS101 homepage or email las@olin.edu.
006     *
007     * Created on February 24, 2003, 9:08 AM
008     */
009    
010    package nodenet.registrar;
011    
012    import nodenet.NodeConfigurationBean;
013    
014    import nodenet.registrar.NodeNameRegistry;
015    
016    import java.io.Serializable;
017    import java.io.ObjectStreamException;
018    
019    import java.beans.PropertyChangeEvent;
020    import java.beans.PropertyVetoException;
021    import java.beans.VetoableChangeListener;
022    
023    /**
024     * A quick implementation of a class that validates destination values
025     * for <code>NodeConfigurationBeans</code>. It is a singleton class of
026     * which there should never be more than one instance.
027     *
028     * FIXME: This should be broken out to a strategy pattern similar to
029     * the <code>NodeNameRegistry</code> class. This class is not in current use
030     * but may be used in future versions.
031     *
032     * @author  Patrick G. Heck, gus.heck@olin.edu
033     * @version $Id: DestinationValidator.java,v 1.6 2004/01/14 20:23:21 gus Exp $
034     */
035    public class DestinationValidator implements VetoableChangeListener, Serializable {
036        
037        private static final DestinationValidator SINGLETON = new DestinationValidator();
038        
039        /** Creates a new instance of DestinationValidator */
040        private DestinationValidator() {
041        }
042        
043        /**
044         * Get the one, the only, the DestinationValidator.
045         *
046         * @return The exact same DestinationValidator object on every call within
047         *         a given instance of the VM.
048         */
049        
050        public static DestinationValidator getInstance() {
051            return SINGLETON;
052        }
053        
054        /**
055         * Decide whether or not to veto a change to the dest property of a
056         * <code>NodeConfigurationBean</code>.
057         */
058        public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
059            if (pce.getPropertyName().equals(NodeConfigurationBean.PROP_DEST)) {
060                if (!NodeNameRegistry.getInstance().isRegistered((String)pce.getNewValue()) &&
061                !pce.getNewValue().equals("")) {
062                    
063                    throw new PropertyVetoException("Destination Node '" +
064                    pce.getNewValue()+"' doesn't exist!",pce);
065                }
066            }
067            
068        }
069        
070        // This allows us to deserialize objects containing a reference to the
071        // NodeNameRegistry and never cause duplicate registries. This technique
072        // means that when nodes are deserialized they need to re-register
073        // with the existing registry.
074        private Object readResolve() throws ObjectStreamException {
075            return SINGLETON;
076        }
077        
078    }
079    
080    /*
081     * $Log: DestinationValidator.java,v $
082     * Revision 1.6  2004/01/14 20:23:21  gus
083     * Javadoc and comment cleanup
084     *
085     * Revision 1.5  2003/02/25 22:33:28  gus
086     * javadoc enhancements
087     *  Modified Files:
088     *      nodenet/NodeConfigurationBean.form
089     *      nodenet/NodeConfigurationBean.java
090     *      nodenet/registrar/DestinationValidator.java
091     *      nodenet/registrar/UniqueNodeNamePolicy.java
092     *
093     * Revision 1.4  2003/02/25 22:17:30  gus
094     * javadocs
095     *
096     * Revision 1.3  2003/02/25 16:02:49  gus
097     * Make this class serializeable so we can save things again
098     *
099     * Revision 1.2  2003/02/24 16:27:47  gus
100     * added Log comment
101     *
102     */