#19 - Exceptions on plugin lookup can now be submitted lazily.

Introduced overloads on PluginRegistry which instead of taking an exception instance immediately (which requires instance creation beforehand) now accept a Supplier<E> so that the exception creation can actually be delayed until it's really needed.

Deprecated the old methods taking exception instances directly.
This commit is contained in:
Oliver Gierke
2016-01-28 10:24:15 +01:00
parent 46d36ec85d
commit ad017e83cd
3 changed files with 95 additions and 10 deletions

View File

@@ -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<T extends Plugin<S>, S> extends Iterable<T> {
* @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
<E extends Exception> 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 <E> 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
*/
<E extends Exception> T getPluginFor(S delimiter, Supplier<E> ex) throws E;
/**
* Retrieves all plugins for the given delimiter or throws an exception if no plugin can be found.
*
* @param <E> the exception type to be thrown
* @param <E> 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
<E extends Exception> List<T> 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 <E> 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
*/
<E extends Exception> List<T> getPluginsFor(S delimiter, E ex) throws E;
<E extends Exception> List<T> getPluginsFor(S delimiter, Supplier<E> 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<T extends Plugin<S>, S> extends Iterable<T> {
* @return
*/
List<T> getPlugins();
}
interface Supplier<E extends Exception> {
E get();
}
}

View File

@@ -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<T extends Plugin<S>, S> extends PluginRegistry
* (non-Javadoc)
* @see org.springframework.plugin.core.PluginRegistry#getPluginFor(java.lang.Object, java.lang.Exception)
*/
public <E extends Exception> T getPluginFor(S delimiter, E ex) throws E {
public <E extends Exception> T getPluginFor(S delimiter, final E ex) throws E {
return getPluginFor(delimiter, new Supplier<E>() {
@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 <E extends Exception> T getPluginFor(S delimiter, Supplier<E> ex) throws E {
T plugin = getPluginFor(delimiter);
if (null == plugin) {
throw ex;
throw ex.get();
}
return plugin;
@@ -119,12 +135,28 @@ public class SimplePluginRegistry<T extends Plugin<S>, S> extends PluginRegistry
* (non-Javadoc)
* @see org.springframework.plugin.core.PluginRegistry#getPluginsFor(java.lang.Object, java.lang.Exception)
*/
public <E extends Exception> List<T> getPluginsFor(S delimiter, E ex) throws E {
public <E extends Exception> List<T> getPluginsFor(S delimiter, final E ex) throws E {
return getPluginsFor(delimiter, new Supplier<E>() {
@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 <E extends Exception> List<T> getPluginsFor(S delimiter, Supplier<E> ex) throws E {
List<T> result = getPluginsFor(delimiter);
if (result.isEmpty()) {
throw ex;
throw ex.get();
}
return result;

View File

@@ -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.<SamplePlugin> emptyList());
registry.getPluginFor("FOO", new Supplier<Exception>() {
@Override
public Exception get() {
return new IllegalStateException();
}
});
}
}