1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 /***
32 * A plain old java object implementation of an AttributeTypeRegistry.
33 *
34 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35 * @version $Rev: 159259 $
36 */
37 public class GlobalAttributeTypeRegistry implements AttributeTypeRegistry
38 {
39 /*** maps an OID to an AttributeType */
40 private final Map byOid;
41 /*** maps an OID to a schema name*/
42 private final Map oidToSchema;
43 /*** the registry used to resolve names to OIDs */
44 private final OidRegistry oidRegistry;
45 /*** monitor notified via callback events */
46 private AttributeTypeRegistryMonitor monitor;
47 /*** the underlying bootstrap registry to delegate on misses to */
48 private BootstrapAttributeTypeRegistry bootstrap;
49 /*** the system partition where we keep attributeType updates */
50 private SystemPartition systemPartition;
51
52
53
54
55
56
57
58 /***
59 * Creates a GlobalAttributeTypeRegistry which accesses data stored within
60 * the system partition and within the bootstrapping registry to service
61 * AttributeType lookup requests.
62 *
63 * @param systemPartition the system database partition under ou=system
64 * @param bootstrap the bootstrapping registry to delegate to
65 */
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 /***
94 * Sets the monitor that is to be notified via callback events.
95 *
96 * @param monitor the new monitor to notify of notable events
97 */
98 public void setMonitor( AttributeTypeRegistryMonitor monitor )
99 {
100 this.monitor = monitor;
101 }
102
103
104
105
106
107
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 }