public void start() { if (isRunning()) { return; } if (service.isStarted()) { throw new IllegalStateException("DirectoryService is already running."); } logger.info("Starting directory server..."); try { service.startup(); server.start(); } catch (Exception e) { logger.error("Server startup failed ", e); return; } try { service.getAdminSession().lookup(partition.getSuffixDn()); } catch (LdapNameNotFoundException e) { try { LdapDN dn = new LdapDN(root); Assert.isTrue(root.startsWith("dc=")); String dc = root.substring(3, root.indexOf(',')); ServerEntry entry = service.newEntry(dn); entry.add("objectClass", "top", "domain", "extensibleObject"); entry.add("dc", dc); service.getAdminSession().add(entry); } catch (Exception e1) { logger.error("Failed to create dc entry", e1); } } catch (Exception e) { logger.error("Lookup failed", e); } running = true; try { importLdifs(); } catch (Exception e) { logger.error("Failed to import LDIF file(s)", e); } }
private void importLDIF(String fileName) throws Exception { InputStream is = getClass().getClassLoader().getResourceAsStream(fileName); CoreSession rootDSE = service.getAdminSession(); for (LdifEntry ldifEntry : new LdifReader(is)) { DefaultServerEntry entry = new DefaultServerEntry( rootDSE.getDirectoryService().getRegistries(), ldifEntry.getEntry()); if (!rootDSE.exists(entry.getDn())) { rootDSE.add(entry); } } }
private void importLdifs() throws Exception { // Import any ldif files Resource[] ldifs; if (ctxt == null) { // Not running within an app context ldifs = new PathMatchingResourcePatternResolver().getResources(ldifResources); } else { ldifs = ctxt.getResources(ldifResources); } // Note that we can't just import using the ServerContext returned // from starting Apace DS, apparently because of the long-running issue DIRSERVER-169. // We need a standard context. // DirContext dirContext = contextSource.getReadWriteContext(); if (ldifs != null && ldifs.length > 0) { String ldifFile = ldifs[0].getFile().getAbsolutePath(); logger.info("Loading LDIF file: " + ldifFile); LdifFileLoader loader = new LdifFileLoader(service.getAdminSession(), ldifFile); loader.execute(); } }