org.vrspace.server
Class VRObject

java.lang.Object
  extended byjava.util.Observable
      extended byorg.vrspace.server.VRObject
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
DBObject, PassiveVRObject, PrivateVRObject, PublicVRObject

public class VRObject
extends java.util.Observable
implements java.lang.Cloneable

Generic VRObject class.
Defines event forwarding, does not check nor set actual field value.
Subclasses should override setValue() method to implement different behavior, i.e. store object to database and/or forward events.

NOTES:
- set_* (see setField()) method may access both Client and Dispatcher via Request
- before invoking set_*, request may forwarded to all the Observers, depending on sendEvent()/setValue() implementation
- along with request, other Clients (Observers) get reference to originating Client - TAKE CARE of public variables !!!
- this way, clients may send requests (Client.update()) to each other without Dispatcher interference - usefull for chat, smart server-side classes etc.
- it's up to each client to check the request origin and perform other actions
- if set_ method is invoked before forwarding, it may cancel forwarding by calling clearChanged(), depending on sendEvent()/setValue() implementation
- in general, sendEvent() should invoke setChanged() or generate VRObjectException, setValue() should invoke notifyObservers( Request ), and somewhere between those field is set and clearChanged() is eventualy called


Field Summary
 long db_id
           
static java.util.HashMap primitiveMap
           
static java.util.HashMap primitives
           
 
Constructor Summary
VRObject()
           
 
Method Summary
static java.lang.String arrayToString(java.lang.Object array)
          Converts array val to String.
 boolean canRead(java.lang.String name)
          Returns true if there's either field or get_ method
 boolean canWrite(java.lang.String name)
          Returns true if there's either field or set_ method
 java.lang.Object clone()
           
 boolean equals(VRObject obj)
          VRObject.equals( VRObject ) if class and db_id are the same
 java.lang.String fieldToText(java.lang.reflect.Field field)
           
 int fromString(java.lang.String s)
          Uses toString() formated String to set variables
static VRObject[] fromText(java.lang.String definition)
          Returns array of objects defined by string in toText() format
 java.lang.String getClassName()
          Returns class name without package name.
 java.lang.Object getField(java.lang.String name)
          Returns a field value.
 java.lang.String[] getFields()
          Returns names of all public member varaibles.
 long getId()
          Returns objects unique id
 ID getID()
          Returns objects unique id
 VRObject[] getMemebers()
          Returns VRObjects that are member variables of this object.
 boolean hasField(java.lang.String name)
          Returns true if this object contains public field with this name
 boolean hasMethod(java.lang.String name)
          Returns true if this object contains public method with this name
 boolean isNew()
          new object?
static VRObject newInstance(java.lang.String className)
          Creates new instance of className.
 void sendEvent(Request r)
          Forwards request r to other clients, Observers added by Dispatcher.
 boolean sendResponse()
          send OK on successfull execution?
 void setField(Request r)
          Sets field r.getEventName() to r.getEventValue()
 void setField(java.lang.String name, java.lang.String value)
          Sets field name to value
 void setField(java.lang.String name, java.lang.String value, Request r)
          Equals to obj.name = value.
 void setFields(java.lang.String[] names, java.lang.String args)
          setFields() on all variables specified in parse args and set each
 void setFields(VRObject o)
          Takes field values from passed object, by field name.
protected  void setValue(Request r)
          This implementation does nothing
static java.lang.Object stringToArray(java.lang.Class cl, java.lang.String val)
          Converts String in arrayToString() format back to array.
 java.lang.String toString()
          Converts VRObject to String.
 java.lang.String toText()
          Returns string representation of objects, used by fromText()
static java.lang.String toText(java.lang.Object[] obj)
          Returns string representation of objects, used by fromText()
 java.lang.String toText(java.lang.String filter)
          As toText(), but does not return fields beginning with filter.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

db_id

public long db_id

primitives

public static final java.util.HashMap primitives

primitiveMap

public static final java.util.HashMap primitiveMap
Constructor Detail

VRObject

public VRObject()
Method Detail

isNew

public boolean isNew()
new object?


sendResponse

public boolean sendResponse()
send OK on successfull execution?


getId

public long getId()
Returns objects unique id


getID

public ID getID()
Returns objects unique id


fromString

public int fromString(java.lang.String s)
               throws VRObjectException,
                      java.lang.NoSuchFieldException,
                      java.lang.ClassNotFoundException,
                      java.lang.IllegalAccessException,
                      java.lang.InstantiationException,
                      java.lang.NoSuchMethodException,
                      java.lang.reflect.InvocationTargetException
