Java Code Examples for org.jboss.forge.addon.projects.Project#getFacet()

The following examples show how to use org.jboss.forge.addon.projects.Project#getFacet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DetectFractionsCommand.java    From thorntail-addon with Eclipse Public License 1.0 6 votes vote down vote up
private DirectoryResource getTargetDirectory(Project project)
{
   MavenFacet mavenFacet = project.getFacet(MavenFacet.class);
   Build build = mavenFacet.getModel().getBuild();
   String targetFolderName;
   if (build != null && build.getOutputDirectory() != null)
   {
      targetFolderName = mavenFacet.resolveProperties(build.getOutputDirectory());
   }
   else
   {
      targetFolderName = "target" + File.separator + "classes";
   }
   DirectoryResource projectRoot = project.getRoot().reify(DirectoryResource.class);
   return projectRoot.getChildDirectory(targetFolderName);
}
 
Example 2
Source File: Fabric8SetupStep.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
public static MavenPluginBuilder setupFabricMavenPlugin(Project project) {
    MavenPluginBuilder pluginBuilder;
    MavenPlugin plugin = MavenHelpers.findPlugin(project, "io.fabric8", "fabric8-maven-plugin");
    if (plugin != null) {
        // if there is an existing then leave it as-is
        LOG.info("Found existing fabric8-maven-plugin");
        pluginBuilder = null;
    } else {
        LOG.info("Adding fabric8-maven-plugin");
        // add fabric8 plugin
        pluginBuilder = MavenPluginBuilder.create()
                .setCoordinate(MavenHelpers.createCoordinate("io.fabric8", "fabric8-maven-plugin", VersionHelper.fabric8MavenPluginVersion()))
                .addExecution(ExecutionBuilder.create().setId("fmp").addGoal("resource").addGoal("helm").addGoal("build"));
    }

    if (pluginBuilder != null) {
        MavenPluginFacet pluginFacet = project.getFacet(MavenPluginFacet.class);
        pluginFacet.addPlugin(pluginBuilder);
    }
    return pluginBuilder;
}
 
Example 3
Source File: AbstractCamelProjectCommand.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
protected String asRelativeFile(UIContext context, String currentFile) {
    Project project = getSelectedProject(context);

    JavaSourceFacet javaSourceFacet = null;
    ResourcesFacet resourcesFacet = null;
    WebResourcesFacet webResourcesFacet = null;
    if (project.hasFacet(JavaSourceFacet.class)) {
        javaSourceFacet = project.getFacet(JavaSourceFacet.class);
    }
    if (project.hasFacet(ResourcesFacet.class)) {
        resourcesFacet = project.getFacet(ResourcesFacet.class);
    }
    if (project.hasFacet(WebResourcesFacet.class)) {
        webResourcesFacet = project.getFacet(WebResourcesFacet.class);
    }
    return asRelativeFile(currentFile, javaSourceFacet, resourcesFacet, webResourcesFacet);
}
 
Example 4
Source File: AbstractCamelProjectCommand.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
protected CurrentLineCompleter createCurrentLineCompleter(int lineNumber, String file, UIContext context) throws Exception {
    Project project = getSelectedProject(context);

    JavaSourceFacet sourceFacet = null;
    ResourcesFacet resourcesFacet = null;
    WebResourcesFacet webResourcesFacet = null;
    if (project.hasFacet(JavaSourceFacet.class)) {
        sourceFacet = project.getFacet(JavaSourceFacet.class);
    }
    if (project.hasFacet(ResourcesFacet.class)) {
        resourcesFacet = project.getFacet(ResourcesFacet.class);
    }
    if (project.hasFacet(WebResourcesFacet.class)) {
        webResourcesFacet = project.getFacet(WebResourcesFacet.class);
    }

    String relativeFile = asRelativeFile(context, file);
    return new CurrentLineCompleter(lineNumber, relativeFile, sourceFacet, resourcesFacet, webResourcesFacet);
}
 
