Skip to content

Commit

Permalink
Fixed usade of MappingException in Mediator and Facade classes
Browse files Browse the repository at this point in the history
Signed-off-by: coduz <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Sep 23, 2020
1 parent 1749e5f commit 79082d7
Show file tree
Hide file tree
Showing 29 changed files with 588 additions and 424 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.service.storable.exception.MappingException;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -71,7 +71,7 @@ public AndPredicate clearPredicates() {
}

@Override
public ObjectNode toSerializedMap() throws KapuaException {
public ObjectNode toSerializedMap() throws MappingException {
ArrayNode conditionsNode = newArrayNodeFromPredicates(predicates);

ObjectNode termNode = newObjectNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
package org.eclipse.kapua.service.storable.model.query.predicate;

import com.fasterxml.jackson.databind.node.ObjectNode;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.service.storable.exception.MappingException;
import org.eclipse.kapua.service.storable.model.utils.KeyValueEntry;

/**
Expand Down Expand Up @@ -62,7 +62,7 @@ public String getName() {
* }
* </pre>
*/
public ObjectNode toSerializedMap() throws KapuaException {
public ObjectNode toSerializedMap() throws MappingException {
ObjectNode termNode = getField(new KeyValueEntry[]{new KeyValueEntry(PredicateConstants.FIELD_KEY, name)});

ObjectNode rootNode = newObjectNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
package org.eclipse.kapua.service.storable.model.query.predicate;

import com.fasterxml.jackson.databind.node.ObjectNode;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.service.storable.exception.MappingException;
import org.eclipse.kapua.service.storable.model.utils.KeyValueEntry;


Expand All @@ -32,7 +32,7 @@ public String getExpression() {
}

@Override
public ObjectNode toSerializedMap() throws KapuaException {
public ObjectNode toSerializedMap() throws MappingException {
ObjectNode rootNode = newObjectNode();
ObjectNode expressionNode = getField(new KeyValueEntry[]{new KeyValueEntry(field, expression)});
rootNode.set(PredicateConstants.PREFIX_KEY, expressionNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.service.storable.exception.MappingException;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -71,7 +71,7 @@ public OrPredicate clearPredicates() {
}

@Override
public ObjectNode toSerializedMap() throws KapuaException {
public ObjectNode toSerializedMap() throws MappingException {
ArrayNode conditionsNode = newArrayNodeFromPredicates(predicates);

ObjectNode termNode = newObjectNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import com.fasterxml.jackson.databind.node.ObjectNode;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.service.storable.exception.MappingException;
import org.eclipse.kapua.service.storable.model.query.StorableField;

/**
Expand Down Expand Up @@ -146,7 +147,7 @@ public <V extends Comparable<V>> RangePredicate setMaxValue(Class<V> clazz, V ma
* @throws KapuaException
*/
@Override
public ObjectNode toSerializedMap() throws KapuaException {
public ObjectNode toSerializedMap() throws MappingException {

ObjectNode valuesNode = newObjectNode();
if (maxValue != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,18 @@
package org.eclipse.kapua.service.storable.model.query.predicate;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.service.storable.exception.MappingException;
import org.eclipse.kapua.service.storable.model.utils.KeyValueEntry;
import org.eclipse.kapua.service.storable.model.utils.MappingUtils;

import java.util.Collection;

public abstract class StorablePredicateImpl implements StorablePredicate {

private final static JsonNodeFactory JSON_NODE_FACTORY = JsonNodeFactory.instance;

private static final String UNSUPPORTED_OBJECT_TYPE_ERROR_MSG = "The conversion of object [%s] is not supported!";
private static final String NOT_VALID_OBJECT_TYPE_ERROR_MSG = "Cannot convert date [%s]";

//
// Appends
//
Expand All @@ -42,7 +37,7 @@ public abstract class StorablePredicateImpl implements StorablePredicate {
* @throws KapuaException
* @since 1.3.0
*/
protected void appendField(ObjectNode node, String name, Object value) throws KapuaException {
protected void appendField(ObjectNode node, String name, Object value) throws MappingException {
MappingUtils.appendField(node, name, value);
}

Expand All @@ -58,7 +53,7 @@ protected void appendField(ObjectNode node, String name, Object value) throws Ka
* @throws KapuaException
* @since 1.3.0
*/
protected ObjectNode getField(KeyValueEntry[] entries) throws KapuaException {
protected ObjectNode getField(KeyValueEntry[] entries) throws MappingException {
return MappingUtils.getField(entries);
}

Expand All @@ -71,7 +66,7 @@ protected ObjectNode getField(KeyValueEntry[] entries) throws KapuaException {
* @throws KapuaException
* @since 1.3.0
*/
protected ObjectNode getField(String name, Object value) throws KapuaException {
protected ObjectNode getField(String name, Object value) throws MappingException {
return MappingUtils.getField(name, value);
}

Expand All @@ -91,7 +86,7 @@ protected ArrayNode newArrayNode(Collection<?> collection) {
return MappingUtils.newArrayNode(collection);
}

protected ArrayNode newArrayNodeFromPredicates(Collection<StorablePredicate> storablePredicates) throws KapuaException {
protected ArrayNode newArrayNodeFromPredicates(Collection<StorablePredicate> storablePredicates) throws MappingException {
return MappingUtils.newArrayNodeFromPredicates(storablePredicates);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
package org.eclipse.kapua.service.storable.model.query.predicate;

import com.fasterxml.jackson.databind.node.ObjectNode;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.service.storable.exception.MappingException;
import org.eclipse.kapua.service.storable.model.query.StorableField;
import org.eclipse.kapua.service.storable.model.utils.KeyValueEntry;

Expand Down Expand Up @@ -89,7 +89,7 @@ public <V> TermPredicate setValue(V value) {
* }
* </pre>
*/
public ObjectNode toSerializedMap() throws KapuaException {
public ObjectNode toSerializedMap() throws MappingException {
ObjectNode termNode = getField(new KeyValueEntry[]{new KeyValueEntry(field.field(), value)});

ObjectNode rootNode = newObjectNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Contributors:
# Eurotech - initial API and implementation
###############################################################################
DATASTORE_DISABLED_EXCEPTION=The messagestore service is disabled for Account: {0}
DATASTORE_DISABLED_EXCEPTION=The Message Store Service is disabled for Account: {0}
DATASTORE_OPERATION_EXCEPTION=Error while performing operation: {0}
DATASTORE_UNAVAILABLE_EXCEPTION=The underlying datastore is not available from provider: {0}
INTERNAL_ERROR=Severe error while performing operation. {0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2020 Eurotech and/or its affiliates 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
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.service.datastore.internal;

import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.service.datastore.internal.client.DatastoreClientFactory;
import org.eclipse.kapua.service.datastore.internal.mediator.ConfigurationException;
import org.eclipse.kapua.service.datastore.internal.mediator.MessageStoreConfiguration;
import org.eclipse.kapua.service.elasticsearch.client.ElasticsearchClient;
import org.eclipse.kapua.service.elasticsearch.client.exception.ClientUnavailableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractRegistryFacade {

private static final Logger LOG = LoggerFactory.getLogger(AbstractRegistryFacade.class);

private final ConfigurationProvider configProvider;

public AbstractRegistryFacade(ConfigurationProvider configProvider) {
this.configProvider = configProvider;
}

public ConfigurationProvider getConfigProvider() {
return configProvider;
}

protected boolean isDatastoreServiceEnabled(KapuaId scopeId) throws ConfigurationException {
MessageStoreConfiguration messageStoreConfiguration = configProvider.getConfiguration(scopeId);
long ttl = messageStoreConfiguration.getDataTimeToLiveMilliseconds();

return messageStoreConfiguration.getDataStorageEnabled() && ttl != MessageStoreConfiguration.DISABLED;
}

protected ElasticsearchClient<?> getElasticsearchClient() throws ClientUnavailableException {
return DatastoreClientFactory.getElasticsearchClient();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
import org.eclipse.kapua.commons.util.ArgumentValidator;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.service.datastore.internal.client.DatastoreClientFactory;
import org.eclipse.kapua.service.datastore.internal.mediator.ChannelInfoField;
import org.eclipse.kapua.service.datastore.internal.mediator.ChannelInfoRegistryMediator;
import org.eclipse.kapua.service.datastore.internal.mediator.ConfigurationException;
import org.eclipse.kapua.service.datastore.internal.mediator.MessageStoreConfiguration;
import org.eclipse.kapua.service.datastore.internal.model.ChannelInfoListResultImpl;
import org.eclipse.kapua.service.datastore.internal.model.query.ChannelInfoQueryImpl;
import org.eclipse.kapua.service.datastore.internal.schema.ChannelInfoSchema;
Expand All @@ -29,13 +27,12 @@
import org.eclipse.kapua.service.datastore.model.ChannelInfo;
import org.eclipse.kapua.service.datastore.model.ChannelInfoListResult;
import org.eclipse.kapua.service.datastore.model.query.ChannelInfoQuery;
import org.eclipse.kapua.service.elasticsearch.client.ElasticsearchClient;
import org.eclipse.kapua.service.elasticsearch.client.exception.ClientException;
import org.eclipse.kapua.service.elasticsearch.client.exception.ClientInitializationException;
import org.eclipse.kapua.service.elasticsearch.client.exception.ClientUnavailableException;
import org.eclipse.kapua.service.elasticsearch.client.model.TypeDescriptor;
import org.eclipse.kapua.service.elasticsearch.client.model.UpdateRequest;
import org.eclipse.kapua.service.elasticsearch.client.model.UpdateResponse;
import org.eclipse.kapua.service.storable.exception.MappingException;
import org.eclipse.kapua.service.storable.model.id.StorableId;
import org.eclipse.kapua.service.storable.model.id.StorableIdFactory;
import org.eclipse.kapua.service.storable.model.query.predicate.IdsPredicate;
Expand All @@ -48,7 +45,7 @@
*
* @since 1.0.0
*/
public class ChannelInfoRegistryFacade {
public class ChannelInfoRegistryFacade extends AbstractRegistryFacade {

private static final Logger LOG = LoggerFactory.getLogger(ChannelInfoRegistryFacade.class);

Expand All @@ -57,7 +54,6 @@ public class ChannelInfoRegistryFacade {
private static final StorablePredicateFactory STORABLE_PREDICATE_FACTORY = LOCATOR.getFactory(StorablePredicateFactory.class);

private final ChannelInfoRegistryMediator mediator;
private final ConfigurationProvider configProvider;
private final Object metadataUpdateSync = new Object();

private static final String QUERY = "query";
Expand All @@ -72,7 +68,8 @@ public class ChannelInfoRegistryFacade {
* @since 1.0.0
*/
public ChannelInfoRegistryFacade(ConfigurationProvider configProvider, ChannelInfoRegistryMediator mediator) {
this.configProvider = configProvider;
super(configProvider);

this.mediator = mediator;
}

Expand All @@ -85,7 +82,7 @@ public ChannelInfoRegistryFacade(ConfigurationProvider configProvider, ChannelIn
* @throws ConfigurationException
* @throws ClientException
*/
public StorableId upstore(ChannelInfo channelInfo) throws KapuaIllegalArgumentException, ConfigurationException, ClientException {
public StorableId upstore(ChannelInfo channelInfo) throws KapuaIllegalArgumentException, ConfigurationException, ClientException, MappingException {
ArgumentValidator.notNull(channelInfo, "channelInfo");
ArgumentValidator.notNull(channelInfo.getScopeId(), "channelInfo.scopeId");
ArgumentValidator.notNull(channelInfo.getName(), "channelInfo.name");
Expand All @@ -106,12 +103,7 @@ public StorableId upstore(ChannelInfo channelInfo) throws KapuaIllegalArgumentEx
if (!DatastoreCacheManager.getInstance().getChannelsCache().get(channelInfoId)) {
ChannelInfo storedField = find(channelInfo.getScopeId(), storableId);
if (storedField == null) {
Metadata metadata = null;
try {
metadata = mediator.getMetadata(channelInfo.getScopeId(), channelInfo.getFirstMessageOn().getTime());
} catch (KapuaException e) {
e.printStackTrace();
}
Metadata metadata = mediator.getMetadata(channelInfo.getScopeId(), channelInfo.getFirstMessageOn().getTime());
String registryIndexName = metadata.getRegistryIndexName();

UpdateRequest request = new UpdateRequest(channelInfo.getId().toString(), new TypeDescriptor(metadata.getRegistryIndexName(), ChannelInfoSchema.CHANNEL_TYPE_NAME), channelInfo);
Expand Down Expand Up @@ -143,10 +135,7 @@ public void delete(KapuaId scopeId, StorableId id) throws KapuaIllegalArgumentEx
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(id, "id");

MessageStoreConfiguration accountServicePlan = configProvider.getConfiguration(scopeId);
long ttl = accountServicePlan.getDataTimeToLiveMilliseconds();

if (!accountServicePlan.getDataStorageEnabled() || ttl == MessageStoreConfiguration.DISABLED) {
if (!isDatastoreServiceEnabled(scopeId)) {
LOG.debug("Storage not enabled for account {}, return", scopeId);
return;
}
Expand Down Expand Up @@ -202,10 +191,7 @@ public ChannelInfoListResult query(ChannelInfoQuery query) throws KapuaIllegalAr
ArgumentValidator.notNull(query, QUERY);
ArgumentValidator.notNull(query.getScopeId(), QUERY_SCOPE_ID);

MessageStoreConfiguration accountServicePlan = configProvider.getConfiguration(query.getScopeId());
long ttl = accountServicePlan.getDataTimeToLiveMilliseconds();

if (!accountServicePlan.getDataStorageEnabled() || ttl == MessageStoreConfiguration.DISABLED) {
if (!isDatastoreServiceEnabled(query.getScopeId())) {
LOG.debug("Storage not enabled for account {}, returning empty result", query.getScopeId());
return new ChannelInfoListResultImpl();
}
Expand All @@ -228,10 +214,7 @@ public long count(ChannelInfoQuery query) throws KapuaIllegalArgumentException,
ArgumentValidator.notNull(query, QUERY);
ArgumentValidator.notNull(query.getScopeId(), QUERY_SCOPE_ID);

MessageStoreConfiguration accountServicePlan = configProvider.getConfiguration(query.getScopeId());
long ttl = accountServicePlan.getDataTimeToLiveMilliseconds();

if (!accountServicePlan.getDataStorageEnabled() || ttl == MessageStoreConfiguration.DISABLED) {
if (!isDatastoreServiceEnabled(query.getScopeId())) {
LOG.debug("Storage not enabled for account {}, returning empty result", query.getScopeId());
return 0;
}
Expand All @@ -256,10 +239,7 @@ void delete(ChannelInfoQuery query) throws KapuaIllegalArgumentException, Config
ArgumentValidator.notNull(query, QUERY);
ArgumentValidator.notNull(query.getScopeId(), QUERY_SCOPE_ID);

MessageStoreConfiguration accountServicePlan = configProvider.getConfiguration(query.getScopeId());
long ttl = accountServicePlan.getDataTimeToLiveMilliseconds();

if (!accountServicePlan.getDataStorageEnabled() || ttl == MessageStoreConfiguration.DISABLED) {
if (!isDatastoreServiceEnabled(query.getScopeId())) {
LOG.debug("Storage not enabled for account {}, skipping delete", query.getScopeId());
return;
}
Expand All @@ -278,8 +258,4 @@ void delete(ChannelInfoQuery query) throws KapuaIllegalArgumentException, Config
TypeDescriptor typeDescriptor = new TypeDescriptor(indexName, ChannelInfoSchema.CHANNEL_TYPE_NAME);
getElasticsearchClient().deleteByQuery(typeDescriptor, query);
}

private ElasticsearchClient<?> getElasticsearchClient() throws ClientInitializationException, ClientUnavailableException {
return DatastoreClientFactory.getElasticsearchClient();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.eclipse.kapua.service.datastore.model.query.ChannelInfoQuery;
import org.eclipse.kapua.service.datastore.model.query.DatastorePredicateFactory;
import org.eclipse.kapua.service.datastore.model.query.MessageQuery;
import org.eclipse.kapua.service.elasticsearch.client.exception.ClientInitializationException;
import org.eclipse.kapua.service.elasticsearch.client.exception.ClientUnavailableException;
import org.eclipse.kapua.service.storable.model.id.StorableId;
import org.eclipse.kapua.service.storable.model.query.SortField;
Expand Down Expand Up @@ -84,7 +83,7 @@ public class ChannelInfoRegistryServiceImpl extends AbstractKapuaService impleme
*
* @throws ClientUnavailableException
*/
public ChannelInfoRegistryServiceImpl() throws ClientInitializationException {
public ChannelInfoRegistryServiceImpl() {
super(DatastoreEntityManagerFactory.getInstance());

KapuaLocator locator = KapuaLocator.getInstance();
Expand Down
Loading

0 comments on commit 79082d7

Please sign in to comment.