Merge pull request #25 from kaikreuzer/npe

fixes NPE if parameters are null and removed toString calls in loggin…
pull/27/head
Kai Kreuzer 2016-02-22 19:34:30 +01:00
commit a06982b442
1 changed files with 37 additions and 30 deletions

View File

@ -9,7 +9,6 @@
package org.openhab.core.events; package org.openhab.core.events;
import org.eclipse.smarthome.core.events.EventPublisher; import org.eclipse.smarthome.core.events.EventPublisher;
import org.eclipse.smarthome.core.items.ItemRegistry;
import org.eclipse.smarthome.core.items.events.ItemCommandEvent; import org.eclipse.smarthome.core.items.events.ItemCommandEvent;
import org.eclipse.smarthome.core.items.events.ItemEventFactory; import org.eclipse.smarthome.core.items.events.ItemEventFactory;
import org.eclipse.smarthome.core.items.events.ItemStateEvent; import org.eclipse.smarthome.core.items.events.ItemStateEvent;
@ -21,43 +20,51 @@ import org.slf4j.LoggerFactory;
public class EventPublisherDelegate implements org.openhab.core.events.EventPublisher { public class EventPublisherDelegate implements org.openhab.core.events.EventPublisher {
private static final Logger logger = LoggerFactory.getLogger(EventPublisherDelegate.class); private static final Logger logger = LoggerFactory.getLogger(EventPublisherDelegate.class);
private EventPublisher eventPublisher; private EventPublisher eventPublisher;
public void setEventPublisher(EventPublisher eventPublisher) { public void setEventPublisher(EventPublisher eventPublisher) {
this.eventPublisher = eventPublisher; this.eventPublisher = eventPublisher;
} }
public void unsetEventPublisher(EventPublisher eventPublisher) { public void unsetEventPublisher(EventPublisher eventPublisher) {
this.eventPublisher = null; this.eventPublisher = null;
} }
@Override @Override
public void sendCommand(String itemName, Command command) { public void sendCommand(String itemName, Command command) {
// we do not offer synchronous sending of commands anymore // we do not offer synchronous sending of commands anymore
postCommand(itemName, command); postCommand(itemName, command);
} }
@Override @Override
public void postCommand(String itemName, Command command) { public void postCommand(String itemName, Command command) {
org.eclipse.smarthome.core.types.Command eshCommand = (org.eclipse.smarthome.core.types.Command) TypeMapper.mapToESHType(command); org.eclipse.smarthome.core.types.Command eshCommand = (org.eclipse.smarthome.core.types.Command) TypeMapper
if(eshCommand!=null) { .mapToESHType(command);
if (eshCommand != null) {
ItemCommandEvent event = ItemEventFactory.createCommandEvent(itemName, eshCommand); ItemCommandEvent event = ItemEventFactory.createCommandEvent(itemName, eshCommand);
eventPublisher.post(event); eventPublisher.post(event);
} else if (command != null) {
logger.warn("Compatibility layer could not convert {} of type {}.", command,
command.getClass().getSimpleName());
} else { } else {
logger.warn("Compatibility layer could not convert {} of type {}.", command.toString(), command.getClass().getSimpleName() ); logger.warn("given command is NULL, couldn't post command for '{}'", itemName);
} }
} }
@Override @Override
public void postUpdate(String itemName, State newState) { public void postUpdate(String itemName, State newState) {
org.eclipse.smarthome.core.types.State eshState = (org.eclipse.smarthome.core.types.State) TypeMapper.mapToESHType(newState); org.eclipse.smarthome.core.types.State eshState = (org.eclipse.smarthome.core.types.State) TypeMapper
if(eshState!=null) { .mapToESHType(newState);
if (eshState != null) {
ItemStateEvent event = ItemEventFactory.createStateEvent(itemName, eshState); ItemStateEvent event = ItemEventFactory.createStateEvent(itemName, eshState);
eventPublisher.post(event); eventPublisher.post(event);
} else { } else if (newState != null) {
logger.warn("Compatibility layer could not convert {} of type {}.", newState.toString(), newState.getClass().getSimpleName() ); logger.warn("Compatibility layer could not convert {} of type {}.", newState,
} newState.getClass().getSimpleName());
} } else {
logger.warn("given new state is NULL, couldn't post update for '{}'", itemName);
}
}
} }