Example 5
Source File: MavenHelpers.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
public static MavenPlugin findPlugin(Project project, String groupId, String artifactId) {
    if (project != null) {
        MavenPluginFacet pluginFacet = project.getFacet(MavenPluginFacet.class);
        if (pluginFacet != null) {
            List<MavenPlugin> plugins = pluginFacet.listConfiguredPlugins();
            if (plugins != null) {
                for (MavenPlugin plugin : plugins) {
                    Coordinate coordinate = plugin.getCoordinate();
                    if (coordinate != null) {
                        if (Objects.equal(groupId, coordinate.getGroupId()) &&
                                Objects.equal(artifactId, coordinate.getArtifactId())) {
                            return plugin;
                        }
                    }
                }
            }
        }
    }
    return null;
}
 
Example 6
Source File: ScaffoldableEntitySelectionWizard.java    From angularjs-addon with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void initializeUI(UIBuilder builder) throws Exception
{
   UIContext uiContext = builder.getUIContext();
   Project project = getSelectedProject(uiContext);

   JPAFacet<?> persistenceFacet = project.getFacet(JPAFacet.class);
   List<JavaClassSource> allEntities = persistenceFacet.getAllEntities();
   List<JavaClassSource> supportedEntities = new ArrayList<>();
   for (JavaClassSource entity : allEntities)
   {
      for (Member<?> member : entity.getMembers())
      {
         // FORGE-823 Only add entities with @Id as valid entities for REST resource generation.
         // Composite keys are not yet supported.
         if (member.hasAnnotation(Id.class))
         {
            supportedEntities.add(entity);
         }
      }
   }
   targets.setValueChoices(supportedEntities);
   targets.setItemLabelConverter(JavaClassSource::getQualifiedName);
   builder.add(targets).add(generateRestResources);
}
 
Example 7
Source File: CamelNewComponentCommand.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
@Override
public void initializeUI(UIBuilder builder) throws Exception {
    Project project = getSelectedProject(builder.getUIContext());
    final JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);

    // filter the list of components based on consumer and producer only
    configureComponentName(project, componentName, false, false);

    targetPackage.setCompleter(new PackageNameCompleter(facet));
    targetPackage.addValidator(new PackageNameValidator());
    targetPackage.getFacet(HintsFacet.class).setInputType(InputType.JAVA_PACKAGE_PICKER);
    // if there is only one package then use that as default
    Set<String> packages = new RouteBuilderCompleter(facet).getPackages();
    if (packages.size() == 1) {
        targetPackage.setDefaultValue(first(packages));
    }

    className.addValidator(new ClassNameValidator(false));
    className.getFacet(HintsFacet.class).setInputType(InputType.JAVA_CLASS_PICKER);

    builder.add(componentName).add(instanceName).add(targetPackage).add(className);
}
 
Example 8
Source File: ProjectHelper.java    From angularjs-addon with Eclipse Public License 1.0 5 votes vote down vote up
public void addNotNullConstraint(Project project, JavaClassSource klass, String propertyName, boolean onAccessor,
         String message)
         throws FileNotFoundException
{
   PropertySource<JavaClassSource> property = klass.getProperty(propertyName);
   final Annotation<JavaClassSource> constraintAnnotation = addConstraintOnProperty(property, onAccessor,
            NotNull.class,
            message);

   JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class);
   javaSourceFacet.saveJavaSource(constraintAnnotation.getOrigin());
}
 
