1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server.schema.bootstrap;
18
19
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.Map;
23
24 import javax.naming.NamingException;
25
26 import org.apache.ldap.common.schema.DITContentRule;
27 import org.apache.ldap.server.schema.DITContentRuleRegistry;
28 import org.apache.ldap.server.schema.DITContentRuleRegistryMonitor;
29 import org.apache.ldap.server.schema.DITContentRuleRegistryMonitorAdapter;
30 import org.apache.ldap.server.schema.OidRegistry;
31
32
33 /***
34 * A plain old java object implementation of an DITContentRuleRegistry.
35 *
36 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37 * @version $Rev: 264732 $
38 */
39 public class BootstrapDitContentRuleRegistry implements DITContentRuleRegistry
40 {
41 /*** maps an OID to an DITContentRule */
42 private final Map byOid;
43 /*** maps an OID to a schema name*/
44 private final Map oidToSchema;
45 /*** the registry used to resolve names to OIDs */
46 private final OidRegistry oidRegistry;
47 /*** monitor notified via callback events */
48 private DITContentRuleRegistryMonitor monitor;
49
50
51
52
53
54
55
56 /***
57 * Creates an empty BootstrapDitContentRuleRegistry.
58 */
59 public BootstrapDitContentRuleRegistry( OidRegistry oidRegistry )
60 {
61 this.byOid = new HashMap();
62 this.oidToSchema = new HashMap();
63 this.oidRegistry = oidRegistry;
64 this.monitor = new DITContentRuleRegistryMonitorAdapter();
65 }
66
67
68 /***
69 * Sets the monitor that is to be notified via callback events.
70 *
71 * @param monitor the new monitor to notify of notable events
72 */
73 public void setMonitor( DITContentRuleRegistryMonitor monitor )
74 {
75 this.monitor = monitor;
76 }
77
78
79
80
81
82
83
84 public void register( String schema, DITContentRule dITContentRule ) throws NamingException
85 {
86 if ( byOid.containsKey( dITContentRule.getOid() ) )
87 {
88 NamingException e = new NamingException( "dITContentRule w/ OID " +
89 dITContentRule.getOid() + " has already been registered!" );
90 monitor.registerFailed( dITContentRule, e );
91 throw e;
92 }
93
94 oidRegistry.register( dITContentRule.getName(), dITContentRule.getOid() ) ;
95 byOid.put( dITContentRule.getOid(), dITContentRule );
96 oidToSchema.put( dITContentRule.getOid(), schema );
97 monitor.registered( dITContentRule );
98 }
99
100
101 public DITContentRule lookup( String id ) throws NamingException
102 {
103 id = oidRegistry.getOid( id );
104
105 if ( ! byOid.containsKey( id ) )
106 {
107 NamingException e = new NamingException( "dITContentRule w/ OID "
108 + id + " not registered!" );
109 monitor.lookupFailed( id, e );
110 throw e;
111 }
112
113 DITContentRule dITContentRule = ( DITContentRule ) byOid.get( id );
114 monitor.lookedUp( dITContentRule );
115 return dITContentRule;
116 }
117
118
119 public boolean hasDITContentRule( String id )
120 {
121 if ( oidRegistry.hasOid( id ) )
122 {
123 try
124 {
125 return byOid.containsKey( oidRegistry.getOid( id ) );
126 }
127 catch ( NamingException e )
128 {
129 return false;
130 }
131 }
132
133 return false;
134 }
135
136
137 public String getSchemaName( String id ) throws NamingException
138 {
139 id = oidRegistry.getOid( id );
140 if ( oidToSchema.containsKey( id ) )
141 {
142 return ( String ) oidToSchema.get( id );
143 }
144
145 throw new NamingException( "OID " + id + " not found in oid to " +
146 "schema name map!" );
147 }
148
149
150 public Iterator list()
151 {
152 return byOid.values().iterator();
153 }
154 }