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<Integer, DitStructureRule>();
61 }
62
63
64
65
66
67 public boolean contains( int ruleId )
68 {
69 return byRuleId.containsKey( ruleId );
70 }
71
72
73
74
75
76 public Iterator<DitStructureRule> iterator()
77 {
78 return byRuleId.values().iterator();
79 }
80
81
82
83
84
85 public Iterator<Integer> ruleIdIterator()
86 {
87 return byRuleId.keySet().iterator();
88 }
89
90
91
92
93
94 public String getSchemaName( int ruleId ) throws LdapException
95 {
96 DitStructureRule ditStructureRule = byRuleId.get( ruleId );
97
98 if ( ditStructureRule != null )
99 {
100 return ditStructureRule.getSchemaName();
101 }
102
103 String msg = I18n.err( I18n.ERR_04263, ruleId );
104 LOG.warn( msg );
105 throw new LdapException( msg );
106 }
107
108
109
110
111
112 public void register( DitStructureRule ditStructureRule ) throws LdapException
113 {
114 int ruleId = ditStructureRule.getRuleId();
115
116 if ( byRuleId.containsKey( ruleId ) )
117 {
118 String msg = I18n.err( I18n.ERR_04264, ruleId );
119 LOG.warn( msg );
120 throw new LdapException( msg );
121 }
122
123 byRuleId.put( ruleId, ditStructureRule );
124
125 if ( LOG.isDebugEnabled() )
126 {
127 LOG.debug( "registered {} for OID {}", ditStructureRule, ruleId );
128 }
129 }
130
131
132
133
134
135 public DitStructureRule lookup( int ruleId ) throws LdapException
136 {
137 DitStructureRule ditStructureRule = byRuleId.get( ruleId );
138
139 if ( ditStructureRule == null )
140 {
141 String msg = I18n.err( I18n.ERR_04265, ruleId );
142 LOG.debug( msg );
143 throw new LdapException( msg );
144 }
145
146 if ( DEBUG )
147 {
148 LOG.debug( "Found {} with ruleId: {}", ditStructureRule, ruleId );
149 }
150
151 return ditStructureRule;
152 }
153
154
155
156
157
158 public void unregister( int ruleId ) throws LdapException
159 {
160 DitStructureRule ditStructureRule = byRuleId.remove( ruleId );
161
162 if ( DEBUG )
163 {
164 LOG.debug( "Removed {} with ruleId {} from the registry", ditStructureRule, ruleId );
165 }
166 }
167
168
169
170
171
172 public void unregisterSchemaElements( String schemaName )
173 {
174 if ( schemaName == null )
175 {
176 return;
177 }
178
179
180
181 for ( DitStructureRule ditStructureRule : this )
182 {
183 if ( schemaName.equalsIgnoreCase( ditStructureRule.getSchemaName() ) )
184 {
185 int ruleId = ditStructureRule.getRuleId();
186 SchemaObject removed = byRuleId.remove( ruleId );
187
188 if ( DEBUG )
189 {
190 LOG.debug( "Removed {} with ruleId {} from the registry", removed, ruleId );
191 }
192 }
193 }
194 }
195
196
197
198
199
200 public void renameSchema( String originalSchemaName, String newSchemaName )
201 {
202
203
204 for ( DitStructureRule ditStructureRule : this )
205 {
206 if ( originalSchemaName.equalsIgnoreCase( ditStructureRule.getSchemaName() ) )
207 {
208 ditStructureRule.setSchemaName( newSchemaName );
209
210 if ( DEBUG )
211 {
212 LOG.debug( "Renamed {} schemaName to {}", ditStructureRule, newSchemaName );
213 }
214 }
215 }
216 }
217
218
219
220
221
222 public DefaultDitStructureRuleRegistry copy()
223 {
224 DefaultDitStructureRuleRegistry copy = new DefaultDitStructureRuleRegistry();
225
226
227 copy.copy( this );
228
229 return copy;
230 }
231 }