commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bali...@apache.org
Subject cvs commit: jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence MetaObject.java PersistentProxy.java TransactionImpl.java
Date Sun, 10 Feb 2002 10:48:34 GMT
baliuka     02/02/10 02:48:34

  Modified:    simplestore/src/sample/org/apache/commons/simplestore/jdbc
                        DBStorage.java
               simplestore/src/sample/org/apache/commons/simplestore/persistence
                        MetaObject.java PersistentProxy.java
                        TransactionImpl.java
  Log:
  Roolback implementation ( not tested )
  
  Revision  Changes    Path
  1.4       +17 -7     jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/jdbc/DBStorage.java
  
  Index: DBStorage.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/jdbc/DBStorage.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DBStorage.java	9 Feb 2002 18:54:10 -0000	1.3
  +++ DBStorage.java	10 Feb 2002 10:48:33 -0000	1.4
  @@ -61,7 +61,7 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: DBStorage.java,v 1.3 2002/02/09 18:54:10 froehlich Exp $
  + *@version    $Id: DBStorage.java,v 1.4 2002/02/10 10:48:33 baliuka Exp $
    */
   public class DBStorage extends AbstractStorage {
   
  @@ -149,7 +149,7 @@
   
           return sb.toString();
       }
  -
  +// TODO :  must be more simple
       public static int excecute(java.sql.Connection connection, String sql, Object[] args,
ResultSetHandler eh) throws StorageException {
   
           int result = 0;
  @@ -172,7 +172,7 @@
                           }
                       }
                   }
  -                if (statement.execute()) {
  +                if ( statement.execute() ) {
                       rs = statement.getResultSet();
                   } else {
                       return statement.getUpdateCount();
  @@ -259,6 +259,7 @@
           }
   
           final java.sql.Connection connection = getConnection();
  +        // TODO : ID must be in some mapping
           final String sql = "SELECT * FROM " + toSQLName(clasz.getName()) + " WHERE ID=?";
           result = (Persistent) PersistentProxy.getPersitent(clasz, id, false, this);
           final MetaObject pc = result.getMetaObject();
  @@ -285,7 +286,7 @@
   
               };
   
  -        if (excecute(connection, sql, new Object[]{id}, rsh) == 0) {
  +        if ( excecute(connection, sql, new Object[]{id}, rsh) == 0 ) {
               throw new ObjectNotFound(pc.getOID().toString(), null);
           }
   
  @@ -298,6 +299,7 @@
       public java.util.Set retrieveAll(final Class clasz) throws StorageException {
   
           final java.sql.Connection connection = getConnection();
  +        // TODO : INTERNAL_OID must be in some mapping
           final String sql = "SELECT ID AS INTERNAL_OID, * FROM " + toSQLName(clasz.getName());
           final java.util.Set objects = new java.util.HashSet();
           final InternalTransaction transaction = getTransaction();
  @@ -344,15 +346,19 @@
               }
           }
           names.setCharAt(names.length() - 1, ' ');
  +        // TODO : ID must be in some mapping
           names.append("WHERE ID=?");
           values.add(properties.getOID());
   
           final String sql = "UPDATE " + name + " SET " + names;
   
  -        excecute(connection, sql, values.toArray(), null);
  +       if(  excecute(connection, sql, values.toArray(), null ) == 0 ){
  +           throw new ObjectNotFound(properties.getOID().toString(), null);
  +       }   
   
       }
   
  +    // TODO : mot very meanigful here
       public void close() {
   
           try {
  @@ -395,7 +401,7 @@
                   params += ",?";
               }
           }
  -
  +       //TODO : ID !!!
           final String sql = "INSERT INTO " + toSQLName(clasz.getName()) + "(ID" + names
