Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/LDAPSchema.java

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38 package org.ietf.ldap;
39
40 import java.io.Serializable;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Comparator;
44 import java.util.Enumeration;
45 import java.util.Hashtable;
46
47 /**
48 * This object represents the schema of an LDAP v3 server.
49 * You can use the <CODE>fetchSchema</CODE> method to retrieve
50 * the schema used by a server. (The server must support LDAP v3
51 * and the capability to retrieve the schema over the LDAP protocol.)
52 * <P>
53 *
54 * After you retrieve the schema, you can use this object to get
55 * the object class, attribute type, and matching rule descriptions
56 * in the schema. You can also add your own object classes,
57 * attribute types, and matching rules to the schema.
58 * <P>
59 *
60 * To remove any object classes, attribute types, and matching rules
61 * that you added, call the <CODE>remove</CODE> methods of the
62 * <CODE>LDAPObjectClassSchema</CODE>, <CODE>LDAPAttributeSchema</CODE>,
63 * and <CODE>LDAPMatchingRuleSchema</CODE> classes. (This method is
64 * inherited from the <CODE>LDAPSchemaElement</CODE> class.)
65 * <P>
66 *
67 * The following class is an example of an LDAP client that can
68 * fetch the schema, get and print object class descriptions and
69 * attribute type descriptions, and add object classes and attribute
70 * types to the schema over the LDAP protocol.
71 * <P>
72 *
73 * <PRE>
74 * import org.ietf.ldap.*;
75 * public class TestSchema {
76 * public static void main( String[] args ) {
77 * String HOSTNAME = "ldap.netscape.com";
78 * int PORT_NUMBER = DEFAULT_PORT;
79 * String ROOT_DN = "cn=Directory Manager";
80 * String ROOT_PASSWORD = "23skidoo";
81 *
82 * LDAPConnection ld = new LDAPConnection();
83 *
84 * // Construct a new <CODE>LDAPSchema</CODE> object to get the schema.
85 * LDAPSchema dirSchema = new LDAPSchema();
86 *
87 * try {
88 * // Connect to the server.
89 * ld.connect( HOSTNAME, PORT_NUMBER );
90 *
91 * // Get the schema from the directory.
92 * dirSchema.fetchSchema( ld );
93 *
94 * // Get and print the inetOrgPerson object class description.
95 * LDAPObjectClassSchema objClass = dirSchema.getObjectClass(
96 * "inetOrgPerson" );
97 * if ( objClass != null ) {
98 * System.out.println("inetOrgPerson := "+objClass.toString());
99 * }
100 *
101 * // Get and print the definition of the userPassword attribute.
102 * LDAPAttributeSchema attrType = dirSchema.getAttribute(
103 * "userpassword" );
104 * if ( attrType != null ) {
105 * System.out.println("userPassword := " + attrType.toString());
106 * }
107 *
108 * // Create a new object class definition.
109 * String[] requiredAttrs = {"cn", "mail"};
110 * String[] optionalAttrs = {"sn", "phoneNumber"};
111 * LDAPObjectClassSchema newObjClass =
112 * new LDAPObjectClassSchema( "newInetOrgPerson",
113 * "1.2.3.4.5.6.7",
114 * "top",
115 * "Experiment",
116 * requiredAttrs,
117 * optionalAttrs );
118 *
119 * // Authenticate as root DN to get permissions to edit the schema.
120 * ld.authenticate( ROOT_DN, ROOT_PASSWORD );
121 *
122 * // Add the new object class to the schema.
123 * newObjClass.add( ld );
124 *
125 * // Create a new attribute type "hairColor".
126 * LDAPAttributeSchema newAttrType =
127 * new LDAPAttributeSchema( "hairColor",
128 * "1.2.3.4.5.4.3.2.1",
129 * "Blonde, red, etc",
130 * LDAPAttributeSchema.cis,
131 * false );
132 * // Add a custom qualifier
133 * newObjClass.setQualifier( "X-OWNER", "John Jacobson" );
134 *
135 * // Add the new attribute type to the schema.
136 * newAttrType.add( ld );
137 *
138 * // Fetch the schema again to verify that changes were made.
139 * dirSchema.fetchSchema( ld );
140 *
141 * // Get and print the new attribute type.
142 * newAttrType = dirSchema.getAttribute( "hairColor" );
143 * if ( newAttrType != null ) {
144 * System.out.println("hairColor := " + newAttrType.toString());
145 * }
146 *
147 * // Get and print the new object class.
148 * newObjClass = dirSchema.getObjectClass( "newInetOrgPerson" );
149 * if ( newObjClass != null ) {
150 * System.out.println("newInetOrgPerson := " +newObjClass.toString());
151 * }
152 *
153 * ld.disconnect();
154 *
155 * } catch ( Exception e ) {
156 * System.err.println( e.toString() );
157 * System.exit( 1 );
158 * }
159 *
160 * System.exit( 0 );
161 * }
162 * }
163 * </PRE>
164 *
165 * If you are using the Netscape Directory Server 3.0, you can also
166 * verify that the class and attribute type have been added through
167 * the directory server manager (go to Schema | Edit or View Attributes
168 * or Schema | Edit or View Object Classes).
169 * <P>
170 *
171 * To remove the classes and attribute types added by the example,
172 * see the examples under the <CODE>LDAPSchemaElement</CODE> class.
173 * <P>
174 *
175 * @see org.ietf.ldap.LDAPAttributeSchema
176 * @see org.ietf.ldap.LDAPObjectClassSchema
177 * @see org.ietf.ldap.LDAPMatchingRuleSchema
178 * @see org.ietf.ldap.LDAPSchemaElement
179 * @version 1.0
180 * @author Rob Weltman
181 **/
182 public class LDAPSchema implements Serializable {
183
184 static final long serialVersionUID = -3911737419783579398L;
185
186 /**
187 * Constructs a new <CODE>LDAPSchema</CODE> object.
188 * Once you construct the object, you can get
189 * the schema by calling <CODE>fetchSchema</CODE>.
190 * <P>
191 *
192 * You can also print out the schema by using the
193 * <CODE>main</CODE> method. For example, you can enter
194 * the following command:
195 * <PRE>
196 * java org.ietf.ldap.LDAPSchema myhost.mydomain.com 389
197 * </PRE>
198 *
199 * Note that you need to call <CODE>fetchSchema</CODE>
200 * to get the schema from the server. Constructing the
201 * object does not fetch the schema.
202 * <P>
203 *
204 * @see org.ietf.ldap.LDAPSchema#fetchSchema
205 * @see org.ietf.ldap.LDAPSchema#main
206 */
207 public LDAPSchema() {
208 }
209
210 public LDAPSchema( LDAPEntry entry ) {
211 initialize( entry );
212 }
213
214 /**
215 * Adds a schema element definition to the schema object. If the schema
216 * object already contains an element with the same class and OID as el,
217 * IllegalArgumentException is thrown. Changes are applied to a directory
218 * by calling saveSchema.
219 *
220 * @param el A non-null schema definition
221 */
222 public void add( LDAPSchemaElement el ) {
223 add( el, true );
224 }
225
226 /**
227 * Adds a schema element definition to the schema object. If the schema
228 * object already contains an element with the same class and OID as el,
229 * IllegalArgumentException is thrown. Changes are applied to a directory
230 * by calling saveSchema.
231 *
232 * @param el A non-null schema definition
233 * @param isChange <CODE>true</CODE> if called by a client, meaning that
234 * a directory is to be changed and not just this object
235 */
236 private void add( LDAPSchemaElement el,
237 boolean isChange ) {
238 String ID = el.getID();
239 if ( ID != null ) {
240 LDAPSchemaElement oldEl = _IDToElement.get( ID );
241 if ( (oldEl != null) && namesTheSame( oldEl, el ) ) {
242 throw new IllegalArgumentException( "Schema element already " +
243 "defined for " + ID +
244 "; old: " +
245 oldEl +
246 " ; new: " + el );
247 }
248 _IDToElement.put( ID, el );
249 }
250 if ( isChange ) {
251 _changeList.add( getModificationForElement( el,
252 LDAPModification.ADD,
253 null ) );
254 }
255 String name;
256 try {
257 name = (el.getNames().length > 0) ?
258 el.getNames()[0].toLowerCase() : "";
259 } catch ( Exception e ) {
260 System.err.println( el );
261 e.printStackTrace();
262 return;
263 }
264 if ( el instanceof LDAPAttributeSchema ) {
265 _attributes.put( name, (LDAPAttributeSchema) el );
266 } else if ( el instanceof LDAPObjectClassSchema ) {
267 _objectClasses.put( name, (LDAPObjectClassSchema) el );
268 } else if ( el instanceof LDAPMatchingRuleSchema ) {
269 _matchingRules.put( name, (LDAPMatchingRuleSchema) el );
270 } else if ( el instanceof LDAPMatchingRuleUseSchema ) {
271 _matchingRuleUses.put( name, (LDAPMatchingRuleUseSchema) el );
272 } else if ( el instanceof LDAPNameFormSchema ) {
273 _nameForms.put( name, (LDAPNameFormSchema) el );
274 } else if ( el instanceof LDAPSyntaxSchema ) {
275 if ( name.length() < 1 ) {
276 name = el.getID();
277 }
278 _syntaxes.put( name, (LDAPSyntaxSchema) el );
279 } else if ( el instanceof LDAPDITContentRuleSchema ) {
280 _contentRules.put( name, (LDAPDITContentRuleSchema) el );
281 } else if ( el instanceof LDAPDITStructureRuleSchema ) {
282 _structureRulesByName.put( name, (LDAPDITStructureRuleSchema) el );
283 _structureRulesById.put( ((LDAPDITStructureRuleSchema)el).getRuleID(), (LDAPDITStructureRuleSchema) el );
284 }
285 }
286
287 /**
288 * Retrieve the entire schema from the root of a Directory Server
289 *
290 * @param ld an active connection to a Directory Server
291 * @exception LDAPException on failure.
292 */
293 public void fetchSchema( LDAPConnection ld ) throws LDAPException {
294 fetchSchema( ld, "" );
295 }
296
297 /**
298 * Retrieve the schema for a specific entry
299 *
300 * @param ld an active connection to a Directory Server
301 * @param dn the entry for which to fetch schema
302 * @exception LDAPException on failure.
303 */
304 public void fetchSchema( LDAPConnection ld, String dn )
305 throws LDAPException {
306 clear();
307 /* Find the subschemasubentry value for this DN */
308 String entryName = getSchemaDN( ld, dn );
309
310 /* Get the entire schema definition entry */
311 LDAPEntry entry = readSchema( ld, entryName );
312 initialize( entry );
313 }
314
315 /**
316 * Get an enumeration of the names of the attribute types in this schema.
317 * @return an enumeration of attribute names (all lower-case).
318 */
319 public Enumeration<String> getAttributeNames() {
320 return _attributes.keys();
321 }
322
323 /**
324 * Gets the definition of the attribute type with the specified name.
325 * @param name name of the attribute type to find
326 * @return an <CODE>LDAPAttributeSchema</CODE> object representing
327 * the attribute type definition, or <CODE>null</CODE> if not found.
328 */
329 public LDAPAttributeSchema getAttributeSchema( String name ) {
330 return _attributes.get( name.toLowerCase() );
331 }
332
333 /**
334 * Gets an enumeration of the attribute type definitions in this schema.
335 * @return an enumeration of attribute type definitions.
336 */
337 public Enumeration<LDAPAttributeSchema> getAttributeSchemas() {
338 return _attributes.elements();
339 }
340
341 /**
342 * Get an enumeration of the names of the content rules in this schema.
343 * @return an enumeration of names of the content rule objects
344 */
345 public Enumeration<String> getDITContentRuleNames() {
346 return _contentRules.keys();
347 }
348
349 /**
350 * Gets the definition of a content rule with the specified name.
351 * @param name name of the rule to find
352 * @return an <CODE>LDAPDITContentRuleSchema</CODE> object representing
353 * the rule, or <CODE>null</CODE> if not found.
354 */
355 public LDAPDITContentRuleSchema getDITContentRuleSchema( String name ) {
356 return _contentRules.get( name.toLowerCase() );
357 }
358
359 /**
360 * Get an enumeration of the content rules in this schema.
361 * @return an enumeration of content rule objects
362 */
363 public Enumeration<LDAPDITContentRuleSchema> getDITContentRuleSchemas() {
364 return _contentRules.elements();
365 }
366
367 /**
368 * Get an enumeration of the names of the structure rules in this schema.
369 * @return an enumeration of names of the structure rule objects
370 */
371 public Enumeration<String> getDITStructureRuleNames() {
372 return _structureRulesByName.keys();
373 }
374
375 /**
376 * Gets the definition of a structure rule with the specified name.
377 * @param name name of the rule to find
378 * @return an <CODE>LDAPDITStructureRuleSchema</CODE> object representing
379 * the rule, or <CODE>null</CODE> if not found.
380 */
381 public LDAPDITStructureRuleSchema getDITStructureRuleSchema( String name ) {
382 return _structureRulesByName.get(name.toLowerCase() );
383 }
384
385 /**
386 * Gets the definition of a structure rule with the specified name.
387 * @param ID ID of the rule to find
388 * @return an <CODE>LDAPDITStructureRuleSchema</CODE> object representing
389 * the rule, or <CODE>null</CODE> if not found.
390 */
391 public LDAPDITStructureRuleSchema getDITStructureRuleSchema( int ID ) {
392 return _structureRulesById.get( ID );
393 }
394
395 /**
396 * Get an enumeration of the structure rules in this schema.
397 * @return an enumeration of structure rule objects
398 */
399 public Enumeration<LDAPDITStructureRuleSchema> getDITStructureRuleSchemas() {
400 return _structureRulesByName.elements();
401 }
402
403 /**
404 * Get an enumeration of the names of the matching rules in this schema.
405 * @return an enumeration of matching rule names (all lower-case).
406 */
407 public Enumeration<String> getMatchingRuleNames() {
408 return _matchingRules.keys();
409 }
410
411 /**
412 * Gets the definition of a matching rule with the specified name.
413 * @param name name of the matching rule to find
414 * @return an <CODE>LDAPMatchingRuleSchema</CODE> object representing
415 * the matching rule definition, or <CODE>null</CODE> if not found.
416 */
417 public LDAPMatchingRuleSchema getMatchingRuleSchema( String name ) {
418 return _matchingRules.get( name.toLowerCase() );
419 }
420
421 /**
422 * Gets an enumeration ofthe matching rule definitions in this schema.
423 * @return an enumeration of matching rule definitions.
424 */
425 public Enumeration<LDAPMatchingRuleSchema> getMatchingRuleSchemas() {
426 return _matchingRules.elements();
427 }
428
429 /**
430 * Get an enumeration of the names of the matching rule uses in this schema.
431 * @return an enumeration of matching rule use names (all lower-case).
432 */
433 public Enumeration<String> getMatchingRuleUseNames() {
434 return _matchingRuleUses.keys();
435 }
436
437 /**
438 * Gets the definition of a matching rule use with the specified name.
439 * @param name name of the matching rule use to find
440 * @return an <CODE>LDAPMatchingRuleUseSchema</CODE> object representing
441 * the matching rule definition, or <CODE>null</CODE> if not found.
442 */
443 public LDAPMatchingRuleUseSchema getMatchingRuleUseSchema( String name ) {
444 return _matchingRuleUses.get( name.toLowerCase() );
445 }
446
447 /**
448 * Gets an enumeration ofthe matching rule use definitions in this schema.
449 * @return an enumeration of matching rule use definitions.
450 */
451 public Enumeration<LDAPMatchingRuleUseSchema> getMatchingRuleUseSchemas() {
452 return _matchingRuleUses.elements();
453 }
454
455 /**
456 * Get an enumeration of the names of the name forms in this schema.
457 * @return an enumeration of names of name form objects
458 */
459 public Enumeration<String> getNameFormNames() {
460 return _nameForms.keys();
461 }
462
463 /**
464 * Gets the definition of a name form with the specified name.
465 * @param name name of the name form to find
466 * @return an <CODE>LDAPNameFormSchema</CODE> object representing
467 * the syntax definition, or <CODE>null</CODE> if not found.
468 */
469 public LDAPNameFormSchema getNameFormSchema( String name ) {
470 return _nameForms.get( name.toLowerCase() );
471 }
472
473 /**
474 * Get an enumeration of the name forms in this schema.
475 * @return an enumeration of name form objects
476 */
477 public Enumeration<LDAPNameFormSchema> getNameFormSchemas() {
478 return _nameForms.elements();
479 }
480
481 /**
482 * Get an enumeration of the names of the object classes in this schema.
483 * @return an enumeration of object class names (all lower-case).
484 */
485 public Enumeration<String> getObjectClassNames() {
486 return _objectClasses.keys();
487 }
488
489 /**
490 * Gets the definition of the object class with the specified name.
491 * @param name name of the object class to find
492 * @return an <CODE>LDAPObjectClassSchema</CODE> object representing
493 * the object class definition, or <CODE>null</CODE> if not found.
494 */
495 public LDAPObjectClassSchema getObjectClassSchema( String name ) {
496 return _objectClasses.get( name.toLowerCase() );
497 }
498
499 /**
500 * Gets an enumeration ofthe object class definitions in this schema.
501 * @return an enumeration of object class definitions.
502 */
503 public Enumeration<LDAPObjectClassSchema> getObjectClassSchemas() {
504 return _objectClasses.elements();
505 }
506
507 /**
508 * Get an enumeration of the names of the syntaxes in this schema.
509 * @return an enumeration of syntax names (all lower-case).
510 */
511 public Enumeration<String> getSyntaxNames() {
512 return _syntaxes.keys();
513 }
514
515 /**
516 * Gets the definition of a syntax with the specified name.
517 * @param name name of the syntax to find
518 * @return an <CODE>LDAPSyntaxSchema</CODE> object representing
519 * the syntax definition, or <CODE>null</CODE> if not found.
520 */
521 public LDAPSyntaxSchema getSyntaxSchema( String name ) {
522 return _syntaxes.get( name.toLowerCase() );
523 }
524
525 /**
526 * Get an enumeration of the syntaxes in this schema.
527 * @return an enumeration of syntax objects
528 */
529 public Enumeration<LDAPSyntaxSchema> getSyntaxSchemas() {
530 return _syntaxes.elements();
531 }
532
533 /**
534 * Modifies a schema element definition of the schema object. If the
535 * schema object does not contain an element with the same class and OID
536 * as el, it is added. Changes are applied to a directory by calling
537 * saveSchema.
538 *
539 * @param el A non-null schema definition
540 */
541 public void modify( LDAPSchemaElement el ) {
542 modify( el, true );
543 }
544
545 /**
546 * Modifies a schema element definition of the schema object. If the
547 * schema object does not contain an element with the same class and OID
548 * as el, it is added. Changes are applied to a directory by calling
549 * saveSchema.
550 *
551 * @param el A non-null schema definition
552 * @param isChange <CODE>true</CODE> if called by a client, meaning that
553 * a directory is to be changed and not just this object
554 */
555 private void modify( LDAPSchemaElement el,
556 boolean isChange ) {
557 String ID = el.getID();
558 if ( isChange ) {
559 // Remove if already present and then add
560 LDAPSchemaElement oldEl = ( ID != null ) ?
561 (LDAPSchemaElement)_IDToElement.get( ID ) : null;
562 if ( oldEl != null ) {
563 _changeList.add(
564 getModificationForElement( oldEl,
565 LDAPModification.DELETE,
566 null ) );
567 }
568 _changeList.add(
569 getModificationForElement( el,
570 LDAPModification.ADD,
571 null ) );
572 }
573 if ( ID != null ) {
574 _IDToElement.put( ID, el );
575 }
576 String name = (el.getNames().length > 0) ?
577 el.getNames()[0].toLowerCase() : "";
578 if ( el instanceof LDAPAttributeSchema ) {
579 _attributes.put( name, (LDAPAttributeSchema) el );
580 } else if ( el instanceof LDAPObjectClassSchema ) {
581 _objectClasses.put( name, (LDAPObjectClassSchema) el );
582 } else if ( el instanceof LDAPMatchingRuleSchema ) {
583 _matchingRules.put( name, (LDAPMatchingRuleSchema) el );
584 } else if ( el instanceof LDAPMatchingRuleUseSchema ) {
585 _matchingRuleUses.put( name, (LDAPMatchingRuleUseSchema) el );
586 } else if ( el instanceof LDAPNameFormSchema ) {
587 _nameForms.put( name, (LDAPNameFormSchema) el );
588 } else if ( el instanceof LDAPSyntaxSchema ) {
589 if ( name.length() < 1 ) {
590 name = el.getID();
591 }
592 _syntaxes.put( name, (LDAPSyntaxSchema) el );
593 } else if ( el instanceof LDAPDITContentRuleSchema ) {
594 _contentRules.put( name, (LDAPDITContentRuleSchema) el );
595 } else if ( el instanceof LDAPDITStructureRuleSchema ) {
596 _structureRulesByName.put( name, (LDAPDITStructureRuleSchema) el );
597 _structureRulesById.put( ((LDAPDITStructureRuleSchema)el).getRuleID(), (LDAPDITStructureRuleSchema) el );
598 }
599 }
600
601 /**
602 * Removes a schema element definition of the schema object. If the
603 * schema object does not contain an element with the same class and OID
604 * as el, this is a noop. Changes are applied to a directory by calling
605 * saveSchema.
606 *
607 * @param el A non-null schema definition
608 */
609 public void remove( LDAPSchemaElement el ) {
610 remove( el, true );
611 }
612
613 /**
614 * Removes a schema element definition of the schema object. If the
615 * schema object does not contain an element with the same class and OID
616 * as el, this is a noop. Changes are applied to a directory by calling
617 * saveSchema.
618 *
619 * @param el A non-null schema definition
620 * @param isChange <CODE>true</CODE> if called by a client, meaning that
621 * a directory is to be changed and not just this object
622 */
623 private void remove( LDAPSchemaElement el, boolean isChange ) {
624 String ID = el.getID();
625 if ( isChange ) {
626 // Remove it if present
627 if ( (ID != null) && _IDToElement.containsKey( ID ) ) {
628 _changeList.add(
629 getModificationForElement( el,
630 LDAPModification.DELETE,
631 null ) );
632 }
633 }
634 if ( ID != null ) {
635 _IDToElement.remove( ID );
636 }
637 String name = (el.getNames().length > 0) ?
638 el.getNames()[0].toLowerCase() : "";
639 if ( el instanceof LDAPAttributeSchema ) {
640 _attributes.remove( name );
641 } else if ( el instanceof LDAPObjectClassSchema ) {
642 _objectClasses.remove( name );
643 } else if ( el instanceof LDAPMatchingRuleSchema ) {
644 _matchingRules.remove( name );
645 } else if ( el instanceof LDAPMatchingRuleUseSchema ) {
646 _matchingRuleUses.remove( name );
647 } else if ( el instanceof LDAPNameFormSchema ) {
648 _nameForms.remove( name );
649 } else if ( el instanceof LDAPSyntaxSchema ) {
650 if ( name.length() < 1 ) {
651 name = el.getID();
652 }
653 _syntaxes.remove( name );
654 } else if ( el instanceof LDAPDITContentRuleSchema ) {
655 _contentRules.remove( name );
656 } else if ( el instanceof LDAPDITStructureRuleSchema ) {
657 _structureRulesByName.remove( name );
658 _structureRulesById.remove( ((LDAPDITStructureRuleSchema)el).getRuleID() );
659 }
660 }
661
662 /**
663 * Save the schema changes to the root of a Directory Server
664 *
665 * @param ld an active connection to a Directory Server
666 * @exception LDAPException on failure.
667 */
668 public void saveSchema( LDAPConnection ld ) throws LDAPException {
669 saveSchema( ld, "" );
670 }
671
672 /**
673 * Save the schema changes to a specific entry
674 *
675 * @param ld an active connection to a Directory Server
676 * @param dn the entry at which to save schema
677 * @exception LDAPException on failure.
678 */
679 public void saveSchema( LDAPConnection ld, String dn )
680 throws LDAPException {
681
682 // Get array of changes to be made
683 LDAPModification[] mods =
684 _changeList.toArray( new LDAPModification[0] );
685 if ( mods.length < 1 ) {
686 return;
687 }
688
689 /* Find the subschemasubentry value for this DN */
690 String entryName = getSchemaDN( ld, dn );
691 /* Make the changes; exception thrown on error */
692 ld.modify( entryName, mods );
693 /* If we got this far, the changes were saved and we can reset
694 the change list */
695 _changeList.clear();
696 }
697
698 /**
699 * Displays the schema (including the descriptions of its object
700 * classes, attribute types, and matching rules) in an easily
701 * readable format (not the same as the format expected by
702 * an LDAP server).
703 * @return a string containing the schema in printable format.
704 */
705 public String toString() {
706 String s = "Object classes:\n";
707 Enumeration<LDAPObjectClassSchema> en1 = getObjectClassSchemas();
708 while( en1.hasMoreElements() ) {
709 s += en1.nextElement().toString();
710 s += '\n';
711 }
712 s += "Attributes:\n";
713 Enumeration<LDAPAttributeSchema> en2 = getAttributeSchemas();
714 while( en2.hasMoreElements() ) {
715 s += en2.nextElement().toString();
716 s += '\n';
717 }
718 s += "Matching rules:\n";
719 Enumeration<LDAPMatchingRuleSchema> en3 = getMatchingRuleSchemas();
720 while( en3.hasMoreElements() ) {
721 s += en3.nextElement().toString();
722 s += '\n';
723 }
724 s += "Syntaxes:\n";
725 Enumeration<LDAPSyntaxSchema> en4 = getSyntaxSchemas();
726 while( en4.hasMoreElements() ) {
727 s += en4.nextElement().toString();
728 s += '\n';
729 }
730 return s;
731 }
732
733 /**
734 * Removes all definitions
735 */
736 void clear() {
737 _attributes.clear();
738 _objectClasses.clear();
739 _matchingRules.clear();
740 _matchingRuleUses.clear();
741 _nameForms.clear();
742 _syntaxes.clear();
743 _contentRules.clear();
744 _structureRulesByName.clear();
745 _structureRulesById.clear();
746 _IDToElement.clear();
747 _changeList.clear();
748 }
749
750 /**
751 * Creates a modification object corresponding to a schema element and
752 * a change type
753 *
754 * @param el The schema element
755 * @param op Type of modification to make
756 * @param ld Optional connection to server where change is to be made
757 */
758 private static LDAPModification getModificationForElement(
759 LDAPSchemaElement el,
760 int op,
761 LDAPConnection ld ) {
762 boolean quotingBug;
763 try {
764 quotingBug = (ld != null) ?
765 !LDAPSchema.isAttributeSyntaxStandardsCompliant( ld ) : false;
766 } catch ( LDAPException e ) {
767 quotingBug = false;
768 }
769 LDAPAttribute attr = new LDAPAttribute( el.getAttributeName(),
770 el.getValue( quotingBug ) );
771 return new LDAPModification( op, attr );
772 }
773
774 /**
775 * Retrieve the DN of the schema definitions for a specific entry.
776 * @param ld an active connection to a Directory Server
777 * @param dn the entry for which to fetch schema
778 * @exception LDAPException on failure.
779 */
780 private static String getSchemaDN( LDAPConnection ld,
781 String dn )
782 throws LDAPException {
783 if ( (ld == null) || !ld.isConnected() ) {
784 throw new LDAPException( "No connection", LDAPException.OTHER );
785 }
786 String[] attrs = { "subschemasubentry" };
787 LDAPEntry entry = ld.read( dn, attrs );
788 if ( entry == null ) {
789 throw new LDAPException( "", LDAPException.NO_SUCH_OBJECT );
790 }
791 LDAPAttribute attr = entry.getAttribute( attrs[0] );
792 String entryName = "cn=schema";
793 if ( attr != null ) {
794 Enumeration<String> en = attr.getStringValues();
795 if ( en.hasMoreElements() ) {
796 entryName = en.nextElement();
797 }
798 }
799 return entryName;
800 }
801
802 /**
803 * Extracts all schema elements from subschema entry
804 *
805 * @param entry entry containing schema definitions
806 */
807 protected void initialize( LDAPEntry entry ) {
808 /* Get all object class definitions */
809 LDAPAttribute attr = entry.getAttribute( "objectclasses" );
810 Enumeration<String> en;
811 if ( attr != null ) {
812 en = attr.getStringValues();
813 while( en.hasMoreElements() ) {
814 LDAPObjectClassSchema sch =
815 new LDAPObjectClassSchema( en.nextElement() );
816 add( sch, false );
817 }
818 }
819
820 /* Get all attribute definitions */
821 attr = entry.getAttribute( "attributetypes" );
822 if ( attr != null ) {
823 en = attr.getStringValues();
824 while( en.hasMoreElements() ) {
825 LDAPAttributeSchema sch =
826 new LDAPAttributeSchema( en.nextElement() );
827 add( sch, false );
828 }
829 }
830
831 /* Get all syntax definitions */
832 attr = entry.getAttribute( "ldapsyntaxes" );
833 if ( attr != null ) {
834 en = attr.getStringValues();
835 while( en.hasMoreElements() ) {
836 LDAPSyntaxSchema sch =
837 new LDAPSyntaxSchema( en.nextElement() );
838 add( sch, false );
839 }
840 }
841
842 /* Get all structure rule definitions */
843 attr = entry.getAttribute( "ldapditstructurerules" );
844 if ( attr != null ) {
845 en = attr.getStringValues();
846 while( en.hasMoreElements() ) {
847 LDAPDITStructureRuleSchema sch =
848 new LDAPDITStructureRuleSchema(
849 en.nextElement() );
850 add( sch, false );
851 }
852 }
853
854 /* Get all content rule definitions */
855 attr = entry.getAttribute( "ldapditcontentrules" );
856 if ( attr != null ) {
857 en = attr.getStringValues();
858 while( en.hasMoreElements() ) {
859 LDAPDITContentRuleSchema sch =
860 new LDAPDITContentRuleSchema(
861 en.nextElement() );
862 add( sch, false );
863 }
864 }
865
866 /* Get all name form definitions */
867 attr = entry.getAttribute( "nameforms" );
868 if ( attr != null ) {
869 en = attr.getStringValues();
870 while( en.hasMoreElements() ) {
871 LDAPNameFormSchema sch =
872 new LDAPNameFormSchema(
873 en.nextElement() );
874 add( sch, false );
875 }
876 }
877
878 /* Matching rules are tricky, because we have to match up a
879 rule with its use. First get all the uses. */
880 Hashtable<String, String> h = new Hashtable<>();
881 attr = entry.getAttribute( "matchingruleuse" );
882 if ( attr != null ) {
883 en = attr.getStringValues();
884 while( en.hasMoreElements() ) {
885 String use = en.nextElement();
886 LDAPMatchingRuleSchema sch =
887 new LDAPMatchingRuleSchema( null, use );
888 h.put( sch.getID(), use );
889 }
890 }
891 /* Now get the rules, and assign uses to them */
892 attr = entry.getAttribute( "matchingrules" );
893 if ( attr != null ) {
894 en = attr.getStringValues();
895 while( en.hasMoreElements() ) {
896 String raw = en.nextElement();
897 LDAPMatchingRuleSchema sch =
898 new LDAPMatchingRuleSchema( raw, null );
899 String use = h.get( sch.getID() );
900 if ( use != null )
901 sch = new LDAPMatchingRuleSchema( raw, use );
902 add( sch, false );
903 }
904 }
905 }
906
907 /**
908 * Read one attribute definition from a server to determine if
909 * attribute syntaxes are quoted (a bug, present in Netscape
910 * and Novell servers).
911 * @param ld an active connection to a Directory Server
912 * @return <CODE>true</CODE> if standards-compliant.
913 * @exception LDAPException on failure.
914 */
915 static boolean isAttributeSyntaxStandardsCompliant( LDAPConnection ld )
916 throws LDAPException {
917
918 /* Check if this has already been investigated */
919 String schemaBug = (String)ld.getProperty( LDAPConnection.SCHEMA_BUG_PROPERTY );
920 if ( schemaBug != null ) {
921 return schemaBug.equalsIgnoreCase( "standard" );
922 }
923
924 boolean compliant = true;
925 /* Get the schema definitions for attributes */
926 String entryName = getSchemaDN( ld, "" );
927 String[] attrs = { "attributetypes" };
928 LDAPEntry entry = ld.read( entryName, attrs );
929 /* Get all attribute definitions, and check the first one */
930 LDAPAttribute attr = entry.getAttribute( "attributetypes" );
931 if ( attr != null ) {
932 Enumeration<String> en = attr.getStringValues();
933 if( en.hasMoreElements() ) {
934 compliant = !isSyntaxQuoted( en.nextElement() );
935 }
936 }
937 ld.setProperty( LDAPConnection.SCHEMA_BUG_PROPERTY, compliant ? "standard" :
938 "NetscapeBug" );
939 return compliant;
940 }
941
942 /**
943 * Parses an attribute schema definition to see if the SYNTAX value
944 * is quoted. It shouldn't be (according to RFC 2252), but it is for
945 * some LDAP servers. It will either be:<BR>
946 * <CODE>SYNTAX 1.3.6.1.4.1.1466.115.121.1.15</CODE> or<BR>
947 * <CODE>SYNTAX '1.3.6.1.4.1.1466.115.121.1.15'</CODE>
948 * @param raw Definition of the attribute type in the
949 * AttributeTypeDescription format.
950 */
951 static boolean isSyntaxQuoted( String raw ) {
952 int ind = raw.indexOf( " SYNTAX " );
953 if ( ind >= 0 ) {
954 ind += 8;
955 int l = raw.length() - ind;
956 // Extract characters
957 char[] ch = new char[l];
958 raw.getChars( ind, raw.length(), ch, 0 );
959 ind = 0;
960 // Skip to ' or start of syntax value
961 while( (ind < ch.length) && (ch[ind] == ' ') ) {
962 ind++;
963 }
964 if ( ind < ch.length ) {
965 return ( ch[ind] == '\'' );
966 }
967 }
968 return false;
969 }
970
971 /**
972 * Compares to schema elements for identical names; order and case are
973 * ignored
974 *
975 * @param el1 First element to compare
976 * @param el2 Second element to compare
977 * @return <CODE>true</CODE> if the elements have the same names
978 */
979 private static boolean namesTheSame( LDAPSchemaElement el1,
980 LDAPSchemaElement el2 ) {
981 // Don't bother if either element is null
982 if ( (el1 == null) || (el2 == null) ) {
983 return false;
984 }
985 String[] names1 = el1.getNames();
986 String[] names2 = el2.getNames();
987 if ( (names1 == null) ^ (names2 == null) ) {
988 return false;
989 }
990 // If neither have any names, return true
991 if ( names1 == null ) {
992 return true;
993 }
994 if ( names1.length != names2.length ) {
995 return false;
996 }
997 if ( names1.length == 0 ) {
998 return true;
999 }
1000 if ( names1.length == 1 ) {
1001 return ( names1[0].equalsIgnoreCase( names2[0] ) );
1002 }
1003 Comparator<String> comp = new Comparator<String>() {
1004 public int compare( String o1, String o2 ) {
1005 return o1.compareToIgnoreCase( o2 );
1006 }
1007 public boolean equals( Object obj ) {
1008 return false;
1009 }
1010 };
1011 Arrays.sort( names1, comp );
1012 Arrays.sort( names2, comp );
1013 for( int i = 0; i < names1.length; i++ ) {
1014 if ( !names1[i].equalsIgnoreCase( names2[i] ) ) {
1015 return false;
1016 }
1017 }
1018 return true;
1019 }
1020
1021 /**
1022 * Helper for "main" to print out schema elements.
1023 *
1024 * @param en enumeration of schema elements
1025 */
1026 private static void printEnum( Enumeration<? extends LDAPSchemaElement> en ) {
1027 while( en.hasMoreElements() ) {
1028 LDAPSchemaElement s = en.nextElement();
1029 System.out.println( " " + s );
1030 }
1031 }
1032
1033 /**
1034 * Reads the schema definitions from a particular subschema entry
1035 */
1036 private static LDAPEntry readSchema( LDAPConnection ld,
1037 String dn,
1038 String[] attrs )
1039 throws LDAPException {
1040 LDAPSearchResults results = ld.search (dn, LDAPConnection.SCOPE_BASE,
1041 "objectclass=subschema",
1042 attrs, false);
1043 if ( !results.hasMore() ) {
1044 throw new LDAPException( "Cannot read schema",
1045 LDAPException.INSUFFICIENT_ACCESS_RIGHTS );
1046 }
1047 return results.next();
1048 }
1049
1050 /**
1051 * Reads the schema definitions from a particular subschema entry
1052 */
1053 private static LDAPEntry readSchema( LDAPConnection ld,
1054 String dn )
1055 throws LDAPException {
1056 return readSchema( ld, dn, new String[] { "*", "ldapSyntaxes",
1057 "matchingRules", "attributeTypes", "objectClasses" } );
1058 }
1059
1060 /**
1061 * Fetch the schema from the LDAP server at the specified
1062 * host and port, and print out the schema (including descriptions
1063 * of its object classes, attribute types, and matching rules).
1064 * The schema is printed in an easily readable format (not the
1065 * same as the format expected by an LDAP server). For example,
1066 * you can enter the following command to print the schema:
1067 * <PRE>
1068 * java org.ietf.ldap.LDAPSchema myhost.mydomain.com 389
1069 * </PRE>
1070 *
1071 * @param args the host name and the port number of the LDAP server
1072 * (for example, <CODE>org.ietf.ldap.LDAPSchema directory.netscape.com
1073 * 389</CODE>)
1074 */
1075 public static void main( String[] args ) {
1076 if ( args.length < 2 ) {
1077 System.err.println( "Usage: org.ietf.ldap.LDAPSchema HOST PORT" );
1078 System.exit(1 );
1079 }
1080 int port = Integer.parseInt( args[1] );
1081 LDAPConnection ld = new LDAPConnection();
1082 try {
1083 ld.connect( args[0], port );
1084 LDAPSchema schema = new LDAPSchema();
1085 schema.fetchSchema( ld );
1086 ld.disconnect();
1087 System.out.println( "Object classes: " );
1088 printEnum( schema.getObjectClassSchemas() );
1089 System.out.println( "\nAttributes: " );
1090 printEnum( schema.getAttributeSchemas() );
1091 System.out.println( "\nMatching rules: " );
1092 printEnum( schema.getMatchingRuleSchemas() );
1093 System.out.println( "\nSyntaxes: " );
1094 printEnum( schema.getSyntaxSchemas() );
1095 System.exit( 0 );
1096 } catch ( LDAPException e ) {
1097 System.err.println( e );
1098 }
1099 }
1100
1101 private Hashtable<String, LDAPObjectClassSchema> _objectClasses = new Hashtable<>();
1102 private Hashtable<String, LDAPAttributeSchema> _attributes = new Hashtable<>();
1103 private Hashtable<String, LDAPMatchingRuleSchema> _matchingRules = new Hashtable<>();
1104 private Hashtable<String, LDAPMatchingRuleUseSchema> _matchingRuleUses = new Hashtable<>();
1105 private Hashtable<String, LDAPSyntaxSchema> _syntaxes = new Hashtable<>();
1106 private Hashtable<String, LDAPDITStructureRuleSchema> _structureRulesByName = new Hashtable<>();
1107 private Hashtable<Integer, LDAPDITStructureRuleSchema> _structureRulesById = new Hashtable<>();
1108 private Hashtable<String, LDAPDITContentRuleSchema> _contentRules = new Hashtable<>();
1109 private Hashtable<String, LDAPNameFormSchema> _nameForms = new Hashtable<>();
1110 private Hashtable<String, LDAPSchemaElement> _IDToElement = new Hashtable<>();
1111 private ArrayList<LDAPModification> _changeList = new ArrayList<>();
1112 }