Merge pull request #667 from mposolda/master

KEYCLOAK-659 Export current version number during export
This commit is contained in:
Marek Posolda 2014-09-02 17:09:10 +02:00
commit f7fcefc11c
5 changed files with 39 additions and 11 deletions

View file

@ -14,7 +14,7 @@ public class Version {
public static String VERSION;
public static String BUILD_TIME;
public static final String UNKNOWN = "UNKNOWN";
public static final Version SINGLETON = new Version();
public static final Version SINGLETON;
private final String version = VERSION;
private final String buildTime = BUILD_TIME;
@ -30,6 +30,8 @@ public class Version {
VERSION=UNKNOWN;
BUILD_TIME=UNKNOWN;
}
SINGLETON = new Version();
}
@JsonProperty("version")

View file

@ -1,6 +1,7 @@
package org.keycloak.exportimport.util;
import org.jboss.logging.Logger;
import org.keycloak.Version;
import org.keycloak.exportimport.ExportImportConfig;
import org.keycloak.exportimport.ExportProvider;
import org.keycloak.exportimport.UsersExportStrategy;
@ -37,12 +38,19 @@ public abstract class MultipleStepsExportProvider implements ExportProvider {
});
for (RealmModel realm : holder.realms) {
exportRealm(factory, realm.getName());
exportRealmImpl(factory, realm.getName());
}
writeVersion("version.json", Version.SINGLETON);
}
@Override
public void exportRealm(KeycloakSessionFactory factory, final String realmName) throws IOException {
public void exportRealm(KeycloakSessionFactory factory, String realmName) throws IOException {
exportRealmImpl(factory, realmName);
writeVersion("version.json", Version.SINGLETON);
}
protected void exportRealmImpl(KeycloakSessionFactory factory, final String realmName) throws IOException {
final UsersExportStrategy usersExportStrategy = ExportImportConfig.getUsersExportStrategy();
final int usersPerFile = ExportImportConfig.getUsersPerFile();
final UsersHolder usersHolder = new UsersHolder();
@ -102,6 +110,8 @@ public abstract class MultipleStepsExportProvider implements ExportProvider {
protected abstract void writeUsers(String fileName, KeycloakSession session, RealmModel realm, List<UserModel> users) throws IOException;
protected abstract void writeVersion(String fileName, Version version) throws IOException;
public static class RealmsHolder {
List<RealmModel> realms;

View file

@ -1,5 +1,6 @@
package org.keycloak.exportimport.dir;
import org.keycloak.Version;
import org.keycloak.exportimport.util.ExportUtils;
import org.keycloak.exportimport.util.MultipleStepsExportProvider;
import org.keycloak.models.KeycloakSession;
@ -68,6 +69,13 @@ public class DirExportProvider extends MultipleStepsExportProvider {
ExportUtils.exportUsersToStream(session, realm, users, JsonSerialization.prettyMapper, os);
}
@Override
protected void writeVersion(String fileName, Version version) throws IOException {
File file = new File(this.rootDirectory, fileName);
FileOutputStream stream = new FileOutputStream(file);
JsonSerialization.prettyMapper.writeValue(stream, version);
}
@Override
public void close() {
}

View file

@ -4,6 +4,7 @@ import de.idyl.winzipaes.AesZipFileEncrypter;
import de.idyl.winzipaes.impl.AESEncrypter;
import de.idyl.winzipaes.impl.AESEncrypterBC;
import org.jboss.logging.Logger;
import org.keycloak.Version;
import org.keycloak.exportimport.util.ExportUtils;
import org.keycloak.exportimport.util.MultipleStepsExportProvider;
import org.keycloak.models.KeycloakSession;
@ -47,17 +48,24 @@ public class ZipExportProvider extends MultipleStepsExportProvider {
protected void writeRealm(String fileName, RealmRepresentation rep) throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
JsonSerialization.mapper.writeValue(stream, rep);
byte[] byteArray = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
this.encrypter.add(fileName, bis, this.password);
writeStream(fileName, stream);
}
@Override
protected void writeUsers(String fileName, KeycloakSession session, RealmModel realm, List<UserModel> users) throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ExportUtils.exportUsersToStream(session, realm, users, JsonSerialization.mapper, stream);
writeStream(fileName, stream);
}
@Override
protected void writeVersion(String fileName, Version version) throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
JsonSerialization.mapper.writeValue(stream, version);
writeStream(fileName, stream);
}
private void writeStream(String fileName, ByteArrayOutputStream stream) throws IOException {
byte[] byteArray = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
this.encrypter.add(fileName, bis, this.password);

View file

@ -147,8 +147,8 @@ public class ExportImportTest {
testFullExportImport();
// There should be 6 files in target directory (3 realm, 3 user)
Assert.assertEquals(6, new File(targetDirPath).listFiles().length);
// There should be 6 files in target directory (3 realm, 3 user, 1 version)
Assert.assertEquals(7, new File(targetDirPath).listFiles().length);
}
@Test
@ -161,8 +161,8 @@ public class ExportImportTest {
testRealmExportImport();
// There should be 3 files in target directory (1 realm, 2 user)
Assert.assertEquals(3, new File(targetDirPath).listFiles().length);
// There should be 3 files in target directory (1 realm, 2 user, 1 version)
Assert.assertEquals(4, new File(targetDirPath).listFiles().length);
}
@Test