Пример #1
0
/**
 * Provides utility methods for code generation tools.
 *
 * @author rmandapati
 */
public class CodeGenUtil {

  public static final String HTTP = "http://";
  public static final char PACKAGE_CLASS_DELIMITER = '.';

  private static CallTrackingLogger logger = LogManager.getInstance(CodeGenUtil.class);

  private static CallTrackingLogger getLogger() {
    return logger;
  }

  public static boolean isEmptyString(String str) {
    return (str == null || str.trim().length() == 0);
  }

  public static String toQualifiedClassName(String javaFilePath) {
    String filePathNoExt = null;
    if (javaFilePath == null) {
      return null;
    }

    boolean isJavaExtensionPresent = javaFilePath.endsWith(".java");
    if (isJavaExtensionPresent) {
      filePathNoExt = javaFilePath.substring(0, javaFilePath.length() - 5);
    }
    boolean isClassExtensionPresent = javaFilePath.endsWith(".class");
    if (isClassExtensionPresent) {
      filePathNoExt = javaFilePath.substring(0, javaFilePath.length() - 6);
    }

    if (filePathNoExt == null) filePathNoExt = javaFilePath;
    return filePathNoExt.replace('\\', '.').replace('/', '.');
  }

  public static String getQualifiedClassName(String javaFilePath, String srcLocation) {
    int pkgStartPos = javaFilePath.indexOf(srcLocation);
    String qualifiedJavaFile = null;
    if (pkgStartPos > -1) {
      String normalizedSrcLoc = CodeGenUtil.normalizePath(srcLocation);
      int startPos = pkgStartPos + normalizedSrcLoc.length();
      qualifiedJavaFile = javaFilePath.substring(startPos);
    } else {
      qualifiedJavaFile = javaFilePath;
    }

    return toQualifiedClassName(qualifiedJavaFile);
  }

  public static String getPackageName(String className) {
    int idx = className.lastIndexOf(".");
    if (idx <= 0) {
      return "";
    }
    return className.substring(0, idx);
  }
  /*
   * This method is changed to private
   * Since hardcoding of "\\" makes it as  OS dependent.
   * Use toOSFilePath() instead of this method.
   */
  private static String normalizePath(String path) {
    if (path == null) {
      return null;
    }

    if (path.endsWith("\\") || path.endsWith("/")) {
      return path;
    } else {
      return path + File.separatorChar;
    }
  }

  public static String toOSFilePath(String path) {
    if (path == null) {
      return null;
    }
    String normaliedOSPath =
        path.replace('\\', File.separatorChar).replace('/', File.separatorChar);

    return normalizePath(normaliedOSPath);
  }

  public static String getFilePath(String dir, String fileName) {
    if (dir == null || fileName == null) {
      return null;
    }

    String filePath = toOSFilePath(dir) + fileName;

    return filePath;
  }

  public static String toJavaSrcFilePath(String srcDir, Class<?> clazz) {
    if (srcDir == null || clazz == null) {
      return null;
    }

    String filePath = toJavaSrcFilePath(srcDir, clazz.getName());
    return filePath;
  }

  public static String toJavaSrcFilePath(String srcDir, String qualifiedJavaName) {
    if (srcDir == null || qualifiedJavaName == null) {
      return null;
    }
    String filePath = toOSFilePath(srcDir) + convertToJavaSrcFilePath(qualifiedJavaName);
    return filePath;
  }

  private static String convertToJavaSrcFilePath(String qualifiedJavaName) {
    if (isEmptyString(qualifiedJavaName)) {
      return qualifiedJavaName;
    }

    int dotJavaPos = qualifiedJavaName.lastIndexOf(".java");
    if (dotJavaPos > -1) {
      return convertToFilePath(qualifiedJavaName.substring(0, dotJavaPos), ".java");
    } else {
      return convertToFilePath(qualifiedJavaName, ".java");
    }
  }

