diff --git a/odm/build.gradle b/odm/build.gradle index 8de817f8..89ef6958 100644 --- a/odm/build.gradle +++ b/odm/build.gradle @@ -18,5 +18,6 @@ dependencies { testCompile project(":spring-ldap-test"), "junit:junit:$junitVersion", - "jdepend:jdepend:2.9.1" + "jdepend:jdepend:2.9.1", + "commons-io:commons-io:2.4" } \ No newline at end of file diff --git a/odm/src/main/java/org/springframework/ldap/odm/tools/SchemaReader.java b/odm/src/main/java/org/springframework/ldap/odm/tools/SchemaReader.java index b45e8bef..8581c358 100755 --- a/odm/src/main/java/org/springframework/ldap/odm/tools/SchemaReader.java +++ b/odm/src/main/java/org/springframework/ldap/odm/tools/SchemaReader.java @@ -1,15 +1,14 @@ package org.springframework.ldap.odm.tools; -import java.util.HashSet; -import java.util.Set; +import org.springframework.ldap.odm.tools.SyntaxToJavaClass.ClassInfo; 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; +import java.util.HashSet; +import java.util.Set; // Processes LDAP Schema /* package */ final class SchemaReader { @@ -55,14 +54,28 @@ import org.springframework.ldap.odm.tools.SyntaxToJavaClass.ClassInfo; return result; } - private AttributeSchema createAttributeSchema(String name, DirContext schemaContext) + 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]; + String syntax = null; + while(syntax == null) { + Attribute syntaxAttribute = attributeSchema.get("SYNTAX"); + if(syntaxAttribute != null) { + syntax = ((String)syntaxAttribute.get()).split("\\{")[0]; + } else { + // Try to recursively retrieve syntax for super definition. + Attribute supAttribute = attributeSchema.get("SUP"); + if(supAttribute == null) { + // Well, at least we tried + throw new IllegalArgumentException("Unable to get syntax definition for attribute " + name); + } else { + attributeSchema = schemaContext.getAttributes("AttributeDefinition/" + supAttribute.get()); + } + } + } // Is it binary? boolean isBinary=binarySet.contains(syntax); diff --git a/odm/src/test/java/org/springframework/ldap/odm/test/TestSchemaToJava.java b/odm/src/test/java/org/springframework/ldap/odm/test/TestSchemaToJava.java index 5b1a5a8f..47251afa 100755 --- a/odm/src/test/java/org/springframework/ldap/odm/test/TestSchemaToJava.java +++ b/odm/src/test/java/org/springframework/ldap/odm/test/TestSchemaToJava.java @@ -1,12 +1,12 @@ package org.springframework.ldap.odm.test; +import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.springframework.core.io.ClassPathResource; import org.springframework.ldap.core.DistinguishedName; @@ -129,19 +129,21 @@ public final class TestSchemaToJava { // 4) Use this OdmManager to read an entry from LDAP and check the results. // @Test - @Ignore public void generate() throws Exception { final String className="Person"; final String packageName="org.springframework.ldap.odm.testclasses"; + File tempFile = File.createTempFile("test-odm-syntax-to-class-map", ".txt"); + FileUtils.copyInputStreamToFile(new ClassPathResource("/syntax-to-class-map.txt").getInputStream(), tempFile); + // Add classes dir to class path - needed for compilation System.setProperty("java.class.path", System.getProperty("java.class.path")+File.pathSeparator+"target/classes"); String[] flags=new String[] { "--url", "ldap://127.0.0.1:"+port, - "--objectclasses", "inetorgperson", - "--syntaxmap", "target/test-classes/syntax-to-class-map.txt", + "--objectclasses", "organizationalperson", + "--syntaxmap", tempFile.getAbsolutePath(), "--class", className, "--package", packageName, "--outputdir", tempDir }; @@ -149,6 +151,8 @@ public final class TestSchemaToJava { // Generate the code using SchemaToJava SchemaToJava.main(flags); + tempFile.delete(); + // Java 5 - we'll use the Java 6 Compiler API once we can drop support for Java 5. String javaDir = calculateOutputDirectory(tempDir, packageName);