-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[System-X] Add methods for configuring service instance
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
system-x/common/src/main/java/software/tnb/common/service/ConfigurableService.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,32 @@ | ||
package software.tnb.common.service; | ||
|
||
import software.tnb.common.service.configuration.ServiceConfiguration; | ||
|
||
import org.junit.platform.commons.util.ReflectionUtils; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
|
||
public abstract class ConfigurableService<C extends ServiceConfiguration> implements Service { | ||
private final C configuration; | ||
|
||
public ConfigurableService() { | ||
Type type = this.getClass().getGenericSuperclass(); | ||
while (true) { | ||
if (type instanceof ParameterizedType) { | ||
if (((ParameterizedType) type).getRawType().equals(ConfigurableService.class)) { | ||
break; | ||
} else { | ||
throw new RuntimeException("Expected to find " + ConfigurableService.class.getSimpleName() + ", but found " | ||
+ ((ParameterizedType) type).getRawType().getTypeName()); | ||
} | ||
} | ||
type = ((Class) type).getGenericSuperclass(); | ||
} | ||
configuration = ReflectionUtils.newInstance((Class<C>) ((ParameterizedType) type).getActualTypeArguments()[0]); | ||
} | ||
|
||
public C getConfiguration() { | ||
return configuration; | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
.../common/src/main/java/software/tnb/common/service/configuration/ServiceConfiguration.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,17 @@ | ||
package software.tnb.common.service.configuration; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ServiceConfiguration { | ||
private final Map<String, Object> configuration = new HashMap<>(); | ||
|
||
protected void set(String key, Object value) { | ||
configuration.put(key, value); | ||
} | ||
|
||
protected <T> T get(String key, Class<T> clazz) { | ||
final Object value = configuration.get(key); | ||
return value == null ? null : clazz.cast(value); | ||
} | ||
} |