abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1196831 - in /abdera/abdera2: activities/src/main/java/org/apache/abdera2/activities/extra/ activities/src/main/java/org/apache/abdera2/activities/model/objects/ examples/src/main/java/org/apache/abdera2/examples/activities/
Date Wed, 02 Nov 2011 22:27:12 GMT
Author: jmsnell
Date: Wed Nov  2 22:27:11 2011
New Revision: 1196831

URL: http://svn.apache.org/viewvc?rev=1196831&view=rev
Log:
Ok.. so this update gives us an alternative way of doing extensions. the ASBase object supports
generic setProperty("foo","bar") style extensions but it's not typesafe and can be cumbersome.
Since we already have cglib in the dependencies now, I decided to use a different approach...
the ASBase object now has an extend method that allows you to attach an Interface using getter/setter
methods to an existing ASBase object. The cglib proxy will route the various get/set methods
called on that interface to the appropriate setProperty/getProperty methods on the underlying
ASBase... for instance,

interface Foo {
  String getBar();
  void setBar(String val);
}

PersonObject obj = new PersonObject();
Foo foo = obj.extend(Foo.class);
foo.setBar("data");
obj.writeTo(System.out);

will result in {"objectType":"person","bar":"data"} ... notice that the "setBar" method name
is mapped to the "bar" property. A property name like "getFooBarBaz" would be mapped to a
property named "fooBarBaz". Use the @Name attribute on the method in the interface to specify
a different name... e.g. "@Name("baz") String getBar()"

The nicest thing about this approach is that it's simple and type-safe.  

Added:
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/AdditionalEventProperties.java
  (contents, props changed)
      - copied, changed from r1196760, abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/ExtendedEventObject.java
Removed:
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/ExtendedEventObject.java
Modified:
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java
    abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java
    abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/ExtendingBaseObjectExample.java
    abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/ExtensionExample.java

Modified: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java?rev=1196831&r1=1196830&r2=1196831&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java
(original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java
Wed Nov  2 22:27:11 2011
@@ -12,7 +12,6 @@ import org.apache.abdera2.activities.mod
 import org.apache.abdera2.activities.model.Activity;
 import org.apache.abdera2.activities.model.Activity.Audience;
 import org.apache.abdera2.activities.model.Verb;
-import org.apache.abdera2.common.anno.AnnoUtil;
 import org.apache.abdera2.common.anno.Name;
 import org.apache.abdera2.common.date.DateTimes;
 import org.apache.abdera2.common.selector.AbstractSelector;
@@ -657,7 +656,6 @@ public class Extra {
       e.setSuperclass(type);
     }
     e.setCallback(new ExtensionWrapper(type,object));
-    object.setProperty("objectType",AnnoUtil.getName(type));
     return (M)e.create();
   }
   
@@ -686,11 +684,13 @@ public class Extra {
         String name = get_name(method);
         if (setter) {
           if (args.length != 1)
-            throw new IllegalArgumentException();
+            throw new UnsupportedOperationException();
           base.setProperty(name,args[0]);
-          return null;
-        } else {
+          return null; 
+        } else if (method.getParameterTypes().length == 0) {
           return method.getReturnType().cast(base.getProperty(name));
+        } else {
+          throw new UnsupportedOperationException();
         }
       } else return proxy.invokeSuper(base, args);
     }    

