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.exception.LdapNamingException;
30 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
31 import org.apache.directory.shared.ldap.schema.DITStructureRule;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36
37
38
39
40
41
42 public class DefaultDitStructureRuleRegistry implements DITStructureRuleRegistry
43 {
44
45 private static final Logger LOG = LoggerFactory.getLogger( DefaultDitStructureRuleRegistry.class );
46
47 private final Map<String,DITStructureRule> byOid;
48
49 private final Map<Integer,DITStructureRule> byRuleId;
50
51 private final OidRegistry oidRegistry;
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public DefaultDitStructureRuleRegistry( OidRegistry oidRegistry )
66 {
67 this.byRuleId = new HashMap<Integer,DITStructureRule>();
68 this.byOid = new HashMap<String,DITStructureRule>();
69 this.oidRegistry = oidRegistry;
70 }
71
72
73
74
75
76
77 public void register( DITStructureRule dITStructureRule ) throws NamingException
78 {
79 if ( byOid.containsKey( dITStructureRule.getOid() ) )
80 {
81 throw new NamingException( "dITStructureRule w/ OID " + dITStructureRule.getOid()
82 + " has already been registered!" );
83 }
84
85 oidRegistry.register( dITStructureRule.getName(), dITStructureRule.getOid() );
86 byOid.put( dITStructureRule.getOid(), dITStructureRule );
87 byRuleId.put( dITStructureRule.getRuleId(), dITStructureRule );
88 if ( LOG.isDebugEnabled() )
89 {
90 LOG.debug( "registered dITStructureRule: " + dITStructureRule );
91 }
92 }
93
94
95 public DITStructureRule lookup( String id ) throws NamingException
96 {
97 id = oidRegistry.getOid( id );
98
99 if ( !byOid.containsKey( id ) )
100 {
101 throw new NamingException( "dITStructureRule w/ OID " + id + " not registered!" );
102 }
103
104 DITStructureRule dITStructureRule = byOid.get( id );
105 if ( LOG.isDebugEnabled() )
106 {
107 LOG.debug( "lookup with id '" + id + "' for dITStructureRule: " + dITStructureRule );
108 }
109 return dITStructureRule;
110 }
111
112
113 public boolean hasDITStructureRule( String id )
114 {
115 if ( oidRegistry.hasOid( id ) )
116 {
117 try
118 {
119 return byOid.containsKey( oidRegistry.getOid( id ) );
120 }
121 catch ( NamingException e )
122 {
123 return false;
124 }
125 }
126
127 return false;
128 }
129
130
131 public String getSchemaName( String id ) throws NamingException
132 {
133 id = oidRegistry.getOid( id );
134 DITStructureRule dsr = byOid.get( id );
135 if ( dsr != null )
136 {
137 return dsr.getSchema();
138 }
139
140 throw new NamingException( "OID " + id + " not found in oid to " + "DITStructureRule map!" );
141 }
142
143
144 public String getSchemaName( Integer ruleId ) throws NamingException
145 {
146 DITStructureRule dsr = byRuleId.get( ruleId );
147 if ( dsr != null )
148 {
149 return dsr.getSchema();
150 }
151
152 throw new NamingException( "A DitStructureRule with ruleId " + ruleId
153 + " not found in the DITStructureRule map!" );
154 }
155
156
157 public Iterator<DITStructureRule> iterator()
158 {
159 return byOid.values().iterator();
160 }
161
162
163 public void unregister( Integer ruleId ) throws NamingException
164 {
165 DITStructureRule dsr = byRuleId.remove( ruleId );
166
167 if ( dsr == null )
168 {
169 if ( dsr == null )
170 {
171 throw new LdapNamingException(
172 "No such DITStructureRule for rule identifier: " + ruleId,
173 ResultCodeEnum.OTHER );
174 }
175 }
176
177 byOid.remove( dsr.getOid() );
178 }
179
180
181 public void unregister( String numericOid ) throws NamingException
182 {
183 if ( ! Character.isDigit( numericOid.charAt( 0 ) ) )
184 {
185 throw new NamingException( "Looks like the arg is not a numeric OID" );
186 }
187
188 DITStructureRule dsr = byOid.remove( numericOid );
189 byRuleId.remove( dsr.getRuleId() );
190 }
191
192
193 public boolean hasDITStructureRule( Integer ruleId )
194 {
195 DITStructureRule dsr = byRuleId.get( ruleId );
196 return dsr != null;
197 }
198
199
200 public DITStructureRule lookup( Integer ruleId ) throws NamingException
201 {
202 DITStructureRule dsr = byRuleId.get( ruleId );
203
204 if ( dsr == null )
205 {
206 throw new LdapNamingException(
207 "No such DITStructureRule for rule identifier: " + ruleId,
208 ResultCodeEnum.OTHER );
209 }
210
211 return dsr;
212 }
213 }