1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.schema.registries;
21
22
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 import javax.naming.NamingException;
28
29 import org.apache.directory.shared.ldap.schema.Syntax;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39
40
41 public class DefaultSyntaxRegistry implements SyntaxRegistry
42 {
43
44 private static final Logger LOG = LoggerFactory.getLogger( DefaultSyntaxRegistry.class );
45
46
47 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
48
49
50 private final Map<String,Syntax> byOid;
51
52 private final OidRegistry oidRegistry;
53
54
55
56
57
58
59
60
61
62
63
64
65 public DefaultSyntaxRegistry( OidRegistry registry )
66 {
67 this.oidRegistry = registry;
68 this.byOid = new HashMap<String,Syntax>();
69 }
70
71
72
73
74
75
76
77 public Syntax lookup( String id ) throws NamingException
78 {
79 id = oidRegistry.getOid( id );
80
81 if ( byOid.containsKey( id ) )
82 {
83 Syntax syntax = byOid.get( id );
84
85 if ( IS_DEBUG )
86 {
87 LOG.debug( "looked up using id '" + id + "': " + syntax );
88 }
89
90 return syntax;
91 }
92
93 throw new NamingException( "Unknown syntax OID " + id );
94 }
95
96
97 public void register( Syntax syntax ) throws NamingException
98 {
99 if ( byOid.containsKey( syntax.getOid() ) )
100 {
101 throw new NamingException( "syntax w/ OID " + syntax.getOid()
102 + " has already been registered!" );
103 }
104
105 if ( syntax.getName() != null )
106 {
107 oidRegistry.register( syntax.getName(), syntax.getOid() );
108 }
109 else
110 {
111 oidRegistry.register( syntax.getOid(), syntax.getOid() );
112 }
113
114 byOid.put( syntax.getOid(), syntax );
115
116 if ( IS_DEBUG )
117 {
118 LOG.debug( "registered syntax: " + syntax );
119 }
120 }
121
122
123 public boolean hasSyntax( String id )
124 {
125 if ( oidRegistry.hasOid( id ) )
126 {
127 try
128 {
129 return byOid.containsKey( oidRegistry.getOid( id ) );
130 }
131 catch ( NamingException e )
132 {
133 return false;
134 }
135 }
136
137 return false;
138 }
139
140
141 public String getSchemaName( String id ) throws NamingException
142 {
143 if ( ! Character.isDigit( id.charAt( 0 ) ) )
144 {
145 throw new NamingException( "Looks like the arg is not a numeric OID" );
146 }
147
148 id = oidRegistry.getOid( id );
149 Syntax syntax = byOid.get( id );
150 if ( syntax != null )
151 {
152 return syntax.getSchema();
153 }
154
155 throw new NamingException( "OID " + id + " not found in oid to " + "Syntax map!" );
156 }
157
158
159 public Iterator<Syntax> iterator()
160 {
161 return byOid.values().iterator();
162 }
163
164
165 public void unregister( String numericOid ) throws NamingException
166 {
167 if ( ! Character.isDigit( numericOid.charAt( 0 ) ) )
168 {
169 throw new NamingException( "Looks like the arg is not a numeric OID" );
170 }
171
172 byOid.remove( numericOid );
173 }
174 }