private void createNewImports(
      final Path context, final List<Import> imports, final ConversionResult result) {

    if (imports == null || imports.isEmpty()) {
      return;
    }

    // Load existing PackageImports
    ProjectImports projectImports = new ProjectImports();
    final org.uberfire.java.nio.file.Path nioExternalImportsPath =
        Paths.convert(context).resolve("project.imports");
    final Path externalImportsPath = Paths.convert(nioExternalImportsPath);
    if (Files.exists(nioExternalImportsPath)) {
      projectImports = importsService.load(externalImportsPath);
    }

    // Make collections of existing Imports so we don't duplicate them when adding the new
    List<String> existingImports = new ArrayList<String>();
    for (org.drools.workbench.models.datamodel.imports.Import item :
        projectImports.getImports().getImports()) {
      existingImports.add(item.getType());
    }

    // Add imports
    boolean isModified = false;
    for (Import item : imports) {
      if (!existingImports.contains(item.getClassName())) {
        isModified = true;
        result.addMessage(
            "Created Import for '" + item.getClassName() + "'.", ConversionMessageType.INFO);
        projectImports
            .getImports()
            .addImport(
                new org.drools.workbench.models.datamodel.imports.Import(item.getClassName()));
      }
    }

    // Save update
    if (isModified) {
      final Metadata metadata = metadataService.getMetadata(context);
      importsService.save(
          externalImportsPath, projectImports, metadata, "Imports added during XLS conversion");
    }
  }
  @Override
  public void setContent(final ProjectImports importTypes, final boolean isReadOnly) {
    checkNotNull("importTypes", importTypes);

    view.setContent(importTypes.getImports().getImports(), isReadOnly);
  }
예제 #3
0
 private ProjectImports createProjectImports() {
   ProjectImports imports = new ProjectImports();
   imports.getImports().addImport(new Import("java.lang.Number"));
   return imports;
 }
예제 #4
0
  public BuildResults build() {
    synchronized (kieFileSystem) {
      // KieBuilder is not re-usable for successive "full" builds
      kieBuilder = createKieBuilder(kieFileSystem);

      // Record RTEs from KieBuilder - that can fail if a rule uses an inaccessible class
      final BuildResults results = new BuildResults(projectGAV);
      try {
        final Results kieResults = kieBuilder.buildAll().getResults();
        results.addAllBuildMessages(convertMessages(kieResults.getMessages(), handles));

      } catch (LinkageError e) {
        final String msg = MessageFormat.format(ERROR_CLASS_NOT_FOUND, e.getLocalizedMessage());
        logger.warn(msg);
        results.addBuildMessage(makeWarningMessage(msg));

      } catch (Throwable e) {
        final String msg = e.getLocalizedMessage();
        logger.error(msg, e);
        results.addBuildMessage(makeErrorMessage(msg));

      } finally {
        pomModelCache.setEntry(project, ((KieBuilderImpl) kieBuilder).getPomModel());
      }

      // Add validate messages from external helpers
      for (Map.Entry<Path, BuildValidationHelper> e : nonKieResourceValidationHelpers.entrySet()) {
        final org.uberfire.backend.vfs.Path vfsPath = Paths.convert(e.getKey());
        final List<ValidationMessage> validationMessages = e.getValue().validate(vfsPath);
        nonKieResourceValidationHelperMessages.put(e.getKey(), validationMessages);
        results.addAllBuildMessages(convertValidationMessages(validationMessages));
      }

      // Check external imports are available. These are loaded when a DMO is requested, but it's
      // better to report them early
      final org.uberfire.java.nio.file.Path nioExternalImportsPath =
          projectRoot.resolve("project.imports");
      if (Files.exists(nioExternalImportsPath)) {
        final org.uberfire.backend.vfs.Path externalImportsPath =
            Paths.convert(nioExternalImportsPath);
        final ProjectImports projectImports = importsService.load(externalImportsPath);
        final Imports imports = projectImports.getImports();
        for (final Import item : imports.getImports()) {
          final String fullyQualifiedClassName = item.getType();
          try {
            Class clazz = this.getClass().getClassLoader().loadClass(item.getType());

          } catch (ClassNotFoundException cnfe) {
            logger.warn(cnfe.getMessage());
            final String msg = MessageFormat.format(ERROR_CLASS_NOT_FOUND, fullyQualifiedClassName);
            results.addBuildMessage(makeWarningMessage(msg));
          }
        }
      }

      // At the end we are interested to ensure that external .jar files referenced as dependencies
      // don't have
      // referential inconsistencies. We will at least provide a basic algorithm to ensure that if
      // an external class
      // X references another external class Y, Y is also accessible by the class loader.
      final KieModuleMetaData kieModuleMetaData =
          KieModuleMetaData.Factory.newKieModuleMetaData(
              ((InternalKieBuilder) kieBuilder).getKieModuleIgnoringErrors());
      final Set<String> packageNamesWhiteList =
          packageNameWhiteList.filterPackageNames(project, kieModuleMetaData.getPackages());
      // store the project dependencies ClassLoader for optimization purposes.
      updateDependenciesClassLoader(project, kieModuleMetaData);

      for (final String packageName : kieModuleMetaData.getPackages()) {
        if (packageNamesWhiteList.contains(packageName)) {
          for (final String className : kieModuleMetaData.getClasses(packageName)) {
            final String fullyQualifiedClassName = packageName + "." + className;
            try {
              final Class clazz = kieModuleMetaData.getClass(packageName, className);
              if (clazz != null) {
                final TypeSource typeSource = getClassSource(kieModuleMetaData, clazz);
                if (TypeSource.JAVA_DEPENDENCY == typeSource) {
                  verifyExternalClass(clazz);
                }
              } else {
                final String msg =
                    MessageFormat.format(ERROR_EXTERNAL_CLASS_VERIFICATON, fullyQualifiedClassName);
                logger.warn(msg);
              }

            } catch (Throwable e) {
              final String msg =
                  MessageFormat.format(ERROR_EXTERNAL_CLASS_VERIFICATON, fullyQualifiedClassName);
              logger.warn(msg);
              results.addBuildMessage(makeWarningMessage(msg));
            }
          }
        }
      }

      return results;
    }
  }