Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.vrspace.server
Class VRObject  view VRObject download VRObject.java

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
           
private static long instances
           
(package private)  boolean isNew
           
private  java.lang.Object lock
           
private static java.util.Hashtable lockedClasses
           
private  java.lang.String myClassName
           
private  int pos
           
static java.util.HashMap primitiveMap
           
static java.util.HashMap primitives
           
private  Request request
           
 
Fields inherited from class java.util.Observable
 
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()
          This method may be called to create a new copy of the Object.
 boolean equals(VRObject obj)
          VRObject.equals( VRObject ) if class and db_id are the same
(package private)  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
 org.vrspace.util.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?
(package private)  boolean lock(VRObject o)
          Lock the object, o is new owner.
(package private)  boolean lockClass(VRObject o)
          Lock the class, o is new owner.
static VRObject newInstance(java.lang.String className)
          Creates new instance of className
private  java.lang.String nextToken(java.util.StringTokenizer st)
           
 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.
(package private)  boolean unlock(VRObject o)
          unlock
(package private)  boolean unlockClass(VRObject o)
          unlock
 
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

pos

private int pos

request

private Request request

myClassName

private java.lang.String myClassName

lock

private java.lang.Object lock

lockedClasses

private static java.util.Hashtable lockedClasses

isNew

boolean isNew

instances

private static long instances

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 org.vrspace.util.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


nextToken

private java.lang.String nextToken(java.util.StringTokenizer st)

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()


toText

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


fieldToText

java.lang.String fieldToText(java.lang.reflect.Field field)
                       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.


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
Returns array of objects defined by string in toText() format


newInstance

public static VRObject newInstance(java.lang.String className)
Creates new instance of className


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


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


setField

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


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 )


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)


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.


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.


equals

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


lock

boolean lock(VRObject o)
Lock the object, o is new owner. Usage: transactions. This should be package protected.


unlock

boolean unlock(VRObject o)
unlock


lockClass

boolean lockClass(VRObject o)
Lock the class, o is new owner. Usage: transactions. This should be package protected.


unlockClass

boolean unlockClass(VRObject o)
unlock


clone

public java.lang.Object clone()
Description copied from class: java.lang.Object
This method may be called to create a new copy of the Object. The typical behavior is as follows:
  • o == o.clone() is false
  • o.getClass() == o.clone().getClass() is true
  • o.equals(o) is true

However, these are not strict requirements, and may be violated if necessary. Of the three requirements, the last is the most commonly violated, particularly if the subclass does not override Object.equals(Object)>Object.equals(Object) 55 .

If the Object you call clone() on does not implement java.lang.Cloneable (which is a placeholder interface), then a CloneNotSupportedException is thrown. Notice that Object does not implement Cloneable; this method exists as a convenience for subclasses that do.

Object's implementation of clone allocates space for the new Object using the correct class, without calling any constructors, and then fills in all of the new field values with the old field values. Thus, it is a shallow copy. However, subclasses are permitted to make a deep copy.

All array types implement Cloneable, and override this method as follows (it should never fail):

 public Object clone()
 {
   try
     {
       super.clone();
     }
   catch (CloneNotSupportedException e)
     {
       throw new InternalError(e.getMessage());
     }
 }