From d559ed32d7175b1448dadaf75e338b8462baff51 Mon Sep 17 00:00:00 2001
From: jdemendi <jdemendi@localhost>
Date: Tue, 02 Dec 2008 14:57:07 +0000
Subject: [PATCH] add notification system for the saturation index of the workflow element

---
 opendj-sdk/opends/src/server/org/opends/server/workflowelement/ObservableWorkflowElement.java       |   67 +++++++++++++
 opendj-sdk/opends/src/server/org/opends/server/workflowelement/WorkflowElement.java                 |   72 ++++++++++++++
 opendj-sdk/opends/src/server/org/opends/server/workflowelement/ObservableWorkflowElementStatus.java |  105 +++++++++++++++++++++
 opendj-sdk/opends/src/server/org/opends/server/workflowelement/ObservableWorkflowElementState.java  |   24 +---
 4 files changed, 247 insertions(+), 21 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/workflowelement/ObservableWorkflowElement.java b/opendj-sdk/opends/src/server/org/opends/server/workflowelement/ObservableWorkflowElement.java
new file mode 100644
index 0000000..581bc5b
--- /dev/null
+++ b/opendj-sdk/opends/src/server/org/opends/server/workflowelement/ObservableWorkflowElement.java
@@ -0,0 +1,67 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE
+ * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Copyright 2008 Sun Microsystems, Inc.
+ */
+package org.opends.server.workflowelement;
+
+
+import java.util.Observable;
+
+
+/**
+ * This class is used to implement a notification system for any
+ * information in a workflow element.
+ */
+public abstract class ObservableWorkflowElement extends Observable
+{
+  // The observed workflow element
+  private final WorkflowElement<?> observedWorkflowElement;
+
+
+  /**
+   * Creates an instance of the observable object for a given workflow
+   * element.
+   *
+   * @param  workflowElement
+   *         The workflow element for which this observable object is created.
+   */
+  protected ObservableWorkflowElement(
+      WorkflowElement<?> workflowElement
+      )
+  {
+    this.observedWorkflowElement = workflowElement;
+  }
+
+
+  /**
+   * Gets the observed workflow element.
+   *
+   * @return the observed workflow element.
+   */
+  public WorkflowElement<?> getObservedWorkflowElement()
+  {
+    return observedWorkflowElement;
+  }
+}
diff --git a/opendj-sdk/opends/src/server/org/opends/server/workflowelement/ObservableWorkflowElementState.java b/opendj-sdk/opends/src/server/org/opends/server/workflowelement/ObservableWorkflowElementState.java
index 6ba7914..4865911 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/workflowelement/ObservableWorkflowElementState.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/workflowelement/ObservableWorkflowElementState.java
@@ -26,8 +26,6 @@
  */
 package org.opends.server.workflowelement;
 
-import java.util.Observable;
-
 
 /**
  * This class implements an observable workflow element state.
@@ -35,12 +33,9 @@
  * state of the workflow element has changed. Typically, observers are
  * notified when a workflow element is enabled or disabled.
  */
