From eedbc609a3b3920a2c402c51f894939b114410f8 Mon Sep 17 00:00:00 2001
From: jdemendi <jdemendi@localhost>
Date: Thu, 20 Sep 2007 07:08:29 +0000
Subject: [PATCH] 

---
 opends/src/server/org/opends/server/workflowelement/localbackend/LocalBackendWorkflowElement.java |  132 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 129 insertions(+), 3 deletions(-)

diff --git a/opends/src/server/org/opends/server/workflowelement/localbackend/LocalBackendWorkflowElement.java b/opends/src/server/org/opends/server/workflowelement/localbackend/LocalBackendWorkflowElement.java
index 255cd3f..c6f4b33 100644
--- a/opends/src/server/org/opends/server/workflowelement/localbackend/LocalBackendWorkflowElement.java
+++ b/opends/src/server/org/opends/server/workflowelement/localbackend/LocalBackendWorkflowElement.java
@@ -46,6 +46,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.TreeMap;
 import java.util.concurrent.locks.Lock;
 
 import org.opends.server.admin.std.meta.PasswordPolicyCfgDefn;
@@ -139,8 +140,17 @@
    */
   private static final DebugTracer TRACER = DebugLogger.getTracer();
 
-  // the backend associated to that workflow element
-  Backend backend;
+  // the backend associated with the local workflow element
+  private Backend backend;
+
+  // the set of local backend workflow elements registered with the server
+  private static
+    TreeMap<String, LocalBackendWorkflowElement> registeredLocalBackends =
+      new TreeMap<String, LocalBackendWorkflowElement>();
+
+  // a lock to guarantee safe concurrent access to the registeredLocalBackends
+  // variable
+  private static Object registeredLocalBackendsLock = new Object();
 
 
   /**
@@ -149,7 +159,7 @@
    * @param workflowElementID  the workflow element identifier
    * @param backend  the backend associated to that workflow element
    */
-  public LocalBackendWorkflowElement(
+  private LocalBackendWorkflowElement(
       String  workflowElementID,
       Backend backend
       )
@@ -166,6 +176,122 @@
 
 
   /**
+   * Creates and registers a local backend with the server.
+   *
+   * @param workflowElementID  the identifier of the workflow element to create
+   * @param backend            the backend to associate with the local backend
+   *                           workflow element
+   *
+   * @return the existing local backend workflow element if it was
+   *         already created or a newly created local backend workflow
+   *         element.
+   */
+  public static LocalBackendWorkflowElement create(
+      String  workflowElementID,
+      Backend backend
+      )
+  {
+    LocalBackendWorkflowElement localBackend = null;
+
+    // If the requested workflow element does not exist then create one.
+    localBackend = registeredLocalBackends.get(workflowElementID);
+    if (localBackend == null)
+    {
+      localBackend = new LocalBackendWorkflowElement(
+          workflowElementID, backend);
+
+      // store the new local backend in the list of registered backends
+      registerLocalBackend(localBackend);
+    }
+
+    return localBackend;
+  }
+
+
+  /**
+   * Removes a local backend that was registered with the server.
+   *
+   * @param workflowElementID  the identifier of the workflow element to remove
+   */
+  public static void remove(
+      String workflowElementID
+      )
+  {
+    deregisterLocalBackend(workflowElementID);
+  }
+
+
+  /**
+   * Removes all the local backends that were registered with the server.
+   * This function is intended to be called when the server is shutting down.
+   */
+  public static void removeAll()
+  {
+    synchronized (registeredLocalBackendsLock)
+    {
+      for (LocalBackendWorkflowElement localBackend:
+           registeredLocalBackends.values())
+      {
+        deregisterLocalBackend(localBackend.getWorkflowElementID());
+      }
+    }
+  }
+
+
+  /**
+   * Registers a local backend with the server.
+   *
+   * @param localBackend  the local backend to register with the server
+   */
+  private static void registerLocalBackend(
+      LocalBackendWorkflowElement localBackend
+      )
+  {
+    synchronized (registeredLocalBackendsLock)
+    {
+      String localBackendID = localBackend.getWorkflowElementID();
+      LocalBackendWorkflowElement existingLocalBackend =
+        registeredLocalBackends.get(localBackendID);
+
+      if (existingLocalBackend == null)
+      {
+        TreeMap<String, LocalBackendWorkflowElement> newLocalBackends =
+          new TreeMap
+            <String, LocalBackendWorkflowElement>(registeredLocalBackends);
+        newLocalBackends.put(localBackendID, localBackend);
+        registeredLocalBackends = newLocalBackends;
+      }
+    }
+  }
+
+
+  /**
+   * Deregisters a local backend with the server.
+   *
+   * @param workflowElementID  the identifier of the workflow element to remove
+   */
+  private static void deregisterLocalBackend(
+      String workflowElementID
+      )
+  {
+    synchronized (registeredLocalBackendsLock)
+    {
+      LocalBackendWorkflowElement existingLocalBackend =
+        registeredLocalBackends.get(workflowElementID);
+
+      if (existingLocalBackend != null)
+      {
+        TreeMap<String, LocalBackendWorkflowElement> newLocalBackends =
+          new TreeMap
+            <String, LocalBackendWorkflowElement>(registeredLocalBackends);
+        newLocalBackends.remove(workflowElementID);
+        registeredLocalBackends = newLocalBackends;
+      }
+    }
+  }
+
+
+  /**
    * {@inheritDoc}
    */
   public void execute(Operation operation)

--
Gitblit v1.10.0