-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from eclipse-passage/559091
559091 - generic API for service registry
- Loading branch information
Showing
12 changed files
with
337 additions
and
2 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
29 changes: 29 additions & 0 deletions
29
...g.eclipse.passage.lic.api/src/org/eclipse/passage/lic/api/internal/registry/Registry.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 (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.api.internal.registry; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* | ||
* @param <S> sub type of {@linkplain Service} | ||
*/ | ||
public interface Registry<I extends ServiceId, S extends Service<I>> { | ||
|
||
boolean hasService(I id); | ||
|
||
S service(I id); | ||
|
||
Collection<S> services(); | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...se.passage.lic.api/src/org/eclipse/passage/lic/api/internal/registry/RuntimeRegistry.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,21 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.api.internal.registry; | ||
|
||
public interface RuntimeRegistry<I extends ServiceId, S extends Service<I>> extends Registry<I, S> { | ||
|
||
void register(S service); | ||
|
||
void unregister(S service); | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...rg.eclipse.passage.lic.api/src/org/eclipse/passage/lic/api/internal/registry/Service.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,28 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.api.internal.registry; | ||
|
||
/** | ||
* <p> | ||
* General notion of a {@code service} interface which can potentially have | ||
* multiple implementations that need to be somehow <i>collected and managed | ||
* together</i>. | ||
* </p> | ||
* | ||
* @param <I> sub type of {@linkplain ServiceId} | ||
*/ | ||
public interface Service<I extends ServiceId> { | ||
|
||
I id(); | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
....eclipse.passage.lic.api/src/org/eclipse/passage/lic/api/internal/registry/ServiceId.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,21 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.api.internal.registry; | ||
|
||
/** | ||
* Implementation is expected to represent <i>data class</i>: with | ||
* {@code hashCode} and {@code equals} overloaded basing on enclosed data. | ||
*/ | ||
public interface ServiceId { | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...lipse.passage.lic.api/src/org/eclipse/passage/lic/api/internal/registry/package-info.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,15 @@ | ||
/** **************************************************************************** | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
/** | ||
*/ | ||
package org.eclipse.passage.lic.api.internal.registry; |
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
136 changes: 136 additions & 0 deletions
136
...sage.lic.base/src/org/eclipse/passage/lic/base/internal/registry/BaseRuntimeRegistry.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,136 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.base.internal.registry; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
import org.eclipse.passage.lic.api.internal.registry.Registry; | ||
import org.eclipse.passage.lic.api.internal.registry.RuntimeRegistry; | ||
import org.eclipse.passage.lic.api.internal.registry.Service; | ||
import org.eclipse.passage.lic.api.internal.registry.ServiceId; | ||
import org.eclipse.passage.lic.internal.base.i18n.BaseMessages; | ||
|
||
/** | ||
* <p> | ||
* {@linkplain Registry} implementation that is filled and managed at runtime | ||
* programmatically. | ||
* </p> | ||
* | ||
* <p> | ||
* Not thread safe (yet) | ||
* </p> | ||
* <p> | ||
* Null free zone. | ||
* </p> | ||
* | ||
* @param <S> type of {@linkplain Service} to keep | ||
*/ | ||
@SuppressWarnings("restriction") | ||
public final class BaseRuntimeRegistry<I extends ServiceId, S extends Service<I>> implements RuntimeRegistry<I, S> { | ||
|
||
private final Map<I, S> services; | ||
private final Consumer<String> handler; | ||
|
||
/** | ||
* Primary constructor | ||
* | ||
* @param init {@linkplain Map} implementation to be used as service storing | ||
* facility | ||
* @param handler error handler | ||
*/ | ||
public BaseRuntimeRegistry(Map<I, S> init, Consumer<String> handler) { | ||
this.services = init; | ||
this.handler = handler; | ||
} | ||
|
||
/** | ||
* Convenience constructor, uses {@linkplain HashMap} as a storage and prints | ||
* errors into {@linkplain Stsrem.err} stream | ||
*/ | ||
public BaseRuntimeRegistry() { | ||
this(new HashMap<>(), System.err::println); | ||
} | ||
|
||
public BaseRuntimeRegistry(Map<I, S> init) { | ||
this(init, System.err::println); | ||
} | ||
|
||
public BaseRuntimeRegistry(Consumer<String> handler) { | ||
this(new HashMap<>(), handler); | ||
} | ||
|
||
@Override | ||
public void register(S service) { | ||
Objects.requireNonNull(service); | ||
checkOverride(service); | ||
services.put(service.id(), service); | ||
} | ||
|
||
@Override | ||
public void unregister(S service) { | ||
checkAbsent(service); | ||
services.remove(service.id()); | ||
} | ||
|
||
@Override | ||
public boolean hasService(I id) { | ||
return services.containsKey(id); | ||
} | ||
|
||
/** | ||
* <p> | ||
* Get the previously registered service by it's {@code id}. It's mandatory to | ||
* either be sure the service has been registered or to check | ||
* {@linkplain #hasService(ServiceId)} first. | ||
* </p> | ||
* | ||
* @throws IllegalStateException if not yet registered service is requested | ||
*/ | ||
@Override | ||
public S service(I id) { | ||
if (!hasService(id)) { | ||
throw new IllegalStateException(String.format(// | ||
BaseMessages.getString("RuntimeRegistry.retrieve_absent_exception"), //$NON-NLS-1$ | ||
id)); | ||
} | ||
return services.get(id); | ||
} | ||
|
||
@Override | ||
public Collection<S> services() { | ||
return services.values(); | ||
} | ||
|
||
private void checkOverride(S service) { | ||
if (!hasService(service.id())) { | ||
return; | ||
} | ||
handler.accept(String.format(// | ||
BaseMessages.getString("RuntimeRegistry.register_override"), //$NON-NLS-1$ | ||
service.id(), service(service.id()), service)); | ||
} | ||
|
||
private void checkAbsent(S service) { | ||
if (hasService(service.id())) { | ||
return; | ||
} | ||
handler.accept(String.format(// | ||
BaseMessages.getString("RuntimeRegistry.unregister_absent"), //$NON-NLS-1$ | ||
service)); | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
...se.passage.lic.base/src/org/eclipse/passage/lic/base/internal/registry/JointRegistry.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,64 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.base.internal.registry; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.passage.lic.api.internal.registry.Registry; | ||
import org.eclipse.passage.lic.api.internal.registry.Service; | ||
import org.eclipse.passage.lic.api.internal.registry.ServiceId; | ||
import org.eclipse.passage.lic.internal.base.i18n.BaseMessages; | ||
|
||
@SuppressWarnings("restriction") | ||
public final class JointRegistry<I extends ServiceId, S extends Service<I>> implements Registry<I, S> { | ||
private final List<Registry<I, S>> delegates; | ||
|
||
public JointRegistry(List<Registry<I, S>> delegates) { | ||
this.delegates = delegates; | ||
} | ||
|
||
@Override | ||
public boolean hasService(I id) { | ||
return delegates.stream().anyMatch(delegate -> delegate.hasService(id)); | ||
} | ||
|
||
@Override | ||
public S service(I id) { | ||
return delegates.stream()// | ||
.filter(delgate -> delgate.hasService(id))// | ||
.map(delgate -> delgate.service(id)) // | ||
.findAny() // | ||
.orElseThrow(() -> new IllegalStateException(notFoundMessage(id))); | ||
} | ||
|
||
@Override | ||
public Collection<S> services() { | ||
return delegates.stream()// | ||
.map(Registry<I, S>::services) // | ||
.flatMap(Collection<S>::stream) // | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private String notFoundMessage(ServiceId id) { | ||
return String.format(// | ||
BaseMessages.getString("JointRegistry.retrieve_absent"), //$NON-NLS-1$ | ||
id, // | ||
delegates.size(), // | ||
delegates.stream()// | ||
.map(Object::toString)// | ||
.collect(Collectors.joining("\n\t"))); //$NON-NLS-1$ | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...pse.passage.lic.base/src/org/eclipse/passage/lic/base/internal/registry/package-info.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,16 @@ | ||
/** **************************************************************************** | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
/** | ||
*/ | ||
package org.eclipse.passage.lic.base.internal.registry; |
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
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