diff --git a/connections/file/src/main/java/org/keycloak/connections/file/DefaultFileConnectionProviderFactory.java b/connections/file/src/main/java/org/keycloak/connections/file/DefaultFileConnectionProviderFactory.java index 26c14de53a9a..9bfe36248c44 100755 --- a/connections/file/src/main/java/org/keycloak/connections/file/DefaultFileConnectionProviderFactory.java +++ b/connections/file/src/main/java/org/keycloak/connections/file/DefaultFileConnectionProviderFactory.java @@ -90,18 +90,7 @@ void readModelFile(KeycloakSession session) { ImportUtils.importFromStream(session, JsonSerialization.mapper, fis, Strategy.IGNORE_EXISTING); session.realms().getMigrationModel().setStoredVersion(model.getModelVersion()); - List realmReps = new ArrayList(); - for (RealmRepresentation realmRep : model.getRealms()) { - if (Config.getAdminRealm().equals(realmRep.getRealm())) { - realmReps.add(0, realmRep); - } else { - realmReps.add(realmRep); - } - } - for (RealmRepresentation realmRep : realmReps) { - ImportUtils.importRealm(session, realmRep, Strategy.IGNORE_EXISTING); - } - + ImportUtils.importRealms(session, model.getRealms(), Strategy.IGNORE_EXISTING); } catch (IOException ioe) { logger.error("Unable to read model file " + kcdata.getAbsolutePath(), ioe); } finally { diff --git a/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/ImportUtils.java b/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/ImportUtils.java index 494082077ae9..2cad3c1ffda9 100755 --- a/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/ImportUtils.java +++ b/export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/ImportUtils.java @@ -20,10 +20,8 @@ import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; + import org.keycloak.exportimport.ExportImportConfig; /** @@ -33,6 +31,21 @@ public class ImportUtils { private static final Logger logger = Logger.getLogger(ImportUtils.class); + public static void importRealms(KeycloakSession session, Collection realms, Strategy strategy) { + // Import admin realm first + for (RealmRepresentation realm : realms) { + if (Config.getAdminRealm().equals(realm.getRealm())) { + importRealm(session, realm, strategy); + } + } + + for (RealmRepresentation realm : realms) { + if (!Config.getAdminRealm().equals(realm.getRealm())) { + importRealm(session, realm, strategy); + } + } + } + /** * Fully import realm from representation, save it to model and return model of newly created realm * @@ -41,7 +54,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 RealmModel importRealm(KeycloakSession session, RealmRepresentation rep, Strategy strategy) { + public static void importRealm(KeycloakSession session, RealmRepresentation rep, Strategy strategy) { String realmName = rep.getRealm(); RealmProvider model = session.realms(); RealmModel realm = model.getRealmByName(realmName); @@ -49,7 +62,7 @@ public static RealmModel importRealm(KeycloakSession session, RealmRepresentatio if (realm != null) { if (strategy == Strategy.IGNORE_EXISTING) { logger.infof("Realm '%s' already exists. Import skipped", realmName); - return realm; + return; } else { logger.infof("Realm '%s' already exists. Removing it before import", realmName); if (Config.getAdminRealm().equals(realm.getId())) { @@ -73,7 +86,7 @@ public static RealmModel importRealm(KeycloakSession session, RealmRepresentatio logger.infof("Realm '%s' imported", realmName); } - return realm; + return; } private static void refreshMasterAdminApps(RealmProvider model, RealmModel realm) { @@ -145,9 +158,7 @@ public static void setupMasterAdminManagement(RealmProvider model, RealmModel re */ public static void importFromStream(KeycloakSession session, ObjectMapper mapper, InputStream is, Strategy strategy) throws IOException { Map realmReps = getRealmsFromStream(mapper, is); - for (RealmRepresentation realmRep : realmReps.values()) { - importRealm(session, realmRep, strategy); - } + importRealms(session, realmReps.values(), strategy); } public static Map getRealmsFromStream(ObjectMapper mapper, InputStream is) throws IOException { diff --git a/export-import/export-import-single-file/src/main/java/org/keycloak/exportimport/singlefile/SingleFileImportProvider.java b/export-import/export-import-single-file/src/main/java/org/keycloak/exportimport/singlefile/SingleFileImportProvider.java index 62ae93b7a3a4..7c600e8df401 100755 --- a/export-import/export-import-single-file/src/main/java/org/keycloak/exportimport/singlefile/SingleFileImportProvider.java +++ b/export-import/export-import-single-file/src/main/java/org/keycloak/exportimport/singlefile/SingleFileImportProvider.java @@ -42,9 +42,7 @@ public void importModel(KeycloakSessionFactory factory, final Strategy strategy) @Override protected void runExportImportTask(KeycloakSession session) throws IOException { - for (RealmRepresentation realmRep : realmReps.values()) { - ImportUtils.importRealm(session, realmRep, strategy); - } + ImportUtils.importRealms(session, realmReps.values(), strategy); } });