1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.model.schema.registries;
21
22
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 import org.apache.directory.api.i18n.I18n;
28 import org.apache.directory.api.ldap.model.exception.LdapException;
29 import org.apache.directory.api.ldap.model.schema.DitStructureRule;
30 import org.apache.directory.api.ldap.model.schema.SchemaObject;
31 import org.apache.directory.api.ldap.model.schema.SchemaObjectType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36
37
38
39
40
41 public class DefaultDitStructureRuleRegistry extends DefaultSchemaObjectRegistry<DitStructureRule>
42 implements DitStructureRuleRegistry
43 {
44
45 private static final Logger LOG = LoggerFactory.getLogger( DefaultDitStructureRuleRegistry.class );
46
47
48 private static final boolean DEBUG = LOG.isDebugEnabled();
49
50
51 protected Map<Integer, DitStructureRule> byRuleId;
52
53
54
55
56
57 public DefaultDitStructureRuleRegistry()
58 {
59 super( SchemaObjectType.DIT_STRUCTURE_RULE, new OidRegistry<DitStructureRule>() );
60 byRuleId = new HashMap<>();
61 }
62
63
64
65
66
67 @Override
68 public boolean contains( int ruleId )
69 {
70 return byRuleId.containsKey( ruleId );
71 }
72
73
74
75
76
77 @Override
78 public Iterator<DitStructureRule> iterator()
79 {
80 return byRuleId.values().iterator();
81 }
82
83
84
85
86
87 @Override
88 public Iterator<Integer> ruleIdIterator()
89 {
90 return byRuleId.keySet().iterator();
91 }
92
93
94
95
96
97 @Override
98 public String getSchemaName( int ruleId ) throws LdapException
99 {
100 DitStructureRule ditStructureRule = byRuleId.get( ruleId );
101
102 if ( ditStructureRule != null )
103 {
104 return ditStructureRule.getSchemaName();
105 }
106
107 String msg = I18n.err( I18n.ERR_04263, ruleId );
108 LOG.warn( msg );
109 throw new LdapException( msg );
110 }
111
112
113
114
115
116 @Override
117 public void register( DitStructureRule ditStructureRule ) throws LdapException
118 {
119 int ruleId = ditStructureRule.getRuleId();
120
121 if ( byRuleId.containsKey( ruleId ) )
122 {
123 String msg = I18n.err( I18n.ERR_04264, ruleId );
124 LOG.warn( msg );
125 throw new LdapException( msg );
126 }
127
128 byRuleId.put( ruleId, ditStructureRule );
129
130 if ( LOG.isDebugEnabled() )
131 {
132 LOG.debug( "registered {} for OID {}", ditStructureRule, ruleId );
133 }
134 }
135
136
137
138
139
140 @Override
141 public DitStructureRule lookup( int ruleId ) throws LdapException
142 {
143 DitStructureRule ditStructureRule = byRuleId.get( ruleId );
144
145 if ( ditStructureRule == null )
146 {
147 String msg = I18n.err( I18n.ERR_04265, ruleId );
148 LOG.debug( msg );
149 throw new LdapException( msg );
150 }
151
152 if ( DEBUG )
153 {
154 LOG.debug( "Found {} with ruleId: {}", ditStructureRule, ruleId );
155 }
156
157 return ditStructureRule;
158 }
159
160
161
162
163
164 @Override
165 public void unregister( int ruleId ) throws LdapException
166 {
167 DitStructureRule ditStructureRule = byRuleId.remove( ruleId );
168
169 if ( DEBUG )
170 {
171 LOG.debug( "Removed {} with ruleId {} from the registry", ditStructureRule, ruleId );
172 }
173 }
174
175
176
177
178
179 @Override
180 public void unregisterSchemaElements( String schemaName )
181 {
182 if ( schemaName == null )
183 {
184 return;
185 }
186
187
188
189 for ( DitStructureRule ditStructureRule : this )
190 {
191 if ( schemaName.equalsIgnoreCase( ditStructureRule.getSchemaName() ) )
192 {
193 int ruleId = ditStructureRule.getRuleId();
194 SchemaObject removed = byRuleId.remove( ruleId );
195
196 if ( DEBUG )
197 {
198 LOG.debug( "Removed {} with ruleId {} from the registry", removed, ruleId );
199 }
200 }
201 }
202 }
203
204
205
206
207
208 @Override
209 public void renameSchema( String originalSchemaName, String newSchemaName )
210 {
211
212
213 for ( DitStructureRule ditStructureRule : this )
214 {
215 if ( originalSchemaName.equalsIgnoreCase( ditStructureRule.getSchemaName() ) )
216 {
217 ditStructureRule.setSchemaName( newSchemaName );
218
219 if ( DEBUG )
220 {
221 LOG.debug( "Renamed {} schemaName to {}", ditStructureRule, newSchemaName );
222 }
223 }
224 }
225 }
226
227
228
229
230
231 @Override
232 public DefaultDitStructureRuleRegistry copy()
233 {
234 DefaultDitStructureRuleRegistry copy = new DefaultDitStructureRuleRegistry();
235
236
237 copy.copy( this );
238
239 return copy;
240 }
241 }