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.ObjectClass;
21 import org.apache.ldap.common.util.JoinIterator;
22 import org.apache.ldap.server.SystemPartition;
23 import org.apache.ldap.server.schema.bootstrap.BootstrapObjectClassRegistry;
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 ObjectClassRegistry.
33 *
34 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35 * @version $Rev: 159259 $
36 */
37 public class GlobalObjectClassRegistry implements ObjectClassRegistry
38 {
39 /*** maps an OID to an ObjectClass */
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 ObjectClassRegistryMonitor monitor;
47 /*** the underlying bootstrap registry to delegate on misses to */
48 private BootstrapObjectClassRegistry bootstrap;
49 /*** the system partition where we keep attributeType updates */
50 private SystemPartition systemPartition;
51
52
53
54
55
56
57
58 /***
59 * Creates an empty BootstrapObjectClassRegistry.
60 */
61 public GlobalObjectClassRegistry( SystemPartition systemPartition,
62 BootstrapObjectClassRegistry bootstrap, OidRegistry oidRegistry )
63 {
64 this.byOid = new HashMap();
65 this.oidToSchema = new HashMap();
66 this.oidRegistry = oidRegistry;
67 this.monitor = new ObjectClassRegistryMonitorAdapter();
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 /***
84 * Sets the monitor that is to be notified via callback events.
85 *
86 * @param monitor the new monitor to notify of notable events
87 */
88 public void setMonitor( ObjectClassRegistryMonitor monitor )
89 {
90 this.monitor = monitor;
91 }
92
93
94
95
96
97
98
99 public void register( String schema, ObjectClass dITContentRule ) throws NamingException
100 {
101 if ( byOid.containsKey( dITContentRule.getOid() ) ||
102 bootstrap.hasObjectClass( 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 ObjectClass lookup( String id ) throws NamingException
118 {
119 id = oidRegistry.getOid( id );
120
121 if ( byOid.containsKey( id ) )
122 {
123 ObjectClass dITContentRule = ( ObjectClass ) byOid.get( id );
124 monitor.lookedUp( dITContentRule );
125 return dITContentRule;
126 }
127
128 if ( bootstrap.hasObjectClass( id ) )
129 {
130 ObjectClass 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 hasObjectClass( String id )
143 {
144 if ( oidRegistry.hasOid( id ) )
145 {
146 try
147 {
148 return byOid.containsKey( oidRegistry.getOid( id ) ) ||
149 bootstrap.hasObjectClass( 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.hasObjectClass( 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 }