001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     */
017    
018    package org.otfeed.support;
019    
020    import java.io.PrintWriter;
021    
022    import org.otfeed.event.IConnectionStateListener;
023    import org.otfeed.event.OTError;
024    import org.otfeed.event.OTHost;
025    import org.otfeed.protocol.ErrorEnum;
026    
027    /**
028     * Implementation of {@link IConnectionStateListener}
029     * that prints events to the <code>PrintWriter</code>.
030     * Default destination for printing is standard error 
031     * output stream.
032     */
033    public class SimpleConnectionStateListener implements IConnectionStateListener {
034    
035            // set autoflush to true
036            private PrintWriter out = new PrintWriter(System.err, true);
037    
038            /**
039             * Determines the print destination.
040             * default is System.err.
041             * 
042             * @return output destination.
043             */
044            public PrintWriter getPrintWriter() {
045                    return out;
046            }
047    
048            /**
049             * Sets the destination.
050             * 
051             * @param val destination.
052             */
053            public void setPrintWriter(PrintWriter val) {
054                    out = val;
055            }
056    
057            public void onConnected() {
058                    out.println("Connected");
059            }
060    
061            public void onConnecting(OTHost arg0) {
062                    out.println("Connecting to: " + arg0);
063            }
064    
065            public void onError(OTError arg0) {
066                    if(arg0.getCode() == ErrorEnum.E_OTFEED_OK.code) {
067                            out.println("All done");
068                    } else {
069                            out.println("Error: " + arg0);
070                    }
071            }
072    
073            public void onLogin() {
074                    out.println("Logged in");
075            }
076    
077            public void onRedirect(OTHost arg0) {
078                    out.println("Redirected to: " + arg0);
079            }
080    }