package be.ibridge.kettle.cluster;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Properties;

import be.ibridge.kettle.core.SharedObjects;
import be.ibridge.kettle.core.exception.KettleXMLException;

/**
 * The program generates a piece of XML that defines a (shared) Cluster Schema
 * 
 * @author Matt
 *
 */
public class GenerateClusterSchema
{
    public static final String PREFIX = "SLAVE_SERVER_";
    public static final String PORT = "_PORT";
    public static final String IP = "_IP";
    
    /**
     * @param args <br> 
     *    - the properties file to read
     *    - the shared file to write to
     *    - the name of the cluster schema
     *    - 
     * @throws IOException 
     * @throws FileNotFoundException 
     * @throws KettleXMLException 
     */
    public static void main(String[] args) throws FileNotFoundException, IOException, KettleXMLException
    {
        Properties properties = new Properties();
        properties.load(new FileInputStream(new File(args[0])));

        SharedObjects sharedObjects = new SharedObjects(args[1], new ArrayList(), new Hashtable());
        
        ClusterSchema clusterSchema = new ClusterSchema();
        clusterSchema.setName(args[2]);
        clusterSchema.setBasePort("40000");
        clusterSchema.setSocketsBufferSize("100000");
        clusterSchema.setSocketsFlushInterval("0");
        clusterSchema.setSocketsCompressed(true);
        
        int max = 1;
        while (properties.getProperty(PREFIX+max+IP)!=null) max++;
        max--;
        
        for (int i=1;i<=max;i++)
        {
            String serverIp   = properties.getProperty(PREFIX+i+IP);
            String serverPort = properties.getProperty(PREFIX+i+PORT);
            
            if (i==1) // use the first as the master
            {
                // add the master
                clusterSchema.getSlaveServers().add(new SlaveServer(serverIp, serverPort, "cluster", "cluster", null, null, null, true));
                
                if (max==1) // if there is just one server here, so we add a slave too besides the master 
                {
                    clusterSchema.getSlaveServers().add(new SlaveServer(serverIp, serverPort, "cluster", "cluster"));
                }
            }
            else
            {
                // Add a slave server
                clusterSchema.getSlaveServers().add(new SlaveServer(serverIp, serverPort, "cluster", "cluster"));
            }
        }
       
        sharedObjects.storeObject(clusterSchema);
        sharedObjects.saveToFile();
    }
}

@@ -290,16 +290,133 @@
 jects;%0D%0A
+import be.ibridge.kettle.core.database.DatabaseMeta;%0D%0Aimport be.ibridge.kettle.core.database.PartitionDatabaseMeta;%0D%0A
 import b
@@ -468,16 +468,16 @@
 ption;%0D%0A
-
 %0D%0A/**%0D%0A 
@@ -1404,32 +1404,158 @@
 ());%0D%0A        %0D%0A
+        DatabaseMeta mysql = new DatabaseMeta(%22MySQL EC2%22, %22MySQL%22, %22JDBC%22, null, %22test%22, %223306%22, %22matt%22, %22abcd%22);%0D%0A        %0D%0A
         ClusterS
@@ -1961,32 +1961,157 @@
 x--;%0D%0A        %0D%0A
+        mysql.setPartitioned(true);%0D%0A        PartitionDatabaseMeta%5B%5D partDbMeta = new PartitionDatabaseMeta%5Bmax%5D;%0D%0A        %0D%0A
         for (int
@@ -2534,24 +2534,165 @@
 l, true));%0D%0A
+                mysql.setHostname(serverIp);%0D%0A                partDbMeta%5Bi-1%5D = new PartitionDatabaseMeta(%22P%22+i, serverIp, %223306%22, %22test%22);%0D%0A
             
@@ -3191,32 +3191,32 @@
     %7D%0D%0A       %0D%0A
-
         sharedOb
@@ -3246,24 +3246,142 @@
 erSchema);%0D%0A
+        %0D%0A        mysql.setPartitioningInformation(partDbMeta);%0D%0A        sharedObjects.storeObject(mysql);%0D%0A        %0D%0A
         shar

Generate db partitions as well