Skip to content

Commit 5dab136

Browse files
Synchronize repository - 2025-02-20
1 parent a900a22 commit 5dab136

File tree

3 files changed

+75
-75
lines changed

3 files changed

+75
-75
lines changed

commercedbsync/src/com/sap/cx/boosters/commercedbsync/logging/JDBCQueriesStore.java

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@
66

77
package com.sap.cx.boosters.commercedbsync.logging;
88

9-
import com.microsoft.azure.storage.CloudStorageAccount;
10-
import com.microsoft.azure.storage.blob.CloudAppendBlob;
11-
import com.microsoft.azure.storage.blob.CloudBlobClient;
12-
import com.microsoft.azure.storage.blob.CloudBlobContainer;
13-
import com.microsoft.azure.storage.blob.CloudBlobDirectory;
14-
import com.microsoft.azure.storage.blob.CloudBlockBlob;
9+
import com.azure.storage.blob.BlobContainerClient;
10+
import com.azure.storage.blob.BlobServiceClient;
11+
import com.azure.storage.blob.BlobServiceClientBuilder;
12+
import com.azure.storage.blob.specialized.AppendBlobClient;
13+
import com.azure.storage.blob.specialized.BlockBlobClient;
1514

1615
import java.io.ByteArrayInputStream;
1716
import java.io.ByteArrayOutputStream;
1817
import java.io.InputStream;
19-
import java.net.URISyntaxException;
2018
import java.nio.charset.StandardCharsets;
21-
import java.security.InvalidKeyException;
2219
import java.util.ArrayList;
2320
import java.util.Collection;
2421
import java.util.Collections;
@@ -45,6 +42,7 @@ public class JDBCQueriesStore {
4542
private final String dbConnectionString;
4643
private final Collection<JdbcQueryLog> queryLogs;
4744
private final MigrationContext context;
45+
private BlobServiceClient blobServiceClient;
4846

4947
private final boolean isSourceDB;
5048
// Unique id of the file in file storage where the jdbc store(s) across
@@ -105,16 +103,14 @@ public void writeToLogFileAndCompress(final String migrationId) {
105103
}
106104

107105
public Pair<byte[], String> getLogFile(final String migrationId) {
108-
final String logFileName = getLogFileName(migrationId, true);
109106
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
110-
CloudBlobDirectory jdbcLogsDirectory = getContainer().getDirectoryReference("jdbclogs");
111-
CloudBlockBlob zippedLogBlobFile = jdbcLogsDirectory.getBlockBlobReference(logFileName);
107+
final BlockBlobClient zippedLogBlobFile = getZippedLogBlobFile(migrationId);
112108
zippedLogBlobFile.download(baos);
113-
return Pair.of(baos.toByteArray(), logFileName);
109+
return Pair.of(baos.toByteArray(), getLogFileName(migrationId, true));
114110
} catch (Exception e) {
115111
String errorMessage = String.format(
116112
"Log file %s for datasource %s does not exist in storage %s or is currently being created",
117-
logFileName, dbConnectionString, context.getFileStorageContainerName());
113+
getLogFileName(migrationId, true), dbConnectionString, context.getFileStorageContainerName());
118114
LOG.error(errorMessage, e);
119115
return Pair.of(errorMessage.getBytes(StandardCharsets.UTF_8), getLogFileName(migrationId, false));
120116
}
@@ -127,8 +123,7 @@ public String toString() {
127123

128124
private void flushQueryLogsToAppendingFile() {
129125
try {
130-
CloudBlobDirectory jdbcLogsDirectory = getContainer().getDirectoryReference(JDBCLOGS_DIRECTORY);
131-
CloudAppendBlob sharedStoreLogFile = jdbcLogsDirectory.getAppendBlobReference(sharedStoreLogFileName);
126+
final AppendBlobClient sharedStoreLogFile = getSharedStoreLogFile();
132127
byte[] queryLogsBytes = getQueryLogsAsString().getBytes(StandardCharsets.UTF_8.name());
133128
try (InputStream is = new ByteArrayInputStream(queryLogsBytes)) {
134129
sharedStoreLogFile.appendBlock(is, queryLogsBytes.length);
@@ -148,13 +143,11 @@ private String getQueryLogsAsString() {
148143

149144
private void compressAppendingFileContent(final String migrationId) {
150145
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
151-
CloudBlobDirectory jdbcLogsDirectory = getContainer().getDirectoryReference(JDBCLOGS_DIRECTORY);
152-
CloudAppendBlob sharedStoreLogFile = jdbcLogsDirectory.getAppendBlobReference(this.sharedStoreLogFileName);
153-
sharedStoreLogFile.download(baos);
146+
final AppendBlobClient sharedStoreLogFile = getSharedStoreLogFile();
147+
sharedStoreLogFile.downloadStream(baos);
154148
byte[] zippedLogBytes = FileUtils.zipBytes(getLogFileName(migrationId, false), baos.toByteArray());
155-
CloudBlockBlob zippedLogBlobFile = jdbcLogsDirectory
156-
.getBlockBlobReference(getLogFileName(migrationId, true));
157-
zippedLogBlobFile.uploadFromByteArray(zippedLogBytes, 0, zippedLogBytes.length);
149+
final BlockBlobClient zippedLogBlobFile = getZippedLogBlobFile(migrationId);
150+
zippedLogBlobFile.upload(new ByteArrayInputStream(zippedLogBytes), zippedLogBytes.length);
158151
} catch (Exception e) {
159152
LOG.error("Failed to compress query logs from file {} in storage {} for datasource {}",
160153
getLogFileName(migrationId, false), context.getFileStorageConnectionString(), dbConnectionString,
@@ -164,34 +157,43 @@ private void compressAppendingFileContent(final String migrationId) {
164157

165158
private void resetAppendingFile() {
166159
try {
167-
CloudBlobClient blobClient = getCloudBlobClient();
168-
CloudBlobDirectory jdbcLogsDirectory = blobClient
169-
.getContainerReference(context.getFileStorageContainerName()).getDirectoryReference("jdbclogs");
170-
CloudAppendBlob logBlobFile = jdbcLogsDirectory.getAppendBlobReference(sharedStoreLogFileName);
171-
logBlobFile.createOrReplace();
160+
getSharedStoreLogFile().create(true);
172161
} catch (Exception e) {
173162
LOG.error("Failed to create or replace appending file {} in storage {} for datasource {}",
174163
sharedStoreLogFileName, context.getFileStorageContainerName(), dbConnectionString, e);
175164
}
176165
}
177166

178-
private CloudBlobClient getCloudBlobClient() throws URISyntaxException, InvalidKeyException {
179-
// if file storage connection string is not set, do not try to connect to the
180-
// storage
167+
protected BlobServiceClient getBlobServiceClient() throws Exception {
181168
if (context.getFileStorageConnectionString() == null) {
182169
throw new IllegalArgumentException("File storage connection string not set");
183170
}
184-
CloudStorageAccount account = CloudStorageAccount.parse(context.getFileStorageConnectionString());
185-
return account.createCloudBlobClient();
171+
172+
if (blobServiceClient == null) {
173+
blobServiceClient = new BlobServiceClientBuilder()
174+
.connectionString(context.getFileStorageConnectionString()).buildClient();
175+
}
176+
177+
return blobServiceClient;
186178
}
187179

188-
private CloudBlobContainer getContainer() throws Exception {
189-
CloudBlobContainer containerReference = getCloudBlobClient()
190-
.getContainerReference(context.getFileStorageContainerName());
180+
protected BlobContainerClient getContainerClient() throws Exception {
181+
final BlobContainerClient containerClient = getBlobServiceClient()
182+
.getBlobContainerClient(context.getFileStorageContainerName());;
183+
containerClient.createIfNotExists();
184+
return containerClient;
185+
}
191186

192-
containerReference.createIfNotExists();
187+
protected AppendBlobClient getSharedStoreLogFile() throws Exception {
188+
final AppendBlobClient appendBlobClient = getContainerClient()
189+
.getBlobClient(JDBCLOGS_DIRECTORY + "/" + sharedStoreLogFileName).getAppendBlobClient();
190+
appendBlobClient.createIfNotExists();
191+
return appendBlobClient;
192+
}
193193

194-
return containerReference;
194+
protected BlockBlobClient getZippedLogBlobFile(final String migrationId) throws Exception {
195+
final String logFileName = getLogFileName(migrationId, true);
196+
return getContainerClient().getBlobClient(JDBCLOGS_DIRECTORY + "/" + logFileName).getBlockBlobClient();
195197
}
196198

197199
private String getLogFileName(final String migrationId, final boolean isZipped) {

commercedbsync/src/com/sap/cx/boosters/commercedbsync/service/impl/BlobDatabaseMigrationReportStorageService.java

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66

77
package com.sap.cx.boosters.commercedbsync.service.impl;
88

9-
import com.microsoft.azure.storage.CloudStorageAccount;
10-
import com.microsoft.azure.storage.NameValidator;
11-
import com.microsoft.azure.storage.blob.CloudBlob;
12-
import com.microsoft.azure.storage.blob.CloudBlobClient;
13-
import com.microsoft.azure.storage.blob.CloudBlobContainer;
14-
import com.microsoft.azure.storage.blob.CloudBlockBlob;
15-
import com.microsoft.azure.storage.blob.ListBlobItem;
9+
import com.azure.storage.blob.BlobClient;
10+
import com.azure.storage.blob.BlobContainerClient;
11+
import com.azure.storage.blob.BlobServiceClient;
12+
import com.azure.storage.blob.BlobServiceClientBuilder;
13+
import com.azure.storage.blob.specialized.BlockBlobClient;
1614
import com.sap.cx.boosters.commercedbsync.service.DatabaseMigrationReportStorageService;
1715
import org.apache.commons.io.IOUtils;
1816
import org.apache.commons.lang.StringUtils;
@@ -31,24 +29,25 @@ public class BlobDatabaseMigrationReportStorageService implements DatabaseMigrat
3129
private static final Logger LOG = LoggerFactory
3230
.getLogger(BlobDatabaseMigrationReportStorageService.class.getName());
3331

34-
private CloudBlobClient cloudBlobClient;
32+
private BlobServiceClient blobServiceClient;
3533

3634
private MigrationContext migrationContext;
3735

3836
protected void init() throws Exception {
3937
LOG.info("Connecting to blob storage {}", migrationContext.getFileStorageConnectionString());
40-
CloudStorageAccount account = CloudStorageAccount.parse(migrationContext.getFileStorageConnectionString());
41-
this.cloudBlobClient = account.createCloudBlobClient();
38+
this.blobServiceClient = new BlobServiceClientBuilder()
39+
.connectionString(migrationContext.getFileStorageConnectionString()).buildClient();
4240
}
4341

4442
@Override
4543
public void store(String fileName, InputStream inputStream) throws Exception {
4644
final String containerName = migrationContext.getFileStorageContainerName();
4745
if (inputStream != null) {
48-
CloudBlockBlob blob = getContainer(containerName, true).getBlockBlobReference(fileName);
46+
final BlockBlobClient blobClient = getContainerClient(containerName, true).getBlobClient(fileName)
47+
.getBlockBlobClient();
4948
byte[] bytes = IOUtils.toByteArray(inputStream);
50-
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
51-
blob.upload(bis, bytes.length);
49+
final ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
50+
blobClient.upload(bis, bytes.length);
5251
bis.close();
5352
LOG.info("File {} written to blob storage at {}/{}", fileName, containerName, fileName);
5453
} else {
@@ -57,22 +56,21 @@ public void store(String fileName, InputStream inputStream) throws Exception {
5756
}
5857
}
5958

60-
protected CloudBlobContainer getContainer(String name, boolean createIfNotExists) throws Exception {
61-
CloudBlobContainer containerReference = getCloudBlobClient().getContainerReference(name);
59+
protected BlobContainerClient getContainerClient(String name, boolean createIfNotExists) throws Exception {
60+
final BlobContainerClient containerClient = getBlobServiceClient().getBlobContainerClient(name);
6261
if (createIfNotExists) {
63-
containerReference.createIfNotExists();
62+
containerClient.createIfNotExists();
6463
}
65-
return containerReference;
64+
return containerClient;
6665
}
6766

68-
public List<CloudBlockBlob> listAllReports() throws Exception {
69-
getCloudBlobClient();
67+
public List<BlobClient> listAllReports() throws Exception {
7068
final String containerName = migrationContext.getFileStorageContainerName();
71-
Iterable<ListBlobItem> migrationBlobs = cloudBlobClient.getContainerReference(containerName).listBlobs();
72-
List<CloudBlockBlob> result = new ArrayList<>();
73-
migrationBlobs.forEach(blob -> {
74-
if (blob instanceof CloudBlockBlob && ((CloudBlockBlob) blob).getName().endsWith(".json")) {
75-
result.add((CloudBlockBlob) blob);
69+
final List<BlobClient> result = new ArrayList<>();
70+
final BlobContainerClient containerClient = getContainerClient(containerName, true);
71+
containerClient.listBlobs().forEach(blob -> {
72+
if (!blob.isPrefix() && blob.getName().endsWith(".json")) {
73+
result.add(containerClient.getBlobClient(blob.getName()));
7674
}
7775
});
7876
return result;
@@ -81,14 +79,13 @@ public List<CloudBlockBlob> listAllReports() throws Exception {
8179
public byte[] getReport(String reportId) throws Exception {
8280
checkReportIdValid(reportId);
8381
final String containerName = migrationContext.getFileStorageContainerName();
84-
CloudBlob blob = cloudBlobClient.getContainerReference(containerName).getBlobReferenceFromServer(reportId);
85-
ByteArrayOutputStream result = new ByteArrayOutputStream();
86-
blob.download(result);
82+
final BlobClient blobClient = getContainerClient(containerName, false).getBlobClient(reportId);
83+
final ByteArrayOutputStream result = new ByteArrayOutputStream();
84+
blobClient.downloadStream(result);
8785
return result.toByteArray();
8886
}
8987

9088
private void checkReportIdValid(String reportId) {
91-
NameValidator.validateFileName(reportId);
9289
if (StringUtils.contains(reportId, "/")) {
9390
throw new IllegalArgumentException("Invalid report id provided");
9491
}
@@ -97,17 +94,17 @@ private void checkReportIdValid(String reportId) {
9794
}
9895
}
9996

100-
protected CloudBlobClient getCloudBlobClient() throws Exception {
101-
if (cloudBlobClient == null) {
97+
protected BlobServiceClient getBlobServiceClient() throws Exception {
98+
if (blobServiceClient == null) {
10299
init();
103100
}
104-
return cloudBlobClient;
101+
return blobServiceClient;
105102
}
106103

107104
@Override
108105
public boolean validateConnection() {
109106
try {
110-
getCloudBlobClient().listContainers();
107+
getBlobServiceClient().listBlobContainers();
111108
} catch (Exception e) {
112109
return false;
113110
}

commercedbsynchac/hac/src/de/hybris/platform/hac/controller/CommercemigrationhacController.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
package de.hybris.platform.hac.controller;
88

9+
import com.azure.storage.blob.BlobClient;
910
import com.google.common.collect.Lists;
1011
import com.google.gson.Gson;
1112
import com.google.gson.GsonBuilder;
12-
import com.microsoft.azure.storage.blob.CloudBlockBlob;
1313
import com.sap.cx.boosters.commercedbsync.MigrationStatus;
1414
import com.sap.cx.boosters.commercedbsync.SchemaDifferenceStatus;
1515
import com.sap.cx.boosters.commercedbsync.constants.CommercedbsyncConstants;
@@ -45,6 +45,7 @@
4545

4646
import javax.servlet.http.HttpServletResponse;
4747
import java.io.Serializable;
48+
import java.net.URI;
4849
import java.nio.charset.StandardCharsets;
4950
import java.text.SimpleDateFormat;
5051
import java.time.Instant;
@@ -451,13 +452,13 @@ private void logAction(String message) {
451452
@ResponseBody
452453
public List<ReportResultData> loadMigrationReports() {
453454
try {
454-
List<CloudBlockBlob> blobs = blobDatabaseMigrationReportStorageService.listAllReports();
455+
List<BlobClient> blobs = blobDatabaseMigrationReportStorageService.listAllReports();
455456
List<ReportResultData> result = new ArrayList<>();
456457
blobs.forEach(blob -> {
457458
ReportResultData reportResultData = new ReportResultData();
458459
reportResultData.setModifiedTimestamp(getSortableTimestamp(blob));
459-
reportResultData.setReportId(blob.getName());
460-
reportResultData.setPrimaryUri(blob.getUri().toString());
460+
reportResultData.setReportId(blob.getBlobName());
461+
reportResultData.setPrimaryUri(URI.create(blob.getBlobUrl()).toString());
461462
result.add(reportResultData);
462463
});
463464
return result;
@@ -467,11 +468,11 @@ public List<ReportResultData> loadMigrationReports() {
467468
return null;
468469
}
469470

470-
private String getSortableTimestamp(CloudBlockBlob blob) {
471+
private String getSortableTimestamp(BlobClient blob) {
471472
if (blob != null && blob.getProperties() != null) {
472-
Date lastModified = blob.getProperties().getLastModified();
473+
OffsetDateTime lastModified = blob.getProperties().getLastModified();
473474
if (lastModified != null) {
474-
return DATE_TIME_FORMATTER.format(lastModified);
475+
return DATE_TIME_FORMATTER.format(Date.from(lastModified.toInstant()));
475476
}
476477
}
477478
return Strings.EMPTY;

0 commit comments

Comments
 (0)