Search in sources :

Example 1 with HasEntryOperationContext

use of org.apache.directory.server.core.api.interceptor.context.HasEntryOperationContext in project structr by structr.

the class LDAPServerService method initSchema.

// ----- private methods -----
private void initSchema(final SchemaManager schemaManager, final CoreSession adminSession, final StructrPartition partition) throws Exception {
    final URL url = SchemaEntityFactory.class.getProtectionDomain().getCodeSource().getLocation();
    final JarFile jarFile = new JarFile(new File(url.toURI()));
    final List<String> names = new LinkedList<>();
    for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements(); ) {
        final ZipEntry zipEntry = e.nextElement();
        final String name = zipEntry.getName();
        if (name.startsWith("schema/") && name.endsWith(".ldif")) {
            names.add(name);
        }
    }
    Collections.sort(names, new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            // first sort key: number of slashes (level)
            Integer s1 = StringUtils.countMatches(o1, "/");
            Integer s2 = StringUtils.countMatches(o2, "/");
            if (s1.equals(s2)) {
                // secondary sort key: string length
                Integer l1 = o1.length();
                Integer l2 = o2.length();
                if (l1.equals(l2)) {
                    // tertiary sort key: the string values
                    return o1.compareTo(o2);
                }
                return l1.compareTo(l2);
            }
            return s1.compareTo(s2);
        }
    });
    for (final String name : names) {
        try (final LdifReader reader = new LdifReader(jarFile.getInputStream(jarFile.getEntry(name)))) {
            for (final LdifEntry entry : reader) {
                final Entry schemaEntry = new DefaultEntry(schemaManager, entry.getEntry());
                final Dn dn = schemaEntry.getDn();
                if (!partition.hasEntry(new HasEntryOperationContext(adminSession, dn))) {
                    logger.info("Importing {}...", name);
                    partition.add(new AddOperationContext(adminSession, schemaEntry));
                }
            }
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Dn(org.apache.directory.api.ldap.model.name.Dn) AddOperationContext(org.apache.directory.server.core.api.interceptor.context.AddOperationContext) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) LinkedList(java.util.LinkedList) HasEntryOperationContext(org.apache.directory.server.core.api.interceptor.context.HasEntryOperationContext) LdifReader(org.apache.directory.api.ldap.model.ldif.LdifReader) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) JarEntry(java.util.jar.JarEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) ZipEntry(java.util.zip.ZipEntry) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry) JarFile(java.util.jar.JarFile) File(java.io.File) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry)

Aggregations

File (java.io.File)1 URL (java.net.URL)1 LinkedList (java.util.LinkedList)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 ZipEntry (java.util.zip.ZipEntry)1 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)1 Entry (org.apache.directory.api.ldap.model.entry.Entry)1 LdifEntry (org.apache.directory.api.ldap.model.ldif.LdifEntry)1 LdifReader (org.apache.directory.api.ldap.model.ldif.LdifReader)1 Dn (org.apache.directory.api.ldap.model.name.Dn)1 AddOperationContext (org.apache.directory.server.core.api.interceptor.context.AddOperationContext)1 HasEntryOperationContext (org.apache.directory.server.core.api.interceptor.context.HasEntryOperationContext)1