/** * Add a default partition to the server * * @param partitionId The partition Id * @param partitionDn The partition DN * @return The newly added partition * @throws Exception If the partition can't be added */ private void createDefaultPartition() throws Exception { Partition partition = new JdbmPartition(); partition.setId("jboss"); partition.setSuffix("dc=jboss,dc=org"); service.addPartition(partition); }
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 initializeWorkingDir() throws IOException { File workingDirectory = new File(DEFAULT_WORKING_DIR); if (workingDirectory.exists()) { FileUtils.deleteDirectory(workingDirectory); workingDirectory.mkdirs(); } service.setWorkingDirectory(workingDirectory); }
public ApacheDSContainer(String root, String ldifs) throws Exception { this.ldifResources = ldifs; service = new DefaultDirectoryService(); List<Interceptor> list = new ArrayList<Interceptor>(); list.add(new NormalizationInterceptor()); list.add(new AuthenticationInterceptor()); list.add(new ReferralInterceptor()); // list.add( new AciAuthorizationInterceptor() ); // list.add( new DefaultAuthorizationInterceptor() ); list.add(new ExceptionInterceptor()); // list.add( new ChangeLogInterceptor() ); list.add(new OperationalAttributeInterceptor()); // list.add( new SchemaInterceptor() ); list.add(new SubentryInterceptor()); // list.add( new CollectiveAttributeInterceptor() ); // list.add( new EventInterceptor() ); // list.add( new TriggerInterceptor() ); // list.add( new JournalInterceptor() ); service.setInterceptors(list); partition = new JdbmPartition(); partition.setId("rootPartition"); partition.setSuffix(root); this.root = root; service.addPartition(partition); service.setExitVmOnShutdown(false); service.setShutdownHookEnabled(false); service.getChangeLog().setEnabled(false); service.setDenormalizeOpAttrsEnabled(true); }
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); } } }
public static void loadLdif(DefaultDirectoryService directoryService, Resource ldifFile) throws IOException { File tempFile = File.createTempFile("spring_ldap_test", ".ldif"); try { InputStream inputStream = ldifFile.getInputStream(); IOUtils.copy(inputStream, new FileOutputStream(tempFile)); LdifFileLoader fileLoader = new LdifFileLoader(directoryService.getSession(), tempFile.getAbsolutePath()); fileLoader.execute(); } finally { try { tempFile.delete(); } catch (Exception e) { // Ignore this } } }
public void setWorkingDirectory(File workingDir) { Assert.notNull(workingDir); logger.info("Setting working directory for LDAP_PROVIDER: " + workingDir.getAbsolutePath()); if (workingDir.exists()) { throw new IllegalArgumentException( "The specified working directory '" + workingDir.getAbsolutePath() + "' already exists. Another directory service instance may be using it or it may be from a " + " previous unclean shutdown. Please confirm and delete it or configure a different " + "working directory"); } this.workingDir = workingDir; service.setWorkingDirectory(workingDir); }
/** * Initialize the server. It creates the partition, adds the index, and injects the context * entries for the created partitions. * * @throws Exception if there were some problems while initializing the system */ private void init() throws Exception { // Initialize the LDAP service service = new DefaultDirectoryService(); // Disable the ChangeLog system service.getChangeLog().setEnabled(false); // Create the default partition createDefaultPartition(); initializeWorkingDir(); // And start the service startServer(); // import default data importLDIF("ldap/users.ldif"); }
public void stop() { if (!isRunning()) { return; } logger.info("Shutting down directory server ..."); try { server.stop(); service.shutdown(); } catch (Exception e) { logger.error("Shutdown failed", e); return; } running = false; if (workingDir.exists()) { logger.info("Deleting working directory " + workingDir.getAbsolutePath()); deleteDir(workingDir); } }
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(); } }
private void startServer() throws Exception { service.startup(); server = new LdapServer(); server.setTransports(new TcpTransport(10389)); server.setDirectoryService(service); }