1 17 package org.apache.ldap.server.schema; 18 19 20 import org.apache.ldap.common.schema.NameForm; 21 import org.apache.ldap.common.util.JoinIterator; 22 import org.apache.ldap.server.SystemPartition; 23 import org.apache.ldap.server.schema.bootstrap.BootstrapNameFormRegistry; 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 GlobalNameFormRegistry implements NameFormRegistry 38 { 39 40 private final Map byOid; 41 42 private final Map oidToSchema; 43 44 private final OidRegistry oidRegistry; 45 46 private NameFormRegistryMonitor monitor; 47 48 private BootstrapNameFormRegistry bootstrap; 49 50 private SystemPartition systemPartition; 51 52 53 57 58 61 public GlobalNameFormRegistry( SystemPartition systemPartition, 62 BootstrapNameFormRegistry bootstrap, OidRegistry oidRegistry ) 63 { 64 this.byOid = new HashMap (); 65 this.oidToSchema = new HashMap (); 66 this.oidRegistry = oidRegistry; 67 this.monitor = new NameFormRegistryMonitorAdapter(); 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( NameFormRegistryMonitor monitor ) 89 { 90 this.monitor = monitor; 91 } 92 93 94 98 99 public void register( String schema, NameForm dITContentRule ) throws NamingException 100 { 101 if ( byOid.containsKey( dITContentRule.getOid() ) || 102 bootstrap.hasNameForm( dITContentRule.getOid() ) ) 103 { 104 NamingException e = new NamingException ( "dITContentRule w/ OID " + 105 dITContentRule.getOid() + " has already been registered!" ); 106 monitor.registerFailed( dITContentRule, e ); 107 throw e; 108 } 109 110 oidRegistry.register( dITContentRule.getName(), dITContentRule.getOid() ) ; 111 byOid.put( dITContentRule.getOid(), dITContentRule ); 112 oidToSchema.put( dITContentRule.getOid(), schema ); 113 monitor.registered( dITContentRule ); 114 } 115 116 117 public NameForm lookup( String id ) throws NamingException 118 { 119 id = oidRegistry.getOid( id ); 120 121 if ( byOid.containsKey( id ) ) 122 { 123 NameForm dITContentRule = ( NameForm ) byOid.get( id ); 124 monitor.lookedUp( dITContentRule ); 125 return dITContentRule; 126 } 127 128 if ( bootstrap.hasNameForm( id ) ) 129 { 130 NameForm dITContentRule = bootstrap.lookup( id ); 131 monitor.lookedUp( dITContentRule ); 132 return dITContentRule; 133 } 134 135 NamingException e = new NamingException ( "dITContentRule w/ OID " 136 + id + " not registered!" ); 137 monitor.lookupFailed( id, e ); 138 throw e; 139 } 140 141 142 public boolean hasNameForm( String id ) 143 { 144 if ( oidRegistry.hasOid( id ) ) 145 { 146 try 147 { 148 return byOid.containsKey( oidRegistry.getOid( id ) ) || 149 bootstrap.hasNameForm( 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.hasNameForm( 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 |