From cf7fb669d1614620d09e24eb515afe482fb2bd99 Mon Sep 17 00:00:00 2001 From: eclipse-kura-bot <77626377+eclipse-kura-bot@users.noreply.github.com> Date: Fri, 4 Oct 2024 17:59:32 +0200 Subject: [PATCH] feat: Added asset name in error logs [backport release-5.6.0] (#5416) feat: Added asset name in error logs (#5384) Signed-off-by: MMaiero (cherry picked from commit 3d57b434ba2cd9135e68e9bff94910a390622c90) Co-authored-by: Matteo Maiero --- .../kura/asset/provider/BaseAsset.java | 55 ++++++++---------- .../kura/internal/wire/asset/WireAsset.java | 58 +++++++++---------- 2 files changed, 51 insertions(+), 62 deletions(-) diff --git a/kura/org.eclipse.kura.asset.provider/src/main/java/org/eclipse/kura/asset/provider/BaseAsset.java b/kura/org.eclipse.kura.asset.provider/src/main/java/org/eclipse/kura/asset/provider/BaseAsset.java index 47cf964ce8d..ac5eeda71bd 100644 --- a/kura/org.eclipse.kura.asset.provider/src/main/java/org/eclipse/kura/asset/provider/BaseAsset.java +++ b/kura/org.eclipse.kura.asset.provider/src/main/java/org/eclipse/kura/asset/provider/BaseAsset.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2016, 2023 Eurotech and/or its affiliates and others + * Copyright (c) 2016, 2024 Eurotech and/or its affiliates and others * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 @@ -51,7 +51,6 @@ import org.eclipse.kura.channel.listener.ChannelEvent; import org.eclipse.kura.channel.listener.ChannelListener; import org.eclipse.kura.configuration.ComponentConfiguration; -import org.eclipse.kura.configuration.ConfigurationService; import org.eclipse.kura.configuration.SelfConfiguringComponent; import org.eclipse.kura.core.configuration.ComponentConfigurationImpl; import org.eclipse.kura.core.configuration.metatype.Tad; @@ -158,9 +157,9 @@ public class BaseAsset implements Asset, SelfConfiguringComponent { * OSGi service component callback while activation. * * @param componentContext - * the component context + * the component context * @param properties - * the service properties + * the service properties */ protected void activate(final ComponentContext componentContext, final Map properties) { logger.info("activating..."); @@ -174,7 +173,7 @@ protected void activate(final ComponentContext componentContext, final Map properties) { @@ -194,7 +193,7 @@ public void updated(final Map properties) { * OSGi service component callback while deactivation. * * @param context - * the component context + * the component context */ protected void deactivate(final ComponentContext context) { logger.debug("deactivating..."); @@ -213,9 +212,9 @@ protected void deactivate(final ComponentContext context) { * PID. * * @param driverId - * the identifier of the driver + * the identifier of the driver * @throws NullPointerException - * if driver id provided is null + * if driver id provided is null */ private void reopenDriverTracker(final String driverId) { requireNonNull(driverId, "Driver PID cannot be null"); @@ -293,15 +292,13 @@ public ComponentConfiguration getConfiguration() throws KuraException { final Map properties = this.config.getProperties(); - final String componentName = properties.get(ConfigurationService.KURA_SERVICE_PID).toString(); - Tocd ocd = this.config.getDefinition(); if (ocd == null) { ocd = getOCD(); } - return new ComponentConfigurationImpl(componentName, ocd, new HashMap<>(properties)); + return new ComponentConfigurationImpl(getKuraServicePid(), ocd, new HashMap<>(properties)); } /** @@ -313,7 +310,7 @@ protected String getFactoryPid() { return CONF_PID; } - protected String getKuraServicePid() throws KuraException { + protected String getKuraServicePid() { return this.config.getKuraServicePid(); } @@ -405,14 +402,13 @@ public List read(final Set channelNames) throws KuraExcep } protected List getFinalRecords(List channelRecords, Map channels) { - channelRecords.stream() - .forEach(channelRecord -> { - Channel channel = channels.get(channelRecord.getChannelName()); + channelRecords.stream().forEach(channelRecord -> { + Channel channel = channels.get(channelRecord.getChannelName()); - if (shouldApplyScaleAndOffset(channelRecord, channel)) { - applyScaleAndOffset(channelRecord, channel); - } - }); + if (shouldApplyScaleAndOffset(channelRecord, channel)) { + applyScaleAndOffset(channelRecord, channel); + } + }); return channelRecords; } @@ -427,19 +423,17 @@ private void applyScaleAndOffset(final ChannelRecord channelRecord, final Channe final double channelOffset = channel.getValueOffset(); if (channelRecord.getValueType().equals(DataType.DOUBLE)) { - channelRecord.setValue(new DoubleValue( - (double) channelRecord.getValue().getValue() * channelScale + channelOffset)); - } else if (channelRecord.getValueType().equals(DataType.FLOAT)) { channelRecord.setValue( - new FloatValue((float) channelRecord.getValue().getValue() * (float) channelScale - + (float) channelOffset)); + new DoubleValue((double) channelRecord.getValue().getValue() * channelScale + channelOffset)); + } else if (channelRecord.getValueType().equals(DataType.FLOAT)) { + channelRecord.setValue(new FloatValue( + (float) channelRecord.getValue().getValue() * (float) channelScale + (float) channelOffset)); } else if (channelRecord.getValueType().equals(DataType.INTEGER)) { channelRecord.setValue(new IntegerValue( (int) channelRecord.getValue().getValue() * (int) channelScale + (int) channelOffset)); } else if (channelRecord.getValueType().equals(DataType.LONG)) { - channelRecord - .setValue(new LongValue((long) channelRecord.getValue().getValue() * (long) channelScale - + (long) channelOffset)); + channelRecord.setValue(new LongValue( + (long) channelRecord.getValue().getValue() * (long) channelScale + (long) channelOffset)); } } @@ -619,8 +613,7 @@ protected class ChannelListenerHolder implements ChannelListener { private final ChannelListener listener; private final Channel channel; - public ChannelListenerHolder(Channel channel, - ChannelListener listener) { + public ChannelListenerHolder(Channel channel, ChannelListener listener) { this.channel = channel; this.listener = listener; } @@ -637,10 +630,10 @@ public ChannelListener getChannelListener() { public void onChannelEvent(ChannelEvent event) { final ChannelRecord originaRecord = event.getChannelRecord(); - if (shouldApplyScaleAndOffset(originaRecord, channel)) { + if (shouldApplyScaleAndOffset(originaRecord, this.channel)) { final ChannelRecord cloned = cloneRecord(originaRecord); - applyScaleAndOffset(cloned, channel); + applyScaleAndOffset(cloned, this.channel); this.listener.onChannelEvent(new ChannelEvent(cloned)); } else { diff --git a/kura/org.eclipse.kura.wire.component.provider/src/main/java/org/eclipse/kura/internal/wire/asset/WireAsset.java b/kura/org.eclipse.kura.wire.component.provider/src/main/java/org/eclipse/kura/internal/wire/asset/WireAsset.java index d2dea5dd950..9e6ba433fb1 100644 --- a/kura/org.eclipse.kura.wire.component.provider/src/main/java/org/eclipse/kura/internal/wire/asset/WireAsset.java +++ b/kura/org.eclipse.kura.wire.component.provider/src/main/java/org/eclipse/kura/internal/wire/asset/WireAsset.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2016, 2023 Eurotech and/or its affiliates and others + * Copyright (c) 2016, 2024 Eurotech and/or its affiliates and others * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 @@ -27,7 +27,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.eclipse.kura.KuraException; import org.eclipse.kura.asset.AssetConfiguration; import org.eclipse.kura.asset.provider.BaseAsset; import org.eclipse.kura.channel.Channel; @@ -116,7 +115,7 @@ public final class WireAsset extends BaseAsset implements WireEmitter, WireRecei * Binds the Wire Helper Service. * * @param wireHelperService - * the new Wire Helper Service + * the new Wire Helper Service */ public void bindWireHelperService(final WireHelperService wireHelperService) { if (isNull(this.wireHelperService)) { @@ -128,7 +127,7 @@ public void bindWireHelperService(final WireHelperService wireHelperService) { * Unbinds the Wire Helper Service. * * @param wireHelperService - * the new Wire Helper Service + * the new Wire Helper Service */ public void unbindWireHelperService(final WireHelperService wireHelperService) { if (this.wireHelperService == wireHelperService) { @@ -140,9 +139,9 @@ public void unbindWireHelperService(final WireHelperService wireHelperService) { * OSGi service component activation callback. * * @param componentContext - * the component context + * the component context * @param properties - * the service properties + * the service properties */ @Override protected void activate(final ComponentContext componentContext, final Map properties) { @@ -157,7 +156,7 @@ protected void activate(final ComponentContext componentContext, final Map properties) { @@ -178,7 +177,7 @@ public void updated(final Map properties) { * OSGi service component deactivate callback. * * @param context - * the context + * the context */ @Override protected void deactivate(final ComponentContext context) { @@ -217,9 +216,9 @@ protected String getFactoryPid() { * Component(s). * * @param wireEnvelope - * the received {@link WireEnvelope} + * the received {@link WireEnvelope} * @throws NullPointerException - * if {@link WireEnvelope} is null + * if {@link WireEnvelope} is null */ @Override public void onWireReceive(final WireEnvelope wireEnvelope) { @@ -249,7 +248,7 @@ private void emitAllReadChannels() { try { emitChannelRecords(readAllChannels()); } catch (final Exception e) { - logger.error("Error while performing read from the Wire Asset...", e); + logger.error("Error while performing read from the Wire Asset: {}", getKuraServicePid(), e); } } } @@ -258,13 +257,13 @@ private void emitAllReadChannels() { * Determine the channels to write * * @param records - * the list of {@link WireRecord}s to parse + * the list of {@link WireRecord}s to parse * @return list of Channel Records containing the values to be written * @throws NullPointerException - * if argument is null + * if argument is null */ - private List determineWritingChannels(final WireRecord record) { - requireNonNull(record, "Wire Record cannot be null"); + private List determineWritingChannels(final WireRecord wireRecord) { + requireNonNull(wireRecord, "Wire Record cannot be null"); final List channelRecordsToWrite = CollectionUtil.newArrayList(); final AssetConfiguration assetConfiguration = getAssetConfiguration(); @@ -278,7 +277,7 @@ private List determineWritingChannels(final WireRecord record) { continue; } - Map> wireRecordProperties = record.getProperties(); + Map> wireRecordProperties = wireRecord.getProperties(); if (wireRecordProperties.containsKey(channelName)) { final TypedValue value = wireRecordProperties.get(channelName); @@ -294,13 +293,13 @@ private List determineWritingChannels(final WireRecord record) { * Emit the provided list of channel records to the associated wires. * * @param channelRecords - * the list of channel records conforming to the - * aforementioned - * specification + * the list of channel records conforming to the + * aforementioned + * specification * @throws NullPointerException - * if provided records list is null + * if provided records list is null * @throws IllegalArgumentException - * if provided records list is empty + * if provided records list is empty */ private void emitChannelRecords(final List channelRecords) { requireNonNull(channelRecords, "List of Channel Records cannot be null"); @@ -323,12 +322,8 @@ private void emitChannelRecords(final List channelRecords) { return; } - try { - wireRecordProperties.put(WireAssetConstants.PROP_ASSET_NAME.value(), - TypedValues.newStringValue(getKuraServicePid())); - } catch (KuraException e) { - logger.error("Configurations cannot be null", e); - } + wireRecordProperties.put(WireAssetConstants.PROP_ASSET_NAME.value(), + TypedValues.newStringValue(getKuraServicePid())); this.wireSupport.emit(Collections.singletonList(new WireRecord(wireRecordProperties))); } @@ -337,9 +332,9 @@ private void emitChannelRecords(final List channelRecords) { * Perform Channel Write operation * * @param channelRecordsToWrite - * the list of {@link ChannelRecord}s + * the list of {@link ChannelRecord}s * @throws NullPointerException - * if the provided list is null + * if the provided list is null */ private void writeChannels(final List channelRecordsToWrite) { requireNonNull(channelRecordsToWrite, "List of Channel Records cannot be null"); @@ -350,7 +345,7 @@ private void writeChannels(final List channelRecordsToWrite) { try { write(channelRecordsToWrite); } catch (final Exception e) { - logger.error("Error while performing write from the Wire Asset...", e); + logger.error("Error while performing write from the Wire Asset: {}", getKuraServicePid(), e); } } @@ -358,7 +353,8 @@ private boolean isListeningChannel(final Map properties) { try { return Boolean.parseBoolean(properties.get(WireAssetConstants.LISTEN_PROP_NAME.value()).toString()); } catch (Exception e) { - logger.warn("Failed to retreive \"listen\" property from channel configuration"); + logger.warn("Failed to retreive \"listen\" property from channel configuration from the Wire Asset: {}", + getKuraServicePid()); return false; } }