keycloak-service
公開メンバ関数 | 静的公開メンバ関数 | 静的公開変数類 | 限定公開メンバ関数 | 限定公開変数類 | 非公開メンバ関数 | 静的非公開メンバ関数 | 静的非公開変数類 | 全メンバ一覧
org.keycloak.services.resources.KeycloakApplication クラス
org.keycloak.services.resources.KeycloakApplication の継承関係図
Inheritance graph
org.keycloak.services.resources.KeycloakApplication 連携図
Collaboration graph

公開メンバ関数

 KeycloakApplication (@Context ServletContext context, @Context Dispatcher dispatcher)
 
String getContextPath ()
 
URI getBaseUri (UriInfo uriInfo)
 
KeycloakSessionFactory getSessionFactory ()
 
Set< Class<?> > getClasses ()
 
Set< Object > getSingletons ()
 
void importRealms ()
 
void importRealm (RealmRepresentation rep, String from)
 
void importAddUser ()
 

静的公開メンバ関数

static void loadConfig (ServletContext context)
 
static KeycloakSessionFactory createSessionFactory ()
 
static void setupScheduledTasks (final KeycloakSessionFactory sessionFactory)
 

静的公開変数類

static final String KEYCLOAK_CONFIG_PARAM_NAME = "org.keycloak.server-subsystem.Config"
 
static final String KEYCLOAK_EMBEDDED = "keycloak.embedded"
 
static final String SERVER_CONTEXT_CONFIG_PROPERTY_OVERRIDES = "keycloak.server.context.config.property-overrides"
 

限定公開メンバ関数

ExportImportManager migrateAndBootstrap ()
 
void migrateModel ()
 

限定公開変数類

boolean embedded = false
 
Set< Object > singletons = new HashSet<Object>()
 
Set< Class<?> > classes = new HashSet<Class<?>>()
 
KeycloakSessionFactory sessionFactory
 
String contextPath
 

非公開メンバ関数

void exit (int status)
 

静的非公開メンバ関数

static String loadDmrConfig (ServletContext context)
 
static< T > T loadJson (InputStream is, Class< T > type)
 

静的非公開変数類

static final Logger logger = Logger.getLogger(KeycloakApplication.class)
 

詳解

著者
Bill Burke
バージョン
Revision
1

構築子と解体子

◆ KeycloakApplication()

org.keycloak.services.resources.KeycloakApplication.KeycloakApplication ( @Context ServletContext  context,
@Context Dispatcher  dispatcher 
)
inline
108  {
109  try {
110  if ("true".equals(context.getInitParameter(KEYCLOAK_EMBEDDED))) {
111  embedded = true;
112  }
113 
114  loadConfig(context);
115 
116  this.contextPath = context.getContextPath();
118 
119  dispatcher.getDefaultContextObjects().put(KeycloakApplication.class, this);
120  ResteasyProviderFactory.pushContext(KeycloakApplication.class, this); // for injection
121  context.setAttribute(KeycloakSessionFactory.class.getName(), this.sessionFactory);
122 
123  singletons.add(new RobotsResource());
124  singletons.add(new RealmsResource());
125  singletons.add(new AdminRoot());
126  classes.add(ThemeResource.class);
127  classes.add(JsResource.class);
128 
129  classes.add(KeycloakTransactionCommitter.class);
130  classes.add(KeycloakErrorHandler.class);
131 
132  singletons.add(new ObjectMapperResolver(Boolean.parseBoolean(System.getProperty("keycloak.jsonPrettyPrint", "false"))));
133 
134  ExportImportManager[] exportImportManager = new ExportImportManager[1];
135 
136  KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {
137 
138  @Override
139  public void run(KeycloakSession lockSession) {
140  DBLockManager dbLockManager = new DBLockManager(lockSession);
141  dbLockManager.checkForcedUnlock();
142  DBLockProvider dbLock = dbLockManager.getDBLock();
143  dbLock.waitForLock();
144  try {
145  exportImportManager[0] = migrateAndBootstrap();
146  } finally {
147  dbLock.releaseLock();
148  }
149  }
150 
151  });
152 
153 
154  if (exportImportManager[0].isRunExport()) {
155  exportImportManager[0].runExport();
156  }
157 
158  AtomicBoolean bootstrapAdminUser = new AtomicBoolean(false);
159  KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {
160 
161  @Override
162  public void run(KeycloakSession session) {
163  boolean shouldBootstrapAdmin = new ApplianceBootstrap(session).isNoMasterUser();
164  bootstrapAdminUser.set(shouldBootstrapAdmin);
165  }
166 
167  });
168 
169  sessionFactory.publish(new PostMigrationEvent());
170 
171  singletons.add(new WelcomeResource(bootstrapAdminUser.get()));
172 
174  } catch (Throwable t) {
175  if (!embedded) {
176  exit(1);
177  }
178  throw t;
179  }
180  }
static void loadConfig(ServletContext context)
Definition: KeycloakApplication.java:269
static void setupScheduledTasks(final KeycloakSessionFactory sessionFactory)
Definition: KeycloakApplication.java:330
static final String KEYCLOAK_EMBEDDED
Definition: KeycloakApplication.java:94
void exit(int status)
Definition: KeycloakApplication.java:472
KeycloakSessionFactory sessionFactory
Definition: KeycloakApplication.java:105
static KeycloakSessionFactory createSessionFactory()
Definition: KeycloakApplication.java:324
Set< Object > singletons
Definition: KeycloakApplication.java:102
boolean embedded
Definition: KeycloakApplication.java:100
KeycloakApplication(@Context ServletContext context, @Context Dispatcher dispatcher)
Definition: KeycloakApplication.java:108
ExportImportManager migrateAndBootstrap()
Definition: KeycloakApplication.java:183
String contextPath
Definition: KeycloakApplication.java:106
Set< Class<?> > classes
Definition: KeycloakApplication.java:103