Example 9
Source File: CamelGetOverviewCommand.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
@Override
public Result execute(UIExecutionContext context) throws Exception {
    Project project = getSelectedProject(context);

    // does the project already have camel?
    Dependency core = findCamelCoreDependency(project);
    if (core == null) {
        return Results.fail("The project does not include camel-core");
    }

    ProjectDto camelProject = new ProjectDto();

    ResourcesFacet resourcesFacet = project.getFacet(ResourcesFacet.class);
    WebResourcesFacet webResourcesFacet = null;
    if (project.hasFacet(WebResourcesFacet.class)) {
        webResourcesFacet = project.getFacet(WebResourcesFacet.class);
    }

    // use value choices instead of completer as that works better in web console
    XmlEndpointsCompleter xmlEndpointCompleter = new XmlEndpointsCompleter(resourcesFacet, webResourcesFacet, null);
    JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class);

    // use value choices instead of completer as that works better in web console
    RouteBuilderEndpointsCompleter javaEndpointsCompleter = new RouteBuilderEndpointsCompleter(javaSourceFacet, null);

    camelProject.addEndpoints(javaEndpointsCompleter.getEndpoints());
    camelProject.addEndpoints(xmlEndpointCompleter.getEndpoints());

    CamelCurrentComponentsFinder componentsFinder = new CamelCurrentComponentsFinder(getCamelCatalog(), project);
    List<ComponentDto> currentComponents = componentsFinder.findCurrentComponents();
    camelProject.setComponents(currentComponents);

    String result = formatResult(camelProject);
    return Results.success(result);
}
 
Example 10
Source File: AbstractCamelProjectCommand.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
protected Set<String> discoverCustomCamelComponentsOnClasspathAndAddToCatalog(CamelCatalog camelCatalog, Project project) {
    Set<String> answer = new LinkedHashSet<>();

    // find the dependency again because forge don't associate artifact on the returned dependency when installed
    MavenDependencyFacet facet = project.getFacet(MavenDependencyFacet.class);
    List<Dependency> list = facet.getEffectiveDependencies();

    for (Dependency dep : list) {
        Properties properties = loadComponentProperties(dep);
        if (properties != null) {
            String components = (String) properties.get("components");
            if (components != null) {
                String[] part = components.split("\\s");
                for (String scheme : part) {
                    if (!camelCatalog.findComponentNames().contains(scheme)) {
                        // find the class name
                        String javaType = extractComponentJavaType(dep, scheme);
                        if (javaType != null) {
                            String json = loadComponentJSonSchema(dep, scheme);
                            if (json != null) {
                                camelCatalog.addComponent(scheme, javaType, json);
                                answer.add(scheme);
                            }
                        }
                    }
                }
            }
        }
    }

    return answer;
}
 
Example 11
Source File: AbstractCamelProjectCommand.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
protected SpringBootConfigurationFileCompleter createSpringBootConfigurationFileCompleter(UIContext context, Function<String, Boolean> filter) {
    Project project = getSelectedProject(context);
    ResourcesFacet resourcesFacet = null;
    if (project.hasFacet(ResourcesFacet.class)) {
        resourcesFacet = project.getFacet(ResourcesFacet.class);
    }
    return new SpringBootConfigurationFileCompleter(resourcesFacet, filter);
}
 
Example 12
Source File: AbstractCamelProjectCommand.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
protected XmlFileCompleter createXmlFileCompleter(Project project, Function<String, Boolean> filter) {
    ResourcesFacet resourcesFacet = null;
    WebResourcesFacet webResourcesFacet = null;
    if (project.hasFacet(ResourcesFacet.class)) {
        resourcesFacet = project.getFacet(ResourcesFacet.class);
    }
    if (project.hasFacet(WebResourcesFacet.class)) {
        webResourcesFacet = project.getFacet(WebResourcesFacet.class);
    }

    return new XmlFileCompleter(resourcesFacet, webResourcesFacet, filter);
}
 
Example 13
Source File: AbstractCamelProjectCommand.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
protected XmlEndpointsCompleter createXmlEndpointsCompleter(Project project, Function<String, Boolean> filter) {
    ResourcesFacet resourcesFacet = null;
    WebResourcesFacet webResourcesFacet = null;
    if (project.hasFacet(ResourcesFacet.class)) {
        resourcesFacet = project.getFacet(ResourcesFacet.class);
    }
    if (project.hasFacet(WebResourcesFacet.class)) {
        webResourcesFacet = project.getFacet(WebResourcesFacet.class);
    }

    return new XmlEndpointsCompleter(resourcesFacet, webResourcesFacet, filter);
}
 
Example 14
Source File: AngularScaffoldProvider.java    From angularjs-addon with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Obtains the root path for REST resources so that the AngularJS resource factory will be generated with the correct
 * REST resource URL.
 *
 * @return The root path of the REST resources generated by the Forge REST plugin.
 */
