Skip to main content

[java-state-managemen~src-git:cf19bf5c] Group editing session changes Jan 30

  • From: mitch.upton@...
  • To: commits@...
  • Subject: [java-state-managemen~src-git:cf19bf5c] Group editing session changes Jan 30
  • Date: Wed, 5 Feb 2014 18:07:17 +0000

Project:    java-state-managemen
Repository: src-git
Revision:   cf19bf5cae8d5be10ee56ba4604c880efe98de50
Author:     mitch.upton
Date:       2014-02-05 16:47:07 UTC
Link:       

Log Message:
------------
Group editing session changes Jan 30



Revisions:
----------
cf19bf5cae8d5be10ee56ba4604c880efe98de50


Modified Paths:
---------------
state/examples/scenarios/src/main/java/EventScenarios.java


Added Paths:
------------
state/api/src/main/java/javax/state/capabilities/event/Access.java
state/api/src/main/java/javax/state/capabilities/event/Create.java
state/api/src/main/java/javax/state/capabilities/event/Delete.java
state/api/src/main/java/javax/state/capabilities/event/Update.java


Diffs:
------
--- /dev/null
+++ b/state/api/src/main/java/javax/state/capabilities/event/Access.java
@@ -0,0 +1,17 @@
+package javax.state.capabilities.event;
+
+import javax.inject.Qualifier;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Qualifier indicating a state object was accessed
+ */
+@Qualifier
+@Target({ElementType.FIELD, ElementType.PARAMETER})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Access {
+
+}--- a/state/api/src/main/java/javax/state/capabilities/event/Created.java
+++ b/state/api/src/main/java/javax/state/capabilities/event/Create.java
@@ -9,11 +9,10 @@ import javax.inject.Qualifier;
 
 /**
  * Qualifier indicating a state object was created
- * TODO: This is a placeholder for making state events CDI-enabled
  */
 @Qualifier
 @Target({ElementType.FIELD, ElementType.PARAMETER})
 @Retention(RetentionPolicy.RUNTIME)
-public @interface Created {
+public @interface Create {
 
 }--- /dev/null
+++ b/state/api/src/main/java/javax/state/capabilities/event/Delete.java
@@ -0,0 +1,17 @@
+package javax.state.capabilities.event;
+
+import javax.inject.Qualifier;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Qualifier indicating a state object was deleted
+ */
+@Qualifier
+@Target({ElementType.FIELD, ElementType.PARAMETER})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Delete {
+
+}--- /dev/null
+++ b/state/api/src/main/java/javax/state/capabilities/event/Update.java
@@ -0,0 +1,17 @@
+package javax.state.capabilities.event;
+
+import javax.inject.Qualifier;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Qualifier indicating a state object was updated
+ */
+@Qualifier
+@Target({ElementType.FIELD, ElementType.PARAMETER})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Update {
+
+}--- a/state/examples/scenarios/src/main/java/EventScenarios.java
+++ b/state/examples/scenarios/src/main/java/EventScenarios.java
@@ -101,6 +101,97 @@ public class EventScenarios {
         // Listener should have received DELETE event
     }
 
+    public void lifecycleEventsCDI() {
+
+        String connId = "com.acme.po.PurchaseOrders";
+
+        StateManagement stateManagement = null; // TODO: Injected? Looked 
up? ...
+
+        assert !stateManagement.getStateConnectionFactory().exists(connId) 
== false;
+
+        ProviderQuery query = stateManagement.createProviderQuery();
+
+        query.require(StateMapCapability.class);
+        query.require(LifecycleEventCapability.class);
+
+        ConfigurationBuilderFactory provider = query.execute();
+        ConfigurationBuilder builder = 
provider.createConfigurationBuilder(connId);
+        LifecycleEventCapabilityConfigurationBuilder eventBuilder = 
builder.configure(LifecycleEventCapability.class);
+
+        // Indicate the events we expect to receive.
+        // We do this because some providers may have to do extra work to 
deliver
+        // some specific event types, and if the client doesn't need them, 
this
+        // extra work would be wasted.
+        eventBuilder.setRequiredEventTypes(LifecycleEventType.ACCESS,
+                                           LifecycleEventType.CREATE,
+                                           LifecycleEventType.DELETE,
+                                           LifecycleEventType.UPDATE);
+
+        // TODO: Where to store this so it can be looked up for later use
+        // TODO: Allow default configuration such that you could inject
+        //       a preconfigured connections that cover the most common
+        //       cases (e.g. a basic persistent map, a persistent map that
+        //       supports lifecycle events, etc.)
+        Configuration connConfig = builder.build();
+
+        Connection conn = 
stateManagement.getStateConnectionFactory().connect(connConfig);
+
+        // Now let's use our connection's event capability to register a 
listener
+
+        LifecycleEventCapability eventCap = 
conn.capability(LifecycleEventCapability.class);
+
+        // TODO: Allow the use of CDI event handling instead of delegation
+        //       event model. This would be OK to *require* and replace
+        //       delegation completely.
+        eventCap.addStateEventListener(new LifecycleEventListener<String, 
PurchaseOrder>() {
+            @Override
+            public void before(LifecycleEvent<String, PurchaseOrder> event) {
+                System.out.println("Purchase Order " + event.getKey() + " 
before " + event.getType() + " object=" + event.getValue());
+                // TODO: Might 'veto' the update by throwing an exception?
+                // TODO: Maybe set a flag on the event object? The 
implementation
+                //       would check the flag after this method returns.
+            }
+
+            @Override
+            public void after(LifecycleEvent<String, PurchaseOrder> event) {
+                System.out.println("Purchase Order " + event.getKey() + " 
after " + event.getType() + " object=" + event.getValue());
+            }
+        }, String.class, PurchaseOrder.class);
+
+        LifecycleEventCapabilityConfiguration eventCapConfig = 
connConfig.capability(LifecycleEventCapability.class);
+        System.out.println("Our connection supports the following event 
types: " + eventCapConfig.getSupportedEventTypes());
+
+        // Now do some real work
+
+        PurchaseOrder po1 = new PurchaseOrder();
+        po1.customerId = "Acme";
+        po1.poId = "PO 1";
+        po1.productId = "Exploding darts";
+        po1.quantity = 100;
+
+        StateMap<String, PurchaseOrder> poMap =
+                conn.capability(StateMapCapability.class).
+                        getStateMap(String.class, PurchaseOrder.class);
+
+        poMap.put(po1.poId, po1);
+
+        // Listener should have received CREATE event
+
+        po1 = poMap.get(po1.poId);
+
+        // Listener should have received ACCESS event
+
+        po1.quantity = 101; // Object is now modified/dirty
+
+        poMap.put(po1.poId, po1);
+
+        // Listener should have received UPDATE event
+
+        poMap.remove(po1.poId);
+
+        // Listener should have received DELETE event
+    }
+
     public void idleTimeoutEvents()
         throws Exception {
         String connId = "com.acme.po.PurchaseOrders";





[java-state-managemen~src-git:cf19bf5c] Group editing session changes Jan 30

mitch . upton 02/05/2014
 
 
Close
loading
Please Confirm
Close