예제 #1
0
  public static void createRealms(String defaultRealm, List<AuthRealm> realms, String configName) {
    assert (realms != null);

    String goodRealm = null; // need at least one good realm

    for (AuthRealm aRealm : realms) {
      String realmName = aRealm.getName();
      String realmClass = aRealm.getClassname();
      assert (realmName != null);
      assert (realmClass != null);

      try {
        List<Property> realmProps = aRealm.getProperty();
        /*V3 Commented ElementProperty[] realmProps =
        aRealm.getElementProperty();*/
        Properties props = new Properties();
        for (Property realmProp : realmProps) {
          props.setProperty(realmProp.getName(), realmProp.getValue());
        }
        Realm.instantiate(realmName, realmClass, props, configName);
        if (logger.isLoggable(Level.FINE)) {
          logger.fine("Configured realm: " + realmName);
        }

        if (goodRealm == null) {
          goodRealm = realmName;
        }
      } catch (Exception e) {
        logger.log(Level.WARNING, SecurityLoggerInfo.realmConfigDisabledError, realmName);
        logger.log(Level.WARNING, SecurityLoggerInfo.securityExceptionError, e);
      }
    }

    // done loading all realms, check that there is at least one
    // in place and that default is installed, or change default
    // to the first one loaded (arbitrarily).

    if (goodRealm == null) {
      logger.severe(SecurityLoggerInfo.noRealmsError);

    } else {
      try {
        Realm.getInstance(defaultRealm);
      } catch (Exception e) {
        defaultRealm = goodRealm;
      }
      Realm.setDefaultRealm(defaultRealm);
      if (logger.isLoggable(Level.FINE)) {
        logger.fine("Default realm is set to: " + defaultRealm);
      }
    }
  }