File: SchemaReader.java

package info (click to toggle)
libspring-ldap-java 1.3.1.RELEASE-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,876 kB
  • sloc: java: 12,509; xml: 4,104; jsp: 36; makefile: 31; sh: 13
file content (157 lines) | stat: -rw-r--r-- 6,175 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package org.springframework.ldap.odm.tools;

import java.util.HashSet;
import java.util.Set;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;

import org.springframework.ldap.odm.tools.SyntaxToJavaClass.ClassInfo;

// Processes LDAP Schema
/* package */ final class SchemaReader {
    private final DirContext schemaContext;

    private final SyntaxToJavaClass syntaxToJavaClass;

    private final Set<String> binarySet;

    public SchemaReader(DirContext schemaContext, SyntaxToJavaClass syntaxToJavaClass, Set<String> binarySet) {
        this.schemaContext = schemaContext;
        this.syntaxToJavaClass = syntaxToJavaClass;
        this.binarySet = binarySet;
    }

    // Get the object schema for the given object classes
    public ObjectSchema getObjectSchema(Set<String> objectClasses) 
        throws NamingException, ClassNotFoundException {
        
        ObjectSchema result = new ObjectSchema();
        createObjectClass(objectClasses, schemaContext, result);
        return result;
    }

    private enum SchemaAttributeType {
        SUP, MUST, MAY, UNKNOWN
    }

    private SchemaAttributeType getSchemaAttributeType(String type) {
        SchemaAttributeType result = SchemaAttributeType.UNKNOWN;

        if (type.equals("SUP")) {
            result = SchemaAttributeType.SUP;
        } else {
            if (type.equals("MUST")) {
                result = SchemaAttributeType.MUST;
            } else {
                if (type.equals("MAY")) {
                    result = SchemaAttributeType.MAY;
                }
            }
        }
        return result;
    }

    private AttributeSchema createAttributeSchema(String name, DirContext schemaContext) 
        throws NamingException, ClassNotFoundException {
        
        // Get the schema definition
        Attributes attributeSchema = schemaContext.getAttributes("AttributeDefinition/" + name);

        // Get the syntax - ditching any trailing length value that { and whatever follows it
        String syntax = ((String)attributeSchema.get("SYNTAX").get()).split("\\{")[0];

        // Is it binary?
        boolean isBinary=binarySet.contains(syntax);
        
        // Use it to look up the required Java class
        ClassInfo classInfo = syntaxToJavaClass.getClassInfo(syntax);

        // Now we can set the java class
        String javaClassName = null;
        boolean isPrimitive = false;
        boolean isArray = false;
        
        if (classInfo!=null) {
            javaClassName=classInfo.getClassName();
            Class<?> javaClass=Class.forName(classInfo.getFullClassName());
            javaClassName=javaClass.getSimpleName();
            isPrimitive=javaClass.isPrimitive();
            isArray=javaClass.isArray();
        } else {
            if (isBinary) {
                javaClassName="byte[]";
                isPrimitive=false;
                isArray=true;
            } else {
                javaClassName="String";
                isPrimitive=false;
                isArray=false;
            }
        }
        
        return new AttributeSchema(name, syntax, 
                attributeSchema.get("SINGLE-VALUE") == null, 
                isPrimitive, isBinary, isArray, javaClassName);
    }

    // Recursively extract schema from the directory and process it
    private void createObjectClass(Set<String> objectClasses, DirContext schemaContext, ObjectSchema schema)
            throws NamingException, ClassNotFoundException {

        // Super classes
        Set<String> supList = new HashSet<String>();

        // For each of the given object classes
        for (String objectClass : objectClasses) {
            // Add to set of included object classes
            schema.addObjectClass(objectClass);
            
            // Grab the LDAP schema of the object class
            Attributes attributes = schemaContext.getAttributes("ClassDefinition/" + objectClass);
            NamingEnumeration<? extends Attribute> valuesEnumeration = attributes.getAll();

            // Loop through each of the attributes
            while (valuesEnumeration.hasMoreElements()) {
                Attribute currentAttribute = valuesEnumeration.nextElement();

                // Get the attribute name and lower case it (as this is all case indep)
                String currentId = currentAttribute.getID().toUpperCase();
                
                // Is this a MUST, MAY or SUP attribute
                SchemaAttributeType type = getSchemaAttributeType(currentId);

                // Loop through all the values
                NamingEnumeration<?> currentValues = currentAttribute.getAll();
                while (currentValues.hasMoreElements()) {
                    String currentValue = (String)currentValues.nextElement();
                    switch (type) {
                        case SUP:
                            // Its a super class
                            String lowerCased=currentValue.toLowerCase();
                            if (!schema.getObjectClass().contains(lowerCased)) {
                                supList.add(lowerCased);
                            }
                            break;
                        case MUST:
                            // Add must attribute
                            schema.addMust(createAttributeSchema(currentValue, schemaContext));
                            break;
                        case MAY:
                            // Add may attribute
                            schema.addMay(createAttributeSchema(currentValue, schemaContext));
                            break;
                        default:
                            // Nothing to do
                    }
                }
            }

            // Recurse for super classes
            createObjectClass(supList, schemaContext, schema);
        }
    }
}