-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WFCORE-6347 Generalize service capture abstractions for use in wildfl…
…y-service.
- Loading branch information
Showing
11 changed files
with
280 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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
20 changes: 20 additions & 0 deletions
20
service/src/main/java/org/wildfly/service/capture/FunctionExecutorRegistry.java
This file contains 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,20 @@ | ||
/* | ||
* Copyright The WildFly Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.wildfly.service.capture; | ||
|
||
/** | ||
* Registry of {@link FunctionExecutor} objects. | ||
* @author Paul Ferraro | ||
* @param <K> the registry key type | ||
* @param <V> the registry value type | ||
*/ | ||
public interface FunctionExecutorRegistry<K, V> { | ||
/** | ||
* Returns the executor for the specified key. | ||
* @param key a registry key | ||
* @return an executor, or null, if no such executor exists in the registry | ||
*/ | ||
FunctionExecutor<V> getExecutor(K key); | ||
} |
42 changes: 42 additions & 0 deletions
42
service/src/main/java/org/wildfly/service/capture/ServiceValueExecutorRegistry.java
This file contains 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,42 @@ | ||
/* | ||
* Copyright The WildFly Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.wildfly.service.capture; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import org.jboss.msc.service.ServiceName; | ||
|
||
/** | ||
* A registry of captured values. | ||
* @author Paul Ferraro | ||
* @param <V> the captured value type | ||
*/ | ||
public interface ServiceValueExecutorRegistry<V> extends ServiceValueRegistry<V>, FunctionExecutorRegistry<ServiceName, V> { | ||
|
||
/** | ||
* Creates a new {@link ServiceValueExecutorRegistry}. | ||
* @param <V> the captured value type | ||
* @return a new value executor registry | ||
*/ | ||
static <V> ServiceValueExecutorRegistry<V> newInstance() { | ||
ValueExecutorRegistry<ServiceName, V> registry = ValueExecutorRegistry.newInstance(); | ||
return new ServiceValueExecutorRegistry<>() { | ||
@Override | ||
public Consumer<V> add(ServiceName key) { | ||
return registry.add(key); | ||
} | ||
|
||
@Override | ||
public void remove(ServiceName key) { | ||
registry.remove(key); | ||
} | ||
|
||
@Override | ||
public FunctionExecutor<V> getExecutor(ServiceName key) { | ||
return registry.getExecutor(key); | ||
} | ||
}; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
service/src/main/java/org/wildfly/service/capture/ServiceValueRegistry.java
This file contains 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 @@ | ||
/* | ||
* Copyright The WildFly Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.wildfly.service.capture; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import org.jboss.msc.service.ServiceName; | ||
import org.wildfly.service.ServiceDependency; | ||
import org.wildfly.service.ServiceInstaller; | ||
|
||
/** | ||
* A registry of service values, keyed by {@link ServiceName}. | ||
* @author Paul Ferraro | ||
* @param <V> the registry value type | ||
*/ | ||
public interface ServiceValueRegistry<V> extends ValueRegistry<ServiceName, V> { | ||
|
||
/** | ||
* Creates a service installer to capture and release the value provided by the specified service dependency. | ||
* @param dependency a service dependency | ||
* @return a service installer | ||
*/ | ||
default ServiceInstaller capture(ServiceName name) { | ||
Consumer<V> startTask = new Consumer<>() { | ||
@Override | ||
public void accept(V value) { | ||
ServiceValueRegistry.this.add(name).accept(value); | ||
} | ||
}; | ||
Consumer<V> stopTask = new Consumer<>() { | ||
@Override | ||
public void accept(V value) { | ||
ServiceValueRegistry.this.remove(name); | ||
} | ||
}; | ||
ServiceDependency<V> dependency = ServiceDependency.on(name); | ||
return ServiceInstaller.builder(dependency).onStart(startTask).onStop(stopTask).build(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
service/src/main/java/org/wildfly/service/capture/ValueExecutorRegistry.java
This file contains 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,56 @@ | ||
/* | ||
* Copyright The WildFly Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.wildfly.service.capture; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* A registry of captured values. | ||
* @author Paul Ferraro | ||
* @param <K> the registry key type | ||
* @param <V> the registry value type | ||
*/ | ||
public interface ValueExecutorRegistry<K, V> extends ValueRegistry<K, V>, FunctionExecutorRegistry<K, V> { | ||
|
||
/** | ||
* Creates a new registry of values. | ||
* @param <K> the registry key type | ||
* @param <V> the registry value type | ||
* @return a new registry instance | ||
*/ | ||
static <K, V> ValueExecutorRegistry<K, V> newInstance() { | ||
return new ValueExecutorRegistry<>() { | ||
private final Map<K, AtomicReference<V>> references = new ConcurrentHashMap<>(); | ||
|
||
private AtomicReference<V> create(K dependency) { | ||
return new AtomicReference<>(); | ||
} | ||
|
||
@Override | ||
public Consumer<V> add(K key) { | ||
AtomicReference<V> reference = this.references.computeIfAbsent(key, this::create); | ||
return reference::set; | ||
} | ||
|
||
@Override | ||
public void remove(K key) { | ||
AtomicReference<V> reference = this.references.remove(key); | ||
if (reference != null) { | ||
reference.set(null); | ||
} | ||
} | ||
|
||
@Override | ||
public FunctionExecutor<V> getExecutor(K key) { | ||
AtomicReference<V> reference = this.references.get(key); | ||
return (reference != null) ? FunctionExecutor.of(reference::get) : null; | ||
} | ||
}; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
service/src/main/java/org/wildfly/service/capture/ValueRegistry.java
This file contains 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,29 @@ | ||
/* | ||
* Copyright The WildFly Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.wildfly.service.capture; | ||
|
||
import java.util.function.Consumer; | ||
|
||
/** | ||
* A registry of values. | ||
* @author Paul Ferraro | ||
* @param <K> the registry key type | ||
* @param <V> the registry value type | ||
*/ | ||
public interface ValueRegistry<K, V> { | ||
|
||
/** | ||
* Adds a value registration for the specified key | ||
* @param key a registry key | ||
* @return a consumer to capture the value | ||
*/ | ||
Consumer<V> add(K key); | ||
|
||
/** | ||
* Removes the registration for the specified key | ||
* @param key a registry key | ||
*/ | ||
void remove(K key); | ||
} |
70 changes: 70 additions & 0 deletions
70
service/src/test/java/org/wildfly/service/capture/ServiceValueExecutorRegistryTestCase.java
This file contains 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,70 @@ | ||
/* | ||
* Copyright The WildFly Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.wildfly.service.capture; | ||
|
||
import java.util.UUID; | ||
import java.util.function.Consumer; | ||
|
||
import org.jboss.msc.service.ServiceName; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.wildfly.common.function.ExceptionFunction; | ||
|
||
/** | ||
* @author Paul Ferraro | ||
*/ | ||
public class ServiceValueExecutorRegistryTestCase { | ||
|
||
private final ServiceValueExecutorRegistry<Object> registry = ServiceValueExecutorRegistry.newInstance(); | ||
|
||
@Test | ||
public void test() { | ||
this.test(ServiceName.JBOSS.append("foo"), ServiceName.JBOSS.append("bar")); | ||
} | ||
|
||
private void test(ServiceName service1, ServiceName service2) { | ||
Object value1 = UUID.randomUUID(); | ||
Object value2 = UUID.randomUUID(); | ||
|
||
Assert.assertNull(this.registry.getExecutor(service1)); | ||
Assert.assertNull(this.registry.getExecutor(service2)); | ||
|
||
ExceptionFunction<Object, Object, RuntimeException> function = value -> value; | ||
|
||
Consumer<Object> captor1 = this.registry.add(service1); | ||
Consumer<Object> captor2 = this.registry.add(service2); | ||
|
||
FunctionExecutor<Object> executor1 = this.registry.getExecutor(service1); | ||
FunctionExecutor<Object> executor2 = this.registry.getExecutor(service2); | ||
|
||
Assert.assertNull(executor1.execute(function)); | ||
Assert.assertNull(executor2.execute(function)); | ||
|
||
captor1.accept(value1); | ||
captor2.accept(value2); | ||
|
||
Assert.assertSame(value1, executor1.execute(function)); | ||
Assert.assertSame(value2, executor2.execute(function)); | ||
|
||
captor1.accept(null); | ||
captor2.accept(null); | ||
|
||
Assert.assertNull(executor1.execute(function)); | ||
Assert.assertNull(executor2.execute(function)); | ||
|
||
captor1.accept(value1); | ||
captor2.accept(value2); | ||
|
||
// Once removed, executor should return null | ||
this.registry.remove(service1); | ||
this.registry.remove(service2); | ||
|
||
Assert.assertNull(this.registry.getExecutor(service1)); | ||
Assert.assertNull(this.registry.getExecutor(service2)); | ||
|
||
Assert.assertNull(executor1.execute(function)); | ||
Assert.assertNull(executor2.execute(function)); | ||
} | ||
} |
21 changes: 0 additions & 21 deletions
21
subsystem/src/main/java/org/wildfly/subsystem/service/capture/FunctionExecutorRegistry.java
This file was deleted.
Oops, something went wrong.
This file contains 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.