-public class ObservableWorkflowElementState extends Observable
+public class ObservableWorkflowElementState
+    extends ObservableWorkflowElement
 {
-  // The observed workflow element
-  private final WorkflowElement<?> observedWorkflowElement;
-
-
   // The "enabled" state of the observed workflow element.
   // By default, a workflow element is enabled (otherwise this
   // instance of workflow element state would not exist).
@@ -51,22 +46,13 @@
    * Creates an instance of an observable object for a given workflow
    * element.
    *
-   * @param observedWorkflowElement  the workflow element to observe
+   * @param  observedWorkflowElement
+   *         The workflow element to observe.
    */
   public ObservableWorkflowElementState(
       WorkflowElement<?> observedWorkflowElement)
   {
-    this.observedWorkflowElement = observedWorkflowElement;
-  }
-
-
-  /**
-   * Gets the observed workflow element.
-   * @return the observed workflow element.
-   */
-  public WorkflowElement<?> getObservedWorkflowElement()
-  {
-    return observedWorkflowElement;
+    super(observedWorkflowElement);
   }
 
 
diff --git a/opendj-sdk/opends/src/server/org/opends/server/workflowelement/ObservableWorkflowElementStatus.java b/opendj-sdk/opends/src/server/org/opends/server/workflowelement/ObservableWorkflowElementStatus.java
new file mode 100644
index 0000000..628c7c3
--- /dev/null
+++ b/opendj-sdk/opends/src/server/org/opends/server/workflowelement/ObservableWorkflowElementStatus.java
@@ -0,0 +1,105 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE
+ * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Copyright 2008 Sun Microsystems, Inc.
+ */
+package org.opends.server.workflowelement;
+
+
+/**
+ * This class implements an observable workflow element status.
+ * The observable workflow element status notifies observers when
+ * the status of the workflow element has changed.
+ * <p>
+ * The status of a workflow reflects the "health" of the workflow element.
+ * The measure of the health is defined by an index - the saturation index -
+ * whose value may vary within the range [0 - 100].
+ * <p>
+ * An index value of 100 means that the workflow element fully operational.
+ * An index value of 0 means that the workflow element is no more operational.
+ * An value in between means that the workflow element is in a degraded mode.
+ * The lower the index value, the more degraded the workflow element is.
+ */
+public class ObservableWorkflowElementStatus
+    extends ObservableWorkflowElement
+{
+  /**
+   * The health indicator (aka saturation index) of the workflow element.
+   */
+  private int saturationIndex = 100;
+  private Object saturationIndexLock = new Object();
+
+
+  /**
+   * Creates an instance of an observable status for a given workflow
+   * element.
+   *
+   * @param  observedWorkflowElement
+   *         The workflow element which exposes its status.
+   */
+  public ObservableWorkflowElementStatus(
+      WorkflowElement<?> observedWorkflowElement
+      )
+  {
+    super(observedWorkflowElement);
+  }
+
+
+  /**
+   * Provides the saturation index of the workflow element.
+   *
+   * @return the saturation index of the workflow element.
+   */
+  public int getSaturationIndex()
+  {
+    return saturationIndex;
+  }
+
+
+  /**
+   * Updates the saturation index of the workflow element. Once the
+   * index has been updated, all the observers registered with this
+   * index are notified.
+   *
+   * @param  newValue
+   *         The new value of the saturation index.
+   */
+  public void setSaturationIndex(int newValue)
+  {
+    synchronized (saturationIndexLock)
+    {
+      if (newValue != saturationIndex)
+      {
+        this.saturationIndex = newValue;
+        setChanged();
+      }
+    }
+
+    // If the value has been updated then notify all the observers.
+    if (hasChanged())
+    {
+      notifyObservers();
+    }
+  }
+}
diff --git a/opendj-sdk/opends/src/server/org/opends/server/workflowelement/WorkflowElement.java b/opendj-sdk/opends/src/server/org/opends/server/workflowelement/WorkflowElement.java
index d639f9a..70d9a3b 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/workflowelement/WorkflowElement.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/workflowelement/WorkflowElement.java
@@ -80,6 +80,13 @@
       new ConcurrentHashMap<String, List<Observer>>();
 
 
+  // The observable status of the workflow element.
+  // The status contains the health indicator (aka saturation index)
+  // of the workflow element.
+  private ObservableWorkflowElementStatus observableStatus =
+    new ObservableWorkflowElementStatus(this);
+
+
   /**
    * Provides the observable state of the workflow element.
    * This method is intended to be called by the WorkflowElementConfigManager
@@ -95,6 +102,17 @@
 
 
   /**
+   * Provides the observable status of the workflow element.
+   *
+   * @return the observable status of the workflow element.
+   */
+  protected ObservableWorkflowElementStatus getObservableStatus()
+  {
+    return observableStatus;
+  }
+
+
+  /**
    * Registers with a specific workflow element to be notified when the
    * workflow element state has changed. This notification system is
    * mainly used to be warned when a workflow element is enabled or
@@ -187,7 +205,8 @@
       ObservableWorkflowElementState westate = we.getObservableState();
       westate.deleteObserver(observer);
     }
-    else
+
+    if (weid != null)
     {
       List<Observer> observers = newWorkflowElementNotificationList.get(weid);
       if (observers != null)
@@ -362,5 +381,54 @@
   {
     return workflowElementID;
   }
-}
 
+
+  /**
+   * Modifies the saturation index of the workflow element.
+   *
+   * @param  newValue
+   *         The new value of the saturation index of the workflow element.
+   */
+  public void setSaturationIndex(int newValue)
+  {
+    observableStatus.setSaturationIndex(newValue);
+  }
+
+
+  /**
+   * Gets the saturation index of the workflow element.
+   *
+   * @return  the value of the saturation index of the workflow element.
+   */
+  public int getSaturationIndex()
+  {
+    return observableStatus.getSaturationIndex();
+  }
+
+
+  /**
+   * Registers an observer with the saturation index of the workflow
+   * element. The observer will be notified when the saturation index
+   * is updated.
+   *
+   * @param  observer
+   *         The observer to notify when the saturation index is modified.
+   */
+  public void registerForSaturationIndexUpdate(Observer observer)
+  {
+    observableStatus.addObserver(observer);
+  }
+
+
+  /**
+   * Deregisters an observer with the saturation index of the workflow
+   * element.
+   *
+   * @param  observer
+   *         The observer to deregister.
+   */
+  public void deregisterForSaturationIndexUpdate(Observer observer)
+  {
+    observableStatus.deleteObserver(observer);
+  }
+}

--
Gitblit v1.10.0