View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.example.errorhandler;
8   
9   import org.mule.api.DefaultMuleException;
10  import org.mule.api.lifecycle.FatalException;
11  import org.mule.api.transformer.TransformerException;
12  import org.mule.config.i18n.MessageFactory;
13  import org.mule.example.errorhandler.exceptions.BusinessException;
14  import org.mule.module.xml.transformer.ObjectToXml;
15  import org.mule.util.FileUtils;
16  
17  import java.io.IOException;
18  
19  public class ErrorHandlerTestDataGenerator
20  {
21  
22      public static void generateTestData(String targetDir) throws IOException, TransformerException
23      {
24          if (!(targetDir.endsWith("/") || targetDir.endsWith("\\")))
25          {
26              targetDir += "/";
27          }
28  
29          ObjectToXml trans = new ObjectToXml();
30          DefaultMuleException exception = new DefaultMuleException(MessageFactory.createStaticMessage("Some default exception"));
31          FatalException fatal = new FatalException(MessageFactory.createStaticMessage("Some fatal exception"),
32              new IOException("Some IO exception"));
33          BusinessException business = new BusinessException("Some business exception");
34  
35          ExceptionBean bean = new ExceptionBean(exception);
36          String xml = (String)trans.transform(bean);
37          FileUtils.stringToFile(targetDir + "DefaultMuleException.xml", xml);
38  
39          bean = new ExceptionBean(fatal);
40          xml = (String)trans.transform(bean);
41          FileUtils.stringToFile(targetDir + "FatalException.xml", xml);
42  
43          bean = new ExceptionBean(business);
44          xml = (String)trans.transform(bean);
45          FileUtils.stringToFile(targetDir + "BusinesException.xml", xml);
46      }
47  
48      public static void main(String[] args)
49      {
50  
51          if (args.length == 0)
52          {
53              System.out.println("You must specifiy a target directory for the output files");
54              System.exit(1);
55          }
56          String path = args[0];
57          try
58          {
59              generateTestData(path);
60          }
61          catch (Exception e)
62          {
63              e.printStackTrace();
64          }
65  
66      }
67  }