Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 76420 invoked from network); 28 Jul 2010 20:06:17 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 28 Jul 2010 20:06:17 -0000 Received: (qmail 92826 invoked by uid 500); 28 Jul 2010 20:06:17 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 92775 invoked by uid 500); 28 Jul 2010 20:06:17 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 92768 invoked by uid 99); 28 Jul 2010 20:06:17 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Jul 2010 20:06:17 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Jul 2010 20:06:14 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id B102223889DD; Wed, 28 Jul 2010 20:04:57 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r980192 - /directory/apacheds/branches/apacheds-dnfactory-experiment/core/src/main/java/org/apache/directory/server/core/DNFactory.java Date: Wed, 28 Jul 2010 20:04:57 -0000 To: commits@directory.apache.org From: kayyagari@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100728200457.B102223889DD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: kayyagari Date: Wed Jul 28 20:04:57 2010 New Revision: 980192 URL: http://svn.apache.org/viewvc?rev=980192&view=rev Log: o a simple cached DN factory implementation Added: directory/apacheds/branches/apacheds-dnfactory-experiment/core/src/main/java/org/apache/directory/server/core/DNFactory.java Added: directory/apacheds/branches/apacheds-dnfactory-experiment/core/src/main/java/org/apache/directory/server/core/DNFactory.java URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-dnfactory-experiment/core/src/main/java/org/apache/directory/server/core/DNFactory.java?rev=980192&view=auto ============================================================================== --- directory/apacheds/branches/apacheds-dnfactory-experiment/core/src/main/java/org/apache/directory/server/core/DNFactory.java (added) +++ directory/apacheds/branches/apacheds-dnfactory-experiment/core/src/main/java/org/apache/directory/server/core/DNFactory.java Wed Jul 28 20:04:57 2010 @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.directory.server.core; + + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.directory.shared.ldap.exception.LdapInvalidDnException; +import org.apache.directory.shared.ldap.name.DN; +import org.apache.directory.shared.ldap.schema.SchemaManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * A DN factory. + * + * @author Apache Directory Project + */ +public class DNFactory +{ + + /** a map of tuples */ + // NOTE: currently a map is used, will eventually be replaced with ehCache if thie experiment succeeds + private static final Map DN_CACHE = new ConcurrentHashMap(); + + private static final Logger LOG = LoggerFactory.getLogger( DNFactory.class ); + + // stat counters + private static int hitCount = 0; + private static int missCount = 0; + + + /** + * searches the cache first for a possible DN value based on the given 'upName' match. + * If a DN is present in the cache will return it (after normalizing if required) + * otherwise will create a new DN instance and stores in the cache before returning it + * + * Note that the DN cache is maintained by using user provided DN name as key + * + * @param dn the upName of the DN + * @param schemaManager the schema manager (optional) + * @return a DN + * @throws LdapInvalidDnException + */ + public static DN create( String dn, SchemaManager schemaManager ) throws LdapInvalidDnException + { + if ( dn == null || dn.trim().length() == 0 ) + { + return null; + } + + DN cachedDN = DN_CACHE.get( dn ); + + if ( cachedDN == null ) + { + LOG.debug( "DN {} not found in the cache, creating", dn ); + + cachedDN = new DN( dn, schemaManager ); + + DN_CACHE.put( dn, cachedDN ); + missCount++; + } + else + { + if ( !cachedDN.isNormalized() && ( schemaManager != null ) ) + { + cachedDN.normalize( schemaManager.getNormalizerMapping() ); + } + + hitCount++; + } + + LOG.debug( "DN {} found in the cache", dn ); + System.out.println( "DN cache hit - " + hitCount + ", miss - " + missCount + " and is normalized = " + + cachedDN.isNormalized() ); + return cachedDN; + } + + + public static DN create( String... upRdns ) throws LdapInvalidDnException + { + return create( null, upRdns ); + } + + + public static DN create( SchemaManager schemaManager, String... upRdns ) throws LdapInvalidDnException + { + StringBuilder sb = new StringBuilder(); + for ( String s : upRdns ) + { + sb.append( s ).append( ',' ); + } + + String dn = sb.toString(); + dn = dn.substring( 0, dn.length() - 1 ); + return create( dn, schemaManager ); + } + + + public static DN create( DN dn ) throws LdapInvalidDnException + { + return create( dn.getName(), null ); + } + + + public static DN create( DN dn, SchemaManager schemaManager ) throws LdapInvalidDnException + { + return create( dn.getName(), schemaManager ); + } + + + public static DN create( String dn ) throws LdapInvalidDnException + { + return create( dn, null ); + } + +}