private String getRootResourcePath(Project project)
{
   RestFacet rest = project.getFacet(RestFacet.class);
   String resourceRootPath = trimSlashes(rest.getApplicationPath());
   return resourceRootPath;
}
 
Example 15
Source File: AbstractDevOpsCommand.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
public MavenFacet getMavenFacet(UIContextProvider builder) {
    final Project project = getSelectedProject(builder);
    if (project != null) {
        MavenFacet maven = project.getFacet(MavenFacet.class);
        return maven;
    }
    return null;
}
 
Example 16
Source File: JSONRestResourceFromEntityCommand.java    From angularjs-addon with Eclipse Public License 1.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void initializeUI(UIBuilder builder) throws Exception
{
   UIContext context = builder.getUIContext();
   Project project = getSelectedProject(context);
   JPAFacet<PersistenceCommonDescriptor> persistenceFacet = project.getFacet(JPAFacet.class);
   JavaSourceFacet javaSourceFacet = project.getFacet(JavaSourceFacet.class);
   List<String> persistenceUnits = new ArrayList<>();
   List<PersistenceUnitCommon> allUnits = persistenceFacet.getConfig().getAllPersistenceUnit();
   for (PersistenceUnitCommon persistenceUnit : allUnits)
   {
      persistenceUnits.add(persistenceUnit.getName());
   }
   if (!persistenceUnits.isEmpty())
   {
      persistenceUnit.setValueChoices(persistenceUnits).setDefaultValue(persistenceUnits.get(0));
   }

   // TODO: May detect where @Path resources are located
   packageName.setDefaultValue(javaSourceFacet.getBasePackage() + ".rest");

   RestResourceGenerator defaultResourceGenerator = null;
   for (RestResourceGenerator generator : resourceGenerators)
   {
      if (generator.getName().equals(RestGenerationConstants.JPA_ENTITY))
      {
         defaultResourceGenerator = generator;
      }
   }
   generator.setDefaultValue(defaultResourceGenerator);
   generator.setItemLabelConverter(
            context.getProvider().isGUI() ? RestResourceGenerator::getDescription : RestResourceGenerator::getName);
   builder.add(generator).add(packageName).add(persistenceUnit);
}
 
Example 17
Source File: ScaffoldableEntitySelectionWizard.java    From angularjs-addon with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void validate(UIValidationContext context)
{
   // Verify if the selected JPA entities have corresponding JAX-RS resources.
   // If yes, then raise warnings if they will be overwritten
   Boolean shouldGenerateRestResources = generateRestResources.getValue();
   List<String> entitiesWithRestResources = new ArrayList<>();
   if (shouldGenerateRestResources.equals(Boolean.TRUE) && targets.getValue() != null)
   {
      for (JavaClassSource klass : targets.getValue())
      {
         Project project = getSelectedProject(context.getUIContext());
         JavaSourceFacet javaSource = project.getFacet(JavaSourceFacet.class);
         RestResourceTypeVisitor restTypeVisitor = new RestResourceTypeVisitor();
         String entityTable = getEntityTable(klass);
         String proposedResourcePath = "/" + inflector.pluralize(entityTable.toLowerCase());
         restTypeVisitor.setProposedPath(proposedResourcePath);
         javaSource.visitJavaSources(restTypeVisitor);
         if (restTypeVisitor.isFound())
         {
            entitiesWithRestResources.add(klass.getQualifiedName());
         }
      }
   }
   if (!entitiesWithRestResources.isEmpty())
   {
      context.addValidationWarning(targets, "Some of the selected entities " + entitiesWithRestResources.toString()
               + " already have associated REST resources that will be overwritten.");
   }
}
 
