001 /* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at 010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE 011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE. 012 * See the License for the specific language governing permissions 013 * and limitations under the License. 014 * 015 * When distributing Covered Code, include this CDDL HEADER in each 016 * file and include the License file at 017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable, 018 * add the following below this CDDL HEADER, with the fields enclosed 019 * by brackets "[]" replaced with your own identifying information: 020 * Portions Copyright [yyyy] [name of copyright owner] 021 * 022 * CDDL HEADER END 023 * 024 * 025 * Copyright 2006-2008 Sun Microsystems, Inc. 026 */ 027 package org.opends.server.schema; 028 029 030 031 import org.opends.server.admin.std.server.OrderingMatchingRuleCfg; 032 import org.opends.server.api.OrderingMatchingRule; 033 import org.opends.server.config.ConfigException; 034 import org.opends.server.core.DirectoryServer; 035 import org.opends.server.loggers.debug.DebugTracer; 036 import org.opends.server.protocols.asn1.ASN1OctetString; 037 import org.opends.server.types.ByteString; 038 import org.opends.server.types.DebugLogLevel; 039 import org.opends.server.types.DirectoryException; 040 import org.opends.server.types.InitializationException; 041 042 import static org.opends.server.loggers.ErrorLogger.*; 043 import static org.opends.server.loggers.debug.DebugLogger.*; 044 import static org.opends.server.schema.SchemaConstants.*; 045 import static org.opends.server.util.ServerConstants.*; 046 047 048 049 /** 050 * This class defines the generalizedTimeOrderingMatch matching rule defined in 051 * X.520 and referenced in RFC 2252. 052 */ 053 public class GeneralizedTimeOrderingMatchingRule 054 extends OrderingMatchingRule 055 { 056 /** 057 * The tracer object for the debug logger. 058 */ 059 private static final DebugTracer TRACER = getTracer(); 060 061 062 063 /** 064 * The serial version identifier required to satisfy the compiler because this 065 * class implements the <CODE>java.io.Serializable</CODE> interface. This 066 * value was generated using the <CODE>serialver</CODE> command-line utility 067 * included with the Java SDK. 068 */ 069 private static final long serialVersionUID = -6343622924726948145L; 070 071 072 073 /** 074 * Creates a new instance of this generalizedTimeMatch matching rule. 075 */ 076 public GeneralizedTimeOrderingMatchingRule() 077 { 078 super(); 079 } 080 081 082 083 /** 084 * {@inheritDoc} 085 */ 086 public void initializeMatchingRule(OrderingMatchingRuleCfg configuration) 087 throws ConfigException, InitializationException 088 { 089 // No initialization is required. 090 } 091 092 093 094 /** 095 * Retrieves the common name for this matching rule. 096 * 097 * @return The common name for this matching rule, or <CODE>null</CODE> if 098 * it does not have a name. 099 */ 100 public String getName() 101 { 102 return OMR_GENERALIZED_TIME_NAME; 103 } 104 105 106 107 /** 108 * Retrieves the OID for this matching rule. 109 * 110 * @return The OID for this matching rule. 111 */ 112 public String getOID() 113 { 114 return OMR_GENERALIZED_TIME_OID; 115 } 116 117 118 119 /** 120 * Retrieves the description for this matching rule. 121 * 122 * @return The description for this matching rule, or <CODE>null</CODE> if 123 * there is none. 124 */ 125 public String getDescription() 126 { 127 // There is no standard description for this matching rule. 128 return null; 129 } 130 131 132 133 /** 134 * Retrieves the OID of the syntax with which this matching rule is 135 * associated. 136 * 137 * @return The OID of the syntax with which this matching rule is associated. 138 */ 139 public String getSyntaxOID() 140 { 141 return SYNTAX_GENERALIZED_TIME_OID; 142 } 143 144 145 146 /** 147 * Retrieves the normalized form of the provided value, which is best suited 148 * for efficiently performing matching operations on that value. 149 * 150 * @param value The value to be normalized. 151 * 152 * @return The normalized version of the provided value. 153 * 154 * @throws DirectoryException If the provided value is invalid according to 155 * the associated attribute syntax. 156 */ 157 public ByteString normalizeValue(ByteString value) 158 throws DirectoryException 159 { 160 try 161 { 162 long timestamp = GeneralizedTimeSyntax.decodeGeneralizedTimeValue(value); 163 return new ASN1OctetString(GeneralizedTimeSyntax.format(timestamp)); 164 } 165 catch (DirectoryException de) 166 { 167 if (debugEnabled()) 168 { 169 TRACER.debugCaught(DebugLogLevel.ERROR, de); 170 } 171 172 switch (DirectoryServer.getSyntaxEnforcementPolicy()) 173 { 174 case REJECT: 175 throw de; 176 177 case WARN: 178 logError(de.getMessageObject()); 179 return new ASN1OctetString(value.value()); 180 181 default: 182 return new ASN1OctetString(value.value()); 183 } 184 } 185 } 186 187 188 189 /** 190 * Compares the first value to the second and returns a value that indicates 191 * their relative order. 192 * 193 * @param value1 The normalized form of the first value to compare. 194 * @param value2 The normalized form of the second value to compare. 195 * 196 * @return A negative integer if <CODE>value1</CODE> should come before 197 * <CODE>value2</CODE> in ascending order, a positive integer if 198 * <CODE>value1</CODE> should come after <CODE>value2</CODE> in 199 * ascending order, or zero if there is no difference between the 200 * values with regard to ordering. 201 */ 202 public int compareValues(ByteString value1, ByteString value2) 203 { 204 try 205 { 206 long time1 = GeneralizedTimeSyntax.decodeGeneralizedTimeValue(value1); 207 long time2 = GeneralizedTimeSyntax.decodeGeneralizedTimeValue(value2); 208 209 if (time1 == time2) 210 { 211 return 0; 212 } 213 else if (time1 > time2) 214 { 215 return 1; 216 } 217 else 218 { 219 return -1; 220 } 221 } 222 catch (DirectoryException de) 223 { 224 if (debugEnabled()) 225 { 226 TRACER.debugCaught(DebugLogLevel.ERROR, de); 227 } 228 229 return 0; 230 } 231 } 232 233 234 235 /** 236 * Compares the contents of the provided byte arrays to determine their 237 * relative order. 238 * 239 * @param b1 The first byte array to use in the comparison. 240 * @param b2 The second byte array to use in the comparison. 241 * 242 * @return A negative integer if <CODE>b1</CODE> should come before 243 * <CODE>b2</CODE> in ascending order, a positive integer if 244 * <CODE>b1</CODE> should come after <CODE>b2</CODE> in ascending 245 * order, or zero if there is no difference between the values with 246 * regard to ordering. 247 */ 248 public int compare(byte[] b1, byte[] b2) 249 { 250 return compareValues(new ASN1OctetString(b1), new ASN1OctetString(b2)); 251 } 252 } 253