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 782
Refactored core.persistence bundle #1962
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
bundles/core/org.eclipse.smarthome.core.persistence/ESH-INF/config/config.xml
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,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> |
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
13 changes: 13 additions & 0 deletions
13
bundles/core/org.eclipse.smarthome.core.persistence/OSGI-INF/persistenceserviceregistry.xml
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,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> |
3 changes: 2 additions & 1 deletion
3
bundles/core/org.eclipse.smarthome.core.persistence/build.properties
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
output.. = target/classes/ | ||
bin.includes = META-INF/,\ | ||
.,\ | ||
about.html | ||
about.html,\ | ||
OSGI-INF/ | ||
source.. = src/main/java/,\ | ||
src/main/resources/ |
49 changes: 49 additions & 0 deletions
49
...ence/src/main/java/org/eclipse/smarthome/core/persistence/PersistenceServiceRegistry.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,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(); | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
.../java/org/eclipse/smarthome/core/persistence/internal/PersistenceServiceRegistryImpl.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,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(); | ||
} 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; | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?