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.comparators;
21  
22  
23  import java.io.Serializable;
24  import java.util.Comparator;
25  
26  import org.apache.directory.api.i18n.I18n;
27  import org.apache.directory.api.ldap.model.exception.LdapException;
28  import org.apache.directory.api.ldap.model.schema.LdapComparator;
29  import org.apache.directory.api.ldap.model.schema.SchemaManager;
30  
31  
32  /**
33   * A serializable wrapper around a Comparator which uses delayed initialization
34   * of the underlying wrapped comparator which is JIT resolved from a static
35   * global registry.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class SerializableComparator<E> extends LdapComparator<E> implements Serializable
40  {
41      /** The serial version UID */
42      private static final long serialVersionUID = 2L;
43  
44      /** the OID of the matchingRule for this comparator */
45      private String matchingRuleOid;
46  
47      /** the transient wrapped comparator */
48      private transient Comparator<E> wrapped;
49  
50      /** A reference to the schema manager */
51      private transient SchemaManager schemaManager;
52  
53  
54      // ------------------------------------------------------------------------
55      // C O N T R U C T O R S
56      // ------------------------------------------------------------------------
57      public SerializableComparator( String matchingRuleOid )
58      {
59          super( matchingRuleOid );
60          this.matchingRuleOid = matchingRuleOid;
61      }
62  
63  
64      // ------------------------------------------------------------------------
65      // C O M P A R A T O R   I M P L E M E N T A T I O N S
66      // ------------------------------------------------------------------------
67  
68      /**
69       * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
70       */
71      @SuppressWarnings("unchecked")
72      public int compare( E o1, E o2 )
73      {
74          if ( wrapped == null )
75          {
76              try
77              {
78                  wrapped = ( Comparator<E> ) schemaManager.lookupComparatorRegistry( matchingRuleOid );
79              }
80              catch ( LdapException e )
81              {
82                  throw new RuntimeException( I18n.err( I18n.ERR_04221, matchingRuleOid ) );
83              }
84          }
85  
86          return wrapped.compare( o1, o2 );
87      }
88  
89  
90      /**
91       * @param schemaManager the schemaManager to set
92       */
93      @SuppressWarnings("unchecked")
94      public void setSchemaManager( SchemaManager schemaManager )
95      {
96          if ( wrapped == null )
97          {
98              try
99              {
100                 wrapped = ( Comparator<E> )
101                     schemaManager.lookupComparatorRegistry( matchingRuleOid );
102             }
103             catch ( LdapException ne )
104             {
105                 // Not found : get the default comparator
106                 wrapped = ( Comparator<E> )
107                     new ComparableComparator<Comparable<E>>( matchingRuleOid );
108             }
109         }
110 
111         ( ( LdapComparator<E> ) wrapped ).setSchemaManager( schemaManager );
112         super.setSchemaManager( schemaManager );
113         this.schemaManager = schemaManager;
114     }
115 }