Exemplo n.º 1
0
  /**
   * This methos is used for coping file from one place to the other.
   *
   * @param srcFilePath
   * @param destFilePath
   * @throws Exception
   */
  private static void copy(String srcFilePath, String destFilePath) {
    FileInputStream input = null;
    FileOutputStream output = null;
    try {
      byte[] bytearray = new byte[512];
      int len = 0;
      input = new FileInputStream(srcFilePath);
      output = new FileOutputStream(destFilePath);
      while ((len = input.read(bytearray)) != -1) {
        output.write(bytearray, 0, len);
      }

    } catch (Exception fe) {
      ExceptionHandler.process(fe);
    } finally {
      if (input != null) {
        try {
          input.close();
        } catch (Exception e) {
          ExceptionHandler.process(e);
        }
      }
      if (output != null) {
        try {
          output.close();
        } catch (Exception e) {
          ExceptionHandler.process(e);
        }
      }
    }
  }
Exemplo n.º 2
0
  /**
   * This method is used for generating HTML file base on given folder, job name and xsl file name.
   *
   * @param tempFolderPath a string
   * @param jobNameOrComponentName a string
   * @param externalNodeHTMLList
   * @param xslFileName a string
   */
  public static void generateHTMLFile(
      String tempFolderPath, String xslFilePath, String xmlFilePath, String htmlFilePath) {
    FileOutputStream output = null;
    Writer writer = null;
    try {
      File xmlFile = new File(xmlFilePath);
      javax.xml.transform.Source xmlSource = new javax.xml.transform.stream.StreamSource(xmlFile);

      // will create the path needed
      Path htmlPath = new Path(htmlFilePath);
      File htmlFile = new File(htmlPath.removeLastSegments(1).toPortableString());
      htmlFile.mkdirs();

      output = new FileOutputStream(htmlFilePath);

      // Note that if the are chinese in the file, should set the encoding
      // type to "UTF-8", this is caused by DOM4J.
      writer = new BufferedWriter(new OutputStreamWriter(output, "UTF-8")); // $NON-NLS-1$

      javax.xml.transform.Result result = new javax.xml.transform.stream.StreamResult(writer);

      // clear cache, otherwise won't change style if have the same path as last
      transformerCache.clear();
      // get transformer from cache
      javax.xml.transform.Transformer trans = transformerCache.get(xslFilePath);
      if (trans == null) {
        File xsltFile = new File(xslFilePath);
        javax.xml.transform.Source xsltSource =
            new javax.xml.transform.stream.StreamSource(xsltFile);
        trans = transformerFactory.newTransformer(xsltSource);
        // put transformer into cache
        transformerCache.put(xslFilePath, trans);
      }

      trans.transform(xmlSource, result);

    } catch (Exception e) {
      ExceptionHandler.process(e);
    } finally {
      try {
        if (output != null) {
          output.close();
        }
      } catch (Exception e) {
        ExceptionHandler.process(e);
      }
      try {
        if (writer != null) {
          writer.close();
        }

      } catch (Exception e1) {
        ExceptionHandler.process(e1);
      }
    }
  }
  @SuppressWarnings("deprecation")
  private ByteArray duplicateByteArray(File initFile) {
    ByteArray byteArray = PropertiesFactory.eINSTANCE.createByteArray();
    InputStream stream = null;
    try {
      stream = initFile.toURL().openStream();
      byte[] innerContent = new byte[stream.available()];
      stream.read(innerContent);

      byteArray.setInnerContent(innerContent);
    } catch (IOException e) {
      ExceptionHandler.process(e);
    } finally {
      if (stream != null) {
        try {
          stream.close();
        } catch (IOException e) {
          //
        }
      }
    }
    String routineContent = new String(byteArray.getInnerContent());
    byteArray.setInnerContent(routineContent.getBytes());
    return byteArray;
  }
  @Override
  public ExecutionResult execute(Item item) {
    ProcessType processType = getProcessType(item);
    IComponentFilter filter = new NameComponentFilter("tMDMConnection");
    try {
      ModifyComponentsAction.searchAndModify(
          item,
          processType,
          filter,
          Arrays.<IComponentConversion>asList(
              new IComponentConversion() {

                public void transform(NodeType node) {

                  ElementParameterType username =
                      ComponentUtilities.getNodeProperty(node, "USER"); // $NON-NLS-1$
                  ElementParameterType passwd =
                      ComponentUtilities.getNodeProperty(node, "PASS"); // $NON-NLS-1$
                  if (username != null) { // $NON-NLS-1$
                    ComponentUtilities.getNodeProperty(node, "USER").setName("USERNAME");
                  }
                  if (passwd != null) { // $NON-NLS-1$
                    ComponentUtilities.getNodeProperty(node, "PASS").setName("PASSWORD");
                  }
                }
              }));
    } catch (PersistenceException e) {
      ExceptionHandler.process(e);
      return ExecutionResult.FAILURE;
    }

    return ExecutionResult.SUCCESS_NO_ALERT;
  }
