myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From we...@apache.org
Subject svn commit: r809918 - in /myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog: Blog.java JSFUtil.java Null.java
Date Tue, 01 Sep 2009 09:19:50 GMT
Author: werpu
Date: Tue Sep  1 09:19:50 2009
New Revision: 809918

URL: http://svn.apache.org/viewvc?rev=809918&view=rev
Log:
https://issues.apache.org/jira/browse/EXTSCRIPT-3
added documentation added a tighter convenience method behavior


Modified:
    myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Blog.java
    myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java
    myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Null.java

Modified: myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Blog.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Blog.java?rev=809918&r1=809917&r2=809918&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Blog.java
(original)
+++ myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Blog.java
Tue Sep  1 09:19:50 2009
@@ -4,6 +4,8 @@
 import org.apache.commons.logging.LogFactory;
 import org.apache.myfaces.scripting.loaders.java.ScriptingClass;
 
+import static org.apache.myfaces.javaloader.blog.JSFUtil.*;
+
 /**
  * @author werpu2
  * @date: 01.09.2009
@@ -16,25 +18,25 @@
     String title1 = "You can alter the code for this small blogging application on the fly,
" +
                     "you even can add new classes on the fly and Grooy will pick it up";
 
+
     String firstName = "";
     String lastName = "";
     String topic = "";
 
     String content = "";
-    
+
 
     private Log getLog() {
         return LogFactory.getLog(this.getClass());
     }
 
 
-    
     public String addEntry() {
         getLog().info("adding entry");
 
-       Object service = JSFUtil.resolveVariable("javaBlogService");
+        Object service = JSFUtil.resolveVariable("javaBlogService");
 
-       if (service == null) {
+        if (service == null) {
             getLog().error("service not found");
         } else {
             getLog().info("service found");
@@ -47,13 +49,22 @@
         entry.setLastName(lastName);
         entry.setTopic(topic);
         entry.setContent(content);
-        
-        if(service != null) {
-            JSFUtil.executeMethod(service, "addEntry", new Cast(Object.class,entry));
-        }
 
+        if (service != null) {
+            /*convenience method to call a method on an object dynamically
+            * executeMethod and cast are static imports which encapsule the
+            * ugly stuff the java introspection provides and reduce
+            * the loc down to sane levels
+            *
+            * note the behavior in case of calling errors
+            * is changed from the default managed behavior
+            * to an unmanaged behavior. This is mostly
+            * the same behavior you get from scripting engines!
+            * 
+            */
+            executeMethod(service, "addEntry", cast(Object.class, entry));
+        }
 
-        
         //we stay on the same page
         return null;
     }

Modified: myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java?rev=809918&r1=809917&r2=809918&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java
(original)
+++ myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java
Tue Sep  1 09:19:50 2009
@@ -12,12 +12,20 @@
 /**
  * @author werpu2
  * @date: 01.09.2009
+ * <p/>
+ * A helper for JSF and introspection related tasks
  */
 public class JSFUtil {
 
     public JSFUtil() {
     }
 
+    /**
+     * resolves a variable in the current facesContext
+     *
+     * @param beanName
+     * @return
+     */
     public static Object resolveVariable(String beanName) {
         Log log = LogFactory.getLog(JSFUtil.class);
         Object facesContext = FacesContext.getCurrentInstance();
@@ -27,7 +35,7 @@
 
         log.info("ElResolver Instance:" + elResolver.toString());
         try {
-            return executeFunction(elResolver, "getValue", new Cast(ClassUtils.classForName("javax.el.ELContext"),
elContext), new Null(Object.class), new Cast(Object.class, beanName));
+            return executeFunction(elResolver, "getValue", cast(ClassUtils.classForName("javax.el.ELContext"),
elContext), nullCast(Object.class), cast(Object.class, beanName));
         } catch (ClassNotFoundException e) {
             throw new RuntimeException(e);
         }
@@ -35,15 +43,21 @@
     }
 
 
+    /**
+     * executes a method
+     *
+     * @param obj        the target object
+     * @param methodName the method name
+     * @param varargs    a list of objects casts or nulls defining the parameter classes
and its values
+     *                   if something occurs on introspection level an unmanaged exception
is throw, just like
+     *                   it would happen in a scripting class
+     */
     public static void executeMethod(Object obj, String methodName, Object... varargs) {
-        
+
         Class[] classes = new Class[varargs.length];
         for (int cnt = 0; cnt < varargs.length; cnt++) {
 
-            if (varargs[cnt] instanceof Null) {
-                classes[cnt] = ((Null) varargs[cnt]).getNulledClass();
-                varargs[cnt] = null;
-            } else if (varargs[cnt] instanceof Cast) {
+            if (varargs[cnt] instanceof Cast) {
                 classes[cnt] = ((Cast) varargs[cnt]).getClazz();
                 varargs[cnt] = ((Cast) varargs[cnt]).getValue();
             } else {
@@ -63,14 +77,22 @@
         }
     }
 
+    /**
+     * executes a function on a target object
+     *
+     * @param obj        the target object
+     * @param methodName the method name
+     * @param varargs    a list of objects casts or nulls defining the parameter classes
and its values
+     *                   if something occurs on introspection level an unmanaged exception
is throw, just like
+     *                   it would happen in a scripting class
+     * @return the result object for the function(method) call
+     * @throws RuntimeException an unmanaged runtime exception in case of an introspection
error
+     */
     public static Object executeFunction(Object obj, String methodName, Object... varargs)
{
         Class[] classes = new Class[varargs.length];
         for (int cnt = 0; cnt < varargs.length; cnt++) {
 
-            if (varargs[cnt] instanceof Null) {
-                classes[cnt] = ((Null) varargs[cnt]).getNulledClass();
-                varargs[cnt] = null;
-            } else if (varargs[cnt] instanceof Cast) {
+            if (varargs[cnt] instanceof Cast) {
                 classes[cnt] = ((Cast) varargs[cnt]).getClazz();
                 varargs[cnt] = ((Cast) varargs[cnt]).getValue();
             } else {
@@ -91,4 +113,13 @@
 
     }
 
+
+    public static Cast cast(Class clazz, Object value) {
+        return new Cast(clazz, value);
+    }
+
+    public static Null nullCast(Class clazz) {
+        return new Null(clazz);
+    }
+
 }

Modified: myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Null.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Null.java?rev=809918&r1=809917&r2=809918&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Null.java
(original)
+++ myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/Null.java
Tue Sep  1 09:19:50 2009
@@ -1,17 +1,14 @@
 package org.apache.myfaces.javaloader.blog;
 
 /**
- * @author werpu2
- * @date: 01.09.2009
+ *
+ * Special cast case null
  */
-public class Null {
-    Class nulledClass = null;
+public class Null extends Cast {
 
     public Null(Class clazz) {
-        nulledClass = clazz;
+        super(clazz, null);   
     }
 
-    public Class getNulledClass() {
-        return nulledClass;
-    }
+
 }



Mime
View raw message