関数詳解

◆ createSessionFactory()

static KeycloakSessionFactory org.keycloak.services.resources.KeycloakApplication.createSessionFactory ( )
inlinestatic
324  {
325  DefaultKeycloakSessionFactory factory = new DefaultKeycloakSessionFactory();
326  factory.init();
327  return factory;
328  }

◆ exit()

void org.keycloak.services.resources.KeycloakApplication.exit ( int  status)
inlineprivate
472  {
473  new Thread() {
474  @Override
475  public void run() {
476  System.exit(status);
477  }
478  }.start();
479  }

◆ getBaseUri()

URI org.keycloak.services.resources.KeycloakApplication.getBaseUri ( UriInfo  uriInfo)
inline

Get base URI of WAR distribution, not JAX-RS

引数
uriInfo
戻り値
265  {
266  return uriInfo.getBaseUriBuilder().replacePath(getContextPath()).build();
267  }
String getContextPath()
Definition: KeycloakApplication.java:255

◆ getClasses()

Set<Class<?> > org.keycloak.services.resources.KeycloakApplication.getClasses ( )
inline
350  {
351  return classes;
352  }
Set< Class<?> > classes
Definition: KeycloakApplication.java:103

◆ getContextPath()

String org.keycloak.services.resources.KeycloakApplication.getContextPath ( )
inline
255  {
256  return contextPath;
257  }
String contextPath
Definition: KeycloakApplication.java:106

◆ getSessionFactory()

KeycloakSessionFactory org.keycloak.services.resources.KeycloakApplication.getSessionFactory ( )
inline
345  {
346  return sessionFactory;
347  }
KeycloakSessionFactory sessionFactory
Definition: KeycloakApplication.java:105

◆ getSingletons()

Set<Object> org.keycloak.services.resources.KeycloakApplication.getSingletons ( )
inline
355  {
356  return singletons;
357  }
Set< Object > singletons
Definition: KeycloakApplication.java:102

◆ importAddUser()

