diff --git a/core/src/main/java/org/springframework/plugin/core/PluginRegistry.java b/core/src/main/java/org/springframework/plugin/core/PluginRegistry.java index b0c2e78..33f0979 100644 --- a/core/src/main/java/org/springframework/plugin/core/PluginRegistry.java +++ b/core/src/main/java/org/springframework/plugin/core/PluginRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2012 the original author or authors. + * Copyright 2008-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,19 +51,48 @@ public interface PluginRegistry, S> extends Iterable { * @param ex the exception to be thrown in case no plugin can be found * @return a single plugin for the given delimiter * @throws E if no plugin can be found for the given delimiter + * @deprecated prefer {@link #getPluginFor(Object, Supplier)} to avoid the exceptions to be created for every plugin + * lookup. */ + @Deprecated T getPluginFor(S delimiter, E ex) throws E; + /** + * Retrieves a required plugin from the registry or throw the given exception if none can be found. If more than one + * plugins are found the first one will be returned. + * + * @param the exception type to be thrown in case no plugin can be found. + * @param delimiter + * @param ex a lazy {@link Supplier} to produce an exception in case no plugin can be found. + * @return a single plugin for the given delimiter + * @throws E if no plugin can be found for the given delimiter + */ + T getPluginFor(S delimiter, Supplier ex) throws E; + /** * Retrieves all plugins for the given delimiter or throws an exception if no plugin can be found. * - * @param the exception type to be thrown + * @param the exception type to be thrown. * @param delimiter - * @param ex + * @param ex the exception to be thrown in case no plugin can be found. + * @return all plugins for the given delimiter. + * @throws E if no plugin can be found + * @deprecated prefer {@link #getPluginFor(Object, Supplier)} to avoid the exceptions to be created for every plugin + * lookup. + */ + @Deprecated + List getPluginsFor(S delimiter, E ex) throws E; + + /** + * Retrieves all plugins for the given delimiter or throws an exception if no plugin can be found. + * + * @param the exception type to be thrown. + * @param delimiter + * @param ex a lazy {@link Supplier} to produce an exception in case no plugin can be found. * @return all plugins for the given delimiter * @throws E if no plugin can be found */ - List getPluginsFor(S delimiter, E ex) throws E; + List getPluginsFor(S delimiter, Supplier ex) throws E; /** * Returns the first {@link Plugin} supporting the given delimiter or the given plugin if none can be found. @@ -113,4 +142,9 @@ public interface PluginRegistry, S> extends Iterable { * @return */ List getPlugins(); -} \ No newline at end of file + + interface Supplier { + + E get(); + } +} diff --git a/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java b/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java index 3515991..85dbcd1 100644 --- a/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java +++ b/core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2012 the original author or authors. + * Copyright 2008-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -104,12 +104,28 @@ public class SimplePluginRegistry, S> extends PluginRegistry * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistry#getPluginFor(java.lang.Object, java.lang.Exception) */ - public T getPluginFor(S delimiter, E ex) throws E { + public T getPluginFor(S delimiter, final E ex) throws E { + + return getPluginFor(delimiter, new Supplier() { + + @Override + public E get() { + return ex; + } + }); + } + + /* + * (non-Javadoc) + * @see org.springframework.plugin.core.PluginRegistry#getPluginFor(java.lang.Object, org.springframework.plugin.core.PluginRegistry.Supplier) + */ + @Override + public T getPluginFor(S delimiter, Supplier ex) throws E { T plugin = getPluginFor(delimiter); if (null == plugin) { - throw ex; + throw ex.get(); } return plugin; @@ -119,12 +135,28 @@ public class SimplePluginRegistry, S> extends PluginRegistry * (non-Javadoc) * @see org.springframework.plugin.core.PluginRegistry#getPluginsFor(java.lang.Object, java.lang.Exception) */ - public List getPluginsFor(S delimiter, E ex) throws E { + public List getPluginsFor(S delimiter, final E ex) throws E { + + return getPluginsFor(delimiter, new Supplier() { + + @Override + public E get() { + return ex; + } + }); + } + + /* + * (non-Javadoc) + * @see org.springframework.plugin.core.PluginRegistry#getPluginsFor(java.lang.Object, org.springframework.plugin.core.PluginRegistry.ExceptionProvider) + */ + @Override + public List getPluginsFor(S delimiter, Supplier ex) throws E { List result = getPluginsFor(delimiter); if (result.isEmpty()) { - throw ex; + throw ex.get(); } return result; diff --git a/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java b/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java index fce09d9..aeb8128 100644 --- a/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java +++ b/core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java @@ -21,10 +21,12 @@ import static org.junit.Assert.*; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.junit.Before; import org.junit.Test; +import org.springframework.plugin.core.PluginRegistry.Supplier; /** * Unit test for {@link SimplePluginRegistry}. @@ -130,4 +132,21 @@ public class SimplePluginRegistryUnitTest { assertThat(registry.countPlugins(), is(0)); } + + /** + * @see #19 + */ + @Test(expected = IllegalStateException.class) + public void testname() throws Exception { + + registry = SimplePluginRegistry.create(Collections. emptyList()); + + registry.getPluginFor("FOO", new Supplier() { + + @Override + public Exception get() { + return new IllegalStateException(); + } + }); + } }