Skip to content

Commit

Permalink
First attempt at FORGE-1102
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Sep 4, 2013
1 parent b25e48c commit e47406c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public interface Imported<T> extends Iterable<T>
*/
void release(T instance);

/**
* Get a fully constructed instance of the exact requested type.
*/
T selectExact(Class<T> type);

/**
* Returns <code>true</code> if an instance of the requested type can be produced.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface ExportedInstance<T>
T get();

void release(T instance);

Class<? extends T> getActualType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jboss.forge.furnace.services.Imported;
import org.jboss.forge.furnace.spi.ExportedInstance;
import org.jboss.forge.furnace.spi.ServiceRegistry;
import org.jboss.forge.furnace.util.Assert;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
Expand Down Expand Up @@ -64,6 +65,9 @@ public Iterator<T> iterator()
@Override
public T get()
{
if (isAmbiguous())
throw new IllegalStateException("Cannot resolve Ambiguous dependencies: " + toString());

ExportedInstance<T> exported = getExportedInstance();
if (exported != null)
{
Expand All @@ -87,6 +91,24 @@ public void release(T instance)
}
}

@Override
public T selectExact(Class<T> type)
{
Assert.notNull(type, "Type to select must not be null.");
Set<ExportedInstance<T>> instances = getExportedInstances();
for (ExportedInstance<T> instance : instances)
{
if (type.equals(instance.getActualType()))

This comment has been minimized.

Copy link
@gastaldi

gastaldi Sep 4, 2013

Member

What about classes loaded from different classloaders? Would that be a problem here?

{
T result = instance.get();
instanceMap.put(result, instance);
return result;
}
}
throw new ContainerException("No @" + Exported.class.getSimpleName()
+ " services of type [" + type + "] could be found in any started addons.");
}

private ExportedInstance<T> getExportedInstance()
{
return lock.performLocked(LockMode.READ, new Callable<ExportedInstance<T>>()
Expand Down Expand Up @@ -183,7 +205,20 @@ public void remove()
@Override
public String toString()
{
return "ImportedImpl [type=" + typeName + "]";
StringBuilder result = new StringBuilder();

result.append("[");
Iterator<ExportedInstance<T>> iterator = this.getExportedInstances().iterator();
while (iterator.hasNext())
{
ExportedInstance<T> instance = iterator.next();
result.append(instance);
if (iterator.hasNext())
result.append(",\n");
}
result.append("]");

return result.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
public class ReflectionExportedInstance<T> implements ExportedInstance<T>
{
private Class<T> type;
private Addon addon;

public ReflectionExportedInstance(Addon addon, Class<T> clazz)
{
this.addon = addon;
this.type = clazz;
}

Expand All @@ -42,4 +44,16 @@ public void release(T instance)
// no action required
}

@Override
public String toString()
{
return type.getName() + " from " + addon;
}

@Override
public Class<? extends T> getActualType()
{
return type;
}

}

0 comments on commit e47406c

Please sign in to comment.