Skip to content

Commit

Permalink
Implemented ItemRegistry, ItemUIRegistry and ChartProvider support fo…
Browse files Browse the repository at this point in the history
…r the compatibility layer. This fixes openhab#146.

Signed-off-by: Kai Kreuzer <[email protected]> (github: @kaikreuzer)
  • Loading branch information
kaikreuzer committed Mar 14, 2015
1 parent 791e777 commit 60c3b88
Show file tree
Hide file tree
Showing 33 changed files with 2,216 additions and 4 deletions.
12 changes: 10 additions & 2 deletions bundles/core/org.openhab.core.compat1x/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ Import-Package: com.google.common.base,
org.apache.commons.io;version="2.0.1",
org.apache.commons.lang,
org.apache.commons.net.util,
org.eclipse.emf.common.util,
org.eclipse.emf.ecore,
org.eclipse.emf.ecore.resource,
org.eclipse.smarthome.core.autoupdate,
org.eclipse.smarthome.core.common.registry,
org.eclipse.smarthome.core.events,
org.eclipse.smarthome.core.items,
org.eclipse.smarthome.core.library.items,
Expand All @@ -32,6 +35,9 @@ Import-Package: com.google.common.base,
org.eclipse.smarthome.core.types,
org.eclipse.smarthome.io.multimedia.tts,
org.eclipse.smarthome.model.item,
org.eclipse.smarthome.model.sitemap,
org.eclipse.smarthome.ui.chart,
org.eclipse.smarthome.ui.items,
org.openhab.io.multimedia.actions,
org.openhab.library.tel.types,
org.osgi.framework,
Expand Down Expand Up @@ -59,7 +65,9 @@ Export-Package: org.openhab.core.autoupdate,
org.openhab.io.net.http,
org.openhab.library.tel.items,
org.openhab.library.tel.types,
org.openhab.model.item.binding
org.openhab.model.item.binding,
org.openhab.ui.chart,
org.openhab.ui.items
Bundle-ClassPath: lib/jl1.0.1.jar,
.
Service-Component: OSGI-INF/*
Service-Component: OSGI-INF/*.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="org.openhab.core.compat1x.chartproviderfactory" immediate="true" >
<implementation class="org.openhab.ui.chart.internal.ChartProviderFactory"/>
<reference bind="addChartProvider" cardinality="0..n" interface="org.openhab.ui.chart.ChartProvider" name="ChartProvider" policy="dynamic" unbind="removeChartProvider"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="org.openhab.core.compat1x.itemuiregistry">
<implementation class="org.openhab.core.items.internal.ItemUIRegistryDelegate"/>
<service>
<provide interface="org.openhab.core.items.ItemRegistry"/>
<provide interface="org.openhab.ui.items.ItemUIRegistry"/>
</service>
<reference bind="setItemUIRegistry" cardinality="1..1" interface="org.eclipse.smarthome.ui.items.ItemUIRegistry" name="ItemUIRegistry" policy="static" unbind="unsetItemUIRegistry"/>
</scr:component>
4 changes: 3 additions & 1 deletion bundles/core/org.openhab.core.compat1x/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ bin.includes = META-INF/,\
OSGI-INF/eventbridge.xml,\
OSGI-INF/eventpublisherdelegate.xml,\
OSGI-INF/bindingconfigreaderfactory.xml,\
OSGI-INF/actionservicefactory.xml
OSGI-INF/actionservicefactory.xml,\
OSGI-INF/itemuiregistry.xml,\
OSGI-INF/chartproviderfactory.xml
source.. = src/main/java/
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package org.openhab.core.compat1x.internal;

import org.eclipse.smarthome.core.items.GroupItem;
import org.eclipse.smarthome.core.items.Item;
import org.eclipse.smarthome.core.library.items.ColorItem;
import org.eclipse.smarthome.core.library.items.ContactItem;
Expand All @@ -34,7 +35,25 @@ public static org.openhab.core.items.Item mapToOpenHABItem(Item item) {
if(item instanceof ColorItem) result = new org.openhab.core.library.items.ColorItem(item.getName());
if(item instanceof DateTimeItem) result = new org.openhab.core.library.items.DateTimeItem(item.getName());
if(item instanceof ESHCallItem) result = new org.openhab.library.tel.items.CallItem(item.getName());


if(item instanceof GroupItem) {
GroupItem gItem = (GroupItem) item;
org.openhab.core.items.Item baseItem = ItemMapper.mapToOpenHABItem(gItem.getBaseItem());
org.openhab.core.items.GroupItem ohgItem;
if(baseItem instanceof GenericItem) {
ohgItem = new org.openhab.core.items.GroupItem(item.getName(), (GenericItem) baseItem);
} else {
ohgItem = new org.openhab.core.items.GroupItem(item.getName());
}
for(Item member : gItem.getMembers()) {
org.openhab.core.items.Item ohMember = ItemMapper.mapToOpenHABItem(member);
if(ohMember != null) {
ohgItem.addMember(ohMember);
}
}
result = ohgItem;
}

if(result instanceof org.openhab.core.items.GenericItem) {
org.openhab.core.items.GenericItem genericItem = (GenericItem) result;
if(item.getState()!=null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package org.openhab.core.items.internal;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.smarthome.core.common.registry.RegistryChangeListener;
import org.openhab.core.compat1x.internal.ItemMapper;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.items.ItemNotUniqueException;
import org.openhab.core.items.ItemRegistryChangeListener;
import org.openhab.core.types.State;
import org.openhab.model.sitemap.LinkableWidget;
import org.openhab.model.sitemap.Sitemap;
import org.openhab.model.sitemap.Widget;
import org.openhab.ui.items.ItemUIRegistry;

public class ItemUIRegistryDelegate implements ItemUIRegistry, RegistryChangeListener<org.eclipse.smarthome.core.items.Item> {

private org.eclipse.smarthome.ui.items.ItemUIRegistry itemUIRegistry;
private Set<ItemRegistryChangeListener> listeners = new HashSet<>();

protected void setItemUIRegistry(org.eclipse.smarthome.ui.items.ItemUIRegistry itemUIRegistry) {
this.itemUIRegistry = itemUIRegistry;
itemUIRegistry.addRegistryChangeListener(this);
}

protected void unsetItemUIRegistry(org.eclipse.smarthome.core.items.ItemRegistry itemUIRegistry) {
this.itemUIRegistry = null;
}

@Override
public Item getItem(String name) throws ItemNotFoundException {
org.eclipse.smarthome.core.items.Item eshItem = itemUIRegistry.get(name);
return ItemMapper.mapToOpenHABItem(eshItem);
}

@Override
public Item getItemByPattern(String name) throws ItemNotFoundException, ItemNotUniqueException {
org.eclipse.smarthome.core.items.Item eshItem;
try {
eshItem = itemUIRegistry.getItemByPattern(name);
} catch (org.eclipse.smarthome.core.items.ItemNotFoundException e) {
throw new ItemNotFoundException(name);
} catch (org.eclipse.smarthome.core.items.ItemNotUniqueException e) {
throw new ItemNotUniqueException(name, null);
}
return ItemMapper.mapToOpenHABItem(eshItem);
}

@Override
public Collection<Item> getItems() {
Collection<org.eclipse.smarthome.core.items.Item> eshItems = itemUIRegistry.getItems();
Collection<Item> ohItems = new HashSet<Item>(eshItems.size());

for(org.eclipse.smarthome.core.items.Item eshItem : eshItems) {
ohItems.add(ItemMapper.mapToOpenHABItem(eshItem));
}
return ohItems;
}

@Override
public Collection<Item> getItems(String pattern) {
Collection<org.eclipse.smarthome.core.items.Item> eshItems = itemUIRegistry.getItems(pattern);
Collection<Item> ohItems = new HashSet<Item>(eshItems.size());

for(org.eclipse.smarthome.core.items.Item eshItem : eshItems) {
ohItems.add(ItemMapper.mapToOpenHABItem(eshItem));
}
return ohItems;
}

@Override
public boolean isValidItemName(String itemName) {
return itemUIRegistry.isValidItemName(itemName);
}

@Override
public void addItemRegistryChangeListener(ItemRegistryChangeListener listener) {
this.listeners.add(listener);
}

@Override
public void removeItemRegistryChangeListener(ItemRegistryChangeListener listener) {
this.listeners.remove(listener);
}

@Override
public void added(org.eclipse.smarthome.core.items.Item element) {
Item ohItem = ItemMapper.mapToOpenHABItem(element);
for(ItemRegistryChangeListener listener : listeners) {
listener.itemAdded(ohItem);
}
}

@Override
public void removed(org.eclipse.smarthome.core.items.Item element) {
Item ohItem = ItemMapper.mapToOpenHABItem(element);
for(ItemRegistryChangeListener listener : listeners) {
listener.itemRemoved(ohItem);
}
}

@Override
public void updated(org.eclipse.smarthome.core.items.Item oldElement, org.eclipse.smarthome.core.items.Item element) {
Item ohItem = ItemMapper.mapToOpenHABItem(element);
for(ItemRegistryChangeListener listener : listeners) {
listener.itemRemoved(ohItem);
listener.itemAdded(ohItem);
}
}

@Override
public String getIcon(String itemName) {
return itemUIRegistry.getIcon(itemName);
}

@Override
public String getLabel(String itemName) {
return itemUIRegistry.getLabel(itemName);
}

@Override
public Widget getDefaultWidget(Class<? extends Item> itemType, String itemName) {
return null;
}

@Override
public Widget getWidget(String itemName) {
return null;
}

@Override
public String getLabel(Widget w) {
return itemUIRegistry.getLabel(w.getItem());
}

@Override
public String getIcon(Widget w) {
return itemUIRegistry.getIcon(w.getItem());
}

@Override
public State getState(Widget w) {
return null;
}

@Override
public Widget getWidget(Sitemap sitemap, String id) {
return null;
}

@Override
public String getWidgetId(Widget w) {
return null;
}

@Override
public EList<Widget> getChildren(LinkableWidget w) {
return null;
}

@Override
public boolean iconExists(String icon) {
return itemUIRegistry.iconExists(icon);
}

@Override
public String getLabelColor(Widget w) {
return null;
}

@Override
public String getValueColor(Widget w) {
return null;
}

@Override
public boolean getVisiblity(Widget w) {
return true;
}

@Override
public State getItemState(String itemName) {
try {
return getItem(itemName).getState();
} catch (ItemNotFoundException e) {
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (c) 2010-2014, openHAB.org and others.
*
* 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.openhab.io.net.http;

import java.io.IOException;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osgi.service.http.HttpContext;


/**
* Implementation of {@link HttpContext} which adds Basic-Authentication
* functionality to openHAB.
*
* @author Thomas.Eichstaedt-Engelen
* @since 0.9.0
*/
public class SecureHttpContext implements HttpContext {

private HttpContext defaultContext = null;

public SecureHttpContext() {
}

public SecureHttpContext(HttpContext defaultContext, final String realm) {
this.defaultContext = defaultContext;
}


/**
* <p>@{inheritDoc}</p>
* <p>Delegates to <code>defaultContext.getMimeType()</code>
*/
public String getMimeType(String name) {
return this.defaultContext.getMimeType(name);
}

/**
* <p>@{inheritDoc}</p>
* <p>Delegates to <code>defaultContext.getResource()</code>
*/
public URL getResource(String name) {
return this.defaultContext.getResource(name);
}

@Override
public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {
return true;
}

}
Loading

0 comments on commit 60c3b88

Please sign in to comment.