Skip to content

Commit

Permalink
Propagate config descriptors from thing definitions.
Browse files Browse the repository at this point in the history
Signed-off-by: Łukasz Dywicki <luke@code-house.org>
  • Loading branch information
splatch committed Feb 12, 2024
1 parent 72be63f commit 77d0f42
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 20 deletions.
Expand Up @@ -17,6 +17,7 @@
*/
package org.thing4.tools.maven;

import java.util.Collection;
import java.util.Optional;

/**
Expand All @@ -25,8 +26,10 @@
* @param <K> Key type.
* @param <V> Value type.
*/
public interface Cache<K, V> {
public interface Cache<K, E, V extends Collection<E>> {

Optional<V> get(K key);

void append(K key, E value);

}
Expand Up @@ -42,16 +42,16 @@ public abstract class DescriptorProcessorMojo extends AbstractMojo {
private MavenProject project;

@Inject
private Cache<MavenProject, List<ConfigDescription>> configCache;
private Cache<MavenProject, ConfigDescription, List<ConfigDescription>> configCache;

@Inject
private Cache<MavenProject, List<ThingType>> thingCache;
private Cache<MavenProject, ThingType, List<ThingType>> thingCache;

@Inject
private Cache<MavenProject, List<ChannelType>> channelTypeCache;
private Cache<MavenProject, ChannelType, List<ChannelType>> channelTypeCache;

@Inject
private Cache<MavenProject, List<ChannelGroupType>> channelGroupTypeCache;
private Cache<MavenProject, ChannelGroupType, List<ChannelGroupType>> channelGroupTypeCache;

@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
Expand Down
Expand Up @@ -17,11 +17,10 @@
*/
package org.thing4.tools.maven.plexus;

import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.openhab.core.thing.type.ChannelGroupType;
import org.openhab.core.thing.type.ChannelType;
import org.openhab.core.thing.xml.internal.ChannelGroupTypeXmlResult;
import org.openhab.core.thing.xml.internal.ChannelTypeXmlResult;
import org.thing4.tools.maven.Cache;

@Component(role = Cache.class, hint = ChannelGroupTypeDescriptorCache.CHANNEL_GROUP_TYPE_HINT)
Expand All @@ -30,7 +29,7 @@ public class ChannelGroupTypeDescriptorCache extends ThingDescriptorCacheBase<Ch
public static final String CHANNEL_GROUP_TYPE_HINT = "channel-group-type";

@Override
protected ChannelGroupType define(Object element) {
protected ChannelGroupType define(MavenProject key, Object element) {
if (element instanceof ChannelGroupTypeXmlResult) {
return ((ChannelGroupTypeXmlResult) element).toChannelGroupType();
}
Expand Down
Expand Up @@ -17,6 +17,7 @@
*/
package org.thing4.tools.maven.plexus;

import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.openhab.core.thing.type.ChannelType;
import org.openhab.core.thing.xml.internal.ChannelTypeXmlResult;
Expand All @@ -28,7 +29,7 @@ public class ChannelTypeDescriptorCache extends ThingDescriptorCacheBase<Channel
public static final String CHANNEL_TYPE_HINT = "channel-type";

@Override
protected ChannelType define(Object element) {
protected ChannelType define(MavenProject key, Object element) {
if (element instanceof ChannelTypeXmlResult) {
return ((ChannelTypeXmlResult) element).toChannelType();
}
Expand Down
Expand Up @@ -19,7 +19,9 @@

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -31,27 +33,38 @@
import org.thing4.tools.maven.Cache;

@Component(role = Cache.class, hint = ConfigDescriptorCache.HINT)
public class ConfigDescriptorCache extends DescriptorCache<List<ConfigDescription>> {
public class ConfigDescriptorCache extends DescriptorCache<ConfigDescription, List<ConfigDescription>> {

public static final String HINT = "config-description";

private final Map<MavenProject, List<ConfigDescription>> cache = new ConcurrentHashMap<>();
private final Map<MavenProject, Map<URI, ConfigDescription>> cache = new ConcurrentHashMap<>();

@Override
public Optional<List<ConfigDescription>> get(MavenProject key) {
return Optional.ofNullable(cache.computeIfAbsent(key, this::load));
return Optional.ofNullable(cache.computeIfAbsent(key, this::load))
.map(map -> new ArrayList<>(map.values()));
}

private List<ConfigDescription> load(MavenProject project) {
List<ConfigDescription> configDescriptions = new ArrayList<>();
@Override
public void append(MavenProject key, ConfigDescription value) {
if (!cache.containsKey(key)) {
cache.put(key, load(key));
}
cache.get(key).putIfAbsent(value.getUID(), value);
}

private Map<URI, ConfigDescription> load(MavenProject project) {
Map<URI, ConfigDescription> configDescriptions = new HashMap<>();
ConfigDescriptionReader reader = new ConfigDescriptionReader();
for (File file : getXmlFiles(project, "OH-INF/config/")) {
try {
List<ConfigDescription> elements = reader.readFromXML(file.toURI().toURL());
if (elements == null) {
continue;
}
configDescriptions.addAll(elements);
for (ConfigDescription element : elements) {
configDescriptions.putIfAbsent(element.getUID(), element);
}
} catch (MalformedURLException e) {
// continue
}
Expand Down
Expand Up @@ -20,12 +20,13 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.maven.model.Resource;
import org.apache.maven.project.MavenProject;
import org.thing4.tools.maven.Cache;

public abstract class DescriptorCache<V> implements Cache<MavenProject, V> {
public abstract class DescriptorCache<E, V extends Collection<E>> implements Cache<MavenProject, E, V> {

protected final File[] getXmlFiles(MavenProject project, String subdirectory) {
List<File> files = new ArrayList<>();
Expand Down
Expand Up @@ -17,6 +17,7 @@
*/
package org.thing4.tools.maven.plexus;

import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.openhab.core.thing.type.ThingType;
import org.openhab.core.thing.xml.internal.ThingTypeXmlResult;
Expand All @@ -28,9 +29,12 @@ public class ThingDescriptorCache extends ThingDescriptorCacheBase<ThingType> {
public static final String THING_HINT = "thing";

@Override
protected ThingType define(Object element) {
protected ThingType define(MavenProject key, Object element) {
if (element instanceof ThingTypeXmlResult) {
ThingTypeXmlResult xmlResult = (ThingTypeXmlResult) element;
if (xmlResult.getConfigDescription() != null) {
configDescriptorCache.append(key, xmlResult.getConfigDescription());
}
return xmlResult.toThingType();
}
return null;
Expand Down
Expand Up @@ -24,18 +24,33 @@
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Inject;
import org.apache.maven.project.MavenProject;
import org.openhab.core.config.core.ConfigDescription;
import org.openhab.core.thing.xml.internal.ThingDescriptionReader;
import org.thing4.tools.maven.Cache;

public abstract class ThingDescriptorCacheBase<T> extends DescriptorCache<List<T>> {
public abstract class ThingDescriptorCacheBase<T> extends DescriptorCache<T, List<T>> {

private final Map<MavenProject, List<T>> cache = new ConcurrentHashMap<>();


@Inject
protected Cache<MavenProject, ConfigDescription, List<ConfigDescription>> configDescriptorCache;

@Override
public Optional<List<T>> get(MavenProject key) {
return Optional.ofNullable(cache.computeIfAbsent(key, this::load));
}

@Override
public void append(MavenProject key, T value) {
if (!cache.containsKey(key)) {
cache.put(key, load(key));
}
cache.get(key).add(value);
}

private List<T> load(MavenProject project) {
List<T> definitions = new ArrayList<>();
ThingDescriptionReader reader = new ThingDescriptionReader();
Expand All @@ -46,7 +61,7 @@ private List<T> load(MavenProject project) {
continue;
}
for (Object element : elements) {
T definition = define(element);
T definition = define(project, element);
if (definition != null) {
definitions.add(definition);
}
Expand All @@ -59,6 +74,6 @@ private List<T> load(MavenProject project) {
return definitions;
}

protected abstract T define(Object element);
protected abstract T define(MavenProject key, Object element);

}

0 comments on commit 77d0f42

Please sign in to comment.