diff --git a/server/pom.xml b/server/pom.xml
index ae947f6c67..3f3c89e836 100755
--- a/server/pom.xml
+++ b/server/pom.xml
@@ -103,6 +103,25 @@
jboss-servlet-api_3.0_spec
provided
+
+ org.jboss.resteasy
+ resteasy-jaxrs
+ provided
+
+
+ log4j
+ log4j
+
+
+ org.slf4j
+ slf4j-api
+
+
+ org.slf4j
+ slf4j-simple
+
+
+
diff --git a/server/src/main/java/org/keycloak/server/KeycloakServerApplication.java b/server/src/main/java/org/keycloak/server/KeycloakServerApplication.java
index cb91e735fe..a4af8be968 100755
--- a/server/src/main/java/org/keycloak/server/KeycloakServerApplication.java
+++ b/server/src/main/java/org/keycloak/server/KeycloakServerApplication.java
@@ -1,6 +1,7 @@
package org.keycloak.server;
import org.jboss.resteasy.jwt.JsonSerialization;
+import org.jboss.resteasy.logging.Logger;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.services.managers.ApplianceBootstrap;
import org.keycloak.services.managers.RealmManager;
@@ -11,18 +12,67 @@ import org.keycloak.services.resources.KeycloakApplication;
import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;
import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class KeycloakServerApplication extends KeycloakApplication {
- public KeycloakServerApplication(@Context ServletContext servletContext) {
+ private static final Logger log = Logger.getLogger(KeycloakServerApplication.class);
+
+ public KeycloakServerApplication(@Context ServletContext servletContext) throws FileNotFoundException {
super(servletContext);
KeycloakSession session = factory.createSession();
session.getTransaction().begin();
ApplianceBootstrap bootstrap = new ApplianceBootstrap();
bootstrap.bootstrap(session);
+
+ String importRealm = System.getProperty("keycloak.import");
+ if (importRealm != null) {
+ RealmRepresentation rep = loadJson(new FileInputStream(importRealm), RealmRepresentation.class);
+ importRealm(session, rep);
+ }
+
session.getTransaction().commit();
}
+ public void importRealm(KeycloakSession session, RealmRepresentation rep ) {
+ try {
+ RealmManager manager = new RealmManager(session);
+
+ if (rep.getId() == null) {
+ throw new RuntimeException("Realm id not specified");
+ }
+
+ if (manager.getRealm(rep.getId()) != null) {
+ log.info("Not importing realm " + rep.getRealm() + " realm already exists");
+ return;
+ }
+
+ RealmModel realm = manager.createRealm(rep.getId(), rep.getRealm());
+ manager.importRealm(rep, realm);
+
+ log.info("Imported realm " + realm.getName());
+
+ session.getTransaction().commit();
+ } finally {
+ session.close();
+ }
+ }
+
+ private static T loadJson(InputStream is, Class type) {
+ try {
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ int c;
+ while ((c = is.read()) != -1) {
+ os.write(c);
+ }
+ byte[] bytes = os.toByteArray();
+ return JsonSerialization.fromBytes(type, bytes);
+ } catch (IOException e) {
+ throw new RuntimeException("Failed to parse json", e);
+ }
+ }
+
}