Exemplo n.º 5
0
 public static void saveResource() {
   if (isModified()) {
     String installLocation =
         new Path(Platform.getConfigurationLocation().getURL().getPath())
             .toFile()
             .getAbsolutePath();
     try {
       Resource resource = createComponentCacheResource(installLocation);
       resource.getContents().add(cache);
       EmfHelper.saveResource(cache.eResource());
     } catch (PersistenceException e1) {
       ExceptionHandler.process(e1);
     }
     ILibraryManagerService repositoryBundleService =
         (ILibraryManagerService)
             GlobalServiceRegister.getDefault().getService(ILibraryManagerService.class);
     repositoryBundleService.clearCache();
     if (GlobalServiceRegister.getDefault().isServiceRegistered(ILibrariesService.class)) {
       ILibrariesService libService =
           (ILibrariesService)
               GlobalServiceRegister.getDefault().getService(ILibrariesService.class);
       if (libService != null) {
         libService.syncLibraries();
       }
     }
     setModified(false);
   }
 }
  /**
   * create jrxml file.
   *
   * @param path
   * @param label
   * @param initFile
   * @param extendtion
   * @return
   */
  public TDQJrxmlItem createJrxml(IPath path, String label, File initFile, String extendtion) {
    Property property = PropertiesFactory.eINSTANCE.createProperty();
    property.setVersion(VersionUtils.DEFAULT_VERSION);
    property.setStatusCode(PluginConstant.EMPTY_STRING);
    property.setLabel(label);

    TDQJrxmlItem routineItem =
        org.talend.dataquality.properties.PropertiesFactory.eINSTANCE.createTDQJrxmlItem();
    routineItem.setProperty(property);
    routineItem.setExtension(extendtion);
    routineItem.setName(label);

    ByteArray byteArray = duplicateByteArray(initFile);
    routineItem.setContent(byteArray);
    IProxyRepositoryFactory repositoryFactory = ProxyRepositoryFactory.getInstance();
    try {
      property.setId(repositoryFactory.getNextId());
      if (path != null) {
        repositoryFactory.createParentFoldersRecursively(
            ERepositoryObjectType.getItemType(routineItem), path);
      }
      repositoryFactory.create(routineItem, path);
    } catch (PersistenceException e) {
      ExceptionHandler.process(e);
    }
    return routineItem;
  }
 public static List<ICheckNodesService> getCheckNodesService() {
   if (checkNodeServices == null) {
     checkNodeServices = new ArrayList<ICheckNodesService>();
     IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
     IExtensionPoint extensionPoint =
         extensionRegistry.getExtensionPoint(
             "org.talend.designer.core.check_nodes"); //$NON-NLS-1$
     if (extensionPoint != null) {
       IExtension[] extensions = extensionPoint.getExtensions();
       for (IExtension extension : extensions) {
         IConfigurationElement[] configurationElements = extension.getConfigurationElements();
         for (IConfigurationElement configurationElement : configurationElements) {
           try {
             Object service =
                 configurationElement.createExecutableExtension("class"); // $NON-NLS-1$
             if (service instanceof ICheckNodesService) {
               checkNodeServices.add((ICheckNodesService) service);
             }
           } catch (CoreException e) {
             ExceptionHandler.process(e);
           }
         }
       }
     }
   }
   return checkNodeServices;
 }
 protected String getContents(File scriptFile) {
   if (scriptFile != null && scriptFile.exists()) {
     try {
       return new Scanner(scriptFile).useDelimiter("\\A").next(); // $NON-NLS-1$
     } catch (FileNotFoundException e) {
       ExceptionHandler.process(e);
     }
   }
   return null;
 }
 protected void performClean() {
   IWizardPage[] pages = getPages();
   try {
     for (IWizardPage page : pages) {
       if (page instanceof AbstractNoSQLWizardPage) {
         ((AbstractNoSQLWizardPage) page).performClean();
       }
     }
   } catch (NoSQLGeneralException e) {
     ExceptionHandler.process(e);
   }
 }
  private boolean checkHCatalogConnection(final HCatalogConnection connection) {
    final boolean[] result = new boolean[] {true};
    IRunnableWithProgress runnableWithProgress =
        new IRunnableWithProgress() {

          @Override
          public void run(IProgressMonitor monitor)
              throws InvocationTargetException, InterruptedException {
            monitor.beginTask(
                Messages.getString("CreateHCatalogSchemaAction.checkConnection"),
                IProgressMonitor.UNKNOWN);
            try {
              final ConnectionStatus connectionStatus =
                  HCatalogServiceUtil.testConnection(connection);
              if (!connectionStatus.getResult()) {
                PlatformUI.getWorkbench()
                    .getDisplay()
                    .syncExec(
                        new Runnable() {

                          @Override
                          public void run() {
                            new ErrorDialogWidthDetailArea(
                                PlatformUI.getWorkbench().getDisplay().getActiveShell(),
                                Activator.PLUGIN_ID,
                                Messages.getString(
                                    "CreateHCatalogSchemaAction.connectionFailure.mainMsg"),
                                connectionStatus //$NON-NLS-1$
                                    .getMessageException());
                            result[0] = false;
                            return;
                          }
                        });
              }

            } catch (Exception e) {
            } finally {
              monitor.done();
            }
          }
        };
    ProgressMonitorDialog dialog = new ProgressMonitorDialog(Display.getDefault().getActiveShell());
    try {
      dialog.run(true, true, runnableWithProgress);
    } catch (Exception e) {
      result[0] = false;
      ExceptionHandler.process(e);
    }

    return result[0];
  }
  /*
   * (non-Javadoc)
   *
   * @see org.talend.core.model.migration.AbstractItemMigrationTask#execute(org.talend.core.model.properties.Item)
   */
  @Override
  public ExecutionResult execute(Item item) {
    ProcessType processType = getProcessType(item);
    if (processType == null) {
      return ExecutionResult.NOTHING_TO_DO;
    }
    String[] componentsName = new String[] {"tPigLoad"}; // $NON-NLS-1$

    try {

      for (String element : componentsName) {
        IComponentFilter filter = new NameComponentFilter(element);
        ModifyComponentsAction.searchAndModify(
            item,
            processType,
            filter,
            Arrays.<IComponentConversion>asList(
                new IComponentConversion() {

                  @Override
                  public void transform(NodeType node) {
                    ElementParameterType ept =
                        ComponentUtilities.getNodeProperty(node, "PIG_VERSION"); // $NON-NLS-1$
                    if (ept != null) {
                      if ("CLOUDERA_0.20_CDH3U1".equals(ept.getValue())) { // $NON-NLS-1$
                        ComponentUtilities.setNodeValue(
                            node,
                            "PIG_VERSION",
                            "Cloudera_0_20_CDH3U1"); //$NON-NLS-1$//$NON-NLS-2$
                      } else if ("CLOUDERA_CDH4".equals(ept.getValue())) { // $NON-NLS-1$
                        ComponentUtilities.setNodeValue(
                            node, "PIG_VERSION", "Cloudera_CDH4"); // $NON-NLS-1$//$NON-NLS-2$
                      } else if ("MAPR".equals(ept.getValue())) { // $NON-NLS-1$
                        ComponentUtilities.setNodeValue(
                            node, "PIG_VERSION", "MapR"); // $NON-NLS-1$//$NON-NLS-2$
                      } else if ("MAPR2".equals(ept.getValue())) { // $NON-NLS-1$
                        ComponentUtilities.setNodeValue(
                            node, "PIG_VERSION", "MapR2"); // $NON-NLS-1$//$NON-NLS-2$
                      }
                    }
                  }
                }));
      }

      return ExecutionResult.SUCCESS_NO_ALERT;
    } catch (Exception e) {
      ExceptionHandler.process(e);
      return ExecutionResult.FAILURE;
    }
  }
Exemplo n.º 12
0
  private File getExportJarTempFolder() {
    File tempFolder = null;
    try {
      tempFolder = File.createTempFile(TEMP, null);
      if (tempFolder.exists() && tempFolder.isFile()) {
        tempFolder.delete();
      }
      tempFolder.mkdirs(); // use the same tmp file to make the tmp folder.

    } catch (IOException e) {
      ExceptionHandler.process(e);
      tempFolder = new File(getTmpFolder());
    }

    return tempFolder;
  }
    public void waitFinish(ILaunch launch) {

      try {
        while (!launchFinished) {
          Thread.sleep(100);
          // if terminated also
          if (launch.getProcesses() != null && launch.getProcesses().length > 0) {
            if (launch.getProcesses()[0].isTerminated()) {
              break;
            }
          }
        }
      } catch (InterruptedException e) {
        ExceptionHandler.process(e);
      }
    }
 protected void setDefault(
     IPreferenceStore preferenceStore, String key, String bunlde, String bundleTemplatePath) {
   try {
     AbstractMavenTemplateManager templateManager =
         MavenTemplateManager.getTemplateManagerMap().get(bunlde);
     if (templateManager != null) {
       InputStream stream = templateManager.readBundleStream(bundleTemplatePath);
       String osgiPomContent = MavenTemplateManager.getContentFromInputStream(stream);
       if (osgiPomContent != null) {
         preferenceStore.setDefault(key, osgiPomContent);
       }
     }
   } catch (Exception e) {
     ExceptionHandler.process(e);
   }
 }
 public static List<IReplaceNodeInProcess> findReplaceNodesProvider() {
   if (providers.isEmpty()) {
     IConfigurationElement[] elems =
         Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_ID);
     for (IConfigurationElement elem : elems) {
       IReplaceNodeInProcess createExecutableExtension;
       try {
         createExecutableExtension =
             (IReplaceNodeInProcess) elem.createExecutableExtension(ATTR_CLASS);
         providers.add(createExecutableExtension);
       } catch (CoreException e) {
         ExceptionHandler.process(e);
       }
     }
   }
   return providers;
 }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.jdt.core.compiler.CompilationParticipant#processAnnotations(org.eclipse.jdt.core.compiler.BuildContext
   * [])
   */
  @Override
  public void processAnnotations(BuildContext[] files) {

    boolean routineToUpdate = false;
    super.processAnnotations(files);

    List<IRepositoryViewObject> routineObjectList = null;
    for (BuildContext context : files) {

      String filePath = (context.getFile().getProjectRelativePath()).toString();

      if (isRoutineFile(filePath)) {
        if (!routineToUpdate) {
          IProxyRepositoryFactory factory = CorePlugin.getDefault().getProxyRepositoryFactory();

          try {

            routineObjectList = factory.getAll(ERepositoryObjectType.ROUTINES, false);
          } catch (PersistenceException e) {
            ExceptionHandler.process(e);
          }
        }
        updateProblems(routineObjectList, filePath);
        routineToUpdate = true;
      }
    }

    if (routineToUpdate) {
      Display.getDefault()
          .asyncExec(
              new Runnable() {

                @Override
                public void run() {
                  try {
                    Problems.refreshProblemTreeView();
                  } catch (Exception e) {
                    // ignore any exception here, as there is no impact if refresh or not.
                    // but if don't ignore, exception could be thrown if refresh is done too early.
                  }
                }
              });
    }
  }
 public static Set<IConnectionValidator> getConnectionValidators() {
   if (validators == null) {
     validators = new HashSet<IConnectionValidator>();
     IConfigurationElement[] validatorExtensions =
         Platform.getExtensionRegistry().getConfigurationElementsFor(CONNECTION_VALIDATOR);
     if (validatorExtensions != null && validatorExtensions.length > 0) {
       for (IConfigurationElement ie : validatorExtensions) {
         try {
           Object execution = ie.createExecutableExtension(CLASS_ATTRIBUTE);
           validators.add((IConnectionValidator) execution);
         } catch (CoreException e) {
           ExceptionHandler.log(e.getMessage());
           continue;
         }
       }
     }
   }
   return validators;
 }
  /** yzhang Comment method "updateProblems". */
  private void updateProblems(List<IRepositoryViewObject> routineObjectList, String filePath) {

    IRunProcessService runProcessService = CorePlugin.getDefault().getRunProcessService();
    try {
      ITalendProcessJavaProject talendProcessJavaProject =
          runProcessService.getTalendProcessJavaProject();
      if (talendProcessJavaProject == null) {
        return;
      }
      IProject javaProject = talendProcessJavaProject.getProject();
      IFile file = javaProject.getFile(filePath);
      String fileName = file.getName();

      for (IRepositoryViewObject repositoryObject : routineObjectList) {
        Property property = repositoryObject.getProperty();
        ITalendSynchronizer synchronizer =
            CorePlugin.getDefault().getCodeGeneratorService().createRoutineSynchronizer();
        Item item = property.getItem();
        if (GlobalServiceRegister.getDefault()
            .isServiceRegistered(ICamelDesignerCoreService.class)) {
          ICamelDesignerCoreService service =
              (ICamelDesignerCoreService)
                  GlobalServiceRegister.getDefault().getService(ICamelDesignerCoreService.class);
          if (service.isInstanceofCamel(item)) {
            synchronizer =
                CorePlugin.getDefault().getCodeGeneratorService().createCamelBeanSynchronizer();
          }
        }
        IFile currentFile = synchronizer.getFile(item);
        if (currentFile != null && fileName.equals(currentFile.getName()) && currentFile.exists()) {
          Problems.addRoutineFile(currentFile, property);
          break;
        }
      }
    } catch (SystemException e) {
      ExceptionHandler.process(e);
    }
  }
  @Override
  public IComponentName getCorrespondingComponentName(Item item, ERepositoryObjectType type) {
    RepositoryComponentSetting setting = null;
    if (item instanceof HCatalogConnectionItem) {
      setting = new RepositoryComponentSetting();
      setting.setName(HCATALOG);
      setting.setRepositoryType(HCATALOG);
      setting.setWithSchema(true);
      setting.setInputComponent(INPUT);
      setting.setOutputComponent(OUTPUT);
      List<Class<Item>> list = new ArrayList<Class<Item>>();
      Class clazz = null;
      try {
        clazz = Class.forName(HCatalogConnectionItem.class.getName());
      } catch (ClassNotFoundException e) {
        ExceptionHandler.process(e);
      }
      list.add(clazz);
      setting.setClasses(list.toArray(new Class[0]));
    }

    return setting;
  }
Exemplo n.º 20
0
  @Override
  public void run() {
    SaveAsRoutesWizard processWizard =
        new SaveAsRoutesWizard((JobEditorInput) editorPart.getEditorInput());

    WizardDialog dlg = new WizardDialog(editorPart.getSite().getShell(), processWizard);
    if (dlg.open() == Window.OK) {

      try {

        // Set readonly to false since created routes will always be editable.
        JobEditorInput newRoutesEditorInput =
            new CamelProcessEditorInput(processWizard.getProcess(), true, true, false);

        IWorkbenchPage page = editorPart.getSite().getPage();

        IRepositoryNode repositoryNode =
            RepositorySeekerManager.getInstance()
                .searchRepoViewNode(newRoutesEditorInput.getItem().getProperty().getId(), false);
        newRoutesEditorInput.setRepositoryNode(repositoryNode);

        // close the old editor
        page.closeEditor(editorPart, false);

        // open the new editor, because at the same time, there will update the routes view
        page.openEditor(newRoutesEditorInput, CamelMultiPageTalendEditor.ID, true);

      } catch (Exception e) {
        MessageDialog.openError(
            editorPart.getSite().getShell(),
            "Error",
            "Routes could not be saved" + " : " + e.getMessage());
        ExceptionHandler.process(e);
      }
    }
  }
Exemplo n.º 21
0
  /**
   * This method is used for generating HTML file base on given folder, job name and xsl file name.
   *
   * @param tempFolderPath a string
   * @param jobName a string
   * @param htmlFileMap
   * @param xslFileName a string
   */
  public static void generateHTMLFile(
      String tempFolderPath,
      String xslFilePath,
      String xmlFilePath,
      String htmlFilePath,
      Map<String, Object> htmlFileMap) {

    Map<String, Object> nodeHTMLMap = htmlFileMap;
    generateHTMLFile(tempFolderPath, xslFilePath, xmlFilePath, htmlFilePath);

    File originalHtmlFile = new File(htmlFilePath);

    if (!originalHtmlFile.exists()) {
      return;
    }

    BufferedReader mainHTMLReader = null;
    BufferedWriter newMainHTMLWriter = null;
    // BufferedReader externalNodeHTMLReader = null;
    File newMainHTMLFile = null;
    try {

      mainHTMLReader = new BufferedReader(new FileReader(originalHtmlFile));

      newMainHTMLFile = new File(htmlFilePath + "temp"); // $NON-NLS-1$

      newMainHTMLWriter = new BufferedWriter(new FileWriter(newMainHTMLFile));
      String lineStr = ""; // $NON-NLS-1$
      while ((lineStr = mainHTMLReader.readLine()) != null) {
        newMainHTMLWriter.write(lineStr);
        for (String key : htmlFileMap.keySet()) {
          String compareStr =
              "<!--" + key + "ended-->"; // tMap_1ended--> //$NON-NLS-1$ //$NON-NLS-2$
          if (lineStr.indexOf(compareStr) != -1) {

            if (htmlFileMap.get(key) instanceof URL) {
              File externalNodeHTMLFile = new File(((URL) nodeHTMLMap.get(key)).getPath());

              String content =
                  (String) externalNodeFileCache.get(externalNodeHTMLFile.getAbsolutePath());
              if (content == null) {
                content = FileUtils.readFileToString(externalNodeHTMLFile);
                // put file content into cache
                externalNodeFileCache.put(externalNodeHTMLFile.getAbsolutePath(), content);
              }
              newMainHTMLWriter.write(content);
            } else if (htmlFileMap.get(key) instanceof String) {
              newMainHTMLWriter.write((String) htmlFileMap.get(key));
            }
          }
          // htmlFileMap.remove(key);
        }
      }

    } catch (Exception e) {
      ExceptionHandler.process(e);
    } finally {
      try {
        if (mainHTMLReader != null) {
          mainHTMLReader.close();
        }
        if (newMainHTMLWriter != null) {
          newMainHTMLWriter.close();
        }
        // if (externalNodeHTMLReader != null) {
        // externalNodeHTMLReader.close();
        // }
      } catch (IOException e) {
        ExceptionHandler.process(e);
      }

      originalHtmlFile.delete();
      newMainHTMLFile.renameTo(new File(htmlFilePath));
      // System.out.println("isWorked= " + isWorked);

      // copy(htmlFilePath + "temp", htmlFilePath);
      //
      // System.out.println("tempFilePath:" + htmlFilePath + "temp");
      // System.out.println("htmlFilePath:" + htmlFilePath);
    }
  }
Exemplo n.º 22
0
  /**
   * Generates xml file base on inputted path, file path and an instance of <code>Document</code>
   *
   * @param tempFolderPath
   * @param filePath
   * @param document
   */
  public static void generateXMLFile(String tempFolderPath, String filePath, Document document) {
    XMLWriter output = null;
    FileOutputStream out = null;
    Writer writer = null;

    try {
      // OutputFormat format = OutputFormat.createPrettyPrint();
      out = new java.io.FileOutputStream(filePath);
      writer = new OutputStreamWriter(out, "UTF-8"); // $NON-NLS-1$

      OutputFormat format = OutputFormat.createPrettyPrint();
      output =
          new XMLWriter(writer, format) {

            /*
             * (non-Javadoc)
             *
             * @see org.dom4j.io.XMLWriter#writeDeclaration()
             */
            @Override
            protected void writeDeclaration() throws IOException {
              OutputFormat formatTmp = this.getOutputFormat();
              String encoding = formatTmp.getEncoding();

              // Only print of declaration is not suppressed
              if (!formatTmp.isSuppressDeclaration()) {
                // Assume 1.0 version
                if (encoding.equals("UTF8")) { // $NON-NLS-1$
                  writer.write("<?xml version=\"1.1\""); // $NON-NLS-1$

                  if (!formatTmp.isOmitEncoding()) {
                    writer.write(" encoding=\"UTF-8\""); // $NON-NLS-1$
                  }

                  writer.write("?>"); // $NON-NLS-1$
                } else {
                  writer.write("<?xml version=\"1.1\""); // $NON-NLS-1$

                  if (!formatTmp.isOmitEncoding()) {
                    writer.write(" encoding=\"" + encoding + "\""); // $NON-NLS-1$ //$NON-NLS-2$
                  }

                  writer.write("?>"); // $NON-NLS-1$
                }

                if (formatTmp.isNewLineAfterDeclaration()) {
                  println();
                }
              }
            }
          };
      output.setMaximumAllowedCharacter(127);
      output.write(document);
      output.flush();
    } catch (Exception e) {
      ExceptionHandler.process(e);
    } finally {
      if (output != null) {
        try {
          output.close();
        } catch (Exception e) {
          ExceptionHandler.process(e);
        }
      }
      if (writer != null) {
        try {
          writer.close();
        } catch (Exception e) {
          ExceptionHandler.process(e);
        }
      }
      if (out != null) {
        try {
          out.close();
        } catch (Exception e) {
          ExceptionHandler.process(e);
        }
      }
    }
  }
  public void execute(IProgressMonitor monitor) throws Exception {

    /*
     * use the ExecutePomAction directly.
     */
    // ExecutePomAction exePomAction = new ExecutePomAction();
    // exePomAction.setInitializationData(null, null, MavenConstants.GOAL_COMPILE);
    // exePomAction.launch(new StructuredSelection(launcherPomFile), ILaunchManager.RUN_MODE);

    /*
     * use launch way
     */

    // try{

    ILaunchConfiguration launchConfiguration = createLaunchConfiguration();
    if (launchConfiguration == null) {
      throw new Exception("Can't create maven command launcher.");
    }
    // if (launchConfiguration instanceof ILaunchConfigurationWorkingCopy) {
    // ILaunchConfigurationWorkingCopy copiedConfig = (ILaunchConfigurationWorkingCopy)
    // launchConfiguration;
    // }
    TalendLauncherWaiter talendWaiter = new TalendLauncherWaiter(launchConfiguration);

    final ILaunch launch = buildAndLaunch(launchConfiguration, launcherMode, monitor);
    talendWaiter.waitFinish(launch);
    StringBuffer errors = new StringBuffer();
    for (IProcess process : launch.getProcesses()) {
      String log = process.getStreamsProxy().getOutputStreamMonitor().getContents();

      if (!isCaptureOutputInConsoleView()) {
        // specially for commandline. if studio, when debug model, will log it in console view, so
        // no need this.
        TalendDebugHandler.debug(
            "\n------------------ Talend Maven Launcher log START -----------------------\n");
        TalendDebugHandler.debug(log);
        TalendDebugHandler.debug(
            "\n------------------ Talend Maven Launcher log END -----------------------\n");
      }
      for (String line : log.split("\n")) { // $NON-NLS-1$
        if (line.startsWith("[ERROR]")) { // $NON-NLS-1$
          errors.append(line + "\n"); // $NON-NLS-1$
        }
      }
    }
    if (errors.length() > 0) {
      if (getGoals() != null
          && getGoals()
              .matches(
                  "(.*)\\b"
                      + TalendMavenConstants.GOAL_TEST
                      + "\\b(.*)")) { //$NON-NLS-1$//$NON-NLS-2$
        ExceptionHandler.process(new Exception(errors.toString()));
      } else {
        throw new Exception(errors.toString());
      }
    }

    // }finally{
    // if (launch != null) {
    // if remove, after execute launch, will remove the console also. so shouldn't remove it.
    // DebugPlugin.getDefault().getLaunchManager().removeLaunch(launch);
    // }
    // }
  }
  protected ILaunchConfiguration createLaunchConfiguration(IContainer basedir, String goal) {
    try {
      ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
      ILaunchConfigurationType launchConfigurationType =
          launchManager.getLaunchConfigurationType(
              MavenLaunchConstants.LAUNCH_CONFIGURATION_TYPE_ID);

      String launchSafeGoalName = goal.replace(':', '-');

      ILaunchConfigurationWorkingCopy workingCopy =
          launchConfigurationType.newInstance(
              null, //
              NLS.bind(
                  org.eclipse.m2e.internal.launch.Messages.ExecutePomAction_executing,
                  launchSafeGoalName,
                  basedir.getLocation().toString().replace('/', '-')));
      workingCopy.setAttribute(
          MavenLaunchConstants.ATTR_POM_DIR, basedir.getLocation().toOSString());
      workingCopy.setAttribute(MavenLaunchConstants.ATTR_GOALS, goal);
      workingCopy.setAttribute(ILaunchManager.ATTR_PRIVATE, true);
      workingCopy.setAttribute(
          RefreshUtil.ATTR_REFRESH_SCOPE, RefreshUtil.MEMENTO_SELECTED_PROJECT);
      workingCopy.setAttribute(RefreshUtil.ATTR_REFRESH_RECURSIVE, true);

      // seems no need refresh project, so won't set it.
      // workingCopy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
      // basedir.getProject().getName());

      // --------------Special settings for Talend----------
      if (isCaptureOutputInConsoleView()) {
        // by default will catch the output in console. so set null
        workingCopy.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, (String) null);
      } else {
        workingCopy.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, Boolean.FALSE.toString());
      }

      // not same, set it. Else use the preference directly.
      if (debugOutput != MavenPlugin.getMavenConfiguration().isDebugOutput()) {
        workingCopy.setAttribute(MavenLaunchConstants.ATTR_DEBUG_OUTPUT, this.debugOutput); // -X -e
      }

      // -Dmaven.test.skip=true -DskipTests
      workingCopy.setAttribute(MavenLaunchConstants.ATTR_SKIP_TESTS, this.skipTests);

      // ------------------------

      setProjectConfiguration(workingCopy, basedir);

      IPath path = getJREContainerPath(basedir);
      if (path != null) {
        workingCopy.setAttribute(
            IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, path.toPortableString());
      }

      String programArgs = getArgumentValue(TalendProcessArgumentConstant.ARG_PROGRAM_ARGUMENTS);
      if (StringUtils.isNotEmpty(programArgs)) {
        workingCopy.setAttribute(
            IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, programArgs);
      }

      // TODO when launching Maven with debugger consider to add the following property
      // -Dmaven.surefire.debug="-Xdebug
      // -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -Xnoagent
      // -Djava.compiler=NONE"

      return workingCopy;
    } catch (CoreException ex) {
      ExceptionHandler.process(ex);
    }
    return null;
  }
