1 17 package org.apache.ldap.server.schema; 18 19 20 import org.apache.ldap.common.schema.DITStructureRule; 21 import org.apache.ldap.common.util.JoinIterator; 22 import org.apache.ldap.server.SystemPartition; 23 import org.apache.ldap.server.schema.bootstrap.BootstrapDitStructureRuleRegistry; 24 25 import javax.naming.NamingException ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 30 31 37 public class GlobalDitStructureRuleRegistry implements DITStructureRuleRegistry 38 { 39 40 private final Map byOid; 41 42 private final Map oidToSchema; 43 44 private final OidRegistry oidRegistry; 45 46 private DITStructureRuleRegistryMonitor monitor; 47 48 private BootstrapDitStructureRuleRegistry bootstrap; 49 50 private SystemPartition systemPartition; 51 52 53 57 58 61 public GlobalDitStructureRuleRegistry( SystemPartition systemPartition, 62 BootstrapDitStructureRuleRegistry bootstrap, OidRegistry oidRegistry ) 63 { 64 this.byOid = new HashMap (); 65 this.oidToSchema = new HashMap (); 66 this.oidRegistry = oidRegistry; 67 this.monitor = new DITStructureRuleRegistryMonitorAdapter(); 68 69 this.bootstrap = bootstrap; 70 if ( this.bootstrap == null ) 71 { 72 throw new NullPointerException ( "the bootstrap registry cannot be null" ) ; 73 } 74 75 this.systemPartition = systemPartition; 76 if ( this.systemPartition == null ) 77 { 78 throw new NullPointerException ( "the system partition cannot be null" ) ; 79 } 80 } 81 82 83 88 public void setMonitor( DITStructureRuleRegistryMonitor monitor ) 89 { 90 this.monitor = monitor; 91 } 92 93 94 98 99 public void register( String schema, DITStructureRule dITStructureRule ) throws NamingException 100 { 101 if ( byOid.containsKey( dITStructureRule.getOid() ) || 102 bootstrap.hasDITStructureRule( dITStructureRule.getOid() ) ) 103 { 104 NamingException e = new NamingException ( "dITStructureRule w/ OID " + 105 dITStructureRule.getOid() + " has already been registered!" ); 106 monitor.registerFailed( dITStructureRule, e ); 107 throw e; 108 } 109 110 oidRegistry.register( dITStructureRule.getName(), dITStructureRule.getOid() ) ; 111 byOid.put( dITStructureRule.getOid(), dITStructureRule ); 112 oidToSchema.put( dITStructureRule.getOid(), schema ); 113 monitor.registered( dITStructureRule ); 114 } 115 116 117 public DITStructureRule lookup( String id ) throws NamingException 118 { 119 id = oidRegistry.getOid( id ); 120 121 if ( byOid.containsKey( id ) ) 122 { 123 DITStructureRule dITStructureRule = ( DITStructureRule ) byOid.get( id ); 124 monitor.lookedUp( dITStructureRule ); 125 return dITStructureRule; 126 } 127 128 if ( bootstrap.hasDITStructureRule( id ) ) 129 { 130 DITStructureRule dITStructureRule = bootstrap.lookup( id ); 131 monitor.lookedUp( dITStructureRule ); 132 return dITStructureRule; 133 } 134 135 NamingException e = new NamingException ( "dITStructureRule w/ OID " 136 + id + " not registered!" ); 137 monitor.lookupFailed( id, e ); 138 throw e; 139 } 140 141 142 public boolean hasDITStructureRule( String id ) 143 { 144 if ( oidRegistry.hasOid( id ) ) 145 { 146 try 147 { 148 return byOid.containsKey( oidRegistry.getOid( id ) ) || 149 bootstrap.hasDITStructureRule( id ); 150 } 151 catch ( NamingException e ) 152 { 153 return false; 154 } 155 } 156 157 return false; 158 } 159 160 161 public String getSchemaName( String id ) throws NamingException 162 { 163 id = oidRegistry.getOid( id ); 164 165 if ( oidToSchema.containsKey( id ) ) 166 { 167 return ( String ) oidToSchema.get( id ); 168 } 169 170 if ( bootstrap.hasDITStructureRule( id ) ) 171 { 172 return bootstrap.getSchemaName( id ); 173 } 174 175 throw new NamingException ( "OID " + id + " not found in oid to " + 176 "schema name map!" ); 177 } 178 179 180 public Iterator list() 181 { 182 return new JoinIterator( new Iterator[] 183 { byOid.values().iterator(),bootstrap.list() } ); 184 } 185 } 186 | Popular Tags |