Copied: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/AdditionalEventProperties.java
(from r1196760, abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/ExtendedEventObject.java)
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/AdditionalEventProperties.java?p2=abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/AdditionalEventProperties.java&p1=abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/ExtendedEventObject.java&r1=1196760&r2=1196831&rev=1196831&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/ExtendedEventObject.java
(original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/AdditionalEventProperties.java
Wed Nov  2 22:27:11 2011
@@ -3,86 +3,24 @@ package org.apache.abdera2.activities.mo
 import org.apache.abdera2.activities.model.ASObject;
 
 /**
- * Abstract extension of the basic event object type that adds
- * additional useful fields. Subclasses of this object MUST
- * define their own objectType names using the Name annotation
- * (@see org.apache.abdera2.common.anno.Name). 
+ * Additional extension properties for the EventObject...
+ * attach this interface to an EventObject using the 
+ * extend method... e.g.
+ * 
+ * AdditionalEventProperties avp = event.extend(AdditionaEventProperties.class);
+ * avp.setHost(...);
+ * avp.setOffers(...);
+ * ...
  */
-@SuppressWarnings("unchecked")
-public abstract class ExtendedEventObject extends EventObject {
-
-  private static final long serialVersionUID = 8368535995814591315L;
-
-  public <T extends ASObject>T getHost() {
-    return (T)getProperty("host");
-  }
-  
-  public void setHost(ASObject host) {
-    setProperty("host", host);
-  }
-  
-  public <T extends ASObject>T getOffers() {
-    return (T)getProperty("offers");
-  }
-  
-  public void setOffers(ASObject offers) {
-    setProperty("offers", offers);
-  }
-  
-  public <T extends ASObject>T getSubEvents() {
-    return (T)getProperty("subEvents");
-  }
-  
-  public void setSubEvents(ASObject subEvents) {
-    setProperty("subEvents", subEvents);
-  }
-  
-  public <T extends ASObject>T getSuperEvent() {
-    return (T)getProperty("superEvent");
-  }
-  
-  public void setSuperEvent(ASObject superEvent) {
-    setProperty("superEvent", superEvent);
-  }
-  
-  public <T extends ASObject>T getPerformers() {
-    return (T)getProperty("performers");
-  }
-  
-  public void setPerformers(ASObject performers) {
-    setProperty("performers", performers);
-  }
-  
-  public static <T extends ExtendedEventObject>ExtendedEventObjectGenerator<T>
makeExtendedEvent() {
-    return new ExtendedEventObjectGenerator<T>();
-  }
-  
-  public static class ExtendedEventObjectGenerator<T extends ExtendedEventObject> extends
EventObjectGenerator<T> {
-    public ExtendedEventObjectGenerator() {
-      super((Class<? extends T>) ExtendedEventObject.class);
-    }
-    public ExtendedEventObjectGenerator(Class<T> _class) {
-      super(_class);
-    }
-    public <X extends ExtendedEventObjectGenerator<T>>X host(ASObject object)
{
-      item.setHost(object);
-      return (X)this;
-    }
-    public <X extends ExtendedEventObjectGenerator<T>>X offers(ASObject object)
{
-      item.setOffers(object);
-      return (X)this;
-    }
-    public <X extends ExtendedEventObjectGenerator<T>>X performers(ASObject object)
{
-      item.setPerformers(object);
-      return (X)this;
-    }
-    public <X extends ExtendedEventObjectGenerator<T>>X subEvents(ASObject object)
{
-      item.setSubEvents(object);
-      return (X)this;
-    }
-    public <X extends ExtendedEventObjectGenerator<T>>X superEvent(ASObject object)
{
-      item.setSuperEvent(object);
-      return (X)this;
-    }
-  }
+public interface AdditionalEventProperties {
+  <T extends ASObject>T getHost();
+  void setHost(ASObject host);
+  <T extends ASObject>T getOffers();
+  void setOffers(ASObject offers);
+  <T extends ASObject>T getSubEvents();
+  void setSubEvents(ASObject subEvents);
+  <T extends ASObject>T getSuperEvent();
+  void setSuperEvent(ASObject superEvent);
+  <T extends ASObject>T getPerformers();
+  void setPerformers(ASObject performers);
 }

Propchange: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/model/objects/AdditionalEventProperties.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java?rev=1196831&r1=1196830&r2=1196831&view=diff
==============================================================================
--- abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java
(original)
+++ abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java
Wed Nov  2 22:27:11 2011
@@ -35,8 +35,8 @@ public class BinaryDataObjectExample {
           makeBinary()
             .data(
               dataHandler, 
-              new Md5(), 
-              DEFLATE)
+              new Md5(),   // generate an md5 hash and set an "md5" property
+              DEFLATE)     // apply deflate compression to the data before encoding it
             .get())
         .get();
     
@@ -54,10 +54,9 @@ public class BinaryDataObjectExample {
     InputStream in = dataObject.getInputStream(); 
     byte[] buf = new byte[100];
     int r = -1;
-    while((r = in.read(buf)) > -1) {
+    while((r = in.read(buf)) > -1)
       check.update(buf, 0, r);
-      System.out.write(buf,0,r);
-    }
+    
     String checks = check.get();
     
     System.out.println(checks.equalsIgnoreCase(md5));

Modified: abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/ExtendingBaseObjectExample.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/ExtendingBaseObjectExample.java?rev=1196831&r1=1196830&r2=1196831&view=diff
==============================================================================
--- abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/ExtendingBaseObjectExample.java
(original)
+++ abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/ExtendingBaseObjectExample.java
Wed Nov  2 22:27:11 2011
@@ -1,9 +1,9 @@
 package org.apache.abdera2.examples.activities;
 
 import org.apache.abdera2.activities.model.IO;
-import org.apache.abdera2.activities.model.objects.ExtendedEventObject;
+import org.apache.abdera2.activities.model.objects.EventObject;
+import org.apache.abdera2.activities.model.objects.AdditionalEventProperties;
 import org.apache.abdera2.activities.model.objects.PersonObject;
-import org.apache.abdera2.common.anno.Name;
 
 /**
  * Quick example that shows how new object types can be
@@ -14,36 +14,16 @@ import org.apache.abdera2.common.anno.Na
  */
 public class ExtendingBaseObjectExample {
 
-  @SuppressWarnings("unchecked")
   public static void main(String... args) throws Exception {
-
-    // Building an activity stream for a reading list
     IO io = IO.get();
-    io.addObjectMapping(Hangout.class);
-    
-    Hangout hangout = new Hangout();
-    hangout.setHost(new PersonObject("james"));
-    hangout.getAttending(true).addItem(new PersonObject("joe"));
-
-    // another way to extend the Activity Stream object 
-    // is by using the extend() method and passing in a 
-    // simple Interface...
-    OtherExtensions oe = hangout.extend(OtherExtensions.class);
-    oe.setFoo("this is the value");
-    
-    // this is a type-safe alternative to using hangout.setProperty("foo","this is the value");
-    
-    io.write(hangout,System.out,"UTF-8");
-  }
-
-  @Name("hangout")
-  public static class Hangout 
-    extends ExtendedEventObject {
-    private static final long serialVersionUID = -5466869609152673390L;
+    EventObject event = new EventObject();
+    event.setObjectType("hangout");
+    AdditionalEventProperties ext = 
+      event.extend(
+        AdditionalEventProperties.class);
+    ext.setHost(new PersonObject("james"));
+    event.getAttending(true).addItem(new PersonObject("joe"));
+    io.write(event,System.out,"UTF-8");
   }
   
-  public static interface OtherExtensions {
-    public String getFoo();
-    public void setFoo(String bar);
-  }
 }

Modified: abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/ExtensionExample.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/ExtensionExample.java?rev=1196831&r1=1196830&r2=1196831&view=diff
==============================================================================
--- abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/ExtensionExample.java
(original)
+++ abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/ExtensionExample.java
Wed Nov  2 22:27:11 2011
@@ -5,8 +5,6 @@ import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.abdera2.activities.io.gson.AdaptedType;
-import org.apache.abdera2.activities.io.gson.Properties;
-import org.apache.abdera2.activities.io.gson.Property;
 import org.apache.abdera2.activities.io.gson.SimpleAdapter;
 import org.apache.abdera2.activities.model.ASObject;
 import org.apache.abdera2.activities.model.IO;
@@ -15,33 +13,33 @@ import org.apache.abdera2.common.http.En
 
 public class ExtensionExample {
 
-  @SuppressWarnings("unchecked")
   public static void main(String... args) throws Exception {
     
     // create the io with our custom type adapter
     IO io = IO.get(new BarAdapter());
+    io.addPropertyMapping("bar", Bar.class);
+    io.addPropertyMapping("etag", EntityTag.class);
     
-    // tell the serializer about our new object type
-    io.addObjectMapping(FooObject.class);
-    
-    FooObject foo = new FooObject();
+    ASObject as = new ASObject("foo");
+    Foo foo = as.extend(Foo.class);
     foo.setETag(new EntityTag("test",true));
     foo.setBar(new Bar("foobarbaz"));
         
     Map<Bar,String> map = new HashMap<Bar,String>();
     map.put(new Bar("z"),"a");
     map.put(new Bar("y"), "b");
-    foo.setProperty("map",map);
+    as.setProperty("map",map);
     
     // outputs: foo
-    System.out.println(foo.getObjectType());
+    System.out.println(as.getObjectType());
     
     // outputs: {"etag":"W/\"test\"","map":{"y":"b","z":"a"},"bar":"foobarbaz","objectType":"foo"}
-    foo.writeTo(io,System.out);
+    as.writeTo(io,System.out);
     
     // now try reading it
-    StringReader sr = new StringReader(io.write(foo));
-    foo = io.readObject(sr);
+    StringReader sr = new StringReader(io.write(as));
+    as = io.readObject(sr);
+    foo = as.extend(Foo.class);
 
     System.out.println();
     
@@ -51,30 +49,17 @@ public class ExtensionExample {
     System.out.println(foo.getBar().getClass());
     
     // map will deserialize as an asobject
-    System.out.println(foo.getProperty("map").getClass());
+    System.out.println(as.getProperty("map").getClass());
     
   }
   
-  @Name("foo")  // the value of the objectType property
-  @Properties({
-    // tell the deserializer to map the etag property to the EntityTag class
-    @Property(name="etag",to=EntityTag.class),
-    @Property(name="bar",to=Bar.class)
-  })
-  public static class FooObject extends ASObject {
-    private static final long serialVersionUID = 3601006822295281310L;
-    public EntityTag getETag() {
-      return getProperty("etag");
-    }
-    public void setETag(EntityTag etag) {
-      setProperty("etag", etag);
-    }
-    public Bar getBar() {
-      return getProperty("bar");
-    }
-    public void setBar(Bar bar) {
-      setProperty("bar",bar);
-    }
+  // the extension interface... defines the additional properties 
+  // we're going to use on our ASObject... 
+  public static interface Foo {
+    @Name("etag") EntityTag getETag();
+    @Name("etag") void setETag(EntityTag etag);
+    Bar getBar();
+    void setBar(Bar bar);
   }
   
   // Some new class that we want to use as a value.. need to tell 



Mime
View raw message