package es.tid.bdp.platform.cluster.server;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.util.RunJar;
import org.apache.thrift.TException;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.server.TThreadPoolServer.Args;
import org.apache.thrift.transport.TServerSocket;

/**
 *
 * @author dmicol
 */
public class ClusterServer implements Cluster.Iface {
    // TODO: put this in a configuration file
    private static final String HDFS_URL = "hdfs://pshdp01:8011";

    public static void main(String args[]) {
        ClusterServer server = new ClusterServer();
        int res = server.start();
        System.exit(res);
    }

    private int start() {
        try {
            ClusterServer cluster = new ClusterServer();
            TServerSocket serverTransport = new TServerSocket(9888);
            Cluster.Processor processor = new Cluster.Processor(cluster);
            Args args = new Args(serverTransport);
            args.processor(processor);
            TServer server = new TThreadPoolServer(args);
            server.serve();
            return 0;
        } catch (Throwable ex) {
            System.err.println(ex.getMessage());
            return 1;
        }
    }

    @Override
    public void copyToHdfs(String src, String dest) throws TException {
        try {
            Configuration config = new Configuration();
            config.set("fs.default.name", HDFS_URL);
            FileSystem fs = FileSystem.get(config);
            fs.moveFromLocalFile(new Path(src), new Path(dest));
        } catch (Exception ex) {
            throw new TException(ex);
        }
    }

    @Override
    public void runJob(String jarPath, String inputPath, String outputPath,
                       String mongoUrl) throws TException {
        try {
            // TODO: this should be done using Jobs, to get tracking info
            RunJar.main(new String[] { jarPath, inputPath,
                                       outputPath, mongoUrl });
        } catch (Throwable ex) {
            throw new TException(ex);
        }
    }
}

@@ -668,24 +668,42 @@
 g args%5B%5D) %7B%0A
+        try %7B%0A    
         Clus
@@ -754,17 +754,11 @@
     
-int res =
+   
  ser
@@ -782,25 +782,70 @@
     
-System.exit(res);
+%7D catch (Throwable ex) %7B%0A            System.exit(1);%0A        %7D
 %0A   
@@ -860,19 +860,20 @@
 private 
-int
+void
  start()
@@ -873,36 +873,35 @@
 start() 
-%7B%0A        try %7B%0A    
+throws Throwable %7B%0A
         
@@ -949,28 +949,24 @@
 ();%0A        
-    
 TServerSocke
@@ -1010,20 +1010,16 @@
 (9888);%0A
-    
         
@@ -1088,20 +1088,16 @@
         
-    
 Args arg
@@ -1135,20 +1135,16 @@
         
-    
 args.pro
@@ -1166,28 +1166,24 @@
 r);%0A        
- 
-   
 TServer serv
@@ -1216,36 +1216,32 @@
 (args);%0A        
-    
 server.serve();%0A
@@ -1244,144 +1244,8 @@
 ();%0A
-            return 0;%0A        %7D catch (Throwable ex) %7B%0A            System.err.println(ex.getMessage());%0A            return 1;%0A        %7D%0A
     

Improve the cluster server code.