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.helper;
21
22
23 import org.apache.directory.api.i18n.I18n;
24 import org.apache.directory.api.ldap.model.exception.LdapException;
25 import org.apache.directory.api.ldap.model.exception.LdapSchemaException;
26 import org.apache.directory.api.ldap.model.exception.LdapSchemaExceptionCodes;
27 import org.apache.directory.api.ldap.model.schema.LdapComparator;
28 import org.apache.directory.api.ldap.model.schema.LdapSyntax;
29 import org.apache.directory.api.ldap.model.schema.MatchingRule;
30 import org.apache.directory.api.ldap.model.schema.Normalizer;
31 import org.apache.directory.api.ldap.model.schema.SchemaErrorHandler;
32 import org.apache.directory.api.ldap.model.schema.comparators.ComparableComparator;
33 import org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer;
34 import org.apache.directory.api.ldap.model.schema.registries.Registries;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41
42
43
44
45 public final class MatchingRuleHelper
46 {
47
48 private static final Logger LOG = LoggerFactory.getLogger( MatchingRuleHelper.class );
49
50
51 private MatchingRuleHelper()
52 {
53 }
54
55
56
57
58
59
60
61
62
63
64
65 @SuppressWarnings("rawtypes")
66 public static void addToRegistries( MatchingRule matchingRule, SchemaErrorHandler errorHandler, Registries registries )
67 throws LdapException
68 {
69 if ( registries != null )
70 {
71 try
72 {
73 matchingRule.unlock();
74
75 LdapComparator<?> ldapComparator = null;
76 Normalizer normalizer = null;
77 LdapSyntax ldapSyntax = null;
78
79 try
80 {
81
82 ldapComparator = registries.getComparatorRegistry().lookup( matchingRule.getOid() );
83 }
84 catch ( LdapException ne )
85 {
86
87 ldapComparator = new ComparableComparator( matchingRule.getOid() );
88 }
89
90 try
91 {
92
93 normalizer = registries.getNormalizerRegistry().lookup( matchingRule.getOid() );
94 }
95 catch ( LdapException ne )
96 {
97
98 normalizer = new NoOpNormalizer( matchingRule.getOid() );
99 }
100
101 try
102 {
103
104 ldapSyntax = registries.getLdapSyntaxRegistry().lookup( matchingRule.getSyntaxOid() );
105 }
106 catch ( LdapException ne )
107 {
108
109 String msg = I18n.err( I18n.ERR_13765_MR_MUST_REFER_EXISTING_SYNTAX );
110
111 LdapSchemaException ldapSchemaException = new LdapSchemaException(
112 LdapSchemaExceptionCodes.MR_NONEXISTENT_SYNTAX, msg, ne );
113 ldapSchemaException.setSourceObject( matchingRule );
114 ldapSchemaException.setRelatedId( matchingRule.getSyntaxOid() );
115 errorHandler.handle( LOG, msg, ldapSchemaException );
116 }
117
118
119
120
121
122
123
124 if ( ldapComparator != null )
125 {
126 registries.addReference( matchingRule, ldapComparator );
127 matchingRule.setLdapComparator( ldapComparator );
128 }
129
130 if ( normalizer != null )
131 {
132 registries.addReference( matchingRule, normalizer );
133 matchingRule.setNormalizer( normalizer );
134 }
135
136 if ( ldapSyntax != null )
137 {
138 registries.addReference( matchingRule, ldapSyntax );
139 matchingRule.setSyntax( ldapSyntax );
140 }
141 }
142 finally
143 {
144 matchingRule.lock();
145 }
146 }
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160
161 public static void removeFromRegistries( MatchingRule matchingRule, SchemaErrorHandler errorHandler, Registries registries )
162 {
163 if ( registries != null )
164 {
165
166
167
168
169
170
171 if ( matchingRule.getLdapComparator() != null )
172 {
173 registries.delReference( matchingRule, matchingRule.getLdapComparator() );
174 }
175
176 if ( matchingRule.getSyntax() != null )
177 {
178 registries.delReference( matchingRule, matchingRule.getSyntax() );
179 }
180
181 if ( matchingRule.getNormalizer() != null )
182 {
183 registries.delReference( matchingRule, matchingRule.getNormalizer() );
184 }
185 }
186 }
187 }