+ ")VALUES(?" + params + ")";
   
           excecute(connection, sql, values.toArray(), null);
  @@ -407,9 +413,12 @@
           final java.sql.Connection connection = getConnection();
           final String name = toSQLName(obj.getPersistentClass().getName());
           final Object id = obj.getOID();
  +        // TODO : ID must be in some mapping
           final String sql = "DELETE FROM " + name + " WHERE ID=?";
   
  -        excecute(connection, sql, new Object[]{id}, null);
  +       if( excecute(connection, sql, new Object[]{id}, null) == 0 ){
  +           throw new ObjectNotFound(obj.getOID().toString(), null);
  +       }
       }
   
       protected void internalCommit() throws StorageException {
  @@ -506,6 +515,7 @@
                       return;
                   }
                   String property = toPropertyName(name);
  +                // TODO : ID must be in some mapping
                   if (property.equals("Id") || map == null) {
                       return;
                   }
  
  
  
  1.4       +8 -2      jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/MetaObject.java
  
  Index: MetaObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/MetaObject.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MetaObject.java	9 Feb 2002 18:51:43 -0000	1.3
  +++ MetaObject.java	10 Feb 2002 10:48:33 -0000	1.4
  @@ -57,10 +57,10 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: MetaObject.java,v 1.3 2002/02/09 18:51:43 froehlich Exp $
  + *@version    $Id: MetaObject.java,v 1.4 2002/02/10 10:48:33 baliuka Exp $
    */
   
  -public interface MetaObject {
  +public interface MetaObject extends Cloneable {
   
       public Object getOID();
   
  @@ -85,5 +85,11 @@
       public void setDirty(boolean dirty);
   
       public Persistent getObject();
  +    
  +    public Object clone()throws CloneNotSupportedException;
  +    
  +    public void assign( MetaObject mo);
  +    
  +    
   }
   
  
  
  
  1.5       +96 -75    jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/PersistentProxy.java
  
  Index: PersistentProxy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/PersistentProxy.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- PersistentProxy.java	9 Feb 2002 18:51:43 -0000	1.4
  +++ PersistentProxy.java	10 Feb 2002 10:48:33 -0000	1.5
  @@ -59,19 +59,19 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: PersistentProxy.java,v 1.4 2002/02/09 18:51:43 froehlich Exp $
  + *@version    $Id: PersistentProxy.java,v 1.5 2002/02/10 10:48:33 baliuka Exp $
    */
  -public class PersistentProxy implements MetaObject, InvocationHandler, java.io.Serializable
{
  -
  +public class PersistentProxy implements MetaObject, InvocationHandler,Cloneable, java.io.Serializable
{
  +    
       private static Method HASH_CODE;
       private static Method EQUALS;
       private static Method TO_STRING;
       private static Method GET_OID;
       private static Method GET_META_OBJECT;
       private static java.util.Map defaults = new java.util.Hashtable();
  -
  +    
       java.util.Map map = new java.util.HashMap();
  -
  +    
       Object oid = null;
       Persistent object;
       Class pClass;
  @@ -79,8 +79,8 @@
       boolean dirty = false;
       boolean deleted = false;
       boolean newCreated;
  -
  -
  +    
  +    
       /**
        * Creates new ValueProxy
        *
  @@ -90,30 +90,30 @@
        *@param  transactionManager
        */
       public PersistentProxy(Class pClass, Object oid, boolean newCreated, TransactionManager
transactionManager) {
  -
  +        
           this.oid = oid;
           this.newCreated = newCreated;
           this.transactionManager = transactionManager;
           this.pClass = pClass;
  -
  +        
       }
  -
  -
  +    
  +    // TODO : "next" interceptor as parameter 
       public static Persistent getPersitent(Class persistent, Object oid, boolean newCreated,
TransactionManager transactionManager) {
           PersistentProxy handler = new PersistentProxy(persistent, oid, newCreated, transactionManager);
           Persistent p = (Persistent) Proxy.newProxyInstance(
  -                Thread.currentThread().getContextClassLoader(), new Class[]{persistent,
Persistent.class}, handler);
  +        Thread.currentThread().getContextClassLoader(), new Class[]{persistent, Persistent.class},
handler);
           handler.object = p;
           if (newCreated) {
  -
  +            
               MetaObject mo = p.getMetaObject();
               transactionManager.getTransaction().add(mo);
           }
           return p;
       }
  -// TODO must be converter interface
  +    // TODO must be converter interface
       public static Object convertNumber(Number number, Class cls) {
  -
  +        
           if (cls.equals(Byte.class)) {
               return new Byte(number.byteValue());
           } else if (cls.equals(Short.class)) {
  @@ -133,11 +133,11 @@
           } else {
               throw new java.lang.UnsupportedOperationException("Number class = " + number.getClass().getName()
+ " Target Class " + cls.getName());
           }
  -
  +        
       }
       // TODO must be converter interface
       public static Object convertPrimityve(Number number, Class cls) {
  -
  +        
           if (cls.equals(Byte.TYPE)) {
               return new Byte(number.byteValue());
           } else if (cls.equals(Short.TYPE)) {
  @@ -157,7 +157,7 @@
           } else {
               throw new java.lang.UnsupportedOperationException("Number class = " + number.getClass().getName()
+ " Target Class " + cls.getName());
           }
  -
  +        
       }
       // TODO must be converter interface
       public static Object convert(Object object, Class cls) {
  @@ -170,32 +170,32 @@
                       return convertPrimityve((Number) object, cls);
                   }
               }
  -
  +            
               if (object == null) {
                   return null;
               }
  -
  +            
               if (cls.isAssignableFrom(object.getClass())) {
                   return object;
               }
  -
  +            
               // if( object instanceof String ){ ?????
               //     return org.apache.commons.beanutils.ConvertUtils.convert((String)object,cls);
               // }
  -
  -
  +            
  +            
               if (cls.isAssignableFrom(Number.class) && object instanceof Number)
{
                   return convertNumber((Number) object, cls);
               }
  -
  +            
               if (cls.equals(Boolean.class) && object instanceof Number) {
                   return new Boolean(((Number) object).intValue() != 0);
               }
  -
  +            
               if (cls.equals(Character.TYPE)) {
                   return new Character(object.toString().charAt(0));
               }
  -
  +            
               throw new java.lang.UnsupportedOperationException(cls.getName() + ":" + object);
           } catch (Throwable t) {
               // TODO
  @@ -203,125 +203,125 @@
               throw new RuntimeException(t.getClass().getName() + ":" + t.getMessage());
           }
       }
  -
  +    
       public void setProperty(String name, Object value) {
  -
  +        
           Object old = map.put(name, value);
           if (old == null || !old.equals(value)) {
               dirty = true;
               transactionManager.getTransaction().add(this);
           }
       }
  -
  +    
       public void setDirty(boolean dirty) {
           this.dirty = dirty;
           this.newCreated = false;
       }
  -
  -
  +    
  +    
       public boolean isDeleted() {
           return deleted;
       }
  -
  +    
       public boolean isNew() {
           return newCreated;
       }
  -
  +    
       public MetaObject getMetaObject() {
           return this;
       }
  -
  +    
       public java.util.Map getProperties() {
           return map;
       }
  -
  +    
       public Object getProperty(String name) {
           return map.get(name);
       }
  -
  +    
       public boolean isLoaded() {
           return true;
       }
  -
  +    
       public Class getPersistentClass() {
           return pClass;
       }
  -
  +    
       public Object getOID() {
           return oid;
       }
  -
  +    
       public boolean isDirty() {
           return dirty;
       }
  -
  +    
       public Persistent getObject() {
           return object;
       }
  -
  +    
       public java.lang.Object handleEquals(java.lang.Object obj) {
  -
  +        
           if (obj == null) {
               return new Boolean(false);
           }
  -
  +        
           if (!(obj instanceof Persistent)) {
               return new Boolean(false);
           }
  -
  +        
           Persistent object = (Persistent) obj;
  -
  +        
           if (oid == null) {
  -
  +            
               return new Boolean(oid == object.getOID());
           } else {
  -
  +            
               return new Boolean(oid.equals(object.getOID()));
           }
  -
  +        
       }
  -
  +    
       public java.lang.Object invoke(Object obj, Method method, Object[] obj2) throws java.lang.Throwable
{
  -
  +        
           synchronized (this) {
  -
  +            
               if (GET_META_OBJECT.equals(method)) {
                   return this;
               } else if (TO_STRING.equals(method)) {
  -
  +                
                   return oid + "";
               } else if (GET_OID.equals(method)) {
  -
  +                
                   return oid;
               } else if (HASH_CODE.equals(method)) {
  -
  +                
                   if (oid == null) {
                       return new Integer(0);
                   }
                   return new Integer(oid.hashCode());
               } else if (EQUALS.equals(method)) {
  -
  +                
                   return handleEquals(obj2[0]);
  -
  +                
               } else {
  -
  +                
                   return handleProperty(obj, method, obj2);
               }
  -
  +            
           }
       }
  -
  -
  +    
  +    
       public java.lang.Object handleProperty(java.lang.Object obj, java.lang.reflect.Method
method, java.lang.Object[] obj2) throws java.lang.Throwable {
  -
  +        
           String name = method.getName();
  -
  +        
           if (name.startsWith("set")) {
  -
  +            
               setProperty(name.substring(3), obj2[0]);
               return null;
           } else if (name.startsWith("get") || name.startsWith("is")) {
  -
  +            
               if (method.getName().startsWith("get")) {
                   name = method.getName().substring(3);
               } else {
  @@ -333,26 +333,26 @@
               }
               return value;
           }
  -
  +        
           throw new java.lang.IllegalStateException("pure method " + method.getName());
  -
  +        
       }
  -
  +    
       public void remove() {
           deleted = true;
       }
  -
  +    
       static {
  -
  +        
           try {
  -
  +            
               GET_OID = Persistent.class.getMethod("getOID", null);
               GET_META_OBJECT = Persistent.class.getMethod("getMetaObject", null);
  -
  +            
               HASH_CODE = Object.class.getMethod("hashCode", null);
               TO_STRING = Object.class.getMethod("toString", null);
               EQUALS = Object.class.getMethod("equals", new Class[]{Object.class});
  -
  +            
               defaults.put(byte.class, new java.lang.Byte((byte) 0));
               defaults.put(short.class, new java.lang.Short((short) 0));
               defaults.put(int.class, new java.lang.Integer(0));
  @@ -361,13 +361,34 @@
               defaults.put(double.class, new java.lang.Double(0));
               defaults.put(char.class, new java.lang.Character('\u0000'));
               defaults.put(boolean.class, new java.lang.Boolean(false));
  -
  +            
           } catch (Exception e) {
               e.printStackTrace();
               throw new Error(e.getMessage());
           }
  -
  +        
  +    }
  +    
  +    public Object clone()throws CloneNotSupportedException{
  +        PersistentProxy cln = (PersistentProxy)super.clone();
  +        cln.dirty = false;
  +        map = (java.util.Map)((java.util.HashMap)map).clone();
  +        return cln;
  +    }
  +    
  +    public void assign(MetaObject mo) {
  +        
  +        map.clear();
  +        map.putAll(mo.getProperties());
  +        oid = mo.getOID();
  +        object = mo.getObject();
  +        pClass = mo.getPersistentClass();
  +        dirty = mo.isDirty();
  +        deleted = mo.isDeleted();
  +        newCreated = mo.isNew();
  +        
  +        
       }
  -
  +    
   }
   
  
  
  
  1.4       +17 -4     jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/TransactionImpl.java
  
  Index: TransactionImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/TransactionImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TransactionImpl.java	10 Feb 2002 10:00:12 -0000	1.3
  +++ TransactionImpl.java	10 Feb 2002 10:48:33 -0000	1.4
  @@ -58,7 +58,7 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: TransactionImpl.java,v 1.3 2002/02/10 10:00:12 baliuka Exp $
  + *@version    $Id: TransactionImpl.java,v 1.4 2002/02/10 10:48:33 baliuka Exp $
    */
   
   public class TransactionImpl implements InternalTransaction {
  @@ -68,7 +68,8 @@
       int threadId = getCurrentThreadId();
       InternalTransaction transaction;
       java.util.Set objects;
  -    java.util.Map attributes = new java.util.HashMap();
  +    java.util.Map attributes  = new java.util.HashMap();
  +    java.util.Map storedState = new java.util.HashMap();
       boolean complete = true;
   
       /**
  @@ -121,7 +122,7 @@
           checkState(!complete);
           transaction.commit(objects);
           complete = true;
  -
  +        storedState.clear();
       }
   
       public void begin() {
  @@ -134,13 +135,25 @@
       public void rollback() {
           checkState();
           checkState(!complete);
  +        java.util.Iterator i = objects.iterator();
  +        while(i.hasNext()){
  +          MetaObject mo = (MetaObject)i.next();   
  +          mo.assign( (MetaObject)storedState.get( mo.getOID() ) );// NPE ?
  +        }
  +        storedState.clear();
           transaction.rollback(objects);
           complete = true;
       }
   
       public void add(MetaObject props) {
           checkState();
  -        objects.add(props);
  +        if(objects.add(props)){
  +         try{   
  +          storedState.put(props.getOID(),props.clone());
  +         }catch( java.lang.CloneNotSupportedException cnse ){
  +           throw new java.lang.UnsupportedOperationException(cnse.getMessage());
  +         } 
  +        }
   
       }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message