View Javadoc
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   *    https://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 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   * An helper class used to store all the methods associated with an MatchingRule
41   * in relation with the Registries and SchemaManager.
42   * 
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public final class MatchingRuleHelper
46  {
47      /** A logger for this class */
48      private static final Logger LOG = LoggerFactory.getLogger( MatchingRuleHelper.class );
49  
50  
51      private MatchingRuleHelper()
52      {
53      }
54  
55  
56      /**
57       * Inject the MatchingRule into the Registries, updating the references to
58       * other SchemaObject
59       *
60       * @param matchingRule The MatchingRule to add to the Registries
61       * @param errorHandler Error handler
62       * @param registries The Registries
63       * @throws LdapException If the addition failed
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                      // Gets the associated Comparator
82                      ldapComparator = registries.getComparatorRegistry().lookup( matchingRule.getOid() );
83                  }
84                  catch ( LdapException ne )
85                  {
86                      // Default to a catch all comparator
87                      ldapComparator = new ComparableComparator( matchingRule.getOid() );
88                  }
89  
90                  try
91                  {
92                      // Gets the associated Normalizer
93                      normalizer = registries.getNormalizerRegistry().lookup( matchingRule.getOid() );
94                  }
95                  catch ( LdapException ne )
96                  {
97                      // Default to the NoOp normalizer
98                      normalizer = new NoOpNormalizer( matchingRule.getOid() );
99                  }
100 
101                 try
102                 {
103                     // Get the associated LdapSyntax
104                     ldapSyntax = registries.getLdapSyntaxRegistry().lookup( matchingRule.getSyntaxOid() );
105                 }
106                 catch ( LdapException ne )
107                 {
108                     // The Syntax is a mandatory element, it must exist.
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                  * Add the MR references (using and usedBy) :
120                  * MR -> C
121                  * MR -> N
122                  * MR -> S
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      * Remove the MatchingRule from the Registries, updating the references to
152      * other SchemaObject.
153      * 
154      * If one of the referenced SchemaObject does not exist,
155      * an exception is thrown.
156      *
157      * @param matchingRule The MatchingRule to remove from the Registries
158      * @param errorHandler Error handler
159      * @param registries The Registries
160      */
161     public static void removeFromRegistries( MatchingRule matchingRule, SchemaErrorHandler errorHandler, Registries registries )
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 }