Skip to content

Commit

Permalink
ProjectFacets must be installed via FacetFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Jun 13, 2013
1 parent 4903623 commit e6d84c4
Show file tree
Hide file tree
Showing 18 changed files with 409 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class ForgeContainerAPIFacet extends AbstractFacet<Project> implements Pr
@Override
public boolean install()
{
installer.install(getOrigin(), FORGE_API_DEPENDENCY);
return true;
Dependency dependency = installer.install(getOrigin(), FORGE_API_DEPENDENCY);
return dependency != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,28 @@
package org.jboss.forge.furnace.util;

/**
* Utility class for creating pre-condition assertions in method implementations.
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class Assert
{
public static void notNull(Object object, String message) throws IllegalStateException
/**
* Assert that the given {@link Object} is not <code>null</code>; otherwise, throw an
* {@link IllegalArgumentException} with the given message.
*/
public static void notNull(Object object, String message) throws IllegalArgumentException
{
if (object == null)
{
throw new IllegalArgumentException(message);
}
}

/**
* Assert that the given boolean value is <code>true</code>; otherwise, throw an {@link IllegalArgumentException}
* with the given message.
*/
public static void isTrue(boolean value, String message)
{
if (value != true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @param <FACETTYPE> the base {@link Facet} type supported by this {@link Faceted} type.
*/
public abstract class AbstractFaceted<FACETTYPE extends Facet<?>> implements Faceted<FACETTYPE>
public abstract class AbstractFaceted<FACETTYPE extends Facet<?>> implements MutableFaceted<FACETTYPE>
{
private Set<FACETTYPE> facets = Collections.newSetFromMap(new ConcurrentHashMap<FACETTYPE, Boolean>());

Expand Down Expand Up @@ -68,7 +68,9 @@ public boolean install(FACETTYPE facet)
{
if (facet.getOrigin() != this)
throw new IllegalArgumentException("[" + facet + "].getOrigin() was [" + facet.getOrigin()
+ "] but needed to be [" + this + "].");
+ "] but needed to be [" + this + "]. If your facet type implements "
+ MutableOrigin.class.getSimpleName() + ", " +
"ensure that a valid origin was supplied during facet creation.");

if (supports(facet))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

/**
* Responsible for instantiation of new {@link Facet} instances.
*
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
@Exported
public interface FacetFactory
{
/**
* Create a new instance of the given {@link Facet} type.
*
*
* @throws FacetNotFoundException if no implementation can be found.
*/
public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> FACET create(Class<FACET> type)
Expand All @@ -27,7 +27,7 @@ public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> FACET cre
/**
* Create a new instance of the given {@link Facet} type. If it is also an instance of {@link MutableOrigin}, then
* use the given origin instance as the {@link Facet#getOrigin()}.
*
*
* @throws FacetNotFoundException if no implementation can be found.
*/
public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> FACET create(Class<FACET> type, E origin)
Expand All @@ -49,9 +49,24 @@ public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> Iterable<

/**
* Create and installs a new instance of the given {@link Facet} type.
*
*
* @return the new {@link Facet} instance. (Never null.)
*
* @throws FacetNotFoundException if no implementation can be found.
* @throws IllegalStateException if installation failed
*/
public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> FACET install(Class<FACET> type, E origin)
throws FacetNotFoundException;

/**
* Install a {@link Facet} instance into the given origin.
*
* @throws IllegalArgumentException when the given {@link Facet#getOrigin()} is not equal to the specified
* {@link Faceted} origin instance, or if the given {@link Faceted} type does not implement
* {@link MutableFaceted}.
*
* @return <code>true</code> if installation was successful; <code>false</code> if installation failed.
*/
public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> boolean install(FACET facet, E origin)
throws IllegalArgumentException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* @author <a href="http://community.jboss.org/people/kenfinni">Ken Finnigan</a>
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*
* @param <FACETTYPE> the base {@link Facet} type supported by this {@link Faceted} type.
*/
Expand Down Expand Up @@ -47,16 +48,6 @@ public interface Faceted<FACETTYPE extends Facet<?>>
*/
<F extends FACETTYPE> Iterable<F> getFacets(Class<F> type);

/**
* Install and register the given {@link Facet}. If the facet is already installed, return true.
*/
boolean install(FACETTYPE facet);

/**
* Remove the given {@link Facet} from the internal collection of installed facets.
*/
boolean uninstall(FACETTYPE facet);

/**
* Return true if the given {@link Facet} is supported.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.addon.facets;

/**
* A mutable {@link Faceted} type.
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*
* @param <FACETTYPE> the base {@link Facet} type supported by this {@link MutableFaceted} type.
*/
public interface MutableFaceted<FACETTYPE extends Facet<?>> extends Faceted<FACETTYPE>
{
/**
* Install and register the given {@link Facet}. If the facet is already installed, return true.
*/
boolean install(FACETTYPE facet);

/**
* Remove the given {@link Facet} from the internal collection of installed facets.
*/
boolean uninstall(FACETTYPE facet);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@

import javax.inject.Inject;

import org.jboss.forge.addon.facets.Facet;
import org.jboss.forge.addon.facets.FacetFactory;
import org.jboss.forge.addon.facets.FacetNotFoundException;
import org.jboss.forge.addon.facets.Faceted;
import org.jboss.forge.addon.facets.MutableOrigin;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.services.ExportedInstance;
import org.jboss.forge.furnace.util.Assert;
Expand Down Expand Up @@ -79,16 +74,46 @@ public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> Iterable<
}

@Override
@SuppressWarnings("unchecked")
public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> FACET install(Class<FACET> type, E origin)
throws FacetNotFoundException
{
FACET facet = create(type, origin);
Faceted<FACET> faceted = (Faceted<FACET>) origin;
if (!faceted.hasFacet(type))
if (!install(facet, origin))
{
faceted.install(facet);
throw new IllegalStateException("Facet type [" + type.getName()
+ "] could not be installed completely into [" + origin
+ "] of type [" + origin.getClass().getName()
+ "]. You may wish to check for inconsistent origin state.");
}
return facet;
}

@Override
@SuppressWarnings("unchecked")
public <FACET extends Facet<E>, E extends Faceted<? extends Facet<?>>> boolean install(FACET facet, E origin)
{
Assert.notNull(origin, "Facet instance must not be null.");
Assert.notNull(origin, "Origin instance must not be null.");

Faceted<FACET> faceted = (Faceted<FACET>) origin;
Assert.isTrue(faceted instanceof MutableFaceted, "The given origin [" + origin + "] is not an instance of ["
+ MutableFaceted.class.getName() + "], and does not support " + Facet.class.getSimpleName()
+ " installation.");

if (facet.getOrigin() == null && facet instanceof MutableOrigin)
{
((MutableOrigin<E>) facet).setOrigin(origin);
}

Assert.isTrue(origin.equals(facet.getOrigin()), "The given origin [" + origin + "] is not an instance of ["
+ MutableFaceted.class.getName() + "], and does not support " + Facet.class.getSimpleName()
+ " installation.");

boolean result = false;
if (faceted.hasFacet((Class<? extends FACET>) facet.getClass()))
result = true;
else
result = ((MutableFaceted<FACET>) faceted).install(facet);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;


@RunWith(Arquillian.class)
public class CDIFacetTest
{
Expand All @@ -50,9 +49,8 @@ public static ForgeArchive getDeployment()
public void testBeansXMLCreatedWhenInstalled() throws Exception
{
Project project = projectFactory.createTempProject();
CDIFacet cdiFacet = facetFactory.create(CDIFacet.class, project);
CDIFacet cdiFacet = facetFactory.install(CDIFacet.class, project);
assertNotNull(cdiFacet);
project.install(cdiFacet);
assertTrue(project.hasFacet(CDIFacet.class));
BeansDescriptor config = project.getFacet(CDIFacet.class).getConfig();
assertNotNull(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@

import org.jboss.forge.addon.facets.FacetFactory;
import org.jboss.forge.addon.maven.dependencies.MavenDependencyResolver;
import org.jboss.forge.addon.maven.projects.MavenFacet;
import org.jboss.forge.addon.maven.projects.MavenPluginFacet;
import org.jboss.forge.addon.maven.projects.facets.MavenDependencyFacet;
import org.jboss.forge.addon.maven.projects.facets.MavenMetadataFacet;
import org.jboss.forge.addon.maven.projects.facets.MavenPackagingFacet;
import org.jboss.forge.addon.maven.projects.facets.MavenResourceFacet;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.projects.ProjectLocator;
import org.jboss.forge.addon.projects.facets.DependencyFacet;
import org.jboss.forge.addon.projects.facets.MetadataFacet;
import org.jboss.forge.addon.projects.facets.PackagingFacet;
import org.jboss.forge.addon.projects.facets.ResourceFacet;
import org.jboss.forge.addon.resource.DirectoryResource;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.resource.Resource;
Expand All @@ -42,21 +36,16 @@ public Project createProject(final DirectoryResource dir)
{
Project project = new MavenProject(dir);

MavenFacet mavenFacet = factory.create(MavenFacet.class, project);
MavenPluginFacet mavenPluginFacet = factory.create(MavenPluginFacet.class, project);
MetadataFacet metadataFacet = factory.create(MavenMetadataFacet.class, project);
PackagingFacet packagingFacet = factory.create(MavenPackagingFacet.class, project);
DependencyFacet dependencyFacet = factory.create(MavenDependencyFacet.class, project);
ResourceFacet resourceFacet = factory.create(MavenResourceFacet.class, project);

if (!(project.install(mavenFacet)
&& project.install(metadataFacet)
&& project.install(packagingFacet)
&& project.install(dependencyFacet)
&& project.install(resourceFacet)
&& project.install(mavenPluginFacet)

))
try
{
factory.install(MavenFacet.class, project);
factory.install(MavenPluginFacet.class, project);
factory.install(MavenMetadataFacet.class, project);
factory.install(MavenPackagingFacet.class, project);
factory.install(MavenDependencyFacet.class, project);
factory.install(MavenResourceFacet.class, project);
}
catch (RuntimeException e)
{
throw new IllegalStateException("Could not install Maven into Project located at ["
+ dir.getFullyQualifiedName() + "]");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.projects.facets;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.jboss.forge.addon.projects.ProjectFacet;

/**
* The annotated element requires the given {@link ProjectFacet}
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface RequiresFacet
{
/**
* The facets required by the annotated {@link ProjectFacet}
*/
Class<? extends ProjectFacet>[] value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.projects.facets;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.jboss.forge.addon.projects.ProjectFacet;

/**
* The annotated element requires the given {@link PackagingType}s
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface RequiresPackagingType
{
/**
* The array of packaging types required by the annotated {@link ProjectFacet}.
*/
String[] value();
}

0 comments on commit e6d84c4

Please sign in to comment.