This repository has been archived by the owner on May 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 780
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kai Kreuzer <[email protected]>
- Loading branch information
1 parent
d7e2bf5
commit a53fb49
Showing
15 changed files
with
664 additions
and
14 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
...me.core/src/main/java/org/eclipse/smarthome/core/internal/items/MetaDataRegistryImpl.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,124 @@ | ||
/** | ||
* Copyright (c) 2014-2017 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.smarthome.core.internal.items; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
import org.eclipse.smarthome.core.common.registry.AbstractRegistry; | ||
import org.eclipse.smarthome.core.common.registry.Provider; | ||
import org.eclipse.smarthome.core.common.registry.RegistryChangeListener; | ||
import org.eclipse.smarthome.core.events.EventPublisher; | ||
import org.eclipse.smarthome.core.items.Item; | ||
import org.eclipse.smarthome.core.items.ItemRegistry; | ||
import org.eclipse.smarthome.core.items.ManagedMetaDataProvider; | ||
import org.eclipse.smarthome.core.items.MetaData; | ||
import org.eclipse.smarthome.core.items.MetaDataKey; | ||
import org.eclipse.smarthome.core.items.MetaDataProvider; | ||
import org.eclipse.smarthome.core.items.MetaDataRegistry; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Deactivate; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.osgi.service.component.annotations.ReferenceCardinality; | ||
import org.osgi.service.component.annotations.ReferencePolicy; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* This is the main implementing class of the {@link MetaDataRegistry} interface. It | ||
* keeps track of all declared meta data of all meta data providers. | ||
* | ||
* @author Kai Kreuzer - Initial contribution and API | ||
* | ||
*/ | ||
@Component(immediate = true, service = { MetaDataRegistry.class }) | ||
public class MetaDataRegistryImpl extends AbstractRegistry<MetaData, MetaDataKey, MetaDataProvider> | ||
implements MetaDataRegistry, RegistryChangeListener<Item> { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(MetaDataRegistryImpl.class); | ||
private ItemRegistry itemRegistry; | ||
|
||
public MetaDataRegistryImpl() { | ||
super(MetaDataProvider.class); | ||
} | ||
|
||
@Override | ||
@Activate | ||
protected void activate(BundleContext context) { | ||
super.activate(context); | ||
itemRegistry.addRegistryChangeListener(this); | ||
} | ||
|
||
@Override | ||
@Deactivate | ||
protected void deactivate() { | ||
super.deactivate(); | ||
itemRegistry.removeRegistryChangeListener(this); | ||
} | ||
|
||
@Override | ||
public MetaData get(MetaDataKey key) { | ||
for (final Map.Entry<Provider<MetaData>, Collection<MetaData>> entry : elementMap.entrySet()) { | ||
for (final MetaData item : entry.getValue()) { | ||
if (item.getUID().equals(key)) { | ||
return item; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.STATIC) | ||
protected void setItemRegistry(ItemRegistry itemRegistry) { | ||
this.itemRegistry = itemRegistry; | ||
} | ||
|
||
protected void unsetItemRegistry(ItemRegistry itemRegistry) { | ||
this.itemRegistry = null; | ||
} | ||
|
||
@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC) | ||
@Override | ||
protected void setEventPublisher(EventPublisher eventPublisher) { | ||
super.setEventPublisher(eventPublisher); | ||
} | ||
|
||
@Override | ||
protected void unsetEventPublisher(EventPublisher eventPublisher) { | ||
super.unsetEventPublisher(eventPublisher); | ||
} | ||
|
||
@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC, name = "ManagedThingProvider") | ||
protected void setManagedProvider(ManagedMetaDataProvider provider) { | ||
super.setManagedProvider(provider); | ||
} | ||
|
||
protected void unsetManagedProvider(ManagedMetaDataProvider managedProvider) { | ||
super.removeManagedProvider(managedProvider); | ||
} | ||
|
||
@Override | ||
public void added(Item element) { | ||
// do nothing | ||
} | ||
|
||
@Override | ||
public void removed(Item element) { | ||
if (managedProvider != null) { | ||
// remove our metadata for that item | ||
((ManagedMetaDataProvider) managedProvider).removeItemMetaData(element.getName()); | ||
} | ||
} | ||
|
||
@Override | ||
public void updated(Item oldElement, Item element) { | ||
// do nothing | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...marthome.core/src/main/java/org/eclipse/smarthome/core/items/ManagedMetaDataProvider.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,71 @@ | ||
/** | ||
* Copyright (c) 2014-2017 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.smarthome.core.items; | ||
|
||
import org.eclipse.jdt.annotation.NonNull; | ||
import org.eclipse.smarthome.core.common.registry.AbstractManagedProvider; | ||
import org.eclipse.smarthome.core.storage.StorageService; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* {@link ManagedMetaDataProvider} is an OSGi service, that allows to add or remove | ||
* meta data for items at runtime. Persistence of added meta data is handled by | ||
* a {@link StorageService}. | ||
* | ||
* @author Kai Kreuzer - Initial contribution | ||
*/ | ||
@Component(immediate = true, service = { MetaDataProvider.class, ManagedMetaDataProvider.class }) | ||
public class ManagedMetaDataProvider extends AbstractManagedProvider<MetaData, MetaDataKey, MetaData> | ||
implements MetaDataProvider { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(ManagedMetaDataProvider.class); | ||
|
||
@Override | ||
protected String getStorageName() { | ||
return MetaData.class.getName(); | ||
} | ||
|
||
@Override | ||
protected @NonNull String keyToString(@NonNull MetaDataKey key) { | ||
return key.toString(); | ||
} | ||
|
||
@Override | ||
protected MetaData toElement(@NonNull String key, @NonNull MetaData persistableElement) { | ||
return persistableElement; | ||
} | ||
|
||
@Override | ||
protected MetaData toPersistableElement(MetaData element) { | ||
return element; | ||
} | ||
|
||
@Override | ||
@Reference | ||
protected void setStorageService(StorageService storageService) { | ||
super.setStorageService(storageService); | ||
} | ||
|
||
@Override | ||
protected void unsetStorageService(StorageService storageService) { | ||
super.unsetStorageService(storageService); | ||
} | ||
|
||
/** | ||
* Removes all meta data of a given item | ||
* | ||
* @param itemname the name of the item for which the meta data is to be removed. | ||
*/ | ||
public void removeItemMetaData(@NonNull String name) { | ||
getAll().stream().filter(MetaDataPredicates.ofItem(name)).forEach(md -> remove(md.getUID())); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...e/org.eclipse.smarthome.core/src/main/java/org/eclipse/smarthome/core/items/MetaData.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,57 @@ | ||
/** | ||
* Copyright (c) 2014-2017 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.smarthome.core.items; | ||
|
||
import java.util.Map; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.smarthome.core.common.registry.Identifiable; | ||
|
||
/** | ||
* This is a data class for storing meta-data for a given item and namespace. | ||
* It is the entity used for within the {@link MetaDataRegistry}. | ||
* | ||
* @author Kai Kreuzer - Initial contribution and API | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public class MetaData implements Identifiable<MetaDataKey> { | ||
|
||
private final MetaDataKey key; | ||
private final String value; | ||
private final Map<String, Object> configuration; | ||
|
||
public MetaData(MetaDataKey key, String value, Map<String, Object> configuration) { | ||
this.key = key; | ||
this.value = value; | ||
this.configuration = configuration; | ||
} | ||
|
||
@Override | ||
public MetaDataKey getUID() { | ||
return key; | ||
} | ||
|
||
/** | ||
* Provides the configuration meta-data. | ||
* | ||
* @return configuration as a map of key-value pairs | ||
*/ | ||
public Map<String, Object> getConfiguration() { | ||
return configuration; | ||
} | ||
|
||
/** | ||
* Provides the main value of the meta-data. | ||
* | ||
* @return the main meta-data as a string | ||
*/ | ||
public String getValue() { | ||
return value; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...rg.eclipse.smarthome.core/src/main/java/org/eclipse/smarthome/core/items/MetaDataKey.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,90 @@ | ||
/** | ||
* Copyright (c) 2014-2017 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.smarthome.core.items; | ||
|
||
import org.eclipse.jdt.annotation.NonNull; | ||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
/** | ||
* This class represents the key of a {@link MetaData} entity. | ||
* It is a simple combination of a namespace and an item name. | ||
* | ||
* @author Kai Kreuzer - Initial contribution and API | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public class MetaDataKey { | ||
|
||
private final String namespace; | ||
private final String itemName; | ||
|
||
/** | ||
* Creates a new instance. | ||
* | ||
* @param namespace | ||
* @param itemName | ||
*/ | ||
public MetaDataKey(String namespace, String itemName) { | ||
this.namespace = namespace; | ||
this.itemName = itemName; | ||
} | ||
|
||
/** | ||
* Provides the item name of this key | ||
* | ||
* @return the item name | ||
*/ | ||
public String getItemName() { | ||
return itemName; | ||
} | ||
|
||
/** | ||
* Provides the namespace of this key | ||
* | ||
* @return the namespace | ||
*/ | ||
public String getNamespace() { | ||
return namespace; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((itemName == null) ? 0 : itemName.hashCode()); | ||
result = prime * result + ((namespace == null) ? 0 : namespace.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(@Nullable Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
MetaDataKey other = (MetaDataKey) obj; | ||
if (!itemName.equals(other.itemName)) { | ||
return false; | ||
} | ||
if (!namespace.equals(other.namespace)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public @NonNull String toString() { | ||
return namespace + ":" + itemName; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...pse.smarthome.core/src/main/java/org/eclipse/smarthome/core/items/MetaDataPredicates.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,39 @@ | ||
/** | ||
* Copyright (c) 2014-2017 by the respective copyright holders. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.eclipse.smarthome.core.items; | ||
|
||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Provides some default predicates that are helpful when working with metadata. | ||
* | ||
* @author Kai Kreuzer - Initial contribution and API | ||
* | ||
*/ | ||
public class MetaDataPredicates { | ||
|
||
/** | ||
* Creates a {@link Predicate} which can be used to filter {@link MetaData} for a given namespace. | ||
* | ||
* @param namespace to filter | ||
* @return created {@link Predicate} | ||
*/ | ||
public static Predicate<MetaData> hasNamespace(String namespace) { | ||
return md -> md.getUID().getNamespace().equals(namespace); | ||
} | ||
|
||
/** | ||
* Creates a {@link Predicate} which can be used to filter {@link MetaData} of a given item. | ||
* | ||
* @param itemname to filter | ||
* @return created {@link Predicate} | ||
*/ | ||
public static Predicate<MetaData> ofItem(String itemname) { | ||
return md -> md.getUID().getItemName().equals(itemname); | ||
} | ||
} |
Oops, something went wrong.