-
Notifications
You must be signed in to change notification settings - Fork 167
split BeanRegistry interface #2488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
annot/src/main/java/com/predic8/membrane/annot/beanregistry/AsyncBeanCollector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.predic8.membrane.annot.beanregistry; | ||
|
|
||
| import javax.annotation.concurrent.GuardedBy; | ||
| import java.util.concurrent.BlockingQueue; | ||
| import java.util.concurrent.LinkedBlockingDeque; | ||
|
|
||
| /** | ||
| * Thread-safe, asynchronous wrapper for {@link BeanCollector}. | ||
| */ | ||
| public class AsyncBeanCollector implements BeanCollector { | ||
|
|
||
| private final BlockingQueue<ChangeEvent> changeEvents = new LinkedBlockingDeque<>(); | ||
| private final BeanCollector delegate; | ||
|
|
||
| @GuardedBy("this") | ||
| Thread t; | ||
|
|
||
| public AsyncBeanCollector(BeanCollector delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public void handle(ChangeEvent changeEvent, boolean isLast) { | ||
| changeEvents.add(changeEvent); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void start() { | ||
| if (t != null) | ||
| return; | ||
| t = Thread.ofVirtual().start(() -> { | ||
| while (true) { | ||
| try { | ||
| ChangeEvent changeEvent = changeEvents.take(); | ||
| delegate.handle(changeEvent, changeEvents.isEmpty()); | ||
|
predic8 marked this conversation as resolved.
|
||
| } catch (InterruptedException e) { | ||
| break; | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
predic8 marked this conversation as resolved.
|
||
|
|
||
| } | ||
41 changes: 41 additions & 0 deletions
41
annot/src/main/java/com/predic8/membrane/annot/beanregistry/BeanCollector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.predic8.membrane.annot.beanregistry; | ||
|
|
||
| import com.predic8.membrane.annot.Grammar; | ||
| import com.predic8.membrane.annot.yaml.GenericYamlParser; | ||
| import com.predic8.membrane.annot.yaml.WatchAction; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * This is the definition side of a {@link BeanRegistryImplementation}. You can start the bean registry | ||
| * and send it a series of change events. | ||
| */ | ||
| public interface BeanCollector { | ||
| /** | ||
| * Utility method to ingest a stream of YAML objects as a static configuration and then | ||
| * start the bean registry. | ||
| * @param yamls stream of YAML objects | ||
| * @param grammar the grammar to use for parsing | ||
| */ | ||
| default void parseYamls(InputStream yamls, Grammar grammar) throws IOException { | ||
| List<BeanDefinition> bds = GenericYamlParser.parseMembraneResources(yamls, grammar); | ||
| for (int i = 0; i < bds.size(); i++) { | ||
| handle(new BeanDefinitionChanged(WatchAction.ADDED, bds.get(i)), i == bds.size() - 1); | ||
| } | ||
| handle(new StaticConfigurationLoaded(), true); | ||
| start(); | ||
| } | ||
|
|
||
| /** | ||
| * @param changeEvent the change event | ||
| * @param isLast indicates whether this is the last change event for this batch of changes | ||
| */ | ||
| void handle(ChangeEvent changeEvent, boolean isLast); | ||
|
|
||
| /** | ||
| * Starts the bean registry. | ||
| */ | ||
| void start(); | ||
| } |
28 changes: 28 additions & 0 deletions
28
annot/src/main/java/com/predic8/membrane/annot/beanregistry/BeanContainer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.predic8.membrane.annot.beanregistry; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
|
predic8 marked this conversation as resolved.
|
||
|
|
||
| public class BeanContainer { | ||
| private final BeanDefinition definition; | ||
| /** | ||
| * Constructed bean after initialization. | ||
| */ | ||
| private volatile Object singleton; | ||
|
|
||
| public BeanContainer(BeanDefinition definition) { | ||
| this.definition = definition; | ||
| } | ||
|
|
||
|
|
||
| public Object getSingleton() { | ||
| return singleton; | ||
| } | ||
|
|
||
| public void setSingleton(Object singleton) { | ||
| this.singleton = singleton; | ||
| } | ||
|
|
||
| public BeanDefinition getDefinition() { | ||
| return definition; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
annot/src/main/java/com/predic8/membrane/annot/beanregistry/BeanDefinitionChanged.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.predic8.membrane.annot.beanregistry; | ||
|
|
||
| import com.predic8.membrane.annot.yaml.WatchAction; | ||
|
|
||
| /** | ||
| * Signals that a BeanDefinition has changed (=was added, modified, or deleted). | ||
| */ | ||
| public record BeanDefinitionChanged( | ||
| WatchAction action, | ||
| BeanDefinition bd) implements ChangeEvent { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.