org.apache.jackrabbit.oak.spi.whiteboard.DefaultWhiteboard Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aem-sdk-api Show documentation
Show all versions of aem-sdk-api Show documentation
The Adobe Experience Manager SDK
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jackrabbit.oak.spi.whiteboard;
import static org.apache.jackrabbit.guava.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static org.apache.jackrabbit.guava.common.collect.Sets.newIdentityHashSet;
import static java.util.Collections.emptyList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.jetbrains.annotations.NotNull;
public class DefaultWhiteboard implements Whiteboard {
private final Map, Set> registry = new HashMap<>();
private synchronized void registered(Class type, Service service) {
Set services = registry.get(type);
if (services == null) {
services = newIdentityHashSet();
registry.put(type, services);
}
services.add(service);
}
private synchronized void unregistered(Class type, Service service) {
Set services = registry.get(type);
if (services != null) {
services.remove(service);
}
}
@SuppressWarnings("unchecked")
private synchronized List lookup(Class type) {
Set services = registry.get(type);
if (services != null) {
return (List) services
.stream()
.map(Service::getService)
.collect(Collectors.toList());
} else {
return emptyList();
}
}
@SuppressWarnings("unchecked")
private synchronized List lookup(Class type, Map filterProperties) {
Set services = registry.get(type);
if (services != null) {
return (List) services
.stream()
.filter(s -> s.matches(filterProperties))
.map(Service::getService)
.collect(Collectors.toList());
} else {
return emptyList();
}
}
//--------------------------------------------------------< Whiteboard >--
@Override
public Registration register(
final Class type, final T service, Map, ?> properties) {
requireNonNull(type);
requireNonNull(service);
checkArgument(type.isInstance(service));
Service s = new Service(service, properties);
registered(type, s);
return new Registration() {
@Override
public void unregister() {
unregistered(type, s);
}
};
}
@Override
public Tracker track(final Class type) {
requireNonNull(type);
return new Tracker() {
@Override
public List getServices() {
return lookup(type);
}
@Override
public void stop() {
}
};
}
@Override
public Tracker track(Class type, Map filterProperties) {
requireNonNull(type);
return new Tracker() {
@Override
public List getServices() {
return lookup(type, filterProperties);
}
@Override
public void stop() {
}
};
}
private static class Service {
private final Object service;
private final Map, ?> properties;
private Service(@NotNull Object service, Map, ?> properties) {
requireNonNull(service);
this.service = service;
this.properties = properties;
}
private Object getService() {
return service;
}
private boolean matches(Map properties) {
return properties.entrySet().stream()
.allMatch(this::propertyMatches);
}
private boolean propertyMatches(Map.Entry filterEntry) {
String key = filterEntry.getKey();
String expectedValue = filterEntry.getValue();
if (properties == null || !properties.containsKey(key)) {
return expectedValue == null;
}
Object value = properties.get(key);
if (value == null) {
return expectedValue == null;
}
return value.toString().equals(expectedValue);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy