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   *    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 final class MatchingRuleHelper
48  {
49      /** A logger for this class */
50      private static final Logger LOG = LoggerFactory.getLogger( MatchingRuleHelper.class );
51  
52  
53      private MatchingRuleHelper()
54      {
55      }
56  
57  
58      /**
59       * Inject the MatchingRule into the Registries, updating the references to
60       * other SchemaObject
61       *
62       * @param matchingRule The MatchingRule to add to the Registries
63       * @param errors The errors we got while adding the MatchingRule to the registries
64       * @param registries The Registries
65       * @throws LdapException If the addition failed
66       */
67      @SuppressWarnings("rawtypes")
68      public static void addToRegistries( MutableMatchingRule matchingRule, List<Throwable> errors, Registries registries )
69          throws LdapException
70      {
71          if ( registries != null )
72          {
73              try
74              {
75                  matchingRule.unlock();
76  
77                  LdapComparator<?> ldapComparator = null;
78                  Normalizer normalizer = null;
79                  LdapSyntax ldapSyntax = null;
80  
81                  try
82                  {
83                      // Gets the associated Comparator
84                      ldapComparator = registries.getComparatorRegistry().lookup( matchingRule.getOid() );
85                  }
86                  catch ( LdapException ne )
87                  {
88                      // Default to a catch all comparator
89                      ldapComparator = new ComparableComparator( matchingRule.getOid() );
90                  }
91  
92                  try
93                  {
94                      // Gets the associated Normalizer
95                      normalizer = registries.getNormalizerRegistry().lookup( matchingRule.getOid() );
96                  }
97                  catch ( LdapException ne )
98                  {
99                      // Default to the NoOp normalizer
100                     normalizer = new NoOpNormalizer( matchingRule.getOid() );
101                 }
102 
103                 try
104                 {
105                     // Get the associated LdapSyntax
106                     ldapSyntax = registries.getLdapSyntaxRegistry().lookup( matchingRule.getSyntaxOid() );
107                 }
108                 catch ( LdapException ne )
109                 {
110                     // The Syntax is a mandatory element, it must exist.
111                     String msg = I18n.err( I18n.ERR_04317 );
112 
113                     LdapSchemaException ldapSchemaException = new LdapSchemaException(
114                         LdapSchemaExceptionCodes.MR_NONEXISTENT_SYNTAX, msg, ne );
115                     ldapSchemaException.setSourceObject( matchingRule );
116                     ldapSchemaException.setRelatedId( matchingRule.getSyntaxOid() );
117                     errors.add( ldapSchemaException );
118                     LOG.info( msg );
119                 }
120 
121                 /**
122                  * Add the MR references (using and usedBy) :
123                  * MR -> C
124                  * MR -> N
125                  * MR -> S
126                  */
127                 if ( ldapComparator != null )
128                 {
129                     registries.addReference( matchingRule, ldapComparator );
130                     matchingRule.setLdapComparator( ldapComparator );
131                 }
132 
133                 if ( normalizer != null )
134                 {
135                     registries.addReference( matchingRule, normalizer );
136                     matchingRule.setNormalizer( normalizer );
137                 }
138 
139                 if ( ldapSyntax != null )
140                 {
141                     registries.addReference( matchingRule, ldapSyntax );
142                     matchingRule.setSyntax( ldapSyntax );
143                 }
144             }
145             finally
146             {
147                 matchingRule.lock();
148             }
149         }
150     }
151 
152 
153     /**
154      * Remove the MatchingRule from the Registries, updating the references to
155      * other SchemaObject.
156      * 
157      * If one of the referenced SchemaObject does not exist,
158      * an exception is thrown.
159      *
160      * @param matchingRule The MatchingRule to remove from the Registries
161      * @param errors The errors we got while removing the MatchingRule from the registries
162      * @param registries The Registries
163      * @throws LdapException If the MatchingRule is not valid
164      */
165     public static void removeFromRegistries( MatchingRule matchingRule, List<Throwable> errors, Registries registries )
166         throws LdapException
167     {
168         if ( registries != null )
169         {
170             /**
171              * Remove the MR references (using and usedBy) :
172              * MR -> C
173              * MR -> N
174              * MR -> S
175              */
176             if ( matchingRule.getLdapComparator() != null )
177             {
178                 registries.delReference( matchingRule, matchingRule.getLdapComparator() );
179             }
180 
181             if ( matchingRule.getSyntax() != null )
182             {
183                 registries.delReference( matchingRule, matchingRule.getSyntax() );
184             }
185 
186             if ( matchingRule.getNormalizer() != null )
187             {
188                 registries.delReference( matchingRule, matchingRule.getNormalizer() );
189             }
190         }
191     }
192 }