void org.keycloak.services.resources.KeycloakApplication.importAddUser ( )
inline
411  {
412  String configDir = System.getProperty("jboss.server.config.dir");
413  if (configDir != null) {
414  File addUserFile = new File(configDir + File.separator + "keycloak-add-user.json");
415  if (addUserFile.isFile()) {
416  ServicesLogger.LOGGER.imprtingUsersFrom(addUserFile);
417 
418  List<RealmRepresentation> realms;
419  try {
420  realms = JsonSerialization.readValue(new FileInputStream(addUserFile), new TypeReference<List<RealmRepresentation>>() {
421  });
422  } catch (IOException e) {
423  ServicesLogger.LOGGER.failedToLoadUsers(e);
424  return;
425  }
426 
427  for (RealmRepresentation realmRep : realms) {
428  for (UserRepresentation userRep : realmRep.getUsers()) {
429  KeycloakSession session = sessionFactory.create();
430  try {
431  session.getTransactionManager().begin();
432 
433  RealmModel realm = session.realms().getRealmByName(realmRep.getRealm());
434  if (realm == null) {
435  ServicesLogger.LOGGER.addUserFailedRealmNotFound(userRep.getUsername(), realmRep.getRealm());
436  } else {
437  UserModel user = session.users().addUser(realm, userRep.getUsername());
438  user.setEnabled(userRep.isEnabled());
439  RepresentationToModel.createCredentials(userRep, session, realm, user, false);
440  RepresentationToModel.createRoleMappings(userRep, user, realm);
441  }
442 
443  session.getTransactionManager().commit();
444  ServicesLogger.LOGGER.addUserSuccess(userRep.getUsername(), realmRep.getRealm());
445  } catch (ModelDuplicateException e) {
446  session.getTransactionManager().rollback();
447  ServicesLogger.LOGGER.addUserFailedUserExists(userRep.getUsername(), realmRep.getRealm());
448  } catch (Throwable t) {
449  session.getTransactionManager().rollback();
450  ServicesLogger.LOGGER.addUserFailed(t, userRep.getUsername(), realmRep.getRealm());
451  } finally {
452  session.close();
453  }
454  }
455  }
456 
457  if (!addUserFile.delete()) {
458  ServicesLogger.LOGGER.failedToDeleteFile(addUserFile.getAbsolutePath());
459  }
460  }
461  }
462  }
KeycloakSessionFactory sessionFactory
Definition: KeycloakApplication.java:105

◆ importRealm()

void org.keycloak.services.resources.KeycloakApplication.importRealm ( RealmRepresentation  rep,
String  from 
)
inline
376  {
377  KeycloakSession session = sessionFactory.create();
378  boolean exists = false;
379  try {
380  session.getTransactionManager().begin();
381 
382  try {
383  RealmManager manager = new RealmManager(session);
384  manager.setContextPath(getContextPath());
385 
386  if (rep.getId() != null && manager.getRealm(rep.getId()) != null) {
387  ServicesLogger.LOGGER.realmExists(rep.getRealm(), from);
388  exists = true;
389  }
390 
391  if (manager.getRealmByName(rep.getRealm()) != null) {
392  ServicesLogger.LOGGER.realmExists(rep.getRealm(), from);
393  exists = true;
394  }
395  if (!exists) {
396  RealmModel realm = manager.importRealm(rep);
397  ServicesLogger.LOGGER.importedRealm(realm.getName(), from);
398  }
399  session.getTransactionManager().commit();
400  } catch (Throwable t) {
401  session.getTransactionManager().rollback();
402  if (!exists) {
403  ServicesLogger.LOGGER.unableToImportRealm(t, rep.getRealm(), from);
404  }
405  }
406  } finally {
407  session.close();
408  }
409  }
KeycloakSessionFactory sessionFactory
Definition: KeycloakApplication.java:105
String getContextPath()
Definition: KeycloakApplication.java:255

◆ importRealms()

void org.keycloak.services.resources.KeycloakApplication.importRealms ( )
inline
359  {
360  String files = System.getProperty("keycloak.import");
361  if (files != null) {
362  StringTokenizer tokenizer = new StringTokenizer(files, ",");
363  while (tokenizer.hasMoreTokens()) {
364  String file = tokenizer.nextToken().trim();
365  RealmRepresentation rep;
366  try {
367  rep = loadJson(new FileInputStream(file), RealmRepresentation.class);
368  } catch (FileNotFoundException e) {
369  throw new RuntimeException(e);
370  }
371  importRealm(rep, "file " + file);
372  }
373  }
374  }
void importRealm(RealmRepresentation rep, String from)
Definition: KeycloakApplication.java:376
static< T > T loadJson(InputStream is, Class< T > type)
Definition: KeycloakApplication.java:464

◆ loadConfig()

