From dc1d0e7f44d5789c4199d567ddb93e70a7a13a3b Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Tue, 8 Mar 2016 08:55:06 +0100 Subject: [PATCH] KEYCLOAK-2596 Fix import with dir and ignore existing --- .../exportimport/dir/DirImportProvider.java | 26 +++++++++++-------- .../exportimport/util/ImportUtils.java | 8 +++--- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/services/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java b/services/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java index dd2ee07565..329b7297c3 100755 --- a/services/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java +++ b/services/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java @@ -35,6 +35,7 @@ import java.io.FilenameFilter; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; /** * @author Marek Posolda @@ -118,26 +119,29 @@ public class DirImportProvider implements ImportProvider { // Import realm first FileInputStream is = new FileInputStream(realmFile); final RealmRepresentation realmRep = JsonSerialization.readValue(is, RealmRepresentation.class); + final AtomicBoolean realmImported = new AtomicBoolean(); KeycloakModelUtils.runJobInTransaction(factory, new ExportImportSessionTask() { @Override public void runExportImportTask(KeycloakSession session) throws IOException { - ImportUtils.importRealm(session, realmRep, strategy); + boolean imported = ImportUtils.importRealm(session, realmRep, strategy); + realmImported.set(imported); } }); - // Import users - for (File userFile : userFiles) { - final FileInputStream fis = new FileInputStream(userFile); - KeycloakModelUtils.runJobInTransaction(factory, new ExportImportSessionTask() { - - @Override - protected void runExportImportTask(KeycloakSession session) throws IOException { - ImportUtils.importUsersFromStream(session, realmName, JsonSerialization.mapper, fis); - } - }); + if (realmImported.get()) { + // Import users + for (File userFile : userFiles) { + final FileInputStream fis = new FileInputStream(userFile); + KeycloakModelUtils.runJobInTransaction(factory, new ExportImportSessionTask() { + @Override + protected void runExportImportTask(KeycloakSession session) throws IOException { + ImportUtils.importUsersFromStream(session, realmName, JsonSerialization.mapper, fis); + } + }); + } } } diff --git a/services/src/main/java/org/keycloak/exportimport/util/ImportUtils.java b/services/src/main/java/org/keycloak/exportimport/util/ImportUtils.java index 06f6e54998..2ee2e11b3f 100755 --- a/services/src/main/java/org/keycloak/exportimport/util/ImportUtils.java +++ b/services/src/main/java/org/keycloak/exportimport/util/ImportUtils.java @@ -68,7 +68,7 @@ public class ImportUtils { * @param strategy specifies whether to overwrite or ignore existing realm or user entries * @return newly imported realm (or existing realm if ignoreExisting is true and realm of this name already exists) */ - public static void importRealm(KeycloakSession session, RealmRepresentation rep, Strategy strategy) { + public static boolean importRealm(KeycloakSession session, RealmRepresentation rep, Strategy strategy) { String realmName = rep.getRealm(); RealmProvider model = session.realms(); RealmModel realm = model.getRealmByName(realmName); @@ -76,7 +76,7 @@ public class ImportUtils { if (realm != null) { if (strategy == Strategy.IGNORE_EXISTING) { logger.infof("Realm '%s' already exists. Import skipped", realmName); - return; + return false; } else { logger.infof("Realm '%s' already exists. Removing it before import", realmName); if (Config.getAdminRealm().equals(realm.getId())) { @@ -91,13 +91,13 @@ public class ImportUtils { } RealmImporter realmManager = session.getContext().getRealmManager(); - realm = realmManager.importRealm(rep); + realmManager.importRealm(rep); if (System.getProperty(ExportImportConfig.ACTION) != null) { logger.infof("Realm '%s' imported", realmName); } - return; + return true; } /**