Exemplo n.º 25
0
  /**
   * 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);
    }
  }
  @SuppressWarnings("unchecked")
  public static List<String> getRequiredRoutineName(IProcess process) {
    Set<String> neededRoutines = process.getNeededRoutines();
    ECodeLanguage currentLanguage = LanguageManager.getCurrentLanguage();
    String perlConn = "::"; // $NON-NLS-1$
    String builtInPath =
        ILibrariesService.SOURCE_PERL_ROUTINES_FOLDER
            + perlConn
            + "system"
            + perlConn; //$NON-NLS-1$

    if (neededRoutines == null || neededRoutines.isEmpty()) {
      try {
        IProxyRepositoryFactory factory = CorePlugin.getDefault().getProxyRepositoryFactory();
        List<IRepositoryViewObject> routines =
            factory.getAll(
                ProjectManager.getInstance().getCurrentProject(), ERepositoryObjectType.ROUTINES);
        for (Project project : ProjectManager.getInstance().getAllReferencedProjects()) {
          List<IRepositoryViewObject> routinesFromRef =
              factory.getAll(project, ERepositoryObjectType.ROUTINES);
          for (IRepositoryViewObject routine : routinesFromRef) {
            if (!((RoutineItem) routine.getProperty().getItem()).isBuiltIn()) {
              routines.add(routine);
            }
          }
        }
        neededRoutines = new HashSet<String>();
        for (IRepositoryViewObject object : routines) {
          neededRoutines.add(object.getLabel());
        }
      } catch (PersistenceException e) {
        ExceptionHandler.process(e);
      }
    }
    if (currentLanguage == ECodeLanguage.PERL) {
      List<IRepositoryViewObject> routines;
      try {
        IProxyRepositoryFactory factory = CorePlugin.getDefault().getProxyRepositoryFactory();
        routines = factory.getAll(ERepositoryObjectType.ROUTINES);
        for (Project project : ProjectManager.getInstance().getAllReferencedProjects()) {
          List<IRepositoryViewObject> routinesFromRef =
              factory.getAll(project, ERepositoryObjectType.ROUTINES);
          for (IRepositoryViewObject routine : routinesFromRef) {
            if (!((RoutineItem) routine.getProperty().getItem()).isBuiltIn()) {
              routines.add(routine);
            }
          }
        }
        Set<String> newNeededRoutines = new HashSet<String>();
        for (IRepositoryViewObject object : routines) {
          if (neededRoutines.contains(object.getLabel())) {
            neededRoutines.remove(object.getLabel());
            if (((RoutineItem) object.getProperty().getItem()).isBuiltIn()) {
              newNeededRoutines.add(builtInPath + object.getLabel());
            } else {
              String userPath =
                  ILibrariesService.SOURCE_PERL_ROUTINES_FOLDER
                      + perlConn
                      + ProjectManager.getInstance()
                          .getProject(object.getProperty().getItem())
                          .getTechnicalLabel()
                      + perlConn;
              newNeededRoutines.add(userPath + object.getLabel());
            }
          }
        }
        neededRoutines = newNeededRoutines;
      } catch (PersistenceException e) {
        ExceptionHandler.process(e);
      }
    }
    return new ArrayList<String>(neededRoutines);
  }
  public NexusServerBean getCustomNexusServer() {
    NexusServerBean serverBean = null;
    try {
      IProxyRepositoryFactory factory = CoreRuntimePlugin.getInstance().getProxyRepositoryFactory();
      RepositoryContext repositoryContext = factory.getRepositoryContext();
      if (factory.isLocalConnectionProvider() || repositoryContext.isOffline()) {
        return null;
      }
      if (repositoryContext != null && repositoryContext.getFields() != null) {
        String adminUrl = repositoryContext.getFields().get(RepositoryConstants.REPOSITORY_URL);
        String userName = "";
        String password = "";
        User user = repositoryContext.getUser();
        if (user != null) {
          userName = user.getLogin();
          password = repositoryContext.getClearPassword();
        }

        if (adminUrl != null
            && !"".equals(adminUrl)
            && GlobalServiceRegister.getDefault().isServiceRegistered(IRemoteService.class)) {
          IRemoteService remoteService =
              (IRemoteService) GlobalServiceRegister.getDefault().getService(IRemoteService.class);
          JSONObject libServerObject;
          libServerObject = remoteService.getLibNexusServer(userName, password, adminUrl);
          if (libServerObject != null) {
            String nexus_url = libServerObject.getString(NexusServerUtils.KEY_NEXUS_RUL);
            String nexus_user = libServerObject.getString(NexusServerUtils.KEY_NEXUS_USER);
            String nexus_pass = libServerObject.getString(NexusServerUtils.KEY_NEXUS_PASS);
            String repositoryId =
                libServerObject.getString(NexusServerUtils.KEY_CUSTOM_LIB_REPOSITORY);

            // TODO check if custom nexus is valid , only check http response for now , need check
            // if it is
            // snapshot latter
            boolean connectionOk =
                NexusServerUtils.checkConnectionStatus(
                    nexus_url, repositoryId, nexus_user, nexus_pass);
            if (!connectionOk) {
              return null;
            }

            String newUrl = nexus_url;
            if (newUrl.endsWith(NexusConstants.SLASH)) {
              newUrl = newUrl.substring(0, newUrl.length() - 1);
            }
            if (nexus_user != null && !"".equals(nexus_user)) { // $NON-NLS-1$
              String[] split = newUrl.split("://"); // $NON-NLS-1$
              if (split.length != 2) {
                throw new RuntimeException(
                    "Nexus url is not valid ,please contract the administrator");
              }
              newUrl =
                  split[0]
                      + ":"
                      + nexus_user
                      + ":"
                      + nexus_pass
                      + "@//" //$NON-NLS-1$
                      + split[1];
            }
            newUrl =
                newUrl
                    + NexusConstants.CONTENT_REPOSITORIES
                    + repositoryId
                    + "@id="
                    + repositoryId; //$NON-NLS-1$

            serverBean = new NexusServerBean();
            serverBean.setServer(nexus_url);
            serverBean.setUserName(nexus_user);
            serverBean.setPassword(nexus_pass);
            serverBean.setRepositoryId(repositoryId);
            serverBean.setRepositoryUrl(newUrl);
          }
        }
      }
    } catch (Exception e) {
      serverBean = null;
      ExceptionHandler.process(e);
    }
    if (previousCustomBean == null && serverBean != null
        || previousCustomBean != null && !previousCustomBean.equals(serverBean)) {
      mavenResolver = null;
    }
    previousCustomBean = serverBean;
    return serverBean;
  }