Guest User

Untitled

a guest
Sep 14th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. embedded ApacheDS in my application, it fails on service.startup() -> DefaultSchemaService.getSchemaManager() returns null
  2. Exception in thread "main" java.lang.NullPointerException
  3. at org.apache.directory.server.core.schema.DefaultSchemaService.initialize(DefaultSchemaService.java:380)
  4. at org.apache.directory.server.core.DefaultDirectoryService.initialize(DefaultDirectoryService.java:1425)
  5. at org.apache.directory.server.core.DefaultDirectoryService.startup(DefaultDirectoryService.java:907)
  6. at Test3.runServer(Test3.java:41)
  7. at Test3.main(Test3.java:24)
  8.  
  9. import java.util.Properties;
  10.  
  11. import javax.naming.Context;
  12. import javax.naming.NamingEnumeration;
  13. import javax.naming.NamingException;
  14. import javax.naming.directory.Attributes;
  15. import javax.naming.directory.DirContext;
  16. import javax.naming.directory.InitialDirContext;
  17. import javax.naming.directory.SearchControls;
  18. import javax.naming.directory.SearchResult;
  19.  
  20. import org.apache.directory.server.core.DefaultDirectoryService;
  21. import org.apache.directory.server.core.partition.Partition;
  22. import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
  23. import org.apache.directory.server.ldap.LdapServer;
  24. import org.apache.directory.server.protocol.shared.transport.TcpTransport;
  25. import org.apache.directory.shared.ldap.entry.ServerEntry;
  26. import org.apache.directory.shared.ldap.name.DN;
  27.  
  28.  
  29. public class Test3 {
  30.  
  31. public static void main(String[] args) throws Exception {
  32. runServer();
  33. testClient();
  34. }
  35.  
  36. static void runServer() throws Exception {
  37. DefaultDirectoryService service = new DefaultDirectoryService();
  38. service.getChangeLog().setEnabled(false);
  39.  
  40. Partition partition = new JdbmPartition();
  41. partition.setId("apache");
  42. partition.setSuffix("dc=apache,dc=org");
  43. service.addPartition(partition);
  44.  
  45. LdapServer ldapService = new LdapServer();
  46. ldapService.setTransports(new TcpTransport(1400));
  47. ldapService.setDirectoryService(service);
  48.  
  49. service.startup();
  50.  
  51. // Inject the apache root entry if it does not already exist
  52. try {
  53. service.getAdminSession().lookup(partition.getSuffixDn());
  54. } catch (Exception lnnfe) {
  55. DN dnApache = new DN("dc=Apache,dc=Org");
  56. ServerEntry entryApache = service.newEntry(dnApache);
  57. entryApache.add("objectClass", "top", "domain", "extensibleObject");
  58. entryApache.add("dc", "Apache");
  59. service.getAdminSession().add(entryApache);
  60. }
  61.  
  62. DN dnApache = new DN("dc=Apache,dc=Org");
  63. ServerEntry entryApache = service.newEntry(dnApache);
  64. entryApache.add("objectClass", "top", "domain", "extensibleObject");
  65. entryApache.add("dc", "Apache");
  66. service.getAdminSession().add(entryApache);
  67.  
  68. ldapService.start();
  69. }
  70.  
  71.  
  72. static void testClient() throws NamingException {
  73. Properties p = new Properties();
  74. p.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  75. p.setProperty(Context.PROVIDER_URL, "ldap://localhost:1400/");
  76. p.setProperty(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
  77. p.setProperty(Context.SECURITY_CREDENTIALS, "secret");
  78. p.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
  79.  
  80. DirContext rootCtx = new InitialDirContext(p);
  81. DirContext ctx = (DirContext) rootCtx.lookup("dc=apache,dc=org");
  82. SearchControls sc = new SearchControls();
  83. sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
  84.  
  85. NamingEnumeration<SearchResult> searchResults = ctx.search("", "(objectclass=*)", sc);
  86.  
  87. while (searchResults.hasMoreElements()) {
  88. SearchResult searchResult = searchResults.next();
  89. Attributes attributes = searchResult.getAttributes();
  90. System.out.println("searchResult.attributes: " + attributes) ;
  91. }
  92. }
  93. }
  94.  
  95. service.setWorkingDirectory(new File("data"));
Add Comment
Please, Sign In to add comment