Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package in.shop2020.googleadwords.service;

import in.shop2020.googleadwords.GoogleAdwordsService.Iface;
import in.shop2020.googleadwords.GoogleAdwordsService.Processor;
import in.shop2020.googleadwords.service.handler.AdwordsServiceHandler;
import in.shop2020.thrift.clients.config.ConfigClient;
import in.shop2020.utils.ConfigClientKeys;

import org.apache.commons.daemon.Daemon;
import org.apache.commons.daemon.DaemonContext;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.server.TThreadPoolServer.Args;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AdwordsServer implements Daemon {
        
        private static Logger logger = LoggerFactory.getLogger(AdwordsServer.class);
        
        private static AdwordsServiceHandler handler;
        
        private static Processor<Iface>       processor;
    
    private static TServer server;
    
    public static String dbHost;
        
    public static void main(String[] args) {
        initialize();
    }
    
    private static void initialize() {
        try {
                
                dbHost = "jdbc:mysql://" + ConfigClient.getClient().get(ConfigClientKeys.adwords_service_db_hostname.toString()) + "/catalog";
            handler = new AdwordsServiceHandler();
                        processor = new Processor<Iface>(handler);
            int port = 9229;
            try {
                String portNo = ConfigClient.getClient().get(
                        ConfigClientKeys.adwords_service_server_port
                                .toString());
                port = Integer.parseInt(portNo);
            } catch (Exception e) {
                logger.warn(
                        "Unable to get port number from the Config server because of:",
                        e);
            }
            
            logger.info("DB Connection String is: " + dbHost);
                logger.info("URL read by data source before setting is: " + handler.getDataSourceUrl());
                handler.setDataSourceUrl(dbHost);
                logger.info("URL read by data source after setting is: " + handler.getDataSourceUrl());


            TServerTransport serverTransport = new TServerSocket(port);
            TTransportFactory tFactory = new TFramedTransport.Factory();
            TProtocolFactory pFactory = new TBinaryProtocol.Factory();
            
                Args serverParams = new Args(serverTransport);
                        serverParams.processor(processor);
                        serverParams.transportFactory(tFactory);
                        serverParams.protocolFactory(pFactory);
                        
                        server = new TThreadPoolServer(serverParams);

            logger.info("Adwords service started on port 9229");
            server.serve();
        } catch (Exception ex) {
            logger.error("Error starting server", ex);
        }
    }
        
        public void destroy() {
                System.out.println("AdwordsServer stopped.");
        }

        public void init(DaemonContext arg0) throws Exception {
                System.out.println("Initializing AdwordsServer...");
        }

        public void start() throws Exception {
                System.out.println("Starting Adwords"); 
                initialize();
        }

        public void stop() throws Exception {
                System.out.println("Stopping CRMServer...");
        server.stop();
        System.exit(0);
        }
        
}