static void org.keycloak.services.resources.KeycloakApplication.loadConfig ( ServletContext  context)
inlinestatic
269  {
270  try {
271  JsonNode node = null;
272 
273  String dmrConfig = loadDmrConfig(context);
274  if (dmrConfig != null) {
275  node = new ObjectMapper().readTree(dmrConfig);
276  ServicesLogger.LOGGER.loadingFrom("standalone.xml or domain.xml");
277  }
278 
279  String configDir = System.getProperty("jboss.server.config.dir");
280  if (node == null && configDir != null) {
281  File f = new File(configDir + File.separator + "keycloak-server.json");
282  if (f.isFile()) {
283  ServicesLogger.LOGGER.loadingFrom(f.getAbsolutePath());
284  node = new ObjectMapper().readTree(f);
285  }
286  }
287 
288  if (node == null) {
289  URL resource = Thread.currentThread().getContextClassLoader().getResource("META-INF/keycloak-server.json");
290  if (resource != null) {
291  ServicesLogger.LOGGER.loadingFrom(resource);
292  node = new ObjectMapper().readTree(resource);
293  }
294  }
295 
296  if (node != null) {
297  Map<String, String> propertyOverridesMap = new HashMap<>();
298  String propertyOverrides = context.getInitParameter(SERVER_CONTEXT_CONFIG_PROPERTY_OVERRIDES);
299  if (context.getInitParameter(SERVER_CONTEXT_CONFIG_PROPERTY_OVERRIDES) != null) {
300  JsonNode jsonObj = new ObjectMapper().readTree(propertyOverrides);
301  jsonObj.fields().forEachRemaining(e -> propertyOverridesMap.put(e.getKey(), e.getValue().asText()));
302  }
303  Properties properties = new SystemEnvProperties(propertyOverridesMap);
304  Config.init(new JsonConfigProvider(node, properties));
305  } else {
306  throw new RuntimeException("Keycloak config not found.");
307  }
308  } catch (IOException e) {
309  throw new RuntimeException("Failed to load config", e);
310  }
311  }
static final String SERVER_CONTEXT_CONFIG_PROPERTY_OVERRIDES
Definition: KeycloakApplication.java:96
static String loadDmrConfig(ServletContext context)
Definition: KeycloakApplication.java:313

◆ loadDmrConfig()

static String org.keycloak.services.resources.KeycloakApplication.loadDmrConfig ( ServletContext  context)
inlinestaticprivate
313  {
314  String dmrConfig = context.getInitParameter(KEYCLOAK_CONFIG_PARAM_NAME);
315  if (dmrConfig == null) return null;
316 
317  ModelNode dmrConfigNode = ModelNode.fromString(dmrConfig);
318  if (dmrConfigNode.asPropertyList().isEmpty()) return null;
319 
320  // note that we need to resolve expressions BEFORE we convert to JSON
321  return dmrConfigNode.resolve().toJSONString(true);
322  }
static final String KEYCLOAK_CONFIG_PARAM_NAME
Definition: KeycloakApplication.java:92

◆ loadJson()

static <T> T org.keycloak.services.resources.KeycloakApplication.loadJson ( InputStream  is,
Class< T >  type 
)
inlinestaticprivate
464  {
465  try {
466  return JsonSerialization.readValue(is, type);
467  } catch (IOException e) {
468  throw new RuntimeException("Failed to parse json", e);
469  }
470  }

◆ migrateAndBootstrap()

ExportImportManager org.keycloak.services.resources.KeycloakApplication.migrateAndBootstrap ( )
inlineprotected
183  {
184  ExportImportManager exportImportManager;
185  logger.debug("Calling migrateModel");
186  migrateModel();
187 
188  logger.debug("bootstrap");
189  KeycloakSession session = sessionFactory.create();
190  try {
191  session.getTransactionManager().begin();
192  JtaTransactionManagerLookup lookup = (JtaTransactionManagerLookup) sessionFactory.getProviderFactory(JtaTransactionManagerLookup.class);
193  if (lookup != null) {
194  if (lookup.getTransactionManager() != null) {
195  try {
196  Transaction transaction = lookup.getTransactionManager().getTransaction();
197  logger.debugv("bootstrap current transaction? {0}", transaction != null);
198  if (transaction != null) {
199  logger.debugv("bootstrap current transaction status? {0}", transaction.getStatus());
200  }
201  } catch (SystemException e) {
202  throw new RuntimeException(e);
203  }
204  }
205  }
206 
207 
208  ApplianceBootstrap applianceBootstrap = new ApplianceBootstrap(session);
209  exportImportManager = new ExportImportManager(session);
210 
211  boolean createMasterRealm = applianceBootstrap.isNewInstall();
212  if (exportImportManager.isRunImport() && exportImportManager.isImportMasterIncluded()) {
213  createMasterRealm = false;
214  }
215 
216  if (createMasterRealm) {
217  applianceBootstrap.createMasterRealm(contextPath);
218  }
219  session.getTransactionManager().commit();
220  } catch (RuntimeException re) {
221  if (session.getTransactionManager().isActive()) {
222  session.getTransactionManager().rollback();
223  }
224  throw re;
225  } finally {
226  session.close();
227  }
228 
229  if (exportImportManager.isRunImport()) {
230  exportImportManager.runImport();
231  } else {
232  importRealms();
233  }
234 
235  importAddUser();
236 
237  return exportImportManager;
238  }
void importAddUser()
Definition: KeycloakApplication.java:411
KeycloakSessionFactory sessionFactory
Definition: KeycloakApplication.java:105
String contextPath
Definition: KeycloakApplication.java:106
void importRealms()
Definition: KeycloakApplication.java:359
void migrateModel()
Definition: KeycloakApplication.java:241
static final Logger logger
Definition: KeycloakApplication.java:98

