Search in sources :

Example 1 with NamespaceService

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);
    }
}
Also used : Predicate(java.util.function.Predicate) Collection(java.util.Collection) RequestScope(org.springframework.web.context.annotation.RequestScope) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Autowired(org.springframework.beans.factory.annotation.Autowired) Collectors(java.util.stream.Collectors) ModelId(org.eclipse.vorto.model.ModelId) Namespace(org.eclipse.vorto.repository.domain.Namespace) NAMESPACE_SEPARATOR(org.eclipse.vorto.repository.services.NamespaceService.NAMESPACE_SEPARATOR) NamespaceService(org.eclipse.vorto.repository.services.NamespaceService) Objects(java.util.Objects) Logger(org.apache.log4j.Logger) NamespaceRepository(org.eclipse.vorto.repository.repositories.NamespaceRepository) Service(org.springframework.stereotype.Service) Map(java.util.Map) Optional(java.util.Optional) Collections(java.util.Collections) PRIVATE_NAMESPACE_PREFIX(org.eclipse.vorto.repository.services.NamespaceService.PRIVATE_NAMESPACE_PREFIX) Namespace(org.eclipse.vorto.repository.domain.Namespace)

Aggregations

Collection (java.util.Collection)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Logger (org.apache.log4j.Logger)1 ModelId (org.eclipse.vorto.model.ModelId)1 Namespace (org.eclipse.vorto.repository.domain.Namespace)1 NamespaceRepository (org.eclipse.vorto.repository.repositories.NamespaceRepository)1 NamespaceService (org.eclipse.vorto.repository.services.NamespaceService)1 NAMESPACE_SEPARATOR (org.eclipse.vorto.repository.services.NamespaceService.NAMESPACE_SEPARATOR)1 PRIVATE_NAMESPACE_PREFIX (org.eclipse.vorto.repository.services.NamespaceService.PRIVATE_NAMESPACE_PREFIX)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Service (org.springframework.stereotype.Service)1 RequestScope (org.springframework.web.context.annotation.RequestScope)1