public static String serviceConfigToXml(final ServiceConfig serviceConfig)
     throws CodeGenFailedException {
   final StringWriter writer = new StringWriter();
   try {
     final JavaXmlBinder javaXmlBinder = JavaXmlBindingFactory.getInstance();
     javaXmlBinder.generateServiceConfigXml(serviceConfig, writer);
   } catch (final Exception ex) {
     throw new CodeGenFailedException("Failed to generate Service Config xml file", ex);
   }
   return writer.toString();
 }
  public static ServiceConfig parseServiceConfig(String filePath) throws Exception {

    ServiceConfig serviceConfig = null;

    try {
      File xmlFile = new File(filePath);

      JavaXmlBinder javaXmlBinder = JavaXmlBindingFactory.getInstance();
      serviceConfig = (ServiceConfig) javaXmlBinder.unmarshal(xmlFile, ServiceConfig.class);
    } catch (Exception ex) {
      String errMsg = "Failed to parse service config xml : " + filePath;
      throw new Exception(errMsg, ex);
    }

    return serviceConfig;
  }
  public static ClientConfigList parseClientConfig(String filePath) throws Exception {

    ClientConfigList clientCfgList = null;

    try {
      File xmlFile = new File(filePath);

      JavaXmlBinder javaXmlBinder = JavaXmlBindingFactory.getInstance();
      clientCfgList = (ClientConfigList) javaXmlBinder.unmarshal(xmlFile, ClientConfigList.class);
    } catch (Exception ex) {
      String errMsg = "Failed to parse client config xml : " + filePath;
      throw new Exception(errMsg, ex);
    }

    return clientCfgList;
  }
  public static void generateClientConfigXml(
      ClientConfigList clientCfgList, String destLoc, String configFileName)
      throws CodeGenFailedException {

    Writer fileWriter = null;
    try {
      fileWriter = CodeGenUtil.getFileWriter(destLoc, configFileName);

      JavaXmlBinder javaXmlBinder = JavaXmlBindingFactory.getInstance();
      javaXmlBinder.generateClientConfigXml(clientCfgList, fileWriter);

    } catch (Exception ex) {
      throw new CodeGenFailedException("Failed to generate Client Config xml file", ex);
    } finally {
      CodeGenUtil.closeQuietly(fileWriter);
    }
  }
  public static ServiceConfig parseServiceConfig(InputStream input) throws Exception {

    ServiceConfig serviceConfig = null;

    try {
      JavaXmlBinder javaXmlBinder = JavaXmlBindingFactory.getInstance();
      serviceConfig = (ServiceConfig) javaXmlBinder.unmarshal(input, ServiceConfig.class);
    } catch (Exception ex) {
      String errMsg = "Failed to parse service config xml : " + input;
      throw new Exception(errMsg, ex);
    } finally {
      CodeGenUtil.closeQuietly(
          input); // TODO: do not close here, let the calling method close its own inputstream.
    }

    return serviceConfig;
  }
  public static ClientConfigList parseClientConfig(InputStream input) throws Exception {

    ClientConfigList clientCfgList = null;

    try {
      JavaXmlBinder javaXmlBinder = JavaXmlBindingFactory.getInstance();
      clientCfgList = (ClientConfigList) javaXmlBinder.unmarshal(input, ClientConfigList.class);
    } catch (Exception ex) {
      String errMsg = "Failed to parse client config xml : " + input;
      throw new Exception(errMsg, ex);
    } finally {
      CodeGenUtil.closeQuietly(
          input); // TODO: do not close here, let the calling method close its own inputstream.
    }

    return clientCfgList;
  }
  public static void generateSecurityPolicyXml(
      ServiceSecurityConfig securityPolicyConfig, String destLoc, String configFileName)
      throws CodeGenFailedException {

    Writer fileWriter = null;
    try {
      fileWriter = CodeGenUtil.getFileWriter(destLoc, configFileName);

      JavaXmlBinder javaXmlBinder = JavaXmlBindingFactory.getInstance();
      javaXmlBinder.generateSecurityPolicyXml(securityPolicyConfig, fileWriter);

    } catch (Exception ex) {
      throw new CodeGenFailedException("Failed to generate Security Policy Config xml file", ex);
    } finally {
      CodeGenUtil.closeQuietly(fileWriter);
    }
  }