◆ migrateModel()

void org.keycloak.services.resources.KeycloakApplication.migrateModel ( )
inlineprotected
241  {
242  KeycloakSession session = sessionFactory.create();
243  try {
244  session.getTransactionManager().begin();
245  MigrationModelManager.migrate(session);
246  session.getTransactionManager().commit();
247  } catch (Exception e) {
248  session.getTransactionManager().rollback();
249  throw e;
250  } finally {
251  session.close();
252  }
253  }
KeycloakSessionFactory sessionFactory
Definition: KeycloakApplication.java:105

◆ setupScheduledTasks()

static void org.keycloak.services.resources.KeycloakApplication.setupScheduledTasks ( final KeycloakSessionFactory  sessionFactory)
inlinestatic
330  {
331  long interval = Config.scope("scheduled").getLong("interval", 60L) * 1000;
332 
333  KeycloakSession session = sessionFactory.create();
334  try {
335  TimerProvider timer = session.getProvider(TimerProvider.class);
336  timer.schedule(new ClusterAwareScheduledTaskRunner(sessionFactory, new ClearExpiredEvents(), interval), interval, "ClearExpiredEvents");
337  timer.schedule(new ClusterAwareScheduledTaskRunner(sessionFactory, new ClearExpiredClientInitialAccessTokens(), interval), interval, "ClearExpiredClientInitialAccessTokens");
338  timer.schedule(new ScheduledTaskRunner(sessionFactory, new ClearExpiredUserSessions()), interval, ClearExpiredUserSessions.TASK_NAME);
339  new UserStorageSyncManager().bootstrapPeriodic(sessionFactory, timer);
340  } finally {
341  session.close();
342  }
343  }
KeycloakSessionFactory sessionFactory
Definition: KeycloakApplication.java:105

メンバ詳解

◆ classes

Set<Class<?> > org.keycloak.services.resources.KeycloakApplication.classes = new HashSet<Class<?>>()
protected

◆ contextPath

String org.keycloak.services.resources.KeycloakApplication.contextPath
protected

◆ embedded

boolean org.keycloak.services.resources.KeycloakApplication.embedded = false
protected

◆ KEYCLOAK_CONFIG_PARAM_NAME

final String org.keycloak.services.resources.KeycloakApplication.KEYCLOAK_CONFIG_PARAM_NAME = "org.keycloak.server-subsystem.Config"
static

◆ KEYCLOAK_EMBEDDED

final String org.keycloak.services.resources.KeycloakApplication.KEYCLOAK_EMBEDDED = "keycloak.embedded"
static

◆ logger

final Logger org.keycloak.services.resources.KeycloakApplication.logger = Logger.getLogger(KeycloakApplication.class)
staticprivate

◆ SERVER_CONTEXT_CONFIG_PROPERTY_OVERRIDES

final String org.keycloak.services.resources.KeycloakApplication.SERVER_CONTEXT_CONFIG_PROPERTY_OVERRIDES = "keycloak.server.context.config.property-overrides"
static

◆ sessionFactory

KeycloakSessionFactory org.keycloak.services.resources.KeycloakApplication.sessionFactory
protected

◆ singletons

Set<Object> org.keycloak.services.resources.KeycloakApplication.singletons = new HashSet<Object>()
protected

このクラス詳解は次のファイルから抽出されました: