« March 2007 »
SunMonTueWedThuFriSat
    
1
2
3
4
10
11
12
13
14
15
17
20
22
23
25
26
27
28
29
31
       
Today
XML

Blog::Navigation

GetJava Download Button
Get the Source
Personal Blog

Blog::Referers

Today's Page Hits: 804

Powered by Roller Weblogger.
« Previous day (Mar 22, 2007) | Main | Next day (Mar 24, 2007) »
20070324 Saturday March 24, 2007

Script Beans - part 2

This is continuation of my earlier post on script beans. On rethinking, I've made few (minor) changes to ScriptObject and Callable. The changes are:



ScriptObject

import javax.script.ScriptException;

/**
 * Any Java object supporting this interface can be
 * accessed from scripts with "simpler" access pattern.
 * For example, a script engine may support natural
 * property/field access syntax for the properties exposed
 * via this interface. We use this interface so that we
 * can dynamically add/delete/modify fields exposed to
 * scripts. Also, script engines may expose this interface for
 * it's own objects.
 */
public interface ScriptObject {
  // special value to denote no-result -- so that
  // null could be used as proper result value
  public static final Object UNDEFINED = new Object();
  // empty object array
  public static final Object[] EMPTY_ARRAY = new Object[0];

  /*
   * Returns all property names supported by this object.
   * Property "name" is either a String or an Integer".
   */
  public Object[] getIds();

  /**
   * Get the value of the named property.
   */
  public Object get(String name);

  /**
   * Get the value of the "indexed" property. 
   * Returns UNDEFINED if the property does not exist.
   */
  public Object get(int index);

  /**
   * Set the value of the named property. Returns
   * whether the put was successful or not.
   */
  public boolean put(String name, Object value);

  /**
   * Set the value of the indexed property. Returns
   * whether the put was successful or not.
   */
  public boolean put(int index, Object value);

  /**
   * Returns whether the named property exists or not.
   */
  public boolean has(String name);

  /**
   * Returns whether the indexed property exists or not.
   */
  public boolean has(int index);

  /**
   * Deletes the named property. Returns true on success.
   */
  public boolean delete(String name);

  /**
   * Deletes the indexed property. Returns true on success.
   */
  public boolean delete(int index);

  /**
   * Call the named method on this script object.
   */
  public Object invoke(String name, Object... arguments)
      throws NoSuchMethodException, ScriptException;
}



Callable.java

import javax.script.ScriptException;

/**
 * This interface is used to represent "function/method" valued
 * properties in ScriptObjects.
 */
public interface Callable {
  /**
   * Call the underlying function passing the given
   * arguments and return the result.
   */
  public Object call(Object... args) throws ScriptException;
}



AbstractScriptObject.java

import javax.script.ScriptException;

/**
 * Simple dummy implementation of ScriptObject.
 */
public abstract class AbstractScriptObject 
  implements ScriptObject {

  public Object[] getIds() {
    return EMPTY_ARRAY;
  }    

  public Object get(String name) {
    return UNDEFINED;
  }

  public Object get(int index) {
    return UNDEFINED;
  }

  public boolean put(String name, Object value) {
    return false;
  }

  public boolean put(int index, Object value) {
    return false;
  }

  public boolean has(String name) {
    return false;
  }

  public boolean has(int index) {
    return false;
  }

  public boolean delete(String name) {
    return false;
  }

  public boolean delete(int index) {
    return false;
  }

  public Object invoke(String name, Object... arguments) 
    throws NoSuchMethodException, ScriptException {
    Object value =  get(name);
    if (value instanceof Callable) {
      return ((Callable)value).call(arguments);
    } else {
      throw new NoSuchMethodException(name);
    }
  }
}



( Mar 24 2007, 02:12:41 PM IST ) Permalink Comments [3] del.icio.us | furl | simpy | slashdot | technorati | digg

Half empty or half full?

As expected (especially after loosing to Bangladesh), loosing to Sri Lanka followed! Here is my "half full" perspective:



( Mar 24 2007, 11:21:52 AM IST ) Permalink Comments [4] del.icio.us | furl | simpy | slashdot | technorati | digg

Copyright (C) 2005, A. Sundararajan's Weblog