abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1177470 - in /abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities: client/ActivitiesClientBatchPusher.java client/ActivitiesClientPusher.java model/Collection.java
Date Fri, 30 Sep 2011 00:47:28 GMT
Author: jmsnell
Date: Fri Sep 30 00:47:28 2011
New Revision: 1177470

URL: http://svn.apache.org/viewvc?rev=1177470&view=rev
Log: (empty)

Added:
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientBatchPusher.java
  (with props)
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientPusher.java
  (with props)
Modified:
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/Collection.java

Added: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientBatchPusher.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientBatchPusher.java?rev=1177470&view=auto
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientBatchPusher.java
(added)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientBatchPusher.java
Fri Sep 30 00:47:28 2011
@@ -0,0 +1,50 @@
+package org.apache.abdera2.activities.client;
+
+import org.apache.abdera2.activities.model.ASObject;
+import org.apache.abdera2.activities.model.Collection;
+import org.apache.abdera2.protocol.client.RequestOptions;
+
+/**
+ * Identical to ActivitiesClientPusher with the exception that the 
+ * pushAll method will create a Collection object containing all of 
+ * the items and will post the single Collection object to the server
+ * rather than sending one post per item
+ */
+public class ActivitiesClientBatchPusher<T extends ASObject> 
+  extends ActivitiesClientPusher<T> {
+
+  public ActivitiesClientBatchPusher(String iri, ActivitiesSession session,
+      RequestOptions options) {
+    super(iri, session, options);
+  }
+
+  public ActivitiesClientBatchPusher(String iri, ActivitiesSession session) {
+    super(iri, session);
+  }
+
+  public ActivitiesClientBatchPusher(String iri, RequestOptions options) {
+    super(iri, options);
+  }
+
+  public ActivitiesClientBatchPusher(String iri) {
+    super(iri);
+  }
+
+  @Override
+  public void pushAll(final Iterable<T> t) {
+    exec.execute(
+        new Runnable() {
+          public void run() {
+            try {
+              Collection<T> col = new Collection<T>();
+              col.setItems(t);
+              handle(session.post(iri, col, options));
+            } catch (Throwable ex) {
+              handle(ex);
+            }
+          }
+        }
+      );
+  }
+
+}

Propchange: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientBatchPusher.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientPusher.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientPusher.java?rev=1177470&view=auto
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientPusher.java
(added)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientPusher.java
Fri Sep 30 00:47:28 2011
@@ -0,0 +1,87 @@
+package org.apache.abdera2.activities.client;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+
+import org.apache.abdera2.activities.model.ASObject;
+import org.apache.abdera2.common.pusher.Pusher;
+import org.apache.abdera2.protocol.client.ClientResponse;
+import org.apache.abdera2.protocol.client.RequestOptions;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Utility that wraps an ActivitiesClient and uses the pusher 
+ * interface to asynchronously send those off to somewhere else
+ * using POST requests. 
+ */
+public class ActivitiesClientPusher<T extends ASObject> 
+  implements Pusher<T> {
+  private final static Log log = LogFactory.getLog(ActivitiesClientPusher.class);
+  
+  protected final ActivitiesSession session;
+  protected final String iri;
+  protected final Executor exec;
+  protected final RequestOptions options;
+
+  public ActivitiesClientPusher(String iri) {
+    this(iri, initSession());
+  }
+  
+  public ActivitiesClientPusher(String iri, RequestOptions options) {
+    this(iri, initSession(), options);
+  }
+  
+  private static ActivitiesSession initSession() {
+    return new ActivitiesClient().newSession();
+  }
+  
+  public ActivitiesClientPusher(
+    String iri, 
+    ActivitiesSession session) {
+      this(iri,session,session.getDefaultRequestOptions());
+  }
+  
+  public ActivitiesClientPusher(
+    String iri, 
+    ActivitiesSession session,
+    RequestOptions options) {
+    this.session = session;
+    this.iri = iri;
+    this.options = options;
+    this.exec = initExecutor();
+  }
+  
+  protected Executor initExecutor() {
+    return Executors.newSingleThreadExecutor();
+  }
+  
+  public void push(final T t) {
+    exec.execute(
+      new Runnable() {
+        public void run() {
+          try {
+            handle(session.post(iri, t, options));
+          } catch (Throwable ex) {
+            handle(ex);
+          }
+        }
+      }
+    );
+  }
+  
+  protected void handle(ClientResponse resp) {
+    // by default, do nothing, fire'n'forget
+    resp.release();
+  }
+  
+  protected void handle(Throwable t) {
+    // by default, do nothing
+    log.error(t);
+  }
+
+  public void pushAll(Iterable<T> t) {
+    for (T i : t) push(i);
+  }
+
+}

Propchange: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientPusher.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/Collection.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/Collection.java?rev=1177470&r1=1177469&r2=1177470&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/Collection.java
(original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/Collection.java
Fri Sep 30 00:47:28 2011
@@ -93,6 +93,12 @@ public class Collection<T extends ASObje
     setTotalItems(items.size());
   }
   
+  public void setItems(Iterable<T> items) {
+    Set<T> set = new LinkedHashSet<T>();
+    for (T item : items) set.add(item);
+    setItems(set);
+  }
+  
   public void addItem(T... items) {
     Set<T> list = getProperty(ITEMS);
     if (list == null) {



Mime
View raw message