KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > directory > DirectoryConfigurator


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.directory;
18
19 import java.io.File JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.naming.directory.BasicAttribute JavaDoc;
28 import javax.naming.directory.BasicAttributes JavaDoc;
29 import javax.xml.namespace.QName JavaDoc;
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 JavaDoc cl,
52             MutableServerStartupConfiguration startup, File JavaDoc file)
53             throws Exception JavaDoc {
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 JavaDoc partition = new HashSet JavaDoc();
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 JavaDoc schemaList[] = schemas.getSchemaArray();
72             if (schemaList != null) {
73                 Set JavaDoc bootStrapSchemas = new HashSet JavaDoc();
74                 for (int i = 0; i < schemaList.length; i++) {
75                     Class JavaDoc 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 JavaDoc attributeList[] = indexedAttributes
91                     .getIndexedAttributeArray();
92             if (attributeList != null) {
93                 Set JavaDoc set = new HashSet JavaDoc();
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 JavaDoc[] attributeList = contextEntry.getAttributeArray();
104             if (attributeList != null) {
105                 BasicAttributes JavaDoc attrs = new BasicAttributes JavaDoc(true);
106                 for (int i = 0; i < attributeList.length; i++) {
107                     BasicAttribute JavaDoc attr = new BasicAttribute JavaDoc(attributeList[i]
108                             .getId());
109                     String JavaDoc 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 JavaDoc file) throws Exception JavaDoc {
123         ArrayList JavaDoc errors = new ArrayList JavaDoc();
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         // If we got here, we will fail due to bad XML. We are doing this get an
137
// explicit error message
138
XmlObject result = config
139                 .changeType(DirDirectoryConfigurationDocument.type);
140         validateDD(result);
141         return (DirDirectoryConfigurationDocument) result;
142     }
143
144     private XmlOptions createXmlOptions(Collection JavaDoc errors) {
145         Map JavaDoc NAMESPACE_UPDATES = new HashMap JavaDoc();
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 JavaDoc errors = new ArrayList JavaDoc();
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         // System.out.println("descriptor: " + dd.toString());
168
}
169 }
170
Popular Tags