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/test/org/apache/commons/simplestore storage.xml TestPersistent.java TestSample.java
Date Thu, 28 Feb 2002 18:28:41 GMT
baliuka     02/02/28 10:28:41

  Modified:    simplestore/src/java/org/apache/commons/simplestore/persistence
                        MetaClass.java
               simplestore/src/java/org/apache/commons/simplestore/persistence/impl
                        MetaClassImpl.java PersistentProxy.java
               simplestore/src/test/org/apache/commons/simplestore
                        TestPersistent.java TestSample.java
  Added:       simplestore/src/test/org/apache/commons/simplestore
                        storage.xml
  Log:
  implemented foreign keys
  
  Revision  Changes    Path
  1.5       +8 -2      jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/MetaClass.java
  
  Index: MetaClass.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/MetaClass.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MetaClass.java	17 Feb 2002 14:09:03 -0000	1.4
  +++ MetaClass.java	28 Feb 2002 18:28:41 -0000	1.5
  @@ -61,7 +61,7 @@
    *
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *     baliuka@mwm.lt</a>
  - *@version    $Id: MetaClass.java,v 1.4 2002/02/17 14:09:03 baliuka Exp $
  + *@version    $Id: MetaClass.java,v 1.5 2002/02/28 18:28:41 baliuka Exp $
    */
   
   public interface MetaClass {
  @@ -92,6 +92,9 @@
   
       
       public int getPropertyIndex(String field);
  +    
  +    
  +    public int getPropertyIndex(java.lang.reflect.Member method);
   
   
       /**
  @@ -115,7 +118,10 @@
       public String getOIDName();
       
       public Object nextOID();
  -
  +    
  +    public boolean isReference(int index);
  +    
  +    public boolean isReference(java.lang.reflect.Member member);
   
       public Persistent newInstance( Object oid );
       
  
  
  
  1.4       +94 -29    jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/MetaClassImpl.java
  
  Index: MetaClassImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/MetaClassImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MetaClassImpl.java	24 Feb 2002 17:01:33 -0000	1.3
  +++ MetaClassImpl.java	28 Feb 2002 18:28:41 -0000	1.4
  @@ -40,7 +40,7 @@
    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  + * USE, DATA, OR PROFITS; OR BUSINESS INToutUPTION) HOWEVER CAUSED AND
    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  @@ -65,7 +65,7 @@
    *
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *     baliuka@mwm.lt</a>
  - *@version    $Id: MetaClassImpl.java,v 1.3 2002/02/24 17:01:33 baliuka Exp $
  + *@version    $Id: MetaClassImpl.java,v 1.4 2002/02/28 18:28:41 baliuka Exp $
    */
   public class MetaClassImpl implements MetaClass {
       
  @@ -74,6 +74,7 @@
       private String sqlName;
       private String oidName;
       private java.util.Map properties = new java.util.Hashtable();
  +    private java.util.Map methods    = new java.util.Hashtable();
       private java.beans.PropertyDescriptor[] descriptors;
       private Context context;
       private static OIDGenerator generator = new RandomOIDGenerator();
  @@ -87,7 +88,7 @@
               for( int i = 0; i < descriptors.length; i++ ){
                   if ( descriptors[i].getName().equals(id) ){
                       field = new Field(id,null);
  -                    fields.put(id,fields);
  +                    fields.put(id,field);
                       return field;
                   }
               }
  @@ -113,7 +114,7 @@
           private String referenceField;
           
           /** Holds value of property referencedClass. */
  -        private String referenceClass;
  +        private Class refClass;
           
           /** Getter for property id.
            * @return Value of property id.
  @@ -160,15 +161,20 @@
           /** Getter for property referencedClass.
            * @return Value of property referencedClass.
            */
  -        public String getReferenceClass() {
  -            return this.referenceClass;
  +        public Class getReferenceClass() {
  +            return this.refClass;
           }
           
           /** Setter for property referencedClass.
            * @param referencedClass New value of property referencedClass.
            */
           public void setReferenceClass( String referencedClass ) {
  -            this.referenceClass = referencedClass;
  +            try{
  +                this.refClass = Class.forName( referencedClass);
  +            }catch(Throwable t){
  +                t.printStackTrace();
  +                throw new java.lang.NoClassDefFoundError(referencedClass);
  +            }
           }
           
           /** Getter for property validator.
  @@ -188,7 +194,7 @@
       
       
       /** Creates new MetaClassImpl */
  -    public MetaClassImpl(){
  +    public MetaClassImpl()throws Exception{
           
       }
       
  @@ -200,6 +206,15 @@
           
       }
       
  +    public boolean isReference(java.lang.reflect.Member member) {
  +        
  +      return isReference(  getPropertyIndex(member) );
  +        
  +    }
  +    public boolean isReference(int index) {
  +        return null != getField(descriptors[ index ].getName()).getReferenceClass();
  +    }
  +    
       /**
        * return field name for DB storage
        *
  @@ -207,8 +222,9 @@
        * @return        field name
        */
       public String getPropertyName( int index ) {
  -        //TODO :
  -        return toSQLName(descriptors[ index ].getName());
  +        
  +        return getField(descriptors[ index ].getName()).getName();
  +        
       }
       
       public Persistent newInstance( Object oid ){
  @@ -245,8 +261,6 @@
        * @return        type
        */
       public Class getPropertyType( int index ) {
  -        // TODO : must return JDBC type
  -        // if Date ...
           return descriptors[index].getPropertyType();
       }
       
  @@ -282,6 +296,32 @@
           return clasz;
       }
       
  +    private void indexMethodsFromMetaData(){
  +        try{
  +            
  +            
  +            
  +            for( int i = 0; i < descriptors.length; i++ ){
  +                String name =  getField(descriptors[i].getName()).getName();
  +                if( name == null ){
  +                    name = toSQLName(descriptors[i].getName());
  +                    getField(descriptors[i].getName()).setName(name);
  +                }
  +                properties.put(name.toUpperCase(),new Integer(i) );
  +                if(descriptors[i].getReadMethod() != null)
  +                    methods.put(descriptors[i].getReadMethod(),new Integer(i));
  +                if(descriptors[i].getWriteMethod() != null)
  +                    methods.put(descriptors[i].getWriteMethod(),new Integer(i));
  +                
  +            }
  +        }catch( java.lang.Throwable t ){
  +            //TODO :
  +            t.printStackTrace();
  +            throw new java.lang.RuntimeException(t.getLocalizedMessage());
  +        }
  +        
  +    }
  +    
       private void indexMethods(){
           try{
               
  @@ -290,17 +330,20 @@
               for( int i = 0; i < descriptors.length; i++ ){
                   
                   properties.put( toSQLName(descriptors[i].getName()),new Integer(i) );
  -                
  +                if(descriptors[i].getReadMethod() != null)
  +                    methods.put(descriptors[i].getReadMethod(),new Integer(i));
  +                if(descriptors[i].getWriteMethod() != null)
  +                    methods.put(descriptors[i].getWriteMethod(),new Integer(i));
               }
           }catch( java.lang.Throwable t ){
               //TODO :
  +            t.printStackTrace();
               throw new java.lang.RuntimeException(t.getLocalizedMessage());
           }
           
       }
       
       
  -    
       static String toSQLName(String clName) {
           
           String result = clName.substring(clName.lastIndexOf('.') + 1, clName.length());
  @@ -358,9 +401,19 @@
        * @param  method  read or write method
        * @return         index
        */
  +    public int getPropertyIndex(java.lang.reflect.Member method) {
  +        
  +        Integer index = (Integer)methods.get(method);
  +        if(index == null)
  +            throw new java.lang.NoSuchMethodError(method.getDeclaringClass().getName()+
"." + method.getName());
  +        return index.intValue();
  +        
  +    }
       public int getPropertyIndex(String field) {
  -        //TODO :
  -        Integer index = (Integer)properties.get(field);
  +        
  +        Integer index = (Integer)properties.get( field.toUpperCase() );
  +        if( index == null )
  +             throw new java.lang.NoSuchFieldError( clasz.getName() + "." + field );
           return index.intValue();
           
       }
  @@ -388,7 +441,7 @@
       
       static  class StorageHandlerImpl implements StorageHandler {
           
  -        MetaClassImpl root = new MetaClassImpl();
  +        
           MetaClassImpl currentClass ;
           Field currentField;
           
  @@ -396,7 +449,7 @@
               
           }
           
  -        public static final boolean DEBUG = false;
  +        public static final boolean DEBUG = true;
           
           public void setDocumentLocator(org.xml.sax.Locator locator ){}
           
  @@ -421,57 +474,69 @@
           public final void skippedEntity(java.lang.String name) throws SAXException {}
           
           public void handle_reference(final Attributes meta) throws SAXException {
  -            if (DEBUG) System.err.println("handle_reference: " + meta);
  +            if (DEBUG) System.out.println("handle_reference: " + meta);
               currentField.setReferenceClass( meta.getValue("mclass") );
               currentField.setReferenceField( meta.getValue("field")  );
           }
           
           public void start_validator(final Attributes meta) throws SAXException {
  -            if (DEBUG) System.err.println("start_validator: " + meta);
  +            if (DEBUG) System.out.println("start_validator: " + meta);
           }
           
           public void end_validator() throws SAXException {
  -            if (DEBUG) System.err.println("end_validator()");
  +            if (DEBUG) System.out.println("end_validator()");
           }
           
           public void handle_property(final java.lang.String data, final Attributes meta)
throws SAXException {
  -            if (DEBUG) System.err.println("handle_property: " + data);
  +            if (DEBUG) System.out.println("handle_property: " + data);
           }
           
           public void start_field(final Attributes meta) throws SAXException {
  -            if (DEBUG) System.err.println("start_field: " + meta);
  +            if (DEBUG) System.out.println("start_field: " + meta);
               currentField = currentClass.getField( meta.getValue("id") );
               currentField.setName( meta.getValue("name") );
           }
           
           public void end_field() throws SAXException {
  -            if (DEBUG) System.err.println("end_field()");
  +            if (DEBUG) System.out.println("end_field()");
               
           }
           
           public void start_storage(final Attributes meta) throws SAXException {
  -            if (DEBUG) System.err.println("start_storage: " + meta);
  +            if (DEBUG) System.out.println("start_storage: " + meta);
           }
           
           public void end_storage() throws SAXException {
  -            if (DEBUG) System.err.println("end_storage()");
  +            if ( DEBUG ) System.out.println("end_storage()");
           }
           public void start_mclass(final Attributes meta) throws SAXException {
  -            if (DEBUG) System.err.println("start_mclass: " + meta);
  +            if ( DEBUG ) System.out.println("start_mclass: " + meta);
               try{
  -                currentClass = (MetaClassImpl)root.forClass( Class.forName( meta.getValue("id"))
);
  +                Class clasz   = Class.forName( meta.getValue("id")) ;
  +                currentClass       =   (MetaClassImpl)persitentClasses.get(clasz);
  +                if ( currentClass == null ){
  +                    currentClass = new MetaClassImpl();
  +                    currentClass.clasz = clasz;
  +                    persitentClasses.put(currentClass.clasz,currentClass);
  +                }
  +                
  +                currentClass.descriptors = java.beans.Introspector.getBeanInfo( currentClass.clasz
).getPropertyDescriptors();
                   currentClass.sqlName = meta.getValue("name");
                   currentClass.oidName = meta.getValue("oid");
  +                
               }catch( Exception e ){
  +                e.printStackTrace();
                   rethrow(e);
               }
           }
           
           public void end_mclass() throws SAXException {
  -            if (DEBUG) System.err.println("end_mclass()");
  +            if (DEBUG) System.out.println("end_mclass()");
  +            currentClass.indexMethodsFromMetaData();
           }
           
           private void rethrow(Exception e )throws SAXException {
  +            e.printStackTrace();
               throw new SAXException( e.getClass().getName() + ":" +  e.getMessage());
           }
           
  
  
  
  1.8       +17 -7     jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/PersistentProxy.java
  
  Index: PersistentProxy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/PersistentProxy.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- PersistentProxy.java	23 Feb 2002 12:11:30 -0000	1.7
  +++ PersistentProxy.java	28 Feb 2002 18:28:41 -0000	1.8
  @@ -74,7 +74,7 @@
    *      baliuka@mwm.lt</a>
    *@author     Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">
    *      g-froehlich@gmx.de</a>
  - *@version    $Id: PersistentProxy.java,v 1.7 2002/02/23 12:11:30 baliuka Exp $
  + *@version    $Id: PersistentProxy.java,v 1.8 2002/02/28 18:28:41 baliuka Exp $
    */
   public class PersistentProxy
   implements MetaObject, InvocationHandler, Cloneable, Serializable {
  @@ -97,6 +97,7 @@
       private TypeConverter m_typeConverter = new DefaultTypeConverter();
       private Persistent m_object;
       private Class m_clazz;
  +    private MetaClass m_metaClass;
       private Context m_context;
       
       public PersistentProxy(Class clazz,
  @@ -107,7 +108,7 @@
           m_newCreated = newCreated;
           m_context = context;
           m_clazz = clazz;
  -        
  +        m_metaClass = context.getMetaClass(clazz);
       }
       
       
  @@ -129,7 +130,7 @@
               handler.m_clean = true;
               context.getTransactionManager().getTransaction().add( handler );
               handler.m_clean = false;
  -           
  +            
           }
           return p;
       }
  @@ -157,6 +158,7 @@
       }
       
       public Object getProperty(String name) {
  +        
           return m_map.get(name);
       }
       
  @@ -253,16 +255,24 @@
               throw new IllegalStateException("Object properties invalidated");
           
           if (name.startsWith("set")) {
  -            setProperty(name.substring(3), obj2[0]);
  +            if( obj2[0] != null && m_metaClass.isReference(method) ){
  +                setProperty(name.substring(3), ((Persistent)obj2[0]).getOID());
  +            }else{
  +                setProperty(name.substring(3), obj2[0]);
  +            }
               
               return null;
  -        } else if (name.startsWith("get") || name.startsWith("is")) {
  -            if (method.getName().startsWith("get")) {
  +        } else if ( name.startsWith("get") || name.startsWith("is") ) {
  +            if ( method.getName().startsWith("get") ) {
                   name = method.getName().substring(3);
               } else {
                   name = method.getName().substring(2);
               }
               Object value = getProperty(name);
  +            if( value != null && m_metaClass.isReference( method ) ){
  +                return m_context.getStorage().
  +                retrieveObject(method.getReturnType(),value);
  +            }
               if ( value == null && method.getReturnType().isPrimitive() ){
                   
                   return DEFAULTS.get(method.getReturnType());
  @@ -273,7 +283,7 @@
               }
               return m_typeConverter.convert( value , method.getReturnType() );
           }
  -        throw new IllegalStateException("pure method " + method.getName());
  +        throw new java.lang.NoSuchMethodError("pure method " + method.getName());
       }
       
       public void remove() {
  
  
  
  1.3       +16 -2     jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestPersistent.java
  
  Index: TestPersistent.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestPersistent.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestPersistent.java	9 Feb 2002 18:57:05 -0000	1.2
  +++ TestPersistent.java	28 Feb 2002 18:28:41 -0000	1.3
  @@ -57,10 +57,14 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: TestPersistent.java,v 1.2 2002/02/09 18:57:05 froehlich Exp $
  + *@version    $Id: TestPersistent.java,v 1.3 2002/02/28 18:28:41 baliuka Exp $
    */
   public interface TestPersistent {
  -
  +    
  +    public TestPersistent getParent();
  +    
  +    public void setParent(TestPersistent tp);
  +    
       public boolean getBoolVal();
   
       public void setBoolVal(boolean val);
  @@ -81,6 +85,16 @@
   
       public String getStrVal();
   
  +    /** Getter for property strVal1.
  +     * @return Value of property strVal1.
  +     */
  +    public String getStrVal1();
  +    
  +    /** Setter for property strVal1.
  +     * @param strVal1 New value of property strVal1.
  +     */
  +    public void setStrVal1(String strVal1);
  +    
       //TODO : PesistentObjectVal and unknown SerializableObjectVal
   
   }
  
  
  
  1.8       +22 -10    jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestSample.java
  
  Index: TestSample.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestSample.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestSample.java	23 Feb 2002 12:11:30 -0000	1.7
  +++ TestSample.java	28 Feb 2002 18:28:41 -0000	1.8
  @@ -72,7 +72,7 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: TestSample.java,v 1.7 2002/02/23 12:11:30 baliuka Exp $
  + *@version    $Id: TestSample.java,v 1.8 2002/02/28 18:28:41 baliuka Exp $
    */
   public class TestSample extends TestCase {
       
  @@ -91,7 +91,7 @@
           junit.textui.TestRunner.main(testCaseName);
       }
       
  -    public void testCreate() throws java.lang.Exception {
  +    public void _testCreate() throws java.lang.Exception {
           for (int i = 0; i < 10; i++) {
               
               Transaction transaction = pm.getTransaction();
  @@ -110,8 +110,9 @@
               object3.setDateVal(new java.util.Date());
               object3.setIntVal(i);
               object3.setFloatVal(i / 2);
  -            object3.setStrVal("" + i);
  +            object3.setStrVal1("" + i);
               object3.setBoolVal(true);
  +            object3.setParent(object2);
               transaction.commit();
               
               assertEquals("Equals " + oid, object2, object1);
  @@ -121,7 +122,7 @@
           
       }
       
  -    public void testRetrieve() throws java.lang.Exception {
  +    public void _testRetrieve() throws java.lang.Exception {
           
           Transaction transaction = pm.getTransaction();
           
  @@ -138,7 +139,8 @@
               System.out.println("DateVal  = " + object.getDateVal());
               System.out.println("IntVal   = " + object.getIntVal());
               System.out.println("FloatVal = " + object.getFloatVal());
  -            System.out.println("StrVal   = " + object.getStrVal());
  +            System.out.println("StrVal1   = " + object.getStrVal1());
  +            System.out.println("Parent Object   = " + object.getParent());
               
           }
           
  @@ -147,6 +149,9 @@
       }
       public void testRollback() throws java.lang.Exception {
           
  +        _testCreate();
  +        _testRetrieve();
  +        
           Transaction transaction = pm.getTransaction();
           
           transaction.begin();
  @@ -202,16 +207,20 @@
       
       protected void setUp() throws java.lang.Exception {
           super.setUp();
  +        if( pm != null )
  +            return;
  +        
  +        MetaClassImpl.parse("org/apache/commons/simplestore/storage.xml");
           
  -        // this datasource is for reentrant connections only !!!
           DriverDataSource ds = new DriverDataSource();
           ds.setDriver("org.hsqldb.jdbcDriver");
           ds.setMaxConnections(1);
           ds.setUrl("jdbc:hsqldb:sample");
           ds.setUser("sa");
           ds.setPassword("");
  -        String sql = "CREATE TABLE TEST_PERSISTENT ( ID bigint not null primary key, Bool_Val
smallint," +
  -        " Int_Val integer, Float_Val float, Date_Val date, Str_Val longVarChar )  ";
  +        String sql = //"DROP TABLE TEST_PERSISTENT";
  +         "CREATE TABLE TEST_PERSISTENT ( ID bigint not null primary key, parent_object
bigint,Bool_Val smallint," +
  +         " Int_Val integer, Float_Val float, Date_Val date, Str_Val longVarChar, s_1_str_val
 longVarChar )  ";
           
           java.sql.Connection con = ds.getConnection();
           try {
  @@ -225,9 +234,12 @@
           } finally {
               con.close();
           }
  -        
  +       
  +        MetaClassImpl mclass = new MetaClassImpl();
           DBStorage storage = new DBStorage(ds);
  -        ContextImpl context = new ContextImpl(new MetaClassImpl(),storage,SoftRefMemoryCache.getInstance(new
java.util.HashMap(),0xFF),storage);
  +        ContextImpl context = new ContextImpl(mclass,storage,
  +              SoftRefMemoryCache.getInstance(new java.util.HashMap(),0xFF),
  +                        storage );
           pm = PersistenceManager.getPersistenceManager(context);
       }
       
  
  
  
  1.1                  jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/storage.xml
  
  Index: storage.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  
  <!DOCTYPE storage >
  
  <storage>
      <mclass id="org.apache.commons.simplestore.TestPersistent" name="TEST_PERSISTENT"
oid="ID">
          <field id="strVal1" name="s_1_str_val"/>
          <field id="parent" name="parent_object"/>
          <reference mclass="org.apache.commons.simplestore.TestPersistent" 
                     field="parent"/>
      </mclass>
  </storage>
  
  
  
  

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