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 java.util.HashMap;
21 import java.util.Map;
22
23 import javax.naming.NamingException;
24
25 import org.apache.ldap.common.schema.SyntaxChecker;
26 import org.apache.ldap.server.schema.bootstrap.BootstrapSyntaxCheckerRegistry;
27
28
29 /***
30 * A simple POJO implementation of the SyntaxCheckerRegistry service interface.
31 *
32 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33 * @version $Rev: 264732 $
34 */
35 public class GlobalSyntaxCheckerRegistry implements SyntaxCheckerRegistry
36 {
37 /*** the syntaxCheckers in this registry */
38 private final Map syntaxCheckers;
39 /*** maps an OID to a schema name*/
40 private final Map oidToSchema;
41 /*** the monitor for delivering callback events */
42 private SyntaxCheckerRegistryMonitor monitor;
43 /*** the underlying bootstrap registry to delegate on misses to */
44 private BootstrapSyntaxCheckerRegistry bootstrap;
45
46
47
48
49
50
51
52 /***
53 * Creates a default SyntaxCheckerRegistry by initializing the map and the
54 * montior.
55 */
56 public GlobalSyntaxCheckerRegistry( BootstrapSyntaxCheckerRegistry bootstrap )
57 {
58 this.oidToSchema = new HashMap();
59 this.syntaxCheckers = new HashMap();
60 this.monitor = new SyntaxCheckerRegistryMonitorAdapter();
61
62 this.bootstrap = bootstrap;
63 if ( this.bootstrap == null )
64 {
65 throw new NullPointerException( "the bootstrap registry cannot be null" ) ;
66 }
67 }
68
69
70 /***
71 * Sets the monitor used by this registry.
72 *
73 * @param monitor the monitor to set for registry event callbacks
74 */
75 public void setMonitor( SyntaxCheckerRegistryMonitor monitor )
76 {
77 this.monitor = monitor;
78 }
79
80
81
82
83
84
85
86 public void register( String schema, String oid, SyntaxChecker syntaxChecker )
87 throws NamingException
88 {
89 if ( syntaxCheckers.containsKey( oid ) || bootstrap.hasSyntaxChecker( oid ) )
90 {
91 NamingException e = new NamingException( "SyntaxChecker with OID "
92 + oid + " already registered!" );
93 monitor.registerFailed( oid, syntaxChecker, e );
94 throw e;
95 }
96
97 oidToSchema.put( oid, schema );
98 syntaxCheckers.put( oid, syntaxChecker );
99 monitor.registered( oid, syntaxChecker );
100 }
101
102
103 public SyntaxChecker lookup( String oid ) throws NamingException
104 {
105 SyntaxChecker c;
106 NamingException e;
107
108 if ( syntaxCheckers.containsKey( oid ) )
109 {
110 c = ( SyntaxChecker ) syntaxCheckers.get( oid );
111 monitor.lookedUp( oid, c );
112 return c;
113 }
114
115 if ( bootstrap.hasSyntaxChecker( oid ) )
116 {
117 c = bootstrap.lookup( oid );
118 monitor.lookedUp( oid, c );
119 return c;
120 }
121
122 e = new NamingException( "SyntaxChecker not found for OID: " + oid );
123 monitor.lookupFailed( oid, e );
124 throw e;
125 }
126
127
128 public boolean hasSyntaxChecker( String oid )
129 {
130 return syntaxCheckers.containsKey( oid ) || bootstrap.hasSyntaxChecker( oid );
131 }
132
133
134 public String getSchemaName( String oid ) throws NamingException
135 {
136 if ( ! Character.isDigit( oid.charAt( 0 ) ) )
137 {
138 throw new NamingException( "OID " + oid + " is not a numeric OID" );
139 }
140
141 if ( oidToSchema.containsKey( oid ) )
142 {
143 return ( String ) oidToSchema.get( oid );
144 }
145
146 if ( bootstrap.hasSyntaxChecker( oid ) )
147 {
148 return bootstrap.getSchemaName( oid );
149 }
150
151 throw new NamingException( "OID " + oid + " not found in oid to " +
152 "schema name map!" );
153 }
154 }