001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.shared.ldap.model.schema.comparators; 021 022 023import java.io.Serializable; 024import java.util.Comparator; 025 026import org.apache.directory.shared.i18n.I18n; 027import org.apache.directory.shared.ldap.model.exception.LdapException; 028import org.apache.directory.shared.ldap.model.schema.LdapComparator; 029import org.apache.directory.shared.ldap.model.schema.SchemaManager; 030 031 032/** 033 * A serializable wrapper around a Comparator which uses delayed initialization 034 * of the underlying wrapped comparator which is JIT resolved from a static 035 * global registry. 036 * 037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 038 */ 039public class SerializableComparator<E> extends LdapComparator<E> implements Serializable 040{ 041 /** The serial version UID */ 042 private static final long serialVersionUID = 2L; 043 044 /** the OID of the matchingRule for this comparator */ 045 private String matchingRuleOid; 046 047 /** the transient wrapped comparator */ 048 private transient Comparator<E> wrapped; 049 050 /** A reference to the schema manager */ 051 private transient SchemaManager schemaManager; 052 053 // ------------------------------------------------------------------------ 054 // C O N T R U C T O R S 055 // ------------------------------------------------------------------------ 056 public SerializableComparator( String matchingRuleOid ) 057 { 058 super( matchingRuleOid ); 059 this.matchingRuleOid = matchingRuleOid; 060 } 061 062 063 // ------------------------------------------------------------------------ 064 // 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 065 // ------------------------------------------------------------------------ 066 067 /** 068 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 069 */ 070 @SuppressWarnings("unchecked") 071 public int compare( E o1, E o2 ) 072 { 073 if ( wrapped == null ) 074 { 075 try 076 { 077 wrapped = (Comparator<E>)schemaManager.lookupComparatorRegistry( matchingRuleOid ); 078 } 079 catch ( LdapException e ) 080 { 081 throw new RuntimeException( I18n.err( I18n.ERR_04221, matchingRuleOid ) ); 082 } 083 } 084 085 return wrapped.compare( o1, o2 ); 086 } 087 088 089 /** 090 * @param schemaManager the schemaManager to set 091 */ 092 @SuppressWarnings("unchecked") 093 public void setSchemaManager( SchemaManager schemaManager ) 094 { 095 if ( wrapped == null ) 096 { 097 try 098 { 099 wrapped = ( Comparator<E> ) 100 schemaManager.lookupComparatorRegistry( matchingRuleOid ); 101 } 102 catch ( LdapException ne ) 103 { 104 // Not found : get the default comparator 105 wrapped = ( Comparator<E> ) 106 new ComparableComparator<Comparable<E>>( matchingRuleOid ); 107 } 108 } 109 110 ((LdapComparator<E>)wrapped).setSchemaManager( schemaManager ); 111 super.setSchemaManager( schemaManager ); 112 } 113}