From 38fe921bd2d09734277828b638a4947e45a99bcd Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Fri, 25 Oct 2013 09:44:02 +0000
Subject: [PATCH] Checkpoint commit for OPENDJ-175: Decouple OpenDJ LDAP SDK from Grizzly

---
 opendj3/opendj-grizzly/src/main/java/com/forgerock/opendj/grizzly/GrizzlyUtils.java |   62 ++++++++++++++++++++++++++++++
 1 files changed, 61 insertions(+), 1 deletions(-)

diff --git a/opendj3/opendj-grizzly/src/main/java/com/forgerock/opendj/grizzly/GrizzlyUtils.java b/opendj3/opendj-grizzly/src/main/java/com/forgerock/opendj/grizzly/GrizzlyUtils.java
index 4750bdd..d81e72f 100644
--- a/opendj3/opendj-grizzly/src/main/java/com/forgerock/opendj/grizzly/GrizzlyUtils.java
+++ b/opendj3/opendj-grizzly/src/main/java/com/forgerock/opendj/grizzly/GrizzlyUtils.java
@@ -25,11 +25,16 @@
  */
 package com.forgerock.opendj.grizzly;
 
+import org.forgerock.opendj.io.LDAPReader;
+import org.forgerock.opendj.io.LDAPWriter;
+import org.forgerock.opendj.ldap.DecodeOptions;
 import org.glassfish.grizzly.Processor;
+import org.glassfish.grizzly.ThreadCache;
 import org.glassfish.grizzly.filterchain.Filter;
 import org.glassfish.grizzly.filterchain.FilterChain;
 import org.glassfish.grizzly.filterchain.FilterChainBuilder;
 import org.glassfish.grizzly.filterchain.TransportFilter;
+import org.glassfish.grizzly.memory.MemoryManager;
 import org.glassfish.grizzly.ssl.SSLFilter;
 
 /**
@@ -38,6 +43,10 @@
 final class GrizzlyUtils {
 
 
+    @SuppressWarnings("rawtypes")
+    private static final ThreadCache.CachedTypeIndex<LDAPWriter> WRITER_INDEX = ThreadCache
+            .obtainIndex(LDAPWriter.class, 1);
+
     /**
      * Build a filter chain from the provided processor if possible and the
      * provided filter.
@@ -57,7 +66,7 @@
      *         is a {@code FilterChain}, and having the provided filter as the
      *         last filter
      */
-    public static FilterChain buildFilterChain(Processor processor, Filter filter) {
+    public static FilterChain buildFilterChain(Processor<?> processor, Filter filter) {
         if (processor instanceof FilterChain) {
             return FilterChainBuilder.stateless().addAll((FilterChain) processor).add(filter).build();
         } else {
@@ -118,6 +127,57 @@
         return FilterChainBuilder.stateless().addAll(chain).add(indexToAddFilter, filter).build();
     }
 
+    /**
+     * Creates a new LDAP Reader with the provided maximum size of ASN1 element,
+     * options and memory manager.
+     *
+     * @param decodeOptions
+     *            allow to control how responses and requests are decoded
+     * @param maxASN1ElementSize
+     *            The maximum BER element size, or <code>0</code> to indicate
+     *            that there is no limit.
+     * @param memoryManager
+     *            The memory manager to use for buffering.
+     * @return a LDAP reader
+     */
+    public static LDAPReader<ASN1BufferReader> createReader(DecodeOptions decodeOptions, int maxASN1ElementSize,
+            MemoryManager<?> memoryManager) {
+        ASN1BufferReader asn1Reader = new ASN1BufferReader(maxASN1ElementSize, memoryManager);
+        return new LDAPReader<ASN1BufferReader>(asn1Reader, decodeOptions);
+    }
+
+    /**
+     * Returns a LDAP writer, with a clean ASN1Writer, possibly from
+     * the thread local cache.
+     * <p>
+     * The writer is either returned from thread local cache or created.
+     * In the former case, the writer is removed from the cache.
+     *
+     * @return a LDAP writer
+     */
+    @SuppressWarnings("unchecked")
+    public static LDAPWriter<ASN1BufferWriter> getWriter() {
+        LDAPWriter<ASN1BufferWriter> writer = ThreadCache.takeFromCache(WRITER_INDEX);
+        if (writer == null) {
+            writer = new LDAPWriter<ASN1BufferWriter>(new ASN1BufferWriter());
+        }
+        writer.getASN1Writer().reset();
+        return writer;
+    }
+
+    /**
+     * Recycle a LDAP writer to a thread local cache.
+     * <p>
+     * The LDAP writer is then available for the thread using the
+     * {@get()} method.
+     *
+     * @param writer LDAP writer to recycle
+     */
+    public static void recycleWriter(LDAPWriter<ASN1BufferWriter> writer) {
+        writer.getASN1Writer().recycle();
+        ThreadCache.putToCache(WRITER_INDEX, writer);
+    }
+
     // Prevent instantiation.
     private GrizzlyUtils() {
         // No implementation required.

--
Gitblit v1.10.0