Friday, September 10, 2010

Migrate Synapse Configuration from 1.2 to 2.0

The current namespace of the synapse-2.0 trunk is http://synapse.apache.org/ns/2010/04/configuration. The earlier namespace of synapse-1.2 was http://ws.apache.org/ns/synapse. Therefore you may encounter some errors, when running a synapse configuration of a 1.2 release against a trunk build(ie. synapse-2.0.0-SNAPSHOT). Since the Synapse configuration has undergone some configuration changes, merely changing the namespace of the the old synapse configuration wont help you to solve your problem.

That's why Synapse ships a migration tool for migrating a 1.2 configuration to a 2.0 configuration. Ruwan has written the xslt transformation for the above. You can find it in the Synapse trunk.

Recently I had to update the Synapse sample configurations of WSO2 ESB's trunk. This tool made my life a lot easier during the migration process. I modified the sample migrator to suit my purpose. I am sharing the code here, if you need to migrate the synapse configurations in bulk.

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
*
*/
public class ConfigurationMigrator {

private static final String MIGRATOR_XSLT_PATH
= "modules/migrator/src/main/resources/synapse-configuration-migrator.xslt";

public static int confgNumber [] = new int []
{0,1,2,3,4,5,6,7,8,9,10,11,
50,51,52,53,54,55,56,57,58,
100,101,102,
150,151,152,153,154,155,
200,201,202,
250,251,252,253,254,255,256,257,258,259,
260,261,262,264,265,
300,
350,351,352,353,354,
360,361,362,363,
370,371,372,
380,381,
390,391,
400,420,430,
500,501,502,503,504,
550,551,
600,601,602,603,604,605,606,
652,654};

public static void doTransform(String xmlFile, String xslFile, String outFile)
throws TransformerException, IOException {

FileReader xslFileReader = new FileReader(xslFile);
StreamSource xslStreamSource = new StreamSource(xslFileReader);

FileReader xmlFileReader = new FileReader(xmlFile);
StreamSource xmlStreamSource = new StreamSource(xmlFileReader);

FileWriter outFileWriter = new FileWriter(outFile);
StreamResult outStreamResult = new StreamResult(outFileWriter);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xslStreamSource);

transformer.transform(xmlStreamSource, outStreamResult);
outFileWriter.flush();
}

public static void migrateAllConfigs(String source, String destination){
System.out.println("\n\t#######################################################");
System.out.println("\t# Apache Synapse - Configuration Migration #");
System.out.println("\t#######################################################");

System.out.println("\n[INFO] Migration STARTED");

for (int aConfgNumber : confgNumber) {
try {
System.out.println("[INFO]Migrating synapse configuration : " + aConfgNumber);
doTransform(source + aConfgNumber + ".xml", MIGRATOR_XSLT_PATH, destination + aConfgNumber +".xml");

} catch (TransformerException e) {
handleException("Migration FAILED\n\t" + e.toString());
} catch (IOException e) {
handleException("Migration FAILED\n\t" + e.toString());
}
}

System.out.println("[INFO] Migration COMPLETED");

}

public static void main(String args[]) {
migrateAllConfigs("/home/heshan/Dev/trunk/carbon/products/esb/modules/samples/src/main/conf/synapse/synapse_sample_",
"/tmp/synapse-config/synapse_sample_");
}

private static void handleException(String message) {
System.out.println("[ERROR] " + message);
}
}

No comments: