Exemplo n.º 1
0
  private String getPackagePathSuffix(
      final org.uberfire.java.nio.file.Path nioProjectRootPath,
      final org.uberfire.java.nio.file.Path nioPackagePath) {
    final org.uberfire.java.nio.file.Path nioMainSrcPath =
        nioProjectRootPath.resolve(MAIN_SRC_PATH);
    final org.uberfire.java.nio.file.Path nioTestSrcPath =
        nioProjectRootPath.resolve(TEST_SRC_PATH);
    final org.uberfire.java.nio.file.Path nioMainResourcesPath =
        nioProjectRootPath.resolve(MAIN_RESOURCES_PATH);
    final org.uberfire.java.nio.file.Path nioTestResourcesPath =
        nioProjectRootPath.resolve(TEST_RESOURCES_PATH);

    String packageName = null;
    org.uberfire.java.nio.file.Path packagePath = null;
    if (nioPackagePath.startsWith(nioMainSrcPath)) {
      packagePath = nioMainSrcPath.relativize(nioPackagePath);
      packageName = packagePath.toString();
    } else if (nioPackagePath.startsWith(nioTestSrcPath)) {
      packagePath = nioTestSrcPath.relativize(nioPackagePath);
      packageName = packagePath.toString();
    } else if (nioPackagePath.startsWith(nioMainResourcesPath)) {
      packagePath = nioMainResourcesPath.relativize(nioPackagePath);
      packageName = packagePath.toString();
    } else if (nioPackagePath.startsWith(nioTestResourcesPath)) {
      packagePath = nioTestResourcesPath.relativize(nioPackagePath);
      packageName = packagePath.toString();
    }

    return packageName;
  }
Exemplo n.º 2
0
  @Override
  public Package resolveDefaultPackage(final Project project) {
    final Set<String> packageNames = new HashSet<String>();
    if (project == null) {
      return null;
    }
    // Build a set of all package names across /src/main/java, /src/main/resources, /src/test/java
    // and /src/test/resources paths
    // It is possible (if the project was not created within the workbench that some packages only
    // exist in certain paths)
    final Path projectRoot = project.getRootPath();
    final org.uberfire.java.nio.file.Path nioProjectRootPath = Paths.convert(projectRoot);
    for (String src : sourcePaths) {
      final org.uberfire.java.nio.file.Path nioPackageRootSrcPath = nioProjectRootPath.resolve(src);
      packageNames.addAll(
          getPackageNames(nioProjectRootPath, nioPackageRootSrcPath, true, true, false));
    }

    // Construct Package objects for each package name
    final java.util.Set<String> resolvedPackages = new java.util.HashSet<String>();
    for (String packagePathSuffix : packageNames) {
      for (String src : sourcePaths) {
        final org.uberfire.java.nio.file.Path nioPackagePath =
            nioProjectRootPath.resolve(src).resolve(packagePathSuffix);
        if (Files.exists(nioPackagePath) && !resolvedPackages.contains(packagePathSuffix)) {
          return resolvePackage(Paths.convert(nioPackagePath));
        }
      }
    }

    return null;
  }
Exemplo n.º 3
0
  public Project simpleProjectInstance(final org.uberfire.java.nio.file.Path nioProjectRootPath) {
    final Path projectRootPath = Paths.convert(nioProjectRootPath);

    return new Project(
        projectRootPath,
        Paths.convert(nioProjectRootPath.resolve(POM_PATH)),
        Paths.convert(nioProjectRootPath.resolve(KMODULE_PATH)),
        Paths.convert(nioProjectRootPath.resolve(PROJECT_IMPORTS_PATH)),
        projectRootPath.getFileName());
  }
Exemplo n.º 4
0
  @Override
  public void copy(final Path pathToPomXML, final String newName, final String comment) {
    try {
      final org.uberfire.java.nio.file.Path projectDirectory =
          Paths.convert(pathToPomXML).getParent();
      final org.uberfire.java.nio.file.Path newProjectPath =
          projectDirectory.resolveSibling(newName);

      final POM content = pomService.load(pathToPomXML);

      if (newProjectPath.equals(projectDirectory)) {
        return;
      }

      if (ioService.exists(newProjectPath)) {
        throw new FileAlreadyExistsException(newProjectPath.toString());
      }

      content.setName(newName);
      final Path newPathToPomXML = Paths.convert(newProjectPath.resolve("pom.xml"));
      ioService.startBatch();
      ioService.copy(projectDirectory, newProjectPath, makeCommentedOption(comment));
      pomService.save(newPathToPomXML, content, null, comment);
      ioService.endBatch();
      final Project newProject = resolveProject(Paths.convert(newProjectPath));
      newProjectEvent.fire(new NewProjectEvent(newProject, sessionInfo));

    } catch (final Exception e) {
      throw ExceptionUtilities.handleException(e);
    }
  }
Exemplo n.º 5
0
  @Override
  public Package resolveParentPackage(final Package pkg) {
    final Set<String> packageNames = new HashSet<String>();

    final org.uberfire.java.nio.file.Path nioProjectRootPath =
        Paths.convert(pkg.getProjectRootPath());
    packageNames.addAll(
        getPackageNames(
            nioProjectRootPath,
            Paths.convert(pkg.getPackageMainSrcPath()).getParent(),
            true,
            false,
            false));

    // Construct Package objects for each package name
    for (String packagePathSuffix : packageNames) {
      for (String src : sourcePaths) {
        if (packagePathSuffix == null) {
          return null;
        }
        final org.uberfire.java.nio.file.Path nioPackagePath =
            nioProjectRootPath.resolve(src).resolve(packagePathSuffix);
        if (Files.exists(nioPackagePath)) {
          return resolvePackage(Paths.convert(nioPackagePath));
        }
      }
    }

    return null;
  }
  @Test
  public void testDeleteProjectObserverBridge() throws URISyntaxException {
    final URI fs = new URI("git://test");
    try {
      FileSystems.getFileSystem(fs);
    } catch (FileSystemNotFoundException e) {
      FileSystems.newFileSystem(fs, new HashMap<String, Object>());
    }

    final Path path = mock(Path.class);
    final org.uberfire.java.nio.file.Path nioPath = mock(org.uberfire.java.nio.file.Path.class);
    when(path.getFileName()).thenReturn("pom.xml");
    when(path.toURI()).thenReturn("git://test/p0/pom.xml");
    when(nioPath.getParent()).thenReturn(nioPath);
    when(nioPath.resolve(any(String.class))).thenReturn(nioPath);
    when(nioPath.toUri()).thenReturn(URI.create("git://test/p0/pom.xml"));
    when(nioPath.getFileSystem()).thenReturn(FileSystems.getFileSystem(fs));
    when(ioService.get(any(URI.class))).thenReturn(nioPath);

    final SessionInfo sessionInfo = mock(SessionInfo.class);
    final Event<DeleteProjectEvent> deleteProjectEvent = mock(Event.class);
    final AbstractProjectService projectServiceSpy = spy(projectService);

    final DeleteProjectObserverBridge bridge =
        new DeleteProjectObserverBridge(ioService, projectServiceSpy, deleteProjectEvent);

    bridge.onBatchResourceChanges(new ResourceDeletedEvent(path, "message", sessionInfo));

    verify(deleteProjectEvent, times(1)).fire(any(DeleteProjectEvent.class));

    verify(projectServiceSpy, times(0))
        .newProject(any(Repository.class), any(String.class), any(POM.class), any(String.class));
    verify(projectServiceSpy, times(1))
        .simpleProjectInstance(any(org.uberfire.java.nio.file.Path.class));
  }
