Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Refactored core.persistence bundle #1962

Merged
merged 1 commit into from
Aug 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<config-description:config-descriptions
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:config-description="http://eclipse.org/smarthome/schemas/config-description/v1.0.0"
xsi:schemaLocation="http://eclipse.org/smarthome/schemas/config-description/v1.0.0
http://eclipse.org/smarthome/schemas/config-description-1.0.0.xsd">

<config-description uri="system:persistence">
<parameter name="default" type="text" required="false">
<label>Default Service</label>
<description>The persistence service to use if no other is specified.</description>
</parameter>
</config-description>

</config-description:config-descriptions>
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ Bundle-Version: 0.9.0.qualifier
Bundle-ManifestVersion: 2
Bundle-License: http://www.eclipse.org/legal/epl-v10.html
Bundle-SymbolicName: org.eclipse.smarthome.core.persistence
Import-Package: org.eclipse.smarthome.core.items,
Import-Package: org.eclipse.smarthome.config.core,
org.eclipse.smarthome.core.items,
org.eclipse.smarthome.core.library.types,
org.eclipse.smarthome.core.persistence,
org.eclipse.smarthome.core.types,
org.slf4j
Export-Package: org.eclipse.smarthome.core.persistence,
org.eclipse.smarthome.core.persistence.dto
Bundle-ClassPath: .
Service-Component: OSGI-INF/*.xml
Bundle-ActivationPolicy: lazy
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" modified="modified" name="org.eclipse.smarthome.persistence">
<implementation class="org.eclipse.smarthome.core.persistence.internal.PersistenceServiceRegistryImpl"/>
<property name="service.pid" type="String" value="org.eclipse.smarthome.persistence"/>
<property name="service.config.description.uri" type="String" value="system:persistence"/>
<property name="service.config.label" type="String" value="Persistence"/>
<property name="service.config.category" type="String" value="system"/>
<reference bind="addPersistenceService" cardinality="0..n" interface="org.eclipse.smarthome.core.persistence.PersistenceService" name="PersistenceService" policy="dynamic" unbind="removePersistenceService"/>
<service>
<provide interface="org.eclipse.smarthome.core.persistence.PersistenceServiceRegistry"/>
<provide interface="org.eclipse.smarthome.config.core.ConfigOptionProvider"/>
</service>
</scr:component>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
output.. = target/classes/
bin.includes = META-INF/,\
.,\
about.html
about.html,\
OSGI-INF/
source.. = src/main/java/,\
src/main/resources/
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2014-2016 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.persistence;

import java.util.Set;

/**
* This is the interface for a central service that provides access to {@link PersistenceService}s.
*
* @author Kai Kreuzer - Initial contribution and API
*
*/
public interface PersistenceServiceRegistry {

/**
* Get the default persistence service.
*
* @return {@link PersistenceService} default service
*/
PersistenceService getDefault();

/**
* Get the persistence service with the given id.
*
* @param serviceId the service id
* @return {@link PersistenceService} the service with the id or null, if not present
*/
PersistenceService get(String serviceId);

/**
* Get the id of the default persistence service.
*
* @return the id of the default persistence service or null, if no default service is defined
*/
String getDefaultId();

/**
* Returns all available persistence services.
*
* @return all available persistence services
*/
Set<PersistenceService> getAll();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Copyright (c) 2014-2016 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.persistence.internal;

import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.eclipse.smarthome.config.core.ConfigOptionProvider;
import org.eclipse.smarthome.config.core.ParameterOption;
import org.eclipse.smarthome.core.persistence.PersistenceService;
import org.eclipse.smarthome.core.persistence.PersistenceServiceRegistry;

/**
* This is a central service for accessing {@link PersistenceService}s. It is registered through DS and also provides
* config options for the UI.
*
* @author Kai Kreuzer - Initial contribution and API
*
*/
public class PersistenceServiceRegistryImpl implements ConfigOptionProvider, PersistenceServiceRegistry {

private Map<String, PersistenceService> services = new HashMap<String, PersistenceService>();
private String defaultServiceId = null;

public PersistenceServiceRegistryImpl() {
}

public void addPersistenceService(PersistenceService service) {
services.put(service.getId(), service);
}

public void removePersistenceService(PersistenceService service) {
services.remove(service.getId());
}

protected void activate(Map<String, Object> config) {
modified(config);
}

protected void modified(Map<String, Object> config) {
if (config != null) {
defaultServiceId = (String) config.get("default");
}
}

@Override
public PersistenceService getDefault() {
return get(getDefaultId());
}

@Override
public PersistenceService get(String serviceId) {
if (serviceId != null) {
return services.get(serviceId);
} else {
return null;
}
}

@Override
public String getDefaultId() {
if (defaultServiceId != null) {
return defaultServiceId;
} else {
// if there is exactly one service available in the system, we assume that this should be used, if no
// default is specifically configured.
if (services.size() == 1) {
return services.keySet().iterator().next();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an idea:

We could change the services member to a sorted map:
private SortedMap<String, PersistenceService> services = new TreeMap<String, PersistenceService>();

Then we could use firstKey() and don't need to create a iteratior.

I assume it will speed up the getDefaultId() method a little bit (insert and remove are perhaps called not so frequently as fetching the default ID and insert / remove in a tree is not to problematic).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that getting in iterator is by any means an expensive operation?

} else {
return null;
}
}
}

@Override
public Set<PersistenceService> getAll() {
return new HashSet<>(services.values());
}

@Override
public Collection<ParameterOption> getParameterOptions(URI uri, String param, Locale locale) {
Set<ParameterOption> options = new HashSet<>();
if (uri.toString().equals("system:persistence") && param.equals("default")) {
for (PersistenceService service : getAll()) {
options.add(new ParameterOption(service.getId(), service.getLabel(locale)));
}
}
return options;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ Import-Package: com.google.common.base,
org.eclipse.smarthome.io.rest,
org.eclipse.smarthome.io.rest.core.item,
org.eclipse.smarthome.io.rest.core.thing,
org.eclipse.smarthome.model.persistence.extensions,
org.osgi.framework,
org.osgi.service.cm,
org.osgi.service.component,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
-->
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="org.eclipse.smarthome.core.persistence.internal.rest">
<implementation class="org.eclipse.smarthome.io.rest.core.persistence.PersistenceResource"/>
<reference bind="addPersistenceService" cardinality="0..n" interface="org.eclipse.smarthome.core.persistence.PersistenceService" name="PersistenceService" policy="dynamic" unbind="removePersistenceService"/>
<reference bind="setItemRegistry" cardinality="1..1" interface="org.eclipse.smarthome.core.items.ItemRegistry" name="ItemRegistry" policy="dynamic" unbind="unsetItemRegistry"/>
<service>
<provide interface="org.eclipse.smarthome.io.rest.RESTResource"/>
</service>
<reference bind="setPersistenceServiceRegistry" cardinality="1..1" interface="org.eclipse.smarthome.core.persistence.PersistenceServiceRegistry" name="PersistenceServiceRegistry" policy="static" unbind="unsetPersistenceServiceRegistry"/>
</scr:component>
Loading