1 17 package org.apache.ldap.server.schema; 18 19 20 import org.apache.ldap.common.schema.AttributeType; 21 import org.apache.ldap.common.util.JoinIterator; 22 import org.apache.ldap.server.SystemPartition; 23 import org.apache.ldap.server.schema.bootstrap.BootstrapAttributeTypeRegistry; 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 GlobalAttributeTypeRegistry implements AttributeTypeRegistry 38 { 39 40 private final Map byOid; 41 42 private final Map oidToSchema; 43 44 private final OidRegistry oidRegistry; 45 46 private AttributeTypeRegistryMonitor monitor; 47 48 private BootstrapAttributeTypeRegistry bootstrap; 49 50 private SystemPartition systemPartition; 51 52 53 57 58 66 public GlobalAttributeTypeRegistry( SystemPartition systemPartition, 67 BootstrapAttributeTypeRegistry bootstrap, OidRegistry oidRegistry ) 68 { 69 this.byOid = new HashMap (); 70 this.oidToSchema = new HashMap (); 71 this.monitor = new AttributeTypeRegistryMonitorAdapter(); 72 73 this.oidRegistry = oidRegistry; 74 if ( this.oidRegistry == null ) 75 { 76 throw new NullPointerException ( "the OID registry cannot be null" ) ; 77 } 78 79 this.bootstrap = bootstrap; 80 if ( this.bootstrap == null ) 81 { 82 throw new NullPointerException ( "the bootstrap registry cannot be null" ) ; 83 } 84 85 this.systemPartition = systemPartition; 86 if ( this.systemPartition == null ) 87 { 88 throw new NullPointerException ( "the system partition cannot be null" ) ; 89 } 90 } 91 92 93 98 public void setMonitor( AttributeTypeRegistryMonitor monitor ) 99 { 100 this.monitor = monitor; 101 } 102 103 104 108 109 public void register( String schema, AttributeType attributeType ) throws NamingException 110 { 111 if ( byOid.containsKey( attributeType.getOid() ) || 112 bootstrap.hasAttributeType( attributeType.getOid() ) ) 113 { 114 NamingException e = new NamingException ( "attributeType w/ OID " + 115 attributeType.getOid() + " has already been registered!" ); 116 monitor.registerFailed( attributeType, e ); 117 throw e; 118 } 119 120 String [] names = attributeType.getNames(); 121 for ( int ii = 0; ii < names.length; ii++ ) 122 { 123 oidRegistry.register( names[ii], attributeType.getOid() ); 124 } 125 126 oidToSchema.put( attributeType.getOid(), schema ); 127 byOid.put( attributeType.getOid(), attributeType ); 128 monitor.registered( attributeType ); 129 } 130 131 132 public AttributeType lookup( String id ) throws NamingException 133 { 134 id = oidRegistry.getOid( id ); 135 136 if ( ! ( byOid.containsKey( id ) || bootstrap.hasAttributeType( id ) ) ) 137 { 138 NamingException e = new NamingException ( "attributeType w/ OID " 139 + id + " not registered!" ); 140 monitor.lookupFailed( id, e ); 141 throw e; 142 } 143 144 AttributeType attributeType = ( AttributeType ) byOid.get( id ); 145 146 if ( attributeType == null ) 147 { 148 attributeType = bootstrap.lookup( id ); 149 } 150 151 monitor.lookedUp( attributeType ); 152 return attributeType; 153 } 154 155 156 public boolean hasAttributeType( String id ) 157 { 158 try 159 { 160 161 if ( oidRegistry.hasOid( id ) ) 162 { 163 return byOid.containsKey( oidRegistry.getOid( id ) ) || 164 bootstrap.hasAttributeType( id ); 165 } 166 } 167 catch ( NamingException e ) 168 { 169 return false; 170 } 171 172 return false; 173 } 174 175 176 public String getSchemaName( String id ) throws NamingException 177 { 178 id = oidRegistry.getOid( id ); 179 180 if ( oidToSchema.containsKey( id ) ) 181 { 182 return ( String ) oidToSchema.get( id ); 183 } 184 185 if ( bootstrap.getSchemaName( id ) != null ) 186 { 187 return bootstrap.getSchemaName( id ); 188 } 189 190 throw new NamingException ( "OID " + id + " not found in oid to " + 191 "schema name map!" ); 192 } 193 194 195 public Iterator list() 196 { 197 return new JoinIterator( new Iterator[] 198 { byOid.values().iterator(),bootstrap.list() } ); 199 } 200 } 201 | Popular Tags |