Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Fixes #5420, import extensions by extensionId instead of id, take hig…
Browse files Browse the repository at this point in the history
…hest extension version
  • Loading branch information
dhirajsb committed Jun 14, 2019
1 parent 652c1aa commit 0875175
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 39 deletions.
5 changes: 5 additions & 0 deletions app/server/endpoint/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@
<artifactId>spotbugs-annotations</artifactId>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
</dependency>

<!-- ===================================================================================== -->

<!-- Atlasmap Data Mapper Services -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import javax.ws.rs.core.UriInfo;

import org.apache.commons.io.IOUtils;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -79,6 +80,7 @@
import io.syndesis.server.dao.file.FileDataManager;
import io.syndesis.server.dao.file.IconDao;
import io.syndesis.server.dao.manager.DataManager;
import io.syndesis.server.endpoint.v1.handler.extension.ExtensionActivator;
import io.syndesis.server.endpoint.v1.handler.integration.IntegrationHandler;
import io.syndesis.server.jsondb.CloseableJsonDB;
import io.syndesis.server.jsondb.JsonDB;
Expand All @@ -103,6 +105,8 @@ public class IntegrationSupportHandler {

private static final BiFunction<Extension, String, Extension> RENAME_EXTENSION = (e, n) -> new Extension.Builder().createFrom(e).name(n).build();
private static final BiFunction<Connection, String, Connection> RENAME_CONNECTION = (c, n) -> new Connection.Builder().createFrom(c).name(n).build();
public static final String PROPERTY_EXTENSION_ID = "extensionId";
public static final String PROPERTY_STATUS = "status";

private final Migrator migrator;
private final SqlJsonDB jsondb;
Expand All @@ -111,17 +115,15 @@ public class IntegrationSupportHandler {
private final IntegrationResourceManager resourceManager;
private final IntegrationHandler integrationHandler;
private final FileDataManager extensionDataManager;
private final ExtensionActivator extensionActivator;
private final IconDao iconDao;

public IntegrationSupportHandler(
final Migrator migrator,
final SqlJsonDB jsondb,
final IntegrationProjectGenerator projectGenerator,
final DataManager dataManager,
final IntegrationResourceManager resourceManager,
final IntegrationHandler integrationHandler,
final FileDataManager extensionDataManager,
final IconDao iconDao) {
public IntegrationSupportHandler(final Migrator migrator, final SqlJsonDB jsondb,
final IntegrationProjectGenerator projectGenerator,
final DataManager dataManager, final IntegrationResourceManager resourceManager,
final IntegrationHandler integrationHandler,
final FileDataManager extensionDataManager,
final ExtensionActivator extensionActivator, final IconDao iconDao) {
this.migrator = migrator;
this.jsondb = jsondb;

Expand All @@ -130,6 +132,7 @@ public IntegrationSupportHandler(
this.resourceManager = resourceManager;
this.integrationHandler = integrationHandler;
this.extensionDataManager = extensionDataManager;
this.extensionActivator = extensionActivator;
this.iconDao = iconDao;
}

Expand Down Expand Up @@ -312,7 +315,11 @@ private void importMissingExtensions(ZipInputStream zis, ZipEntry entry,
String path = "/extensions/" + extension.getId().get();
InputStream existing = extensionDataManager.getExtensionDataAccess().read(path);
if( existing == null ) {
// write blob in jsondb
extensionDataManager.getExtensionDataAccess().write(path, zis);

// also activate the imported extension
extensionActivator.activateExtension((Extension) extension);
} else {
existing.close();
}
Expand Down Expand Up @@ -363,10 +370,34 @@ public Class<Extension> getType() {
}
};

// undelete existing deleted extensions that were referenced in the model
undeleteImportedExtensions(extensionDao, result);
final Map<String, String> replaceExtensions = new HashMap<>();
importModels(extensionDao, RENAME_EXTENSION, renamedIds, result, (e, i) -> {

boolean doImport = false;

importModels(extensionDao, RENAME_EXTENSION, renamedIds, result);
final Set<String> ids = dataManager.fetchIdsByPropertyValue(e.getType(),
PROPERTY_EXTENSION_ID, i.getExtensionId(),
PROPERTY_STATUS, Extension.Status.Installed.name());
if (ids.isEmpty()) {
// new extension
doImport = true;
} else {
for (String id : ids) {
final Extension extension = dataManager.fetch(e.getType(), id);
final DefaultArtifactVersion existingVersion = new DefaultArtifactVersion(extension.getVersion());
final DefaultArtifactVersion importedVersion = new DefaultArtifactVersion(i.getVersion());

// only import newer version, otherwise replace it in imported integration later
if (existingVersion.compareTo(importedVersion) < 0) {
doImport = true;
} else {
replaceExtensions.put(i.getId().get(), id);
}
}
}

return doImport;
});

// NOTE: connectors are imported without renaming and ignoring renamed ids
// as a matter of fact, the lambda should never be called
Expand All @@ -389,7 +420,7 @@ public Class<Connection> getType() {
public Class<Integration> getType() {
return Integration.class;
}
}, renamedIds, result);
}, renamedIds, replaceExtensions, result);

importModels(new JsonDbDao<OpenApi>(given) {
@Override
Expand All @@ -408,26 +439,9 @@ public Class<Icon> getType() {
return result;
}

private void undeleteImportedExtensions(JsonDbDao<Extension> extensionDao, Map<String, List<WithResourceId>> result) {

for (String id : extensionDao.fetchIds()) {
final Extension extension = dataManager.fetch(Extension.class, id);
if (extension != null) {
final Optional<Extension.Status> status = extension.getStatus();
if (status.isPresent() && status.get() == Extension.Status.Deleted) {
// undelete the extension
final Extension updated = new Extension.Builder()
.createFrom(extension)
.status(Extension.Status.Installed)
.build();
dataManager.update(updated);
addImportedItemResult(result, updated);
}
}
}
}

private void importIntegrations(SecurityContext sec, JsonDbDao<Integration> export, Map<String, String> renamedIds, Map<String, List<WithResourceId>> result) {
private void importIntegrations(SecurityContext sec, JsonDbDao<Integration> export,
Map<String, String> renamedIds, Map<String, String> replacedExtensions,
Map<String, List<WithResourceId>> result) {
for (Integration integration : export.fetchAll().getItems()) {
Integration.Builder builder = new Integration.Builder()
.createFrom(integration)
Expand All @@ -437,7 +451,7 @@ private void importIntegrations(SecurityContext sec, JsonDbDao<Integration> expo
// Do we need to create it?
String id = integration.getId().get();
Integration previous = dataManager.fetch(Integration.class, id);
resolveDuplicateNames(integration, builder, renamedIds);
resolveDuplicateNames(integration, builder, renamedIds, replacedExtensions);
if (previous == null) {
LOG.info("Creating integration: {}", integration.getName());
integrationHandler.create(sec, builder.build());
Expand All @@ -450,7 +464,8 @@ private void importIntegrations(SecurityContext sec, JsonDbDao<Integration> expo
}
}

private void resolveDuplicateNames(Integration integration, Integration.Builder builder, Map<String, String> renamedIds) {
private void resolveDuplicateNames(Integration integration, Integration.Builder builder,
Map<String, String> renamedIds, Map<String, String> replacedExtensions) {

// check for duplicate integration name
String integrationName = integration.getName();
Expand All @@ -472,7 +487,11 @@ private void resolveDuplicateNames(Integration integration, Integration.Builder
// step connections
.connection(s.getConnection().map(c -> renameIfNeeded(c, renamedIds, RENAME_CONNECTION)))
// step extensions
.extension(s.getExtension().map(e -> renameIfNeeded(e, renamedIds, RENAME_EXTENSION)))
.extension(s.getExtension().map(e -> {
final String existingId = replacedExtensions.get(e.getId().get());
return existingId != null ? dataManager.fetch(Extension.class, existingId) :
renameIfNeeded(e, renamedIds, RENAME_EXTENSION);
}))
.build())
.collect(Collectors.toList()))
.build())
Expand Down Expand Up @@ -519,12 +538,16 @@ private <T extends WithId<T>> void importModels(JsonDbDao<T> export, Map<String,
}
}
}
private <T extends WithId<T> & WithName> void importModels(JsonDbDao<T> export, BiFunction<T, String, T> renameFunc,
Map<String, String> renames, Map<String, List<WithResourceId>> result) {

private <T extends WithId<T> & WithName> void importModels(JsonDbDao<T> export, BiFunction<T, String, T> renameFunc, Map<String, String> renames, Map<String, List<WithResourceId>> result) {
importModels(export, renameFunc, renames, result, (e, i) -> dataManager.fetch(e.getType(), i.getId().get()) == null);
}

private <T extends WithId<T> & WithName> void importModels(JsonDbDao<T> export, BiFunction<T, String, T> renameFunc, Map<String, String> renames, Map<String, List<WithResourceId>> result, BiFunction<JsonDbDao<T>, T, Boolean> compareFunc) {
final Set<String> names = getAllPropertyValues(export.getType(), o -> o.getName());
for (T item : export.fetchAll().getItems()) {
String id = item.getId().get();
if (dataManager.fetch(export.getType(), id) == null) {
if (compareFunc.apply(export, item)) {

// resolve duplicate names
String name = item.getName();
Expand Down

0 comments on commit 0875175

Please sign in to comment.