Skip to content

Commit 856f662

Browse files
committed
spring-projects#41 - Added PluginRegistry.getRequiredPlugin(…).
Added aforementioned method for clients to look up a required plugin with the registry throwing a default exception if none is found. An overload taking a Supplier<String> is available to customize the exception message if needed.
1 parent 605b22e commit 856f662

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

core/src/main/java/org/springframework/plugin/core/PluginRegistry.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ public interface PluginRegistry<T extends Plugin<S>, S> extends Iterable<T> {
3636
*/
3737
Optional<T> getPluginFor(S delimiter);
3838

39+
/**
40+
* Returns the first {@link Plugin} found for the given delimiter. Thus, further configured {@link Plugin}s are
41+
* ignored.
42+
*
43+
* @param delimiter
44+
* @return a {@link Plugin} for the given originating system or {@link Optional#empty()} if none found.
45+
* @throws IllegalArgumentException in case no {@link Plugin} for the given delimiter
46+
*/
47+
T getRequiredPluginFor(S delimiter) throws IllegalArgumentException;
48+
49+
/**
50+
* Returns the first {@link Plugin} found for the given delimiter. Thus, further configured {@link Plugin}s are
51+
* ignored.
52+
*
53+
* @param delimiter
54+
* @param message a {@link Supplier} to produce an exception message in case no plugin is found.
55+
* @return a {@link Plugin} for the given originating system or {@link Optional#empty()} if none found.
56+
* @throws IllegalArgumentException in case no {@link Plugin} for the given delimiter
57+
*/
58+
T getRequiredPluginFor(S delimiter, Supplier<String> message) throws IllegalArgumentException;
59+
3960
/**
4061
* Returns all plugins for the given delimiter.
4162
*

core/src/main/java/org/springframework/plugin/core/SimplePluginRegistry.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.function.Supplier;
2323
import java.util.stream.Collectors;
2424

25+
import org.springframework.util.Assert;
26+
2527
/**
2628
* Basic implementation of {@link PluginRegistry}. Simply holds all given plugins in a list dropping {@literal null}
2729
* values silently on adding.
@@ -83,6 +85,29 @@ public Optional<T> getPluginFor(S delimiter) {
8385
.findFirst();
8486
}
8587

88+
/*
89+
* (non-Javadoc)
90+
* @see org.springframework.plugin.core.PluginRegistry#getRequiredPluginFor(java.lang.Object)
91+
*/
92+
@Override
93+
public T getRequiredPluginFor(S delimiter) {
94+
95+
return getRequiredPluginFor(delimiter,
96+
() -> String.format("No plugin found for delimiter %s! Registered plugins: %s.", delimiter, getPlugins()));
97+
}
98+
99+
/*
100+
* (non-Javadoc)
101+
* @see org.springframework.plugin.core.PluginRegistry#getRequiredPluginFor(java.lang.Object, java.util.function.Supplier)
102+
*/
103+
@Override
104+
public T getRequiredPluginFor(S delimiter, Supplier<String> message) throws IllegalArgumentException {
105+
106+
Assert.notNull(message, "Message must not be null!");
107+
108+
return getPluginFor(delimiter, () -> new IllegalArgumentException(message.get()));
109+
}
110+
86111
/*
87112
* (non-Javadoc)
88113
* @see org.springframework.plugin.core.PluginRegistry#getPluginsFor(java.lang.Object)

core/src/test/java/org/springframework/plugin/core/SimplePluginRegistryUnitTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import java.util.List;
2626

2727
import org.junit.Before;
28+
import org.junit.Rule;
2829
import org.junit.Test;
30+
import org.junit.rules.ExpectedException;
2931

3032
/**
3133
* Unit test for {@link SimplePluginRegistry}.
@@ -38,6 +40,8 @@ public class SimplePluginRegistryUnitTest {
3840

3941
SimplePluginRegistry<SamplePlugin, String> registry;
4042

43+
public @Rule ExpectedException o_O = ExpectedException.none();
44+
4145
/**
4246
* Initializes a {@code PluginRegistry} and equips it with an {@code EmailNotificationProvider}.
4347
*/
@@ -142,4 +146,29 @@ public void testname() throws Exception {
142146

143147
registry.getPluginFor("FOO", () -> new IllegalStateException());
144148
}
149+
150+
/**
151+
* @see #41
152+
*/
153+
public void throwsExceptionIfRequiredPluginIsNotFound() {
154+
155+
registry = SimplePluginRegistry.create(Collections.emptyList());
156+
157+
o_O.expect(IllegalArgumentException.class);
158+
159+
registry.getRequiredPluginFor("FOO");
160+
}
161+
162+
/**
163+
* @see #41
164+
*/
165+
public void throwsExceptionWithMessafeIfRequiredPluginIsNotFound() {
166+
167+
registry = SimplePluginRegistry.create(Collections.emptyList());
168+
169+
o_O.expect(IllegalArgumentException.class);
170+
o_O.expectMessage("message");
171+
172+
registry.getRequiredPluginFor("FOO", () -> "message");
173+
}
145174
}

0 commit comments

Comments
 (0)