use of org.eclipse.vorto.repository.services.NamespaceService in project vorto by eclipse.
the class NamespaceRequestCache method namespace.
/**
* Resolves a cached {@link Namespace} by name if applicable.<br/>
* This will also attempt to resolve a "virtual" namespace string, consisting in the real
* namespace appended with an arbitrary number of {@literal .[sub-namespace]} strings to the
* root namespace.<br/>
* The latter is useful when invoking e.g. {@link NamespaceService#getByName(String)} with
* {@link ModelId#getNamespace()} since the latter does not trim out the "virtual" sub-namespaces.
* <br/>
* For instance, when given {@literal com.bosch.iot.suite.example.octopussuiteedition}:
* <ol>
* <li>
* Attempts to resolve {@literal com.bosch.iot.suite.example.octopussuiteedition} and get
* its workspace ID, which fails
* </li>
* <li>
* Attempts to resolve {@literal com.bosch.iot.suite.example} and get its workspace ID, which
* fails again
* </li>
* <li>
* Attempts to resolve {@literal com.bosch.iot.suite} and get its workspace ID, which
* succeeds
* </li>
* </ol>
* Note: virtual namespaces are cached within the scope of a request. <br/>
*
* @param name
* @return
*/
public Optional<Namespace> namespace(String name) {
// boilerplate null/empty checks
if (Objects.isNull(name) || name.trim().isEmpty()) {
return Optional.empty();
}
// lazy population
populateIfEmpty();
// lookup into virtual namespace map first
if (virtualNamespaces.containsKey(name)) {
Namespace result = virtualNamespaces.get(name);
LOGGER.debug(String.format("Resolved virtual namespace [%s] to [%s]", name, result.getName()));
return Optional.of(result);
}
// resolving by name equality
Optional<Namespace> result = this.namespaces.stream().filter(n -> n.getName().equals(name)).findAny();
if (result.isPresent()) {
LOGGER.debug(String.format("Resolved namespace [%s]", name));
return result;
} else {
return resolveVirtualNamespaceRecursively(name, name);
}
}
Aggregations