1 17 package org.apache.geronimo.directory; 18 19 import java.io.File ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import javax.naming.directory.BasicAttribute ; 28 import javax.naming.directory.BasicAttributes ; 29 import javax.xml.namespace.QName ; 30 31 import org.apache.geronimo.xbeans.geronimo.directory.DirDirectoryConfigurationDocument; 32 import org.apache.geronimo.xbeans.geronimo.directory.DirAttributeDocument.Attribute; 33 import org.apache.geronimo.xbeans.geronimo.directory.DirBootStrapSchemasDocument.BootStrapSchemas; 34 import org.apache.geronimo.xbeans.geronimo.directory.DirContextEntryDocument.ContextEntry; 35 import org.apache.geronimo.xbeans.geronimo.directory.DirDirectoryConfigurationDocument.DirectoryConfiguration; 36 import org.apache.geronimo.xbeans.geronimo.directory.DirIndexedAttributesDocument.IndexedAttributes; 37 import org.apache.geronimo.xbeans.geronimo.directory.DirPartitionDocument.Partition; 38 import org.apache.geronimo.xbeans.geronimo.directory.DirPartitionsDocument.Partitions; 39 import org.apache.ldap.server.configuration.MutableContextPartitionConfiguration; 40 import org.apache.xmlbeans.XmlCursor; 41 import org.apache.xmlbeans.XmlDocumentProperties; 42 import org.apache.xmlbeans.XmlException; 43 import org.apache.xmlbeans.XmlObject; 44 import org.apache.xmlbeans.XmlOptions; 45 46 public class DirectoryConfigurator { 47 48 public DirectoryConfigurator() { 49 } 50 51 public void configure(ClassLoader cl, 52 MutableServerStartupConfiguration startup, File file) 53 throws Exception { 54 DirDirectoryConfigurationDocument doc = parse(file); 55 DirectoryConfiguration dirConfig = doc.getDirectoryConfiguration(); 56 57 Partitions partitions = dirConfig.getPartitions(); 58 if (partitions != null) { 59 Partition partitionList[] = partitions.getPartitionArray(); 60 if (partitionList != null) { 61 Set partition = new HashSet (); 62 for (int i = 0; i < partitionList.length; i++) { 63 partition.add(processPartition(partitionList[i])); 64 } 65 startup.setContextPartitionConfigurations(partition); 66 } 67 } 68 69 BootStrapSchemas schemas = dirConfig.getBootStrapSchemas(); 70 if (schemas != null) { 71 String schemaList[] = schemas.getSchemaArray(); 72 if (schemaList != null) { 73 Set bootStrapSchemas = new HashSet (); 74 for (int i = 0; i < schemaList.length; i++) { 75 Class clazz = cl.loadClass(schemaList[i]); 76 bootStrapSchemas.add(clazz.newInstance()); 77 } 78 startup.setBootstrapSchemas(bootStrapSchemas); 79 } 80 } 81 } 82 83 private MutableContextPartitionConfiguration processPartition( 84 Partition partition) { 85 MutableContextPartitionConfiguration mcpc = new MutableContextPartitionConfiguration(); 86 mcpc.setName(partition.getName()); 87 mcpc.setSuffix(partition.getSuffix()); 88 IndexedAttributes indexedAttributes = partition.getIndexedAttributes(); 89 if (indexedAttributes != null) { 90 String attributeList[] = indexedAttributes 91 .getIndexedAttributeArray(); 92 if (attributeList != null) { 93 Set set = new HashSet (); 94 for (int i = 0; i < attributeList.length; i++) { 95 set.add(attributeList[i]); 96 } 97 mcpc.setIndexedAttributes(set); 98 } 99 } 100 101 ContextEntry contextEntry = partition.getContextEntry(); 102 if (contextEntry != null) { 103 Attribute [] attributeList = contextEntry.getAttributeArray(); 104 if (attributeList != null) { 105 BasicAttributes attrs = new BasicAttributes (true); 106 for (int i = 0; i < attributeList.length; i++) { 107 BasicAttribute attr = new BasicAttribute (attributeList[i] 108 .getId()); 109 String values[] = attributeList[i].getValueArray(); 110 for (int j = 0; j < values.length; j++) { 111 attr.add(values[j]); 112 } 113 attrs.put(attr); 114 } 115 mcpc.setContextEntry(attrs); 116 } 117 } 118 119 return mcpc; 120 } 121 122 private DirDirectoryConfigurationDocument parse(File file) throws Exception { 123 ArrayList errors = new ArrayList (); 124 XmlObject config = XmlObject.Factory.parse(file, 125 createXmlOptions(errors)); 126 127 if (errors.size() != 0) { 128 throw new XmlException(errors.toArray().toString()); 129 } 130 131 if (DirDirectoryConfigurationDocument.type.equals(config.schemaType())) { 132 validateDD(config); 133 return (DirDirectoryConfigurationDocument) config; 134 } 135 136 XmlObject result = config 139 .changeType(DirDirectoryConfigurationDocument.type); 140 validateDD(result); 141 return (DirDirectoryConfigurationDocument) result; 142 } 143 144 private XmlOptions createXmlOptions(Collection errors) { 145 Map NAMESPACE_UPDATES = new HashMap (); 146 NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/directory", 147 "http://geronimo.apache.org/xml/ns/directory-1.0"); 148 XmlOptions options = new XmlOptions(); 149 options.setLoadLineNumbers(); 150 options.setErrorListener(errors); 151 options.setLoadSubstituteNamespaces(NAMESPACE_UPDATES); 152 options.setUseDefaultNamespace(); 153 return options; 154 } 155 156 private void validateDD(XmlObject dd) throws XmlException { 157 XmlOptions xmlOptions = new XmlOptions(); 158 xmlOptions.setDocumentType(DirDirectoryConfigurationDocument.type); 159 xmlOptions.setLoadLineNumbers(); 160 xmlOptions.setUseDefaultNamespace(); 161 Collection errors = new ArrayList (); 162 xmlOptions.setErrorListener(errors); 163 if (!dd.validate(xmlOptions)) { 164 throw new XmlException("Invalid directory descriptor: " + errors 165 + "\nDescriptor: " + dd.toString(), null, errors); 166 } 167 } 169 } 170 | Popular Tags |