  public static String convertToFilePath(String qualifiedJavaName, String suffix) {
    return qualifiedJavaName.replace('.', File.separatorChar) + suffix;
  }

  public static String normalizePackageName(String packageName) {
    if (isEmptyString(packageName) || !packageName.endsWith(".")) {
      return packageName;
    } else {
      return packageName.substring(0, packageName.length() - 1);
    }
  }

  public static boolean isParameterizedType(Type type) {
    return (type instanceof ParameterizedType);
  }

  public static boolean isWildCardType(Type type) {
    return (type instanceof WildcardType);
  }

  public static boolean isGenericArrayType(Type type) {
    return (type instanceof GenericArrayType);
  }

  public static String makeFirstLetterUpper(String str) {
    if (isEmptyString(str)) {
      return str;
    }

    char firstChar = str.charAt(0);
    if (Character.isLetter(firstChar) && Character.isLowerCase(firstChar)) {
      char[] chars = str.toCharArray();
      chars[0] = Character.toUpperCase(firstChar);
      return String.valueOf(chars);
    } else {
      return str;
    }
  }

  public static String makeFirstLetterLower(String str) {
    if (isEmptyString(str)) {
      return str;
    }

    char firstChar = str.charAt(0);
    if (Character.isLetter(firstChar) && Character.isUpperCase(firstChar)) {
      char[] chars = str.toCharArray();
      chars[0] = Character.toLowerCase(firstChar);
      return String.valueOf(chars);
    } else {
      return str;
    }
  }

  public static File getDir(String destDir) throws IOException {
    if (destDir == null) {
      return null;
    }

    File dir = new File(destDir);
    if (!dir.exists() || !dir.isDirectory()) {
      throw new IOException(destDir + ": non-existent directory");
    }

    return dir;
  }

  public static String genDestFolderPath(String destLoc, String serviceName, String suffixPath) {

    StringBuilder destFolderPath = new StringBuilder();

    destFolderPath.append(toOSFilePath(destLoc));
    destFolderPath.append(toOSFilePath(suffixPath));
    destFolderPath.append(serviceName);

    return destFolderPath.toString();
  }

  public static String genDestFolderPath(String destLoc, String suffixLoc) {
    if (isEmptyString(destLoc)) {
      return destLoc;
    }

    String destPath = toOSFilePath(destLoc);
    if (!isEmptyString(suffixLoc)) {
      destPath = destPath + toOSFilePath(suffixLoc);
    }
    return destPath;
  }

  public static String getNSFromPackageName(String packageName) {

    StringBuffer strBuf = new StringBuffer();
    int prevIndex = packageName.length();
    int currentIndex = packageName.lastIndexOf(PACKAGE_CLASS_DELIMITER);
    if (currentIndex > 0) {
      strBuf.append(HTTP);
    } else if (prevIndex > 0) {
      strBuf.append(HTTP);
      strBuf.append(packageName);
      return strBuf.toString();
    } else if (currentIndex == -1) {
      return strBuf.toString();
    }
    while (currentIndex != -1) {
      strBuf.append(packageName.substring(currentIndex + 1, prevIndex));
      prevIndex = currentIndex;
      currentIndex = packageName.lastIndexOf(PACKAGE_CLASS_DELIMITER, prevIndex - 1);
      strBuf.append(PACKAGE_CLASS_DELIMITER);

      if (currentIndex == -1) {
        strBuf.append(packageName.substring(0, prevIndex));
      }
    }
    return strBuf.toString();
  }

  public static File createDir(String dirPath) throws IOException {
    File dir = new File(dirPath);
    if (dir.exists()) {
      // It exists. all done.
      return dir;
    }

    if (dir.mkdirs() == false) {
      // Unable to create directories.
      throw new IOException("Failed to create dir(s) : " + dirPath);
    }

    return dir;
  }

  public static boolean isFileExists(String filePath) {
    if (isEmptyString(filePath)) {
      return false;
    }

    File file = new File(filePath);
    return file.exists();
  }

