Skip to content

Commit

Permalink
Implemented FORGE-1057 - Imported<?> instances
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Jul 30, 2013
1 parent e45ac60 commit 651e7f0
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.jboss.forge.addon.convert.exception.ConverterNotFoundException;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.services.Exported;
import org.jboss.forge.furnace.services.ExportedInstance;
import org.jboss.forge.furnace.services.Imported;
import org.jboss.forge.furnace.util.Annotations;

@Exported
Expand All @@ -31,10 +31,9 @@ public class ConverterFactoryImpl implements ConverterFactory
public <S, T> Converter<S, T> getConverter(Class<S> source, Class<T> target)
{
Converter<S, T> result = null;
for (ExportedInstance<ConverterGenerator> generatorInstance : registry
.getExportedInstances(ConverterGenerator.class))
Imported<ConverterGenerator> instances = registry.getInstance(ConverterGenerator.class);
for (ConverterGenerator generator : instances)
{
ConverterGenerator generator = generatorInstance.get();
if (generator.handles(source, target))
{
result = (Converter<S, T>) generator.generateConverter(source, target);
Expand All @@ -46,7 +45,7 @@ public <S, T> Converter<S, T> getConverter(Class<S> source, Class<T> target)
{
if (String.class.equals(source) && Annotations.isAnnotationPresent(target, Exported.class))
{
result = (Converter<S, T>) new StringToExportedConverter<T>(target, registry);
result = (Converter<S, T>) new StringToImportedInstanceConverter<T>(target, registry);
}
else if (String.class.equals(target))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@

package org.jboss.forge.addon.convert.impl;

import java.util.Set;

import javax.enterprise.inject.Vetoed;

import org.jboss.forge.addon.convert.AbstractConverter;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.services.Exported;
import org.jboss.forge.furnace.services.ExportedInstance;
import org.jboss.forge.furnace.services.Imported;

/**
* Lookups in the {@link AddonRegistry} an {@link Exported} instance based on the {@link Object#toString()} method
Expand All @@ -24,11 +22,11 @@
* @param <TARGETTYPE> a type declaring the {@link Exported} annotation
*/
@Vetoed
public class StringToExportedConverter<TARGETTYPE> extends AbstractConverter<String, TARGETTYPE>
public class StringToImportedInstanceConverter<TARGETTYPE> extends AbstractConverter<String, TARGETTYPE>
{
private AddonRegistry addonRegistry;

public StringToExportedConverter(Class<TARGETTYPE> targetType, AddonRegistry addonRegistry)
public StringToImportedInstanceConverter(Class<TARGETTYPE> targetType, AddonRegistry addonRegistry)
{
super(String.class, targetType);
this.addonRegistry = addonRegistry;
Expand All @@ -37,21 +35,12 @@ public StringToExportedConverter(Class<TARGETTYPE> targetType, AddonRegistry add
@Override
public TARGETTYPE convert(String source)
{
Set<ExportedInstance<TARGETTYPE>> exportedInstances = addonRegistry.getExportedInstances(getTargetType());
for (ExportedInstance<TARGETTYPE> exportedInstance : exportedInstances)
Imported<TARGETTYPE> instances = addonRegistry.getInstance(getTargetType());
for (TARGETTYPE instance : instances)
{
TARGETTYPE targetObj = null;
try
{
targetObj = exportedInstance.get();
if (source.equals(targetObj.toString()))
{
return targetObj;
}
}
finally
if (source.equals(instance.toString()))
{
exportedInstance.release(targetObj);
return instance;
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.jboss.forge.addon.facets.constraints.FacetInspector;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.services.ExportedInstance;
import org.jboss.forge.furnace.services.Imported;
import org.jboss.forge.furnace.util.Assert;

/**
Expand All @@ -43,8 +43,8 @@ private <FACETEDTYPE extends Faceted<?>, FACETTYPE extends Facet<FACETEDTYPE>> F
Class<FACETTYPE> type)
{
Assert.notNull(type, "Facet type must not be null.");
ExportedInstance<FACETTYPE> instance = registry.getExportedInstance(type);
if (instance == null)
Imported<FACETTYPE> instance = registry.getInstance(type);
if (!instance.isSatisfied() && !instance.isAmbiguous())
throw new FacetNotFoundException("Could not find Facet of type [" + type.getName() + "]");
return instance.get();
}
Expand All @@ -69,11 +69,11 @@ private <FACETEDTYPE extends Faceted<?>, FACETTYPE extends Facet<FACETEDTYPE>> I
Class<FACETTYPE> type)
{
Assert.notNull(type, "Facet type must not be null.");
Set<ExportedInstance<FACETTYPE>> instances = registry.getExportedInstances(type);
Set<FACETTYPE> facets = new HashSet<FACETTYPE>(instances.size());
for (ExportedInstance<FACETTYPE> instance : instances)
Imported<FACETTYPE> instances = registry.getInstance(type);
Set<FACETTYPE> facets = new HashSet<FACETTYPE>();
for (FACETTYPE instance : instances)
{
facets.add(instance.get());
facets.add(instance);
}
return facets;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,20 @@ public void testNotFoundFacetCreation() throws Exception
try
{
facetFactory.create(new MockFaceted(), NotFoundMockFacet.class);
Assert.fail("Should have thrown exception.");
}
catch (Throwable e)
{
boolean found = false;
boolean causeIsFacetNotFoundException = false;
while (e.getCause() != null && e.getCause() != e)
{
e = e.getCause();
if (e instanceof FacetNotFoundException)
{
found = true;
causeIsFacetNotFoundException = true;
}
}
Assert.assertTrue(found);
Assert.assertTrue(causeIsFacetNotFoundException);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.repositories.AddonRepository;
import org.jboss.forge.furnace.repositories.MutableAddonRepository;
import org.jboss.forge.furnace.services.ExportedInstance;
import org.jboss.forge.furnace.services.Imported;
import org.jboss.forge.furnace.spi.ListenerRegistration;
import org.jboss.forge.furnace.util.Predicate;

Expand Down Expand Up @@ -78,12 +78,12 @@ public boolean accept(Project type)
}

Project result = null;
for (ExportedInstance<ProjectLocator> instance : registry.getExportedInstances(ProjectLocator.class))
Imported<ProjectLocator> instances = registry.getInstance(ProjectLocator.class);
for (ProjectLocator locator : instances)
{
DirectoryResource r = (target instanceof DirectoryResource) ? (DirectoryResource) target : target.getParent();
while (r != null && result == null)
{
ProjectLocator locator = instance.get();
if (locator.containsProject(r))
{
result = locator.createProject(r);
Expand All @@ -93,6 +93,7 @@ public boolean accept(Project type)

r = r.getParent();
}
instances.release(locator);
}

if (result != null)
Expand Down Expand Up @@ -120,27 +121,29 @@ public Project createProject(DirectoryResource projectDir)
public Project createProject(DirectoryResource target, Iterable<Class<? extends ProjectFacet>> facetTypes)
{
Project result = null;
for (ExportedInstance<ProjectLocator> instance : registry.getExportedInstances(ProjectLocator.class))
Imported<ProjectLocator> instances = registry.getInstance(ProjectLocator.class);
for (ProjectLocator locator : instances)
{
ProjectLocator locator = instance.get();
result = locator.createProject(target);
if (result != null)
break;
instances.release(locator);
}

if (result != null)
{
DirectoryResource parentDir = result.getProjectRoot().getParent().reify(DirectoryResource.class);
if (parentDir != null)
{
for (ExportedInstance<ProjectAssociationProvider> providerInstance : registry
.getExportedInstances(ProjectAssociationProvider.class))
Imported<ProjectAssociationProvider> locatorInstances = registry
.getInstance(ProjectAssociationProvider.class);
for (ProjectAssociationProvider provider : locatorInstances)
{
ProjectAssociationProvider provider = providerInstance.get();
if (provider.canAssociate(result, parentDir))
{
provider.associate(result, parentDir);
}
locatorInstances.release(provider);
}
}
}
Expand Down Expand Up @@ -240,9 +243,9 @@ public boolean containsProject(FileResource<?> target)
{
boolean result = false;
DirectoryResource dir = (target instanceof DirectoryResource) ? (DirectoryResource) target : target.getParent();
for (ExportedInstance<ProjectLocator> instance : registry.getExportedInstances(ProjectLocator.class))
Imported<ProjectLocator> instances = registry.getInstance(ProjectLocator.class);
for (ProjectLocator locator : instances)
{
ProjectLocator locator = instance.get();
DirectoryResource r = dir;
while (r != null && !result)
{
Expand All @@ -251,6 +254,7 @@ public boolean containsProject(FileResource<?> target)
}
if (result)
break;
instances.release(locator);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.jboss.forge.addon.resource.transaction.ResourceTransactionManager;
import org.jboss.forge.addon.resource.util.RelatedClassComparator;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.services.ExportedInstance;
import org.jboss.forge.furnace.services.Imported;

/**
* @author <a href="http://community.jboss.org/people/kenfinni">Ken Finnigan</a>
Expand All @@ -43,9 +43,9 @@ public <E, T extends Resource<E>> T create(final Class<T> type, final E underlyi
{
TreeMap<Class<?>, ResourceGenerator> generated = new TreeMap<Class<?>, ResourceGenerator>(
new RelatedClassComparator());
for (ExportedInstance<ResourceGenerator> instance : getRegisteredResourceGenerators())
Imported<ResourceGenerator> instances = registry.getInstance(ResourceGenerator.class);
for (ResourceGenerator generator : instances)
{
ResourceGenerator generator = instance.get();
if (generator.handles(type, underlyingResource))
{
Class resourceType = generator.getResourceType(type, underlyingResource);
Expand All @@ -54,7 +54,7 @@ public <E, T extends Resource<E>> T create(final Class<T> type, final E underlyi
generated.put(resourceType, generator);
}
}
instance.release(generator);
instances.release(generator);
}
if (generated.size() > 0)
{
Expand All @@ -71,12 +71,6 @@ public <E> Resource<E> create(E underlyingResource)
return create(Resource.class, underlyingResource);
}

@SuppressWarnings("rawtypes")
private Iterable<ExportedInstance<ResourceGenerator>> getRegisteredResourceGenerators()
{
return registry.getExportedInstances(ResourceGenerator.class);
}

@Override
public ResourceFactory fireEvent(ResourceEvent event)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import org.jboss.forge.furnace.event.PostStartup;
import org.jboss.forge.furnace.event.PreShutdown;
import org.jboss.forge.furnace.services.Exported;
import org.jboss.forge.furnace.services.ExportedInstance;
import org.jboss.forge.furnace.services.Imported;
import org.jboss.forge.furnace.spi.ListenerRegistration;
import org.jboss.forge.furnace.util.Assert;

Expand Down Expand Up @@ -111,10 +111,11 @@ private void initShell() throws Exception
Settings.getInstance().setReadInputrc(false);
Settings.getInstance().setLogging(true);

for (ExportedInstance<ShellConfiguration> instance : registry.getExportedInstances(ShellConfiguration.class))
Imported<ShellConfiguration> instances = registry.getInstance(ShellConfiguration.class);
for (ShellConfiguration config : instances)
{
ShellConfiguration provider = instance.get();
provider.configure();
config.configure();
instances.release(config);
}

commands = new ArrayList<ShellCommand>();
Expand All @@ -127,12 +128,11 @@ private void initShell() throws Exception

private void refreshAvailableCommands()
{
Set<ExportedInstance<UICommand>> instances = registry.getExportedInstances(UICommand.class);
Imported<UICommand> instances = registry.getInstance(UICommand.class);

Set<UICommand> loaded = new HashSet<UICommand>();
for (ExportedInstance<UICommand> instance : instances)
for (UICommand command : instances)
{
UICommand command = instance.get();
loaded.add(command);
if (!isCommandLoaded(command))
{
Expand All @@ -142,7 +142,7 @@ private void refreshAvailableCommands()
}
catch (Exception e)
{
logger.log(Level.SEVERE, "Failed to load command [" + instance + "]");
logger.log(Level.SEVERE, "Failed to load command [" + command + "]");
}
}
}
Expand Down Expand Up @@ -332,13 +332,13 @@ private void invokePreCommandExecutedListeners(UICommand command, ShellContext c
listener.preCommandExecuted(command, context);
}

Set<ExportedInstance<CommandExecutionListener>> instances = registry
.getExportedInstances(CommandExecutionListener.class);
for (ExportedInstance<CommandExecutionListener> exportedInstance : instances)
Imported<CommandExecutionListener> instances = registry
.getInstance(CommandExecutionListener.class);
for (CommandExecutionListener listener : instances)
{
CommandExecutionListener listener = exportedInstance.get();
if (listener != null)
listener.preCommandExecuted(command, context);
instances.release(listener);
}
}

Expand All @@ -349,13 +349,13 @@ private void invokePostCommandExecutedListeners(UICommand command, ShellContext
listener.postCommandExecuted(command, context, result);
}

Set<ExportedInstance<CommandExecutionListener>> instances = registry
.getExportedInstances(CommandExecutionListener.class);
for (ExportedInstance<CommandExecutionListener> exportedInstance : instances)
Imported<CommandExecutionListener> instances = registry
.getInstance(CommandExecutionListener.class);
for (CommandExecutionListener listener : instances)
{
CommandExecutionListener listener = exportedInstance.get();
if (listener != null)
listener.postCommandExecuted(command, context, result);
instances.release(listener);
}
}

Expand Down
Loading

0 comments on commit 651e7f0

Please sign in to comment.