private void addDQDependencies(IFolder parentFolder, List<Item> items) throws IOException {
   if (GlobalServiceRegister.getDefault().isServiceRegistered(ITDQItemService.class)) {
     ITDQItemService tdqItemService =
         (ITDQItemService) GlobalServiceRegister.getDefault().getService(ITDQItemService.class);
     for (Item item : items) {
       if (tdqItemService != null
           && tdqItemService.hasProcessItemDependencies(Arrays.asList(new Item[] {item}))) {
         // add .Talend.definition file
         String defIdxFolderName = "TDQ_Libraries"; // $NON-NLS-1$
         String defIdxFileName = ".Talend.definition"; // $NON-NLS-1$
         Project pro = getProject(processItem);
         IFolder itemsProjectFolder =
             parentFolder.getFolder(pro.getTechnicalLabel().toLowerCase());
         File itemsFolderDir = new File(parentFolder.getLocation().toFile().getAbsolutePath());
         IProject project = ReponsitoryContextBridge.getRootProject();
         String defIdxRelativePath = defIdxFolderName + PATH_SEPARATOR + defIdxFileName;
         IFile defIdxFile = project.getFile(defIdxRelativePath);
         if (defIdxFile.exists()) {
           File defIdxFileSource =
               new File(
                   project
                       .getLocation()
                       .makeAbsolute()
                       .append(defIdxFolderName)
                       .append(defIdxFileName)
                       .toFile()
                       .toURI());
           File defIdxFileTarget =
               new File(
                   itemsProjectFolder
                       .getFile(defIdxRelativePath)
                       .getLocation()
                       .toFile()
                       .getAbsolutePath());
           FilesUtils.copyFile(defIdxFileSource, defIdxFileTarget);
         }
         // add report header image & template files
         String reportingBundlePath =
             PluginChecker.getBundlePath("org.talend.dataquality.reporting"); // $NON-NLS-1$
         File imageFolder =
             new File(reportingBundlePath + PATH_SEPARATOR + "images"); // $NON-NLS-1$
         if (imageFolder.exists()) {
           FilesUtils.copyDirectory(imageFolder, itemsFolderDir);
         }
         File templateFolder =
             new File(reportingBundlePath + PATH_SEPARATOR + "reports"); // $NON-NLS-1$
         if (templateFolder.exists() && templateFolder.isDirectory()) {
           FilesUtils.copyDirectory(templateFolder, itemsFolderDir);
         }
       }
     }
   }
 }
  /**
   * DOC nrousseau Comment method "addTDMDependencies".
   *
   * @param items
   * @param itemsFolder
   */
  private void addTDMDependencies(IFolder itemsFolder, List<Item> items) {
    ITransformService tdmService = null;
    if (GlobalServiceRegister.getDefault().isServiceRegistered(ITransformService.class)) {
      tdmService =
          (ITransformService)
              GlobalServiceRegister.getDefault().getService(ITransformService.class);
    }
    try {
      // add __tdm dependencies
      ExportFileResource resouece = new ExportFileResource();
      BuildExportManager.getInstance().exportDependencies(resouece, processItem);
      if (!resouece.getAllResources().isEmpty()) {
        final Iterator<String> relativepath = resouece.getRelativePathList().iterator();
        while (relativepath.hasNext()) {
          String relativePath = relativepath.next();
          Set<URL> sources = resouece.getResourcesByRelativePath(relativePath);
          for (URL sourceUrl : sources) {
            File currentResource =
                new File(
                    org.talend.commons.utils.io.FilesUtils.getFileRealPath(sourceUrl.getPath()));
            if (currentResource.exists()) {
              // the __tdm will be out of items folder, same level for items
              IFolder targetFolder = ((IFolder) itemsFolder.getParent()).getFolder(relativePath);
              if (!targetFolder.exists()) {
                targetFolder.create(true, true, new NullProgressMonitor());
              }
              FilesUtils.copyFile(
                  currentResource,
                  new File(
                      targetFolder.getLocation().toPortableString()
                          + File.separator
                          + currentResource.getName()));
            }
          }
        }
      }

      itemsFolder.refreshLocal(IResource.DEPTH_INFINITE, null);

      // add .settings/com.oaklandsw.base.projectProps for tdm, it should be added via
      // ExportItemUtil, here just
      // make sure to export again.
      for (Item item : items) {
        if (tdmService != null && tdmService.isTransformItem(item)) {
          String itemProjectFolder = getProject(item).getTechnicalLabel();
          if (isProjectNameLowerCase()) { // should be same as ExportItemUtil.getProjectOutputPath
            itemProjectFolder = itemProjectFolder.toLowerCase();
          }
          IPath targetSettingPath =
              new Path(itemProjectFolder).append(RepositoryConstants.SETTING_DIRECTORY);
          IFolder targetSettingsFolder =
              talendProcessJavaProject.createSubFolder(
                  null, itemsFolder, targetSettingPath.toString());

          IProject sourceProject = getCorrespondingProjectRootFolder(item);

          if (sourceProject.exists()) {
            IFile targetTdmPropsFile = targetSettingsFolder.getFile(FileConstants.TDM_PROPS);
            IFile sourceTdmPropsFile =
                sourceProject
                    .getFolder(RepositoryConstants.SETTING_DIRECTORY)
                    .getFile(FileConstants.TDM_PROPS);

            // if have existed, no need copy again.
            if (sourceTdmPropsFile.exists() && !targetTdmPropsFile.exists()) {
              sourceTdmPropsFile.copy(targetTdmPropsFile.getFullPath(), true, null);
            }
          }
          break; // only deal with one time.
        }
      }
    } catch (Exception e) {
      // don't block all the export if got exception here
      ExceptionHandler.process(e);
    }
  }
Exemplo n.º 3
0
  /**
   * exports the jar to specific location.
   *
   * @param root
   * @param list
   * @param manifest
   */
  private void exportJar(File root, Collection<File> list, Manifest manifest) throws IOException {
    if (jarFile.exists()) {
      StringBuffer tempFolderBuffer = new StringBuffer(jarFile.getParent());
      String tempFolderPath = tempFolderBuffer.append(File.separatorChar).toString();
      // TDI-29008:should create the temp folder use timestamp when export several jobs at the same
      // time,the
      // the jar contains classes will not affected by other jobs
      File tempFolder = getExportJarTempFolder();
      tempFolderPath = tempFolderPath + tempFolder.getName();
      ZipFileUtils.unZip(jarFile, tempFolderPath);
      for (File subf : list) {
        String desFileName = subf.getAbsolutePath().substring(root.getAbsolutePath().length() + 1);
        File srcFile = subf;
        while (!srcFile.getParentFile().getAbsolutePath().equals(root.getAbsolutePath())) {
          srcFile = srcFile.getParentFile();
        }
        createTempSubFolder(tempFolderPath, srcFile);
        FileChannel srcChannel = null;
        FileChannel dstChannel = null;
        try {
          srcChannel = new FileInputStream(subf.getAbsoluteFile()).getChannel();
          dstChannel =
              new FileOutputStream(tempFolderPath + File.separatorChar + desFileName).getChannel();
          dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
        } finally {
          if (null != srcChannel) {
            srcChannel.close();
          }
          if (null != dstChannel) {
            dstChannel.close();
          }
        }
      }
      if (libPath != null && libPath.size() > 0) {
        File tempLib = new File(tempFolderPath + File.separatorChar + JavaUtils.JAVA_LIB_DIRECTORY);
        for (File file : libPath) {
          if (isStorm) {
            // unzip the jar file
            unZipFile(file.getAbsolutePath(), new File(tempFolderPath).getAbsolutePath());
          } else {
            if (!tempLib.exists()) {
              tempLib.mkdir();
            }
            FilesUtils.copyFile(
                new FileInputStream(file),
                new File(tempLib.getAbsolutePath() + File.separatorChar + file.getName()));
          }
        }
      }
      ZipFileUtils.zip(tempFolderPath, jarFile.getPath(), false);
    } else {
      JarOutputStream jarOut = null;
      try {
        jarOut = new JarOutputStream(new FileOutputStream(jarFile), manifest);

        for (File subf : list) {
          String filename = subf.getAbsolutePath().substring(root.getAbsolutePath().length() + 1);
          JarEntry entry = new JarEntry(filename.replace('\\', '/'));
          jarOut.putNextEntry(entry);

          FileInputStream fin = new FileInputStream(subf);
          byte[] buf = new byte[4096];
          int read;
          while ((read = fin.read(buf)) != -1) {
            jarOut.write(buf, 0, read);
          }
          fin.close();

          jarOut.closeEntry();
          jarOut.flush();
        }
      } finally {
        if (jarOut != null) {
          jarOut.close();
        }
      }
    }
  }