Remy Maucherat wrote:
> 
> > The neccesary changes weren't made to the JDBC store, though. Right now,
> > this stores (in the object table) a uri and a type. Type is just a
> > number. Perhaps instead this should be changed to the classname - then
> > when the data is read back, it could instantiate this class straight
> > away, and not have to worry about special cases for roles. I suppose
> > this would also get the other cases (link and action) correct. It does
> > mean changing the database format, though, which you may not wish to do.
> > Other than that, this would be straightforward (and would mostly involve
> > removal of code, not addition)
> 
> That should (and will) definitely be fixed.

Indeed. Patch attached. 

Be warned that it DOES require recreating the tables (because it
replaces the integer 'type' column with a varchar column. By the way -
you might want to check the type and size I used there - I don't know
much SQL, so it might not be the best choice).

I spent quite a while trying to figure out why things weren't working
after I editing the wrong version of the file, not realising another
existed - why are there two (very similar, but not identical) copies of
each of the stores? The patch is for the one which is actually used - is
the other dead code, or is it used under other circumstances (in which
case the patch needs to be applied to that as well - it should do so
reasonably cleanly, but no guarantees).

> 
> I'll also add a set of standard roles to make things easier (Guest, User,
> Root).

I haven't done this, but it's pretty trivial. Anyway, roles now actually
work with the JDBC store. Yay!

Michael
Index: JDBCDescriptorsStore.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-slide/src/stores/slidestore/reference/JDBCDescriptorsStore.java,v
retrieving revision 1.6
diff -u -r1.6 JDBCDescriptorsStore.java
--- JDBCDescriptorsStore.java   2001/01/20 20:08:29     1.6
+++ JDBCDescriptorsStore.java   2001/01/30 00:56:59
@@ -63,6 +63,7 @@
 
 package slidestore.reference;
 
+import java.lang.reflect.Constructor;
 import java.util.Hashtable;
 import java.util.Enumeration;
 import java.util.Vector;
@@ -95,18 +96,12 @@
     // -------------------------------------------------------------- Constants
     
     
-    // Object types
-    
-    protected static final int SUBJECT = 0;
-    protected static final int ACTION = 1;
-    protected static final int LINK = 2;
-    
     // Column numbers
     
     // Structure descriptors
     
     protected static final int OBJECTS_URI = 1;
-    protected static final int OBJECTS_TYPE = 2;
+    protected static final int OBJECTS_CLASS = 2;
     
     protected static final int CHILDREN_URI = 1;
     protected static final int CHILDREN_CHILDURI = 2;
@@ -268,7 +263,7 @@
             Statement statement = connection.createStatement();
             
             String s = "create table objects(uri varchar(65536) " 
-                + " primary key, type int)";
+                + " primary key, classname varchar(4096))";
             statement.execute(s);
             
             s = "create table children(uri varchar(65536), childuri " 
@@ -534,11 +529,11 @@
             
             // Parsing result set
             
-            int objectType = 0;
+            String className;
             
             if (res.next()) {
                 // Retrieving and loading the object
-                objectType = res.getInt(OBJECTS_TYPE);
+                className = res.getString(OBJECTS_CLASS);
             } else {
                 // Object was not found ...
                 throw new ObjectNotFoundException(uri);
@@ -567,32 +562,36 @@
             while (res.next()) {
                 // Load each permission
                 linksVector.addElement(res.getString(LINKS_LINKTO));
-            }
-            
-            switch (objectType) {
-            case SUBJECT :
-                result = new SubjectNode(uri.toString(), childrenVector, 
-                                         linksVector);
-                break;
-            case ACTION :
-                result = new ActionNode(uri.toString(), childrenVector, 
-                                        linksVector);
-                break;
-            case LINK :
-                String linkTo = new String();
-                s = "select * from links where link='" + uri + "'";
-                statement.execute(s);
-                res = statement.getResultSet();
-                
-                // Parse result set
-                if (res.next()) {
-                    linkTo = res.getString(LINKS_LINKTO);
-                }
-                result = new LinkNode(uri.toString(), childrenVector, 
-                                      linksVector, linkTo);
-                break;
             }
-            
+
+                       if(className.equals("org.apache.slide.structure.LinkNode")) {
+                               String linkTo = new String();
+                               s = "select * from links where link='" + uri + "'";
+                               statement.execute(s);
+                               res = statement.getResultSet();
+
+                               if(res.next())
+                                       linkTo = res.getString(LINKS_LINKTO);
+
+                               result = new LinkNode(uri.toString(), childrenVector,
+                                               linksVector, linkTo);
+                       }
+                       else
+                       {
+                               try {
+                                       Class objclass = Class.forName(className);
+
+                                       Class[] argClasses = { 
+Class.forName("java.lang.String"),
+                                                                                  
+Class.forName("java.util.Vector"),
+                                                                                  
+Class.forName("java.util.Vector") };
+                                       Object[] arguments = { uri.toString(), 
+childrenVector, linksVector };
+
+                                       Constructor constructor = 
+objclass.getConstructor(argClasses);
+                                       result = 
+(ObjectNode)constructor.newInstance(arguments);
+                               } catch(Exception e) { // ClassNotFoundException, 
+NoSuchMethodException, etc. 
+                                       throw new ServiceAccessException(this, e);
+                               }
+                       }
         } catch (SQLException e) {
             throw new ServiceAccessException(this, e);
         }
@@ -678,20 +677,8 @@
         
         try {
             
-            int objectType = 0;
-            
-            if (object instanceof SubjectNode) {
-                objectType = SUBJECT;
-            }
-            
-            if (object instanceof LinkNode) {
-                objectType = LINK;
-            }
-            
-            if (object instanceof ActionNode) {
-                objectType = ACTION;
-            }
-            
+                       String className = object.getClass().getName();
+
             Statement statement = connection.createStatement();
 
             String s = "select * from objects where uri='" + uri + "'";
@@ -705,8 +692,8 @@
                 throw new ObjectAlreadyExistsException(uri.toString());
             }
             
-            s = "insert into objects values('" + uri + "', "  
-                + objectType + ")";
+            s = "insert into objects values('" + uri + "', '"  
+                + className + "')";
             
             statement.execute(s);
             
@@ -730,7 +717,7 @@
             */
             
             // If the object is a link, also store the link information
-            if (objectType == LINK) {
+            if (object instanceof LinkNode) {
                 s = "insert into links values('" + uri + "', '" 
                     + ((LinkNode) object).getLinkedUri() + "')";
                 statement.execute(s);

Reply via email to