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.samples;
019    
020    import java.io.FileInputStream;
021    import java.io.IOException;
022    import java.util.Properties;
023    
024    import org.otfeed.IConnection;
025    import org.otfeed.OTConnectionFactory;
026    import org.otfeed.event.IConnectionStateListener;
027    import org.otfeed.event.OTError;
028    import org.otfeed.event.OTHost;
029    
030    /**
031     * Utility class: connects to the OpenTick server. 
032     * Uses System properties to configure the connection.
033     */
034    public final class Connector {
035            private Connector() { }
036            
037            private static final IConnectionStateListener connectionStateListener = new IConnectionStateListener() {
038    
039                    public void onConnected() {
040                            System.err.println("Connected");
041                    }
042    
043                    public void onConnecting(OTHost host) {
044                            System.err.println("Connecting to: " + host);
045                    }
046    
047                    public void onError(OTError error) {
048                            System.err.println("Connection terminated: " + error);
049                    }
050    
051                    public void onLogin() {
052                            System.err.println("Logged in");
053                    }
054    
055                    public void onRedirect(OTHost host) {
056                            System.err.println("Redirected to: " + host);
057                    }
058            };
059            
060            public static final IConnection connect() throws IOException {
061                    OTConnectionFactory factory = new OTConnectionFactory();
062    
063                    Properties properties = System.getProperties();
064                    
065                    String config = properties.getProperty("config");
066                    if(config != null) {
067                            properties = new Properties();
068    
069                            try {
070                                    System.err.println("Loading properties from: " + config);
071                                    properties.load(new FileInputStream(config));
072                            } catch(IOException ex) {
073                                    System.err.println("Error reading config file [" + config + "]: " + ex);
074                                    System.exit(-1);
075                            }
076                    }
077                    
078                    String host = properties.getProperty("host");
079                    String user = properties.getProperty("username");
080                    String pass = properties.getProperty("password");
081                    if(host == null || user == null || pass == null) {
082                            System.err.println("Can not find host, port, usernane, or password property. Can not continue");
083                            System.err.println("Please, create a simple property file, specifying the connection parameters");
084                            System.err.println(" host, port, username, password");
085                            System.err.println("and pass it to java, using -Dconfig=<filename> syntax.");
086                            System.err.println("Alternatively, you can specify all necesary properties on the command line");
087                            System.err.println("for example: -Dhost=feed1.opentick.com -Dport=10010 -Dusername=shoot -Dpassword=shootme");
088                            System.exit(-1);
089                    }
090                    int port = Integer.parseInt(properties.getProperty("port"));
091                    
092                    factory.getHosts().add(new OTHost(host, port));
093                    factory.setUsername(user);
094                    factory.setPassword(pass);
095                    
096                    return factory.connect(null, connectionStateListener);
097            }
098    }