Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Jackson object mapper, readers and writers at class level #7251

Merged
merged 12 commits into from
Jan 25, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package org.odpi.openmetadata.conformance.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.odpi.openmetadata.adapters.connectors.restclients.ffdc.exceptions.RESTConfigurationException;
import org.odpi.openmetadata.adapters.connectors.restclients.ffdc.exceptions.RESTServerException;
Expand All @@ -25,12 +26,14 @@
*/
public class OpenMetadataConformanceTestReport
{
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
davidradl marked this conversation as resolved.
Show resolved Hide resolved
private static final ObjectWriter OBJECT_WRITER = OBJECT_MAPPER.writer();

private String serverName; /* Initialized in constructor */
private String serverURLRoot; /* Initialized in constructor */
private String testClientUserId; /* Initialized in constructor */
private RESTClient restClient; /* Initialized in constructor */

private final ObjectMapper objectMapper;
private RESTExceptionHandler exceptionHandler = new RESTExceptionHandler();

/**
Expand All @@ -49,7 +52,6 @@ private OpenMetadataConformanceTestReport(String serverName,
this.serverURLRoot = serverURLRoot;
this.testClientUserId = testClientUserId;

this.objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
this.restClient = new RESTClient(serverName, serverURLRoot);
}

Expand All @@ -72,7 +74,6 @@ private OpenMetadataConformanceTestReport(String serverName,
this.serverURLRoot = serverURLRoot;
this.testClientUserId = testClientUserId;

this.objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
this.restClient = new RESTClient(serverName, serverURLRoot, testClientUserId, testClientPassword);
}

Expand Down Expand Up @@ -290,7 +291,7 @@ private void writeJsonToFile(String path, Object value) throws IOException
{
String noSpaces = path.replaceAll("\\s+", "_");
File file = new File(noSpaces);
objectMapper.writeValue(file, value);
OBJECT_WRITER.writeValue(file, value);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,9 @@
/* Copyright Contributors to the ODPi Egeria project. */
package org.odpi.openmetadata.accessservices.analyticsmodeling.assets;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.apache.commons.collections4.CollectionUtils;
import org.odpi.openmetadata.accessservices.analyticsmodeling.contentmanager.OMEntityDao;
import org.odpi.openmetadata.accessservices.analyticsmodeling.converter.GlossaryTermConverter;
Expand All @@ -25,8 +13,20 @@
import org.odpi.openmetadata.accessservices.analyticsmodeling.metadata.Database;
import org.odpi.openmetadata.accessservices.analyticsmodeling.metadata.GlossaryTerm;
import org.odpi.openmetadata.accessservices.analyticsmodeling.metadata.Schema;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.*;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.module.*;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.ModuleTableFilter;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.ResponseContainerDatabase;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.ResponseContainerDatabaseSchema;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.ResponseContainerModule;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.ResponseContainerSchemaTables;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.module.BaseObjectType;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.module.Column;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.module.DataSource;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.module.ForeignColumn;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.module.ForeignKey;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.module.MetadataModule;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.module.PrimaryKey;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.module.Table;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.module.TableItem;
import org.odpi.openmetadata.accessservices.analyticsmodeling.model.response.Messages;
import org.odpi.openmetadata.accessservices.analyticsmodeling.utils.Constants;
import org.odpi.openmetadata.accessservices.analyticsmodeling.utils.ExecutionContext;
Expand All @@ -42,8 +42,20 @@
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.instances.InstanceProperties;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.instances.Relationship;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* The class builds data content of the Analytics Modeling OMAS responses.<br>
Expand All @@ -64,6 +76,8 @@
*/
public class DatabaseContextHandler {

private static final ObjectWriter OBJECT_WRITER = new ObjectMapper().writer();

public static final String DATA_SOURCE_GUID = "dataSourceGUID";

private RelationalDataHandler<Database,
Expand Down Expand Up @@ -460,7 +474,7 @@ private String buildGlossaryTerm(GlossaryTerm term)
}

try {
return new ObjectMapper().writeValueAsString(json);
return OBJECT_WRITER.writeValueAsString(json);
} catch (JsonProcessingException e) {
// log warning in context
ctx.addMessage(AnalyticsModelingErrorCode.BUILD_GLOSSARY_TERM_EXCEPTION.getMessageDefinition(term.getName()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

package org.odpi.openmetadata.accessservices.analyticsmodeling.synchronization.converters;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.odpi.openmetadata.accessservices.analyticsmodeling.synchronization.model.AnalyticsAsset;
import org.odpi.openmetadata.accessservices.analyticsmodeling.synchronization.model.AssetReference;
import org.odpi.openmetadata.accessservices.analyticsmodeling.utils.Constants;
Expand All @@ -17,11 +15,14 @@
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.typedefs.TypeDefCategory;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.repositoryconnector.OMRSRepositoryHelper;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class AssetConverter extends OpenMetadataAPIGenericConverter<AnalyticsAsset> {

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

public AssetConverter(OMRSRepositoryHelper repositoryHelper, String serviceName, String serverName) {
super(repositoryHelper, serviceName, serverName);
}
Expand Down Expand Up @@ -63,7 +64,7 @@ public AnalyticsAsset getNewBean(Class<AnalyticsAsset> beanClass,
String jsonReference = props.get(Constants.REFERENCE);
if (jsonReference != null && !jsonReference.isEmpty()) {
try {
bean.setReference(new ObjectMapper()
bean.setReference(OBJECT_MAPPER
.readValue(jsonReference.getBytes(), new TypeReference<List<AssetReference>>(){}));
} catch (IOException e) {
// log warning into execution context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

package org.odpi.openmetadata.accessservices.analyticsmodeling.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.odpi.openmetadata.accessservices.analyticsmodeling.synchronization.model.AnalyticsAsset;
import org.odpi.openmetadata.accessservices.analyticsmodeling.synchronization.model.AssetReference;
import org.odpi.openmetadata.accessservices.analyticsmodeling.synchronization.model.MetadataContainer;
import org.odpi.openmetadata.accessservices.analyticsmodeling.synchronization.model.MetadataItem;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
* Class provides useful operations with bean class
Expand All @@ -22,6 +22,11 @@
*/
public class AnalyticsAssetUtils {

private static final ObjectWriter OBJECT_WRITER = new ObjectMapper().writer();

private AnalyticsAssetUtils() {
}

/**
* Add container to the asset.
* @param asset to add new container to.
Expand Down Expand Up @@ -129,7 +134,7 @@ public static Map<String, String> buildAdditionalProperties(AnalyticsAsset asset

if (asset.getReference() != null ) {
try {
String references = new ObjectMapper().writeValueAsString(asset.getReference());
String references = OBJECT_WRITER.writeValueAsString(asset.getReference());
additionalProperties.put(Constants.REFERENCE, references);
} catch (JsonProcessingException e) {
// log to execution context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.odpi.openmetadata.repositoryservices.connectors.openmetadatatopic.OpenMetadataTopicConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -15,7 +16,8 @@
*/
public class AssetCatalogSearchPublisher {

private static final Logger log = LoggerFactory.getLogger(AssetCatalogSearchPublisher.class);
private static final Logger LOG = LoggerFactory.getLogger(AssetCatalogSearchPublisher.class);
private static final ObjectWriter OBJECT_WRITER = new ObjectMapper().writer();

private final OpenMetadataTopicConnector topicConnector;

Expand All @@ -35,21 +37,20 @@ public void publishEvent(Serializable event)
{
if (topicConnector != null)
{
ObjectMapper objectMapper = new ObjectMapper();
topicConnector.sendEvent(objectMapper.writeValueAsString(event));
topicConnector.sendEvent(OBJECT_WRITER.writeValueAsString(event));
}
else
{
log.error("Cannot publish event: topic connector is null!");
LOG.error("Cannot publish event: topic connector is null!");
}
}
catch (JsonProcessingException e)
{
log.error("Unable to create json for publishing: " + event.toString(), e);
LOG.error("Unable to create json for publishing: " + event.toString(), e);
}
catch (Exception e)
{
log.error("Unable to publish new asset event", e);
LOG.error("Unable to publish new asset event", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package org.odpi.openmetadata.accessservices.assetconsumer.connectors.outtopic;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.odpi.openmetadata.accessservices.assetconsumer.events.AssetConsumerOutTopicEvent;
import org.odpi.openmetadata.accessservices.assetconsumer.ffdc.AssetConsumerAuditCode;
import org.odpi.openmetadata.accessservices.assetconsumer.ffdc.AssetConsumerErrorCode;
Expand All @@ -20,6 +21,9 @@
*/
public class AssetConsumerOutTopicServerConnector extends OpenMetadataTopicSenderConnectorBase
{

private static final ObjectWriter OBJECT_WRITER = new ObjectMapper().writer();

/**
* Send the request to the embedded event bus connector(s).
*
Expand All @@ -30,11 +34,10 @@ public class AssetConsumerOutTopicServerConnector extends OpenMetadataTopicSende
public void sendEvent(AssetConsumerOutTopicEvent event) throws InvalidParameterException, ConnectorCheckedException
{
final String methodName = "sendEvent";
ObjectMapper objectMapper = new ObjectMapper();

try
{
String eventString = objectMapper.writeValueAsString(event);
String eventString = OBJECT_WRITER.writeValueAsString(event);
super.sendEvent(eventString).join();

if (super.auditLog != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -54,6 +55,7 @@
public class AssetLineagePublisher {

private static final Logger log = LoggerFactory.getLogger(AssetLineagePublisher.class);
private static final ObjectWriter OBJECT_WRITER = new ObjectMapper().writer();
private static final AssetLineageInstanceHandler instanceHandler = new AssetLineageInstanceHandler();
private final OpenMetadataTopicConnector outTopicConnector;
private final String serverUserName;
Expand Down Expand Up @@ -314,8 +316,7 @@ public void publishEvent(AssetLineageEventHeader event) throws JsonProcessingExc
if (outTopicConnector == null)
return;

ObjectMapper objectMapper = new ObjectMapper();
outTopicConnector.sendEvent(objectMapper.writeValueAsString(event));
outTopicConnector.sendEvent(OBJECT_WRITER.writeValueAsString(event));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package org.odpi.openmetadata.accessservices.assetmanager.connectors.outtopic;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.odpi.openmetadata.accessservices.assetmanager.ffdc.AssetManagerAuditCode;
import org.odpi.openmetadata.accessservices.assetmanager.events.AssetManagerOutTopicEvent;
import org.odpi.openmetadata.accessservices.assetmanager.ffdc.AssetManagerErrorCode;
Expand All @@ -20,6 +21,9 @@
*/
public class AssetManagerOutTopicServerConnector extends OpenMetadataTopicSenderConnectorBase
{

private static final ObjectWriter OBJECT_WRITER = new ObjectMapper().writer();

/**
* Send the request to the embedded event bus connector(s).
*
Expand All @@ -30,11 +34,10 @@ public class AssetManagerOutTopicServerConnector extends OpenMetadataTopicSender
public void sendEvent(AssetManagerOutTopicEvent event) throws InvalidParameterException, ConnectorCheckedException
{
final String methodName = "sendEvent";
ObjectMapper objectMapper = new ObjectMapper();

try
{
String eventString = objectMapper.writeValueAsString(event);
String eventString = OBJECT_WRITER.writeValueAsString(event);
super.sendEvent(eventString).join();

if (super.auditLog != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package org.odpi.openmetadata.accessservices.assetowner.connectors.outtopic;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.odpi.openmetadata.accessservices.assetowner.events.AssetOwnerOutTopicEvent;
import org.odpi.openmetadata.accessservices.assetowner.ffdc.AssetOwnerAuditCode;
import org.odpi.openmetadata.accessservices.assetowner.ffdc.AssetOwnerErrorCode;
Expand All @@ -20,6 +21,9 @@
*/
public class AssetOwnerOutTopicServerConnector extends OpenMetadataTopicSenderConnectorBase
{

private static final ObjectWriter OBJECT_WRITER = new ObjectMapper().writer();

/**
* Send the request to the embedded event bus connector(s).
*
Expand All @@ -30,11 +34,10 @@ public class AssetOwnerOutTopicServerConnector extends OpenMetadataTopicSenderCo
public void sendEvent(AssetOwnerOutTopicEvent event) throws InvalidParameterException, ConnectorCheckedException
{
final String methodName = "sendEvent";
ObjectMapper objectMapper = new ObjectMapper();

try
{
String eventString = objectMapper.writeValueAsString(event);
String eventString = OBJECT_WRITER.writeValueAsString(event);
super.sendEvent(eventString).join();

if (super.auditLog != null)
Expand Down
Loading