Exemplo n.º 7
0
  private Package makePackage(final Project project, final Path resource) {
    final Path projectRoot = project.getRootPath();
    final org.uberfire.java.nio.file.Path nioProjectRoot = Paths.convert(projectRoot);
    final org.uberfire.java.nio.file.Path nioMainSrcPath = nioProjectRoot.resolve(MAIN_SRC_PATH);
    final org.uberfire.java.nio.file.Path nioTestSrcPath = nioProjectRoot.resolve(TEST_SRC_PATH);
    final org.uberfire.java.nio.file.Path nioMainResourcesPath =
        nioProjectRoot.resolve(MAIN_RESOURCES_PATH);
    final org.uberfire.java.nio.file.Path nioTestResourcesPath =
        nioProjectRoot.resolve(TEST_RESOURCES_PATH);

    org.uberfire.java.nio.file.Path nioResource = Paths.convert(resource);

    if (Files.isRegularFile(nioResource)) {
      nioResource = nioResource.getParent();
    }

    String packageName = null;
    org.uberfire.java.nio.file.Path packagePath = null;
    if (nioResource.startsWith(nioMainSrcPath)) {
      packagePath = nioMainSrcPath.relativize(nioResource);
      packageName = packagePath.toString().replaceAll("/", ".");
    } else if (nioResource.startsWith(nioTestSrcPath)) {
      packagePath = nioTestSrcPath.relativize(nioResource);
      packageName = packagePath.toString().replaceAll("/", ".");
    } else if (nioResource.startsWith(nioMainResourcesPath)) {
      packagePath = nioMainResourcesPath.relativize(nioResource);
      packageName = packagePath.toString().replaceAll("/", ".");
    } else if (nioResource.startsWith(nioTestResourcesPath)) {
      packagePath = nioTestResourcesPath.relativize(nioResource);
      packageName = packagePath.toString().replaceAll("/", ".");
    }

    // Resource was not inside a package
    if (packageName == null) {
      return null;
    }

    final Path mainSrcPath = Paths.convert(nioMainSrcPath.resolve(packagePath));
    final Path testSrcPath = Paths.convert(nioTestSrcPath.resolve(packagePath));
    final Path mainResourcesPath = Paths.convert(nioMainResourcesPath.resolve(packagePath));
    final Path testResourcesPath = Paths.convert(nioTestResourcesPath.resolve(packagePath));

    final String displayName = getPackageDisplayName(packageName);

    final Package pkg =
        new Package(
            project.getRootPath(),
            mainSrcPath,
            testSrcPath,
            mainResourcesPath,
            testResourcesPath,
            packageName,
            displayName,
            getPackageRelativeCaption(displayName, resource.getFileName()));
    return pkg;
  }
  private void restoreContainers() {
    DirectoryStream<Path> ds = null;
    try {
      Path containersPath = fs.getPath("/containers");
      if (ios.exists(containersPath)) {
        ds =
            ios.newDirectoryStream(
                containersPath,
                new Filter<Path>() {

                  @Override
                  public boolean accept(Path entry) throws IOException {
                    return Files.isDirectory(entry);
                  }
                });
        if (ds != null) {
          XStream xs = XStreamXml.newXStreamMarshaller(KieServerImpl.class.getClassLoader());
          for (Path entry : ds) {
            BufferedReader reader = null;
            try {
              logger.info("Restoring state of kie container '" + entry.getFileName() + "'");
              reader =
                  Files.newBufferedReader(
                      entry.resolve(CONTAINER_STATE_FILE), Charset.forName("UTF-8"));
              KieContainerResource resource = (KieContainerResource) xs.fromXML(reader);
              restore(resource);
            } catch (Exception e) {
              logger.error("Error restoring kie container state", e);
            } finally {
              if (reader != null) {
                try {
                  reader.close();
                } catch (java.io.IOException e) {
                }
              }
            }
          }
        }
      }
    } catch (Exception e) {
      logger.error("Error restoring kie server state", e);
    } finally {
      if (ds != null) ds.close();
    }
  }
  @PostConstruct
  public void setup() {
    try {
      fileSystem =
          ioService.newFileSystem(
              URI.create("default://bpmn"),
              new HashMap<String, Object>() {
                {
                  put("init", Boolean.TRUE);
                  put("internal", Boolean.TRUE);
                }
              });
    } catch (final FileSystemAlreadyExistsException e) {
      fileSystem = ioService.getFileSystem(URI.create("default://bpmn"));
    }
    this.root = fileSystem.getRootDirectories().iterator().next();

    ioService.write(
        root.resolve("file1.bpmn"), BpmnPersistence.getInstance().marshal(new ProcessNode()));
  }
Exemplo n.º 10
0
 private boolean hasKModule(final org.uberfire.java.nio.file.Path path) {
   final org.uberfire.java.nio.file.Path kmodulePath = path.resolve(KMODULE_PATH);
   return Files.exists(kmodulePath);
 }
Exemplo n.º 11
0
 private boolean hasPom(final org.uberfire.java.nio.file.Path path) {
   final org.uberfire.java.nio.file.Path pomPath = path.resolve(POM_PATH);
   return Files.exists(pomPath);
 }
Exemplo n.º 12
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;
    }
  }