1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.api.ldap.model.schema.registries.helper;
21
22
23 import java.util.List;
24
25 import org.apache.directory.api.i18n.I18n;
26 import org.apache.directory.api.ldap.model.exception.LdapException;
27 import org.apache.directory.api.ldap.model.exception.LdapSchemaException;
28 import org.apache.directory.api.ldap.model.exception.LdapSchemaExceptionCodes;
29 import org.apache.directory.api.ldap.model.schema.LdapComparator;
30 import org.apache.directory.api.ldap.model.schema.LdapSyntax;
31 import org.apache.directory.api.ldap.model.schema.MatchingRule;
32 import org.apache.directory.api.ldap.model.schema.MutableMatchingRule;
33 import org.apache.directory.api.ldap.model.schema.Normalizer;
34 import org.apache.directory.api.ldap.model.schema.comparators.ComparableComparator;
35 import org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer;
36 import org.apache.directory.api.ldap.model.schema.registries.Registries;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41 /**
42 * An helper class used to store all the methods associated with an MatchingRule
43 * in relation with the Registries and SchemaManager.
44 *
45 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46 */
47 public class MatchingRuleHelper
48 {
49 /** A logger for this class */
50 private static final Logger LOG = LoggerFactory.getLogger( MatchingRuleHelper.class );
51
52
53 /**
54 * Inject the MatchingRule into the Registries, updating the references to
55 * other SchemaObject
56 *
57 * @param matchingRule The MatchingRule to add to the Registries
58 * @param errors The errors we got while adding the MatchingRule to the registries
59 * @param registries The Registries
60 * @exception If the addition failed
61 */
62 @SuppressWarnings("rawtypes")
63 public static void addToRegistries( MutableMatchingRule matchingRule, List<Throwable> errors, Registries registries )
64 throws LdapException
65 {
66 if ( registries != null )
67 {
68 try
69 {
70 matchingRule.unlock();
71
72 LdapComparator<?> ldapComparator = null;
73 Normalizer normalizer = null;
74 LdapSyntax ldapSyntax = null;
75
76 try
77 {
78 // Gets the associated Comparator
79 ldapComparator = registries.getComparatorRegistry().lookup( matchingRule.getOid() );
80 }
81 catch ( LdapException ne )
82 {
83 // Default to a catch all comparator
84 ldapComparator = new ComparableComparator( matchingRule.getOid() );
85 }
86
87 try
88 {
89 // Gets the associated Normalizer
90 normalizer = registries.getNormalizerRegistry().lookup( matchingRule.getOid() );
91 }
92 catch ( LdapException ne )
93 {
94 // Default to the NoOp normalizer
95 normalizer = new NoOpNormalizer( matchingRule.getOid() );
96 }
97
98 try
99 {
100 // Get the associated LdapSyntax
101 ldapSyntax = registries.getLdapSyntaxRegistry().lookup( matchingRule.getSyntaxOid() );
102 }
103 catch ( LdapException ne )
104 {
105 // The Syntax is a mandatory element, it must exist.
106 String msg = I18n.err( I18n.ERR_04317 );
107
108 LdapSchemaException ldapSchemaException = new LdapSchemaException(
109 LdapSchemaExceptionCodes.MR_NONEXISTENT_SYNTAX, msg, ne );
110 ldapSchemaException.setSourceObject( matchingRule );
111 ldapSchemaException.setRelatedId( matchingRule.getSyntaxOid() );
112 errors.add( ldapSchemaException );
113 LOG.info( msg );
114 }
115
116 /**
117 * Add the MR references (using and usedBy) :
118 * MR -> C
119 * MR -> N
120 * MR -> S
121 */
122 if ( ldapComparator != null )
123 {
124 registries.addReference( matchingRule, ldapComparator );
125 matchingRule.setLdapComparator( ldapComparator );
126 }
127
128 if ( normalizer != null )
129 {
130 registries.addReference( matchingRule, normalizer );
131 matchingRule.setNormalizer( normalizer );
132 }
133
134 if ( ldapSyntax != null )
135 {
136 registries.addReference( matchingRule, ldapSyntax );
137 matchingRule.setSyntax( ldapSyntax );
138 }
139 }
140 finally
141 {
142 matchingRule.lock();
143 }
144 }
145 }
146
147
148 /**
149 * Remove the MatchingRule from the Registries, updating the references to
150 * other SchemaObject.
151 *
152 * If one of the referenced SchemaObject does not exist,
153 * an exception is thrown.
154 *
155 * @param matchingRule The MatchingRule to remove from the Registries
156 * @param errors The errors we got while removing the MatchingRule from the registries
157 * @param registries The Registries
158 * @exception If the MatchingRule is not valid
159 */
160 public static void removeFromRegistries( MatchingRule matchingRule, List<Throwable> errors, Registries registries )
161 throws LdapException
162 {
163 if ( registries != null )
164 {
165 /**
166 * Remove the MR references (using and usedBy) :
167 * MR -> C
168 * MR -> N
169 * MR -> S
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 }