1 17 package org.apache.ldap.server.schema; 18 19 20 import org.apache.ldap.server.SystemPartition; 21 import org.apache.ldap.server.schema.bootstrap.BootstrapComparatorRegistry; 22 23 import javax.naming.NamingException ; 24 import java.util.Comparator ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 28 29 35 public class GlobalComparatorRegistry implements ComparatorRegistry 36 { 37 38 private final Map comparators; 39 40 private final Map oidToSchema; 41 42 private ComparatorRegistryMonitor monitor; 43 44 private BootstrapComparatorRegistry bootstrap; 45 46 private SystemPartition systemPartition; 47 48 49 53 54 58 public GlobalComparatorRegistry( SystemPartition systemPartition, 59 BootstrapComparatorRegistry bootstrap ) 60 { 61 this.oidToSchema = new HashMap (); 62 this.comparators = new HashMap (); 63 this.monitor = new ComparatorRegistryMonitorAdapter(); 64 65 SerializableComparator.setRegistry( this ); 67 68 this.bootstrap = bootstrap; 69 if ( this.bootstrap == null ) 70 { 71 throw new NullPointerException ( "the bootstrap registry cannot be null" ) ; 72 } 73 74 this.systemPartition = systemPartition; 75 if ( this.systemPartition == null ) 76 { 77 throw new NullPointerException ( "the system partition cannot be null" ) ; 78 } 79 } 80 81 82 87 public void setMonitor( ComparatorRegistryMonitor monitor ) 88 { 89 this.monitor = monitor; 90 } 91 92 93 97 98 public void register( String schema, String oid, Comparator comparator ) 99 throws NamingException 100 { 101 if ( comparators.containsKey( oid ) || bootstrap.hasComparator( oid ) ) 102 { 103 NamingException e = new NamingException ( "Comparator with OID " 104 + oid + " already registered!" ); 105 monitor.registerFailed( oid, comparator, e ); 106 throw e; 107 } 108 109 oidToSchema.put( oid, schema ); 110 comparators.put( oid, comparator ); 111 monitor.registered( oid, comparator ); 112 } 113 114 115 public Comparator lookup( String oid ) throws NamingException 116 { 117 Comparator c; 118 NamingException e; 119 120 if ( comparators.containsKey( oid ) ) 121 { 122 c = ( Comparator ) comparators.get( oid ); 123 monitor.lookedUp( oid, c ); 124 return c; 125 } 126 127 if ( bootstrap.hasComparator( oid ) ) 128 { 129 c = bootstrap.lookup( oid ); 130 monitor.lookedUp( oid, c ); 131 return c; 132 } 133 134 e = new NamingException ( "Comparator not found for OID: " + oid ); 135 monitor.lookupFailed( oid, e ); 136 throw e; 137 } 138 139 140 public boolean hasComparator( String oid ) 141 { 142 return comparators.containsKey( oid ) || bootstrap.hasComparator( oid ); 143 } 144 145 146 public String getSchemaName( String oid ) throws NamingException 147 { 148 if ( ! Character.isDigit( oid.charAt( 0 ) ) ) 149 { 150 throw new NamingException ( "OID " + oid + " is not a numeric OID" ); 151 } 152 153 if ( oidToSchema.containsKey( oid ) ) 154 { 155 return ( String ) oidToSchema.get( oid ); 156 } 157 158 if ( bootstrap.hasComparator( oid ) ) 159 { 160 return bootstrap.getSchemaName( oid ); 161 } 162 163 throw new NamingException ( "OID " + oid + " not found in oid to " + 164 "schema name map!" ); 165 } 166 } 167 | Popular Tags |