Source code: org/vrspace/server/Add.java
1 package org.vrspace.server;
2 import org.vrspace.attributes.Admin;
3 import org.vrspace.util.*;
4
5 /**
6 Encapsulates a new object.<br>
7 Two purposes:<br>
8 - to notify the client a new object enters the scene
9 (Client calls Add( VRObject ))<br>
10 - to add a new object to the database
11 (Client receives Add <b>id</b> className <b>name</b> request)
12 */
13
14 public class Add extends PassiveDBObject {
15 public String className;
16 boolean isTransform;
17 private VRObject object;
18 public Add( VRObject obj ) {
19 db_id = obj.db_id;
20 className = obj.getClassName();
21 isTransform = (obj instanceof Transform);
22 object = obj;
23 }
24 protected Add(){}
25
26 public boolean isTransform() {
27 return isTransform;
28 }
29 /**
30 Override a PassiveDBObject method
31 This can receive only one event: Add <b>id</b> className <b>className</b>
32 */
33 public void sendEvent( Request r ) throws RequestException
34 {
35 // check privileges
36 if ( !(r.getClient() instanceof Admin )) {
37 super.sendEvent(r);
38 //r.getClient().addObject( ... ); //???
39 } else {
40 if (r.getEventName().equals( "className" )) {
41 try {
42 VRObject obj = null;
43 try {
44 obj = (VRObject) r.getClient().dispatcher.space.get( r.getEventValue(), r.getId() );
45 } catch (Exception e) {
46 Logger.logError("Error fetching "+r.getEventValue() +" "+ r.getId()+": ", e);
47 }
48 if ( obj == null ) {
49 obj = (VRObject) r.getClient().getClassLoader().loadClass( r.getEventValue() ).newInstance();
50 obj.db_id = db_id;
51 r.getClient().dispatcher.space.put( obj );
52 //r.getClient().addObject( obj ); // problems with multiple proxy clients?
53 //Logger.logDebug( "Added "+r.getEventValue()+" "+db_id );
54 } else {
55 // just ignore existing objects?
56 }
57 } catch ( Throwable t ) {
58 RequestException e = new RequestException( r, "Error creating "+r.getEventValue()+" object: "+t.toString() );
59 throw ( e );
60 }
61 } else {
62 RequestException e = new RequestException( r, "Invalid event name: "+r.getEventName() );
63 }
64 }
65 }
66 /**
67 Returns underlying object
68 */
69 public VRObject getObject() {
70 return object;
71 }
72 }