Example 18
Source File: DetectFractionsCommand.java    From thorntail-addon with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Result execute(UIExecutionContext executionContext) throws Exception
{
   UIProgressMonitor progressMonitor = executionContext.getProgressMonitor();
   UIOutput output = executionContext.getUIContext().getProvider().getOutput();
   FractionUsageAnalyzer analyzer = ThorntailFacet.getFractionUsageAnalyzer();
   DirectoryResource value = inputDir.getValue();
   analyzer.source(value.getUnderlyingResourceObject());
   Project project = getSelectedProject(executionContext);
   int total = 1;
   if (build.getValue())
      total++;
   if (depend.getValue())
      total++;
   progressMonitor.beginTask("Detecting fractions", total);
   if (build.getValue())
   {
      PackagingFacet packaging = project.getFacet(PackagingFacet.class);
      progressMonitor.setTaskName("Building the project...");
      FileResource<?> finalArtifact = packaging.createBuilder().build(output.out(), output.err())
               .reify(FileResource.class);
      analyzer.source(finalArtifact.getUnderlyingResourceObject());
      progressMonitor.worked(1);
   }
   Collection<FractionDescriptor> detectedFractions = analyzer.detectNeededFractions();
   output.info(output.out(), "Detected fractions: " + detectedFractions);
   progressMonitor.worked(1);
   if (depend.getValue() && detectedFractions.size() > 0)
   {
      progressMonitor.setTaskName("Adding missing fractions as project dependencies...");
      ThorntailFacet facet = project.getFacet(ThorntailFacet.class);
      detectedFractions.removeAll(facet.getInstalledFractions());
      // detectedFractions.remove(fractionList.getFractionDescriptor(Swarm.DEFAULT_FRACTION_GROUPID, "container"));
      if (detectedFractions.isEmpty())
      {
         output.warn(output.out(), "Project already contains all the installed fractions. Doing nothing.");
      }
      else
      {
         output.info(output.out(), "Installing the following dependencies: " + detectedFractions);
         facet.installFractions(detectedFractions);
      }
      progressMonitor.worked(1);
   }
   progressMonitor.done();
   return Results.success();
}
 
Example 19
Source File: CamelNewRouteBuilderCommand.java    From fabric8-forge with Apache License 2.0 4 votes vote down vote up
@Override
public Result execute(UIExecutionContext context) throws Exception {
    Project project = getSelectedProject(context);
    JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);

    // does the project already have camel?
    Dependency core = findCamelCoreDependency(project);
    if (core == null) {
        return Results.fail("The project does not include camel-core");
    }

    // do we already have a class with the name
    String fqn = targetPackage.getValue() != null ? targetPackage.getValue() + "." + name.getValue() : name.getValue();

    JavaResource existing = facet.getJavaResource(fqn);
    if (existing != null && existing.exists()) {
        return Results.fail("A class with name " + fqn + " already exists");
    }

    // need to parse to be able to extends another class
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    javaClass.setName(name.getValue());
    if (targetPackage.getValue() != null) {
        javaClass.setPackage(targetPackage.getValue());
    }
    javaClass.setSuperType("RouteBuilder");
    javaClass.addImport("org.apache.camel.builder.RouteBuilder");

    boolean cdi = CamelCommandsHelper.isCdiProject(getSelectedProject(context));
    boolean spring = CamelCommandsHelper.isSpringProject(getSelectedProject(context));

    if (cdi) {
        javaClass.addAnnotation("javax.inject.Singleton");
    } else if (spring) {
        javaClass.addAnnotation("org.springframework.stereotype.Component");
    }

    javaClass.addMethod()
            .setPublic()
            .setReturnTypeVoid()
            .setName("configure")
            .addThrows(Exception.class).setBody("");

    JavaResource javaResource = facet.saveJavaSource(javaClass);

    // if we are in an GUI editor then open the file
    if (isRunningInGui(context.getUIContext())) {
        context.getUIContext().setSelection(javaResource);
    }

    return Results.success("Created new RouteBuilder class " + name.getValue());
}
 
Example 20
Source File: ClassScanner.java    From fabric8-forge with Apache License 2.0 4 votes vote down vote up
public static ClassScanner newInstance(Project project) {
	ClassLoaderFacet classLoaderFacet = project.getFacet(ClassLoaderFacet.class);
	ClassScanner answer = new ClassScanner(classLoaderFacet.getClassLoader());
	answer.setProject(project);
	return answer;
}