  public static boolean dirExists(String path) {
    if (isEmptyString(path)) {
      return false;
    }

    File dir = new File(path);
    return dir.exists() && dir.isDirectory();
  }

  public static void deleteFile(File file) throws IOException {
    if (file == null || !file.exists()) {
      return;
    }

    if (!file.delete()) {
      throw new IOException("Can't delete file : " + file.getPath());
    }
  }

  // Deletes all sub-dir, files under a dir,
  // and also deletes given dir
  public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
      String[] children = dir.list();
      for (int i = 0; i < children.length; i++) {
        boolean success = deleteDir(new File(dir, children[i]));
        if (!success) {
          return false;
        }
      }
    }

    // The directory is now empty so delete it
    return dir.delete();
  }

  // Deletes all sub-dir, files under a dir,
  // it does not deletes given dir
  public static boolean deleteContentsOfDir(File dir) {
    if (dir.isDirectory()) {
      String[] children = dir.list();
      for (int i = 0; i < children.length; i++) {
        boolean success = deleteDir(new File(dir, children[i]));
        if (!success) {
          return false;
        }
      }
    }
    return true;
  }

  public static Writer getFileWriter(String destDir, String fileName) throws IOException {

    if (isEmptyString(destDir) || isEmptyString(fileName)) {
      return null;
    }

    File dir = createDir(destDir);
    File outputFile = new File(dir, fileName);

    // delete previous file, if exists
    deleteFile(outputFile);

    Charset defaultCharset = Charset.defaultCharset();
    FileOutputStream fileOutStream = new FileOutputStream(outputFile);
    OutputStreamWriter bw = new OutputStreamWriter(fileOutStream, defaultCharset);
    Writer buffWriter = new BufferedWriter(bw);

    return buffWriter;
  }

  public static BufferedReader getFileReader(String destDir, String fileName) throws IOException {

    if (isEmptyString(destDir) || isEmptyString(fileName)) {
      return null;
    }

    File dir = createDir(destDir);
    File inFile = new File(dir, fileName);

    Charset defaultCharset = Charset.defaultCharset();
    FileInputStream fileInStream = new FileInputStream(inFile);
    InputStreamReader bw = new InputStreamReader(fileInStream, defaultCharset);
    BufferedReader buffReader = new BufferedReader(bw);

    return buffReader;
  }

  public static OutputStream getFileOutputStream(String destDir, String fileName)
      throws IOException {

    if (isEmptyString(destDir) || isEmptyString(fileName)) {
      return null;
    }

    File dir = createDir(destDir);
    File outputFile = new File(dir, fileName);

    // delete previous file, if exists
    deleteFile(outputFile);

    FileOutputStream fileOutStream = new FileOutputStream(outputFile);
    return fileOutStream;
  }

  public static void closeQuietly(InputSource inputSource) {
    if (inputSource == null) {
      return; // nothing to do
    }

    closeQuietly(inputSource.getCharacterStream());
    closeQuietly(inputSource.getByteStream());
  }

  public static void closeQuietly(WSDLLocator locator) {
    if (locator == null) {
      return; // nothing to do
    }

    locator.close();
  }

  public static void closeQuietly(XMLStreamWriter writer) {
    if (writer == null) {
      return; // nothing to do
    }

    try {
      writer.close();
    } catch (XMLStreamException ignore) {
      /* ignore */
    }
  }

  public static void closeQuietly(JarFile jarfile) {
    if (jarfile == null) {
      return; // nothing to do
    }
    try {
      jarfile.close();
    } catch (IOException ignore) {
      /* ignore */
    }
  }

  public static void closeQuietly(FileHandler fileHandler) {
    if (fileHandler == null) {
      return; // nothing to do
    }
    fileHandler.close();
  }

  public static void closeQuietly(Closeable closeable) {
    if (closeable == null) {
      return; // nothing to do
    }
    try {
      closeable.close();
    } catch (IOException ignore) {
      /* ignore */
    }
  }

  public static void flushAndCloseQuietly(Closeable closeable) {
    if (closeable == null) {
      return; // nothing to do
    }

    try {
      if (closeable instanceof Flushable) {
        ((Flushable) closeable).flush();
      }
    } catch (IOException e) {
      /* ignore */
    }

    try {
      closeable.close();
    } catch (IOException ignore) {
      /* ignore */
    }
  }

  public static void move(String srcFilePath, String destLoc, boolean override) throws IOException {

    File srcFile = new File(srcFilePath);
    File destDir = createDir(destLoc);

    File newFile = new File(destDir, srcFile.getName());
    if (newFile.exists()) {
      if (override == false) {
        return;
      } else {
        deleteFile(newFile);
      }
    }

    // Move file to new directory
    boolean success = srcFile.renameTo(newFile);
    if (success == false) {
      throw new IOException("Failed to move file : " + srcFilePath + " to " + destLoc);
    }
  }

  public static void addAllFiles(File dir, List<String> files) {
    if (dir.isDirectory()) {
      if (!dir.getName().equals(".") && !dir.getName().equals("..")) {
        File[] children = dir.listFiles();
        for (int i = 0; i < children.length; i++) {
          if (children[i].isDirectory()) {
            addAllFiles(children[i], files);
          } else {
            files.add(children[i].getAbsolutePath());
          }
        }
      }
    }
  }

  public static String getTemplateContent(String templateName) throws IOException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream(templateName);
    if (inputStream == null) {
      throw new IOException("Failed to load resource : " + templateName);
    }

    String templateContent = readContent(inputStream);

    return templateContent;
  }

  private static String readContent(InputStream input) throws IOException {
    Charset defaultCharset = Charset.defaultCharset();
    InputStreamReader isr = new InputStreamReader(input, defaultCharset);
    BufferedReader reader = new BufferedReader(isr);

    StringBuilder strBuff = new StringBuilder();
    try {
      char[] charBuff = new char[512];
      int charsRead = -1;
      while ((charsRead = reader.read(charBuff)) > -1) {
        strBuff.append(charBuff, 0, charsRead);
      }
    } finally {
      reader.close();
    }
    return strBuff.toString();
  }

  public static String getFileContents(String filePath) throws IOException {
    FileInputStream fileInStream = new FileInputStream(filePath);
    return readContent(fileInStream);
  }

  public static void writeToFile(String destLoc, String fileName, String contents)
      throws IOException {

    Writer fileWriter = null;
    try {
      fileWriter = getFileWriter(destLoc, fileName);
      fileWriter.write(contents);
    } finally {
      closeQuietly(fileWriter);
    }
  }

  public static Map<String, String> createNS2PackageMap(InputOptions inputOptions) {

    Map<String, String> ns2PkgMap = new HashMap<String, String>();

    PkgToNSMappingList pkgNsMapList = inputOptions.getPkgNSMappings();
    if (pkgNsMapList != null && !pkgNsMapList.getPkgNsMap().isEmpty()) {
      for (PkgNSMappingType pkgNsMapType : pkgNsMapList.getPkgNsMap()) {
        ns2PkgMap.put(pkgNsMapType.getNamespace(), pkgNsMapType.getPackage());
      }
    }

    return ns2PkgMap;
  }

  public static String getJavaClassName(String className) {
    int idx = className.lastIndexOf(".");
    if (idx <= 0) {
      return "";
    }
    return className.substring(idx + 1);
  }

  public static String getFolderPathFrompackageName(String packageName) {
    if (packageName == null) return null;

    packageName = packageName.replace('.', File.separatorChar);
    return toOSFilePath(packageName);
  }

  public static File urlToFile(URL url) {
    File file = null;
    try {
      file = new File(url.toURI());
    } catch (Exception exception) {
      file = new File(url.getPath());
    }
    return file;
  }

  /**
   * Tries to get the input stream using the classloader used for the loading the class passed as
   * input param. If not found then it tries to load the file from the current Threads context
   * classloader
   *
   * @param relativeFilePath
   * @param parentClassLoader (optional)
   * @return
   */
  public static InputStream getInputStreamForAFileFromClasspath(
      String relativeFilePath, ClassLoader parentClassLoader) {

    relativeFilePath = relativeFilePath.replace("\\", "/");

    getLogger()
        .log(
            Level.INFO,
            "call to getInputStreamForAFileFromClasspath for path : " + relativeFilePath);

    InputStream inStream = null;
    if (parentClassLoader != null)
      inStream = parentClassLoader.getResourceAsStream(relativeFilePath);

    if (inStream == null) {
      ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader();
      inStream = myClassLoader.getResourceAsStream(relativeFilePath);
    }

    if (inStream == null)
      inStream = CodeGenUtil.class.getClassLoader().getResourceAsStream(relativeFilePath);

    if (inStream == null)
      getLogger()
          .log(
              Level.WARNING,
              "Could not find the file from classpath : "
                  + relativeFilePath
                  + "  in the method getInputStreamForAFileFromClasspath");
    else
      getLogger()
          .log(
              Level.INFO,
              "Found the file from classpath : "
                  + relativeFilePath
                  + "  in the method getInputStreamForAFileFromClasspath");

    return inStream;
  }

  public static File getFileFromInputStream(InputStream inputStream, String fileExtension) {

    FileOutputStream fileOutputStream = null;
    File file = null;
    try {

      file = File.createTempFile("ebayCodegen", fileExtension);

      byte[] bytes = new byte[10000];

      fileOutputStream = new FileOutputStream(file);

      int readCount = 0;
      while ((readCount = inputStream.read(bytes)) > 0) {
        fileOutputStream.write(bytes, 0, readCount);
      }

    } catch (IOException e) {
      getLogger()
          .log(
              Level.INFO,
              "exception while trying to create the tekmp file : exception is : " + e.getMessage());
    } finally {
      if (fileOutputStream != null) {
        try {
          fileOutputStream.close();
        } catch (IOException e) {
          getLogger()
              .log(Level.FINE, "Exception while closing the file outut stream for the file ");
        }
      }
    }

    return file;
  }
}
/**
 * Service client configuration generator.
 *
 * <p>Generates either default client configuration or configuration spcified by the user as input
 * in the xml file.
 *
 * @author rmandapati
 */