Uses toString() formated String to set variables

Throws:
VRObjectException
java.lang.NoSuchFieldException
java.lang.ClassNotFoundException
java.lang.IllegalAccessException
java.lang.InstantiationException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException

toString

public java.lang.String toString()
Converts VRObject to String. BUG: quotes inside string


arrayToString

public static java.lang.String arrayToString(java.lang.Object array)
Converts array val to String.


stringToArray

public static java.lang.Object stringToArray(java.lang.Class cl,
                                             java.lang.String val)
Converts String in arrayToString() format back to array.


toText

public static java.lang.String toText(java.lang.Object[] obj)
                               throws java.lang.IllegalAccessException
Returns string representation of objects, used by fromText()

Throws:
java.lang.IllegalAccessException

toText

public java.lang.String toText()
                        throws java.lang.IllegalAccessException
Returns string representation of objects, used by fromText()

Throws:
java.lang.IllegalAccessException

fieldToText

public java.lang.String fieldToText(java.lang.reflect.Field field)
                             throws java.lang.IllegalAccessException
Throws:
java.lang.IllegalAccessException

toText

public java.lang.String toText(java.lang.String filter)
                        throws java.lang.IllegalAccessException
As toText(), but does not return fields beginning with filter. Used for sending events over the network.

Throws:
java.lang.IllegalAccessException

getClassName

public java.lang.String getClassName()
Returns class name without package name.


fromText

public static VRObject[] fromText(java.lang.String definition)
                           throws java.lang.NoSuchMethodException,
                                  java.lang.NoSuchFieldException,
                                  java.lang.IllegalAccessException,
                                  java.lang.InstantiationException,
                                  java.lang.ClassNotFoundException,
                                  java.lang.reflect.InvocationTargetException,
                                  VRObjectException,
                                  java.lang.Exception
Returns array of objects defined by string in toText() format

Throws:
java.lang.NoSuchMethodException
java.lang.NoSuchFieldException
java.lang.IllegalAccessException
java.lang.InstantiationException
java.lang.ClassNotFoundException
java.lang.reflect.InvocationTargetException
VRObjectException
java.lang.Exception

newInstance

public static VRObject newInstance(java.lang.String className)
Creates new instance of className. May return null if instantiation fails,


setFields

public void setFields(java.lang.String[] names,
                      java.lang.String args)
               throws VRObjectException
setFields() on all variables specified in parse args and set each

Throws:
VRObjectException

setFields

public void setFields(VRObject o)
Takes field values from passed object, by field name. Non-exisisting fields ignored.


setField

public void setField(java.lang.String name,
                     java.lang.String value)
              throws VRObjectException
Sets field name to value

Throws:
VRObjectException

setField

public void setField(Request r)
              throws VRObjectException
Sets field r.getEventName() to r.getEventValue()

Throws:
VRObjectException
See Also:
Request

setField

public void setField(java.lang.String name,
                     java.lang.String value,
                     Request r)
              throws VRObjectException
Equals to obj.name = value. If this raises exception, tries obj.set_name( Request r, String value )

Throws:
VRObjectException

sendEvent

public void sendEvent(Request r)
               throws RequestException
Forwards request r to other clients, Observers added by Dispatcher. Throws RequestException if request target is another object. Calls setValue() method which actualy forwards the event. NOTE: All the Observers (Clients!) have access to VRObject - TAKE CARE of public variables (reading) and public methodes (generating events)

Throws:
RequestException

setValue

protected void setValue(Request r)
This implementation does nothing


getMemebers

public VRObject[] getMemebers()
Returns VRObjects that are member variables of this object.


getFields

public java.lang.String[] getFields()
Returns names of all public member varaibles. Use getField( String ) to retreive value.

See Also:
getField(java.lang.String)

hasField

public boolean hasField(java.lang.String name)
Returns true if this object contains public field with this name


hasMethod

public boolean hasMethod(java.lang.String name)
Returns true if this object contains public method with this name


canWrite

public boolean canWrite(java.lang.String name)
Returns true if there's either field or set_ method


canRead

public boolean canRead(java.lang.String name)
Returns true if there's either field or get_ method


getField

public java.lang.Object getField(java.lang.String name)
                          throws VRObjectException
Returns a field value. If there's no member with this name, tries to execute "get_"+name method.

Throws:
VRObjectException - if neither field nor method were found

equals

public boolean equals(VRObject obj)
VRObject.equals( VRObject ) if class and db_id are the same


clone

public java.lang.Object clone()