/**
  * Creates directory services, starts LDAP server and KDCServer
  *
  * @param managementClient
  * @param containerId
  * @throws Exception
  * @see
  *     org.jboss.as.arquillian.api.ServerSetupTask#setup(org.jboss.as.arquillian.container.ManagementClient,
  *     java.lang.String)
  */
 public void setup(ManagementClient managementClient, String containerId) throws Exception {
   try {
     if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
       Security.addProvider(new BouncyCastleProvider());
       removeBouncyCastle = true;
     }
   } catch (SecurityException ex) {
     LOGGER.warn("Cannot register BouncyCastleProvider", ex);
   }
   directoryService = DSAnnotationProcessor.getDirectoryService();
   final SchemaManager schemaManager = directoryService.getSchemaManager();
   try {
     for (LdifEntry ldifEntry :
         new LdifReader(
             LdapLoginModuleTestCase.class.getResourceAsStream(
                 LdapLoginModuleTestCase.class.getSimpleName() + ".ldif"))) {
       directoryService
           .getAdminSession()
           .add(new DefaultEntry(schemaManager, ldifEntry.getEntry()));
     }
   } catch (Exception e) {
     e.printStackTrace();
     throw e;
   }
   final ManagedCreateLdapServer createLdapServer =
       new ManagedCreateLdapServer(
           (CreateLdapServer) AnnotationUtils.getInstance(CreateLdapServer.class));
   FileOutputStream fos = new FileOutputStream(KEYSTORE_FILE);
   IOUtils.copy(getClass().getResourceAsStream(KEYSTORE_FILENAME), fos);
   fos.close();
   createLdapServer.setKeyStore(KEYSTORE_FILE.getAbsolutePath());
   Utils.fixApacheDSTransportAddress(
       createLdapServer, Utils.getSecondaryTestAddress(managementClient, false));
   ldapServer =
       ServerAnnotationProcessor.instantiateLdapServer(createLdapServer, directoryService);
   ldapServer.start();
 }
  // @formatter:off
  @CreateDS(
      name = "JBossDS",
      factory = org.jboss.as.test.integration.ldap.InMemoryDirectoryServiceFactory.class,
      partitions = {
        @CreatePartition(
            name = "jboss",
            suffix = "dc=jboss,dc=org",
            contextEntry =
                @ContextEntry(
                    entryLdif =
                        "dn: dc=jboss,dc=org\n"
                            + "dc: jboss\n"
                            + "objectClass: top\n"
                            + "objectClass: domain\n\n"),
            indexes = {
              @CreateIndex(attribute = "objectClass"),
              @CreateIndex(attribute = "dc"),
              @CreateIndex(attribute = "ou")
            })
      },
      additionalInterceptors = {KeyDerivationInterceptor.class})
  @CreateLdapServer(
      transports = {
        @CreateTransport(protocol = "LDAP", port = LDAP_PORT),
        @CreateTransport(protocol = "LDAPS", port = LDAPS_PORT)
      },
      certificatePassword = "******")
  @CreateKdcServer(
      primaryRealm = KERBEROS_PRIMARY_REALM,
      kdcPrincipal = "krbtgt/" + KERBEROS_PRIMARY_REALM + "@" + KERBEROS_PRIMARY_REALM,
      searchBaseDn = "dc=jboss,dc=org",
      transports = {
        @CreateTransport(protocol = "UDP", port = KERBEROS_PORT),
        @CreateTransport(protocol = "TCP", port = KERBEROS_PORT)
      })
  // @formatter:on
  public void createLdap1(ManagementClient managementClient, final String hostname)
      throws Exception, IOException, ClassNotFoundException, FileNotFoundException {
    final Map<String, String> map = new HashMap<String, String>();
    final String cannonicalHost =
        NetworkUtils.formatPossibleIpv6Address(Utils.getCannonicalHost(managementClient));
    map.put("hostname", cannonicalHost);
    map.put("realm", KERBEROS_PRIMARY_REALM);
    directoryService1 = DSAnnotationProcessor.getDirectoryService();
    final String ldifContent =
        StrSubstitutor.replace(
            IOUtils.toString(
                KerberosServerSetupTask.class.getResourceAsStream(
                    KerberosServerSetupTask.class.getSimpleName() + ".ldif"),
                "UTF-8"),
            map);
    LOGGER.info(ldifContent);

    final SchemaManager schemaManager = directoryService1.getSchemaManager();
    try {
      for (LdifEntry ldifEntry : new LdifReader(IOUtils.toInputStream(ldifContent))) {
        directoryService1
            .getAdminSession()
            .add(new DefaultEntry(schemaManager, ldifEntry.getEntry()));
      }
    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
    final ManagedCreateLdapServer createLdapServer =
        new ManagedCreateLdapServer(
            (CreateLdapServer) AnnotationUtils.getInstance(CreateLdapServer.class));
    FileOutputStream fos = new FileOutputStream(KEYSTORE_FILE);
    IOUtils.copy(getClass().getResourceAsStream(KEYSTORE_FILENAME), fos);
    fos.close();

    createLdapServer.setKeyStore(KEYSTORE_FILE.getAbsolutePath());
    fixTransportAddress(createLdapServer, cannonicalHost);

    ldapServer1 =
        ServerAnnotationProcessor.instantiateLdapServer(createLdapServer, directoryService1);
    krbServer1 =
        KDCServerAnnotationProcessor.getKdcServer(directoryService1, KERBEROS_PORT, cannonicalHost);
    ldapServer1.start();
  }