public class ClientConfigGenerator implements SourceGenerator {

  private static Logger s_logger = LogManager.getInstance(ClientConfigGenerator.class);

  private Logger getLogger() {
    return s_logger;
  }

  private static final String GEN_CLIENT_CONFIG_DIR = "META-INF/soa/client/config";
  private static final String CLIENT_CONFIG_FILE_NAME = "ClientConfig.xml";

  private static final String CLIENT_CONFIG_TEMPLATE =
      "org/ebayopensource/turmeric/tools/codegen/template/clientconfig.tpt";

  private static final String CONFIG_GROUP_NAME = "@@ClientConfigGroupName@@";
  private static final String CONFIG_GROUP_NAME_ATTR = "group=\"@@ClientConfigGroupName@@\"";
  private static final String CONFIG_SERVICE_NAME = "@@ServiceName@@";
  private static final String CONFIG_SERVICE_NAME_ATTR = "service-name=\"@@ServiceName@@\"";
  private static final String PREFERRED_TRANSPORT_NAME = "@@PreferredTransport@@";
  private static final String PREFERRED_TRANSPORT_NAME_ATTR = "name=\"@@PreferredTransport@@\"";

  private static final String SERVICE_INT_NAME = "@@ServiceInterfaceClassName@@";
  private static final String SERVICE_INT_NAME_NODE =
      "<service-interface-class-name>@@ServiceInterfaceClassName@@</service-interface-class-name>";
  private static final String WSDL_LOCATION = "@@WSDLLocation@@";
  private static final String WSDL_LOCATION_NODE =
      "<wsdl-location>@@WSDLLocation@@</wsdl-location>";
  private static final String SERVICE_LOCATION = "@@ServiceLocation@@";
  private static final String SERVICE_LOCATION_NODE =
      "<service-location>@@ServiceLocation@@</service-location>";

  private static final String REQ_DATA_BINDING = "@@RequestDataBinding@@";
  private static final String REQ_DATA_BINDING_NODE =
      "<request-data-binding>@@RequestDataBinding@@</request-data-binding>";
  private static final String RESP_DATA_BINDING = "@@ResponseDataBinding@@";
  private static final String RESP_DATA_BINDING_NODE =
      "<response-data-binding>@@ResponseDataBinding@@</response-data-binding>";
  private static final String CONSUMER_ID = "@@ConsumerId@@";
  private static final String CONSUMER_ID_NODE = "<consumer-id>@@ConsumerId@@</consumer-id>";

  private static final String INVOCATION_USE_CASE = "@@InvocationUseCase@@";
  private static final String INVOCATION_USE_CASE_NODE =
      "<invocation-use-case>@@InvocationUseCase@@</invocation-use-case>";

  private static ClientConfigGenerator s_clientConfigGenerator = new ClientConfigGenerator();

  private ClientConfigGenerator() {}

  public static ClientConfigGenerator getInstance() {
    return s_clientConfigGenerator;
  }

  public boolean continueOnError() {
    return false;
  }

  public void generate(CodeGenContext codeGenCtx) throws CodeGenFailedException {

    ClientConfigList clientCfgList = null;

    InputOptions inputOptions = codeGenCtx.getInputOptions();
    ServiceCodeGenDefType svcCodeGenDef = inputOptions.getSvcCodeGenDefType();
    // If config info is specified in XML
    if (svcCodeGenDef != null
        && svcCodeGenDef.getConfigInfo() != null
        && svcCodeGenDef.getConfigInfo().getClientCfg() != null) {
      ConfigType configType = svcCodeGenDef.getConfigInfo();
      clientCfgList = configType.getClientCfg();
    } else {
      clientCfgList = createClientConfigList(codeGenCtx);
    }

    generateClientConfigXml(codeGenCtx, clientCfgList);
  }

  private void generateClientConfigXml(CodeGenContext codeGenCtx, ClientConfigList clientCfgList)
      throws CodeGenFailedException {

    Writer fileWriter = null;
    try {
      if (CodeGenUtil.isEmptyString(codeGenCtx.getClientName()))
        codeGenCtx.getInputOptions().setClientName(codeGenCtx.getServiceAdminName());
      String destFolderPath =
          CodeGenUtil.genDestFolderPath(
              codeGenCtx.getMetaSrcDestLocation(), getSuffixPath(codeGenCtx.getClientName()));
      if (!CodeGenUtil.isEmptyString(codeGenCtx.getInputOptions().getEnvironment())) {
        destFolderPath =
            destFolderPath
                + codeGenCtx.getInputOptions().getEnvironment()
                + File.separator
                + codeGenCtx.getServiceAdminName()
                + File.separator;
        destFolderPath = CodeGenUtil.toOSFilePath(destFolderPath);
      }

      String contents = getUpdatedClientConfigTemplate(codeGenCtx, clientCfgList);

      fileWriter = CodeGenUtil.getFileWriter(destFolderPath, CLIENT_CONFIG_FILE_NAME);
      fileWriter.write(contents);

      getLogger()
          .log(
              Level.INFO,
              "Successfully generated " + CLIENT_CONFIG_FILE_NAME + " under " + destFolderPath);

    } catch (Exception ex) {
      getLogger().log(Level.SEVERE, "Error " + ex.toString());
      throw new CodeGenFailedException("Failed to generate Client Config xml file", ex);
    } finally {
      CodeGenUtil.flushAndCloseQuietly(fileWriter);
    }
  }

  private String getUpdatedClientConfigTemplate(
      CodeGenContext codeGenCtx, ClientConfigList clientCfgList) throws CodeGenFailedException {
    try {
      if (clientCfgList == null || clientCfgList.getClientConfig().size() == 0) {
        throw new CodeGenFailedException("The content of 'ClientConfigList' is empty.");
      }

      String contents = CodeGenUtil.getTemplateContent(CLIENT_CONFIG_TEMPLATE);
      ClientConfig clientConfig = clientCfgList.getClientConfig().get(0);

      contents =
          CodeGenConfigUtil.replaceTemplate(
              contents, CONFIG_GROUP_NAME, clientConfig.getGroup(), CONFIG_GROUP_NAME_ATTR);

      contents =
          CodeGenConfigUtil.replaceTemplate(
              contents,
              CONFIG_SERVICE_NAME,
              clientConfig.getServiceName(),
              CONFIG_SERVICE_NAME_ATTR);
      contents =
          CodeGenConfigUtil.replaceTemplate(
              contents,
              SERVICE_INT_NAME,
              clientConfig.getServiceInterfaceClassName(),
              SERVICE_INT_NAME_NODE);
      contents =
          CodeGenConfigUtil.replaceTemplate(
              contents, WSDL_LOCATION, clientConfig.getWsdlLocation(), WSDL_LOCATION_NODE);
      contents =
          CodeGenConfigUtil.replaceTemplate(
              contents, SERVICE_LOCATION, clientConfig.getServiceLocation(), SERVICE_LOCATION_NODE);

      ClientGroupConfig clientInstanceConfig = clientConfig.getClientInstanceConfig();
      InvocationOptionConfig invocationOptions = null;
      PreferredTransportConfig preferredTransport = null;

      if (clientInstanceConfig != null) {
        invocationOptions = clientConfig.getClientInstanceConfig().getInvocationOptions();
      }
      if (invocationOptions != null) {
        preferredTransport = invocationOptions.getPreferredTransport();

        contents =
            CodeGenConfigUtil.replaceTemplate(
                contents,
                REQ_DATA_BINDING,
                invocationOptions.getRequestDataBinding(),
                REQ_DATA_BINDING_NODE);
        contents =
            CodeGenConfigUtil.replaceTemplate(
                contents,
                RESP_DATA_BINDING,
                invocationOptions.getResponseDataBinding(),
                RESP_DATA_BINDING_NODE);
        contents =
            CodeGenConfigUtil.replaceTemplate(
                contents, CONSUMER_ID, invocationOptions.getConsumerId(), CONSUMER_ID_NODE);
        contents =
            CodeGenConfigUtil.replaceTemplate(
                contents,
                INVOCATION_USE_CASE,
                invocationOptions.getInvocationUseCase(),
                INVOCATION_USE_CASE_NODE);
      }

      String transportName = null;
      if (preferredTransport != null) {
        transportName = preferredTransport.getName();
      }
      contents =
          CodeGenConfigUtil.replaceTemplate(
              contents, PREFERRED_TRANSPORT_NAME, transportName, PREFERRED_TRANSPORT_NAME_ATTR);
      return contents;
    } catch (Throwable e) {
      throw new CodeGenFailedException(
          "Failed in retriveing the client config template "
              + CLIENT_CONFIG_TEMPLATE
              + e.getMessage(),
          e);
    }
  }

  private ClientConfigList createClientConfigList(CodeGenContext codeGenCtx) {

    ClientConfigList clientCfgList = new ClientConfigList();

    ClientConfig clientCfg = createClientConfig(codeGenCtx);
    clientCfgList.getClientConfig().add(clientCfg);

    return clientCfgList;
  }

  private ClientConfig createClientConfig(CodeGenContext codeGenCtx) {

    InputOptions inputOptions = codeGenCtx.getInputOptions();

    ClientConfig clientConfig = new ClientConfig();
    clientConfig.setGroup(inputOptions.getClientCfgGroupName());
    // put serviceNametag in cc.xml only if pre 2.4 consumer
    if (codeGenCtx.getInputOptions().isServiceNameRequired())
      clientConfig.setServiceName(codeGenCtx.getServiceQName().toString());
    String svcInterfaceName =
        CodeGenUtil.toQualifiedClassName(codeGenCtx.getServiceInterfaceClassName());
    clientConfig.setServiceInterfaceClassName(svcInterfaceName);
    if (inputOptions.getSvcCodeGenDefType() == null) {
      clientConfig.setServiceLocation(inputOptions.getServiceLocation());
      clientConfig.setWsdlLocation(inputOptions.getWSDLLocation());
    } else {
      ServiceCodeGenDefType svcCodeGenDef = inputOptions.getSvcCodeGenDefType();
      clientConfig.setServiceLocation(svcCodeGenDef.getServiceInfo().getServiceLocation());
      clientConfig.setWsdlLocation(svcCodeGenDef.getServiceInfo().getWsdlLocation());
    }

    ClientGroupConfig clientGrpCfg = createClientGroupConfig(codeGenCtx);
    clientConfig.setClientInstanceConfig(clientGrpCfg);

    return clientConfig;
  }

  private ClientGroupConfig createClientGroupConfig(CodeGenContext codeGenCtx) {
    ClientGroupConfig clientGrpCfg = new ClientGroupConfig();

    InvocationOptionConfig invOptionsCfg = defaultInvocationOptions(codeGenCtx);
    clientGrpCfg.setInvocationOptions(invOptionsCfg);

    return clientGrpCfg;
  }

  private InvocationOptionConfig defaultInvocationOptions(CodeGenContext codeGenCtx) {

    String prefDataBinding = "XML";
    InputOptions inputOptions = codeGenCtx.getInputOptions();

    InvocationOptionConfig defaultInvOptions = new InvocationOptionConfig();
    // if consumerId is set then it should be used else invocation Use
    if (!CodeGenUtil.isEmptyString(codeGenCtx.getInputOptions().getConsumerId()))
      defaultInvOptions.setConsumerId(codeGenCtx.getInputOptions().getConsumerId());
    else defaultInvOptions.setInvocationUseCase(codeGenCtx.getServiceAdminName() + "Client");

    PreferredTransportConfig prefTransportCfg = new PreferredTransportConfig();
    if (inputOptions.getServiceLocation() != null
        && inputOptions.getServiceLocation().startsWith("http")) {
      prefTransportCfg.setName(SOAConstants.TRANSPORT_HTTP_11);
    } else {
      prefTransportCfg.setName(SOAConstants.TRANSPORT_LOCAL);
    }

    // TransportOptionConfig transportOptCfg = new TransportOptionConfig();
    // transportOptCfg.setTransportName(CodeGenConstants.PREF_TRANSPORT_BINDING);
    // transportOptCfg.setNumRetries(Integer.valueOf(CodeGenConstants.NUM_OF_CONN_RETRIES));
    // prefTransportCfg.setOverrideOptions(transportOptCfg);

    defaultInvOptions.setPreferredTransport(prefTransportCfg);

    defaultInvOptions.setRequestDataBinding(prefDataBinding);
    defaultInvOptions.setResponseDataBinding(prefDataBinding);

    return defaultInvOptions;
  }

  private String getSuffixPath(String clientName) {
    if (CodeGenUtil.isEmptyString(clientName)) {
      return GEN_CLIENT_CONFIG_DIR + File.separatorChar + CodeGenConstants.DEFAULT_CLIENT_NAME;
    } else {
      return GEN_CLIENT_CONFIG_DIR + File.separatorChar + clientName;
    }
  }

  public String getFilePath(String serviceAdminName, String interfaceName) {

    return CodeGenUtil.toOSFilePath(getSuffixPath(serviceAdminName)) + CLIENT_CONFIG_FILE_NAME;
  }
}