You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2002/02/17 15:09:04 UTC
cvs commit: jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore TestSample.java
baliuka 02/02/17 06:09:04
Modified: simplestore/src/java/org/apache/commons/simplestore/persistence
MetaClass.java OIDGenerator.java Storage.java
TypeConverter.java
simplestore/src/java/org/apache/commons/simplestore/persistence/impl
AbstractStorage.java DBStorage.java
DefaultTypeConverter.java MetaClassImpl.java
PersistenceManager.java PersistentProxy.java
simplestore/src/test/org/apache/commons/simplestore
TestSample.java
Added: simplestore/src/java/org/apache/commons/simplestore/persistence
Context.java
simplestore/src/java/org/apache/commons/simplestore/persistence/impl
ContextImpl.java
Log:
Implemented default Metaclass, adde Context interface, it used to plug
implementations
Revision Changes Path
1.4 +11 -3 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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- MetaClass.java 16 Feb 2002 14:26:05 -0000 1.3
+++ MetaClass.java 17 Feb 2002 14:09:03 -0000 1.4
@@ -61,7 +61,7 @@
*
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- *@version $Id: MetaClass.java,v 1.3 2002/02/16 14:26:05 baliuka Exp $
+ *@version $Id: MetaClass.java,v 1.4 2002/02/17 14:09:03 baliuka Exp $
*/
public interface MetaClass {
@@ -108,14 +108,22 @@
*@param index property index
*@return type
*/
- public Class getPropertyType(int index);
+ public Class getPropertyType(int index);
+ public Class getOIDType();
+
+ public String getOIDName();
+
+ public Object nextOID();
- public Persistent newInstance(Object oid, boolean create);
+ public Persistent newInstance( Object oid );
+ public Persistent newInstance();
public Persistent newInstance(Object oid, boolean create,java.lang.ClassLoader cl);
+
+ public void setContext( Context context );
}
1.2 +3 -1 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/OIDGenerator.java
Index: OIDGenerator.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/OIDGenerator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- OIDGenerator.java 12 Feb 2002 11:38:21 -0000 1.1
+++ OIDGenerator.java 17 Feb 2002 14:09:03 -0000 1.2
@@ -57,13 +57,15 @@
/**
*@author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">
* g-froehlich@gmx.de</a>
- *@version $Id: OIDGenerator.java,v 1.1 2002/02/12 11:38:21 froehlich Exp $
+ *@version $Id: OIDGenerator.java,v 1.2 2002/02/17 14:09:03 baliuka Exp $
*/
public interface OIDGenerator {
public Long getLongOID();
public Integer getIntOID();
+
+
}
1.3 +2 -2 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/Storage.java
Index: Storage.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/Storage.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Storage.java 11 Feb 2002 20:30:16 -0000 1.2
+++ Storage.java 17 Feb 2002 14:09:03 -0000 1.3
@@ -61,7 +61,7 @@
/**
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- *@version $Id: Storage.java,v 1.2 2002/02/11 20:30:16 froehlich Exp $
+ *@version $Id: Storage.java,v 1.3 2002/02/17 14:09:03 baliuka Exp $
*/
public interface Storage {
@@ -72,7 +72,7 @@
public void registerClass(Class clasz) throws StorageException;
- public void setContext(Cache cache);
+ public void setContext(Context context );
public Set retrieveAll(Class clasz) throws StorageException;
1.3 +3 -1 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/TypeConverter.java
Index: TypeConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/TypeConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TypeConverter.java 17 Feb 2002 11:24:15 -0000 1.2
+++ TypeConverter.java 17 Feb 2002 14:09:03 -0000 1.3
@@ -59,7 +59,7 @@
*
*@author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">
* g-froehlich@gmx.de</a>
- *@version $Id: TypeConverter.java,v 1.2 2002/02/17 11:24:15 froehlich Exp $
+ *@version $Id: TypeConverter.java,v 1.3 2002/02/17 14:09:03 baliuka Exp $
*/
public interface TypeConverter {
@@ -67,5 +67,7 @@
public Object convertNumber(Number number, Class clazz);
public Object convertPrimitives(Number number, Class clazz);
+
+ public Object convert(Object object , Class clazz);
}
1.1 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/Context.java
Index: Context.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache Cocoon" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* 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
* 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
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.simplestore.persistence;
import java.util.Set;
import org.apache.commons.simplestore.cache.Cache;
/**
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
*@version $Id: Context.java,v 1.1 2002/02/17 14:09:03 baliuka Exp $
*/
public interface Context {
MetaClass getMetaClass( Class clasz );
Storage getStorage();
TransactionManager getTransactionManager();
Cache getCache();
}
1.3 +38 -39 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/AbstractStorage.java
Index: AbstractStorage.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/AbstractStorage.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- AbstractStorage.java 11 Feb 2002 20:30:09 -0000 1.2
+++ AbstractStorage.java 17 Feb 2002 14:09:04 -0000 1.3
@@ -68,30 +68,32 @@
/**
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- *@version $Id: AbstractStorage.java,v 1.2 2002/02/11 20:30:09 froehlich Exp $
+ *@version $Id: AbstractStorage.java,v 1.3 2002/02/17 14:09:04 baliuka Exp $
*/
public abstract class AbstractStorage
- implements Storage, InternalTransaction, TransactionManager {
-
- Map attributes = new HashMap();
-
- public void setAttribute(String name, Object value) {
-
- attributes.put(name, value);
- }
-
-
+implements Storage, InternalTransaction, TransactionManager {
+
+
+
public InternalTransaction getTransaction() {
-
+
return TransactionImpl.getInstance(this);
}
-
+
public Object getAttribute(String name) {
- return attributes.get(name);
+ return getTransaction().getAttribute(name);
}
-
-
+
+ public void removeAttribute(String name) {
+ getTransaction().removeAttribute(name);
+ }
+
+ public void setAttribute(String name, Object value) {
+ getTransaction().setAttribute( name, value);
+ }
+
+
public void begin(Set objects) {
try {
internalBegin();
@@ -101,11 +103,11 @@
throw new RuntimeException(t.getClass() + ":" + t.getMessage());
}
}
-
+
public void add(MetaObject props) {
((InternalTransaction) getTransaction()).add(props);
}
-
+
public void commit() {
try {
getTransaction().commit();
@@ -116,7 +118,7 @@
throw new RuntimeException(t.getClass() + ":" + t.getMessage());
}
}
-
+
public void begin() {
try {
getTransaction().begin();
@@ -126,7 +128,7 @@
throw new RuntimeException(t.getClass() + ":" + t.getMessage());
}
}
-
+
public void rollback(Set objects) {
try {
//TODO implement roolback
@@ -136,9 +138,9 @@
throw new RuntimeException(t.getClass() + ":" + t.getMessage());
}
}
-
+
public void rollback() {
-
+
try {
getTransaction().rollback();
internalRollback();
@@ -146,26 +148,26 @@
// TODO
throw new RuntimeException(t.getClass() + ":" + t.getMessage());
}
-
+
}
-
+
public void commit(Set objects) {
-
+
try {
Iterator iterator = objects.iterator();
while (iterator.hasNext()) {
-
+
MetaObject pp = (MetaObject) iterator.next();
if (pp.isNew()) {
createObject(pp);
-
+
}
if (pp.isDirty() && !pp.isNew()) {
storeObject(pp);
-
+
}
if (pp.isDeleted()) {
-
+
removeObject(pp);
}
pp.setDirty(false);
@@ -177,21 +179,18 @@
t.printStackTrace();
throw new RuntimeException("Error");
}
-
+
}
-
- public void removeAttribute(String name) {
- attributes.remove(name);
- }
-
+
+
protected abstract void createObject(MetaObject properties) throws StorageException;
-
+
protected abstract void removeObject(MetaObject properties) throws StorageException;
-
+
protected abstract void internalCommit() throws StorageException;
-
+
protected abstract void internalRollback() throws StorageException;
-
+
protected abstract void internalBegin() throws StorageException;
}
1.4 +56 -86 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/DBStorage.java
Index: DBStorage.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/DBStorage.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DBStorage.java 16 Feb 2002 14:26:05 -0000 1.3
+++ DBStorage.java 17 Feb 2002 14:09:04 -0000 1.4
@@ -61,18 +61,17 @@
import org.apache.commons.simplestore.persistence.ObjectNotFound;
import org.apache.commons.simplestore.persistence.Persistent;
import org.apache.commons.simplestore.persistence.StorageException;
+import org.apache.commons.simplestore.persistence.Context;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
-import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
-import java.util.Date;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
@@ -86,16 +85,16 @@
/**
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- *@version $Id: DBStorage.java,v 1.3 2002/02/16 14:26:05 baliuka Exp $
+ *@version $Id: DBStorage.java,v 1.4 2002/02/17 14:09:04 baliuka Exp $
*/
public class DBStorage extends AbstractStorage {
private final static boolean DEBUG = true;
private final static String CONNECTION = "org.apache.commons.simplestore.jdbc.DBStorage.connection";
private static Properties procedures = new Properties();
- private Cache cache = null;
private DataSource ds;
- private MetaClass rootMetaClass = new MetaClassImpl();
+ private Context context;
+ //private MetaClass rootMetaClass = new MetaClassImpl(this);
/**
* Creates new DBStorageManager
*
@@ -108,8 +107,8 @@
// TODO must be converter interface
public static Object toSQLType(Object object) {
- if (object == null) {
- return null;
+ if ( object == null ) {
+ return null;
}
if (object instanceof Boolean) {
boolean value = ((Boolean) object).booleanValue();
@@ -125,15 +124,16 @@
- // TODO : must be more simple
- public static int excecute(Connection connection, String sql, Object[] args, ResultSetHandler eh) throws StorageException {
+
+ private int excecute( String sql, Object[] args, ResultSetHandler eh) throws StorageException {
+ Connection connection = getConnection();
int result = 0;
try {
final PreparedStatement statement = connection.prepareStatement(sql);
ResultSet rs = null;
- if (DEBUG) {
+ if ( DEBUG ) {
System.out.println(sql);
}
try {
@@ -154,7 +154,7 @@
return statement.getUpdateCount();
}
- do {
+
if (rs != null && eh != null) {
ResultSetMetaData rsmd = rs.getMetaData();
@@ -170,18 +170,13 @@
}
}
- if (statement.getMoreResults()) {
- rs = statement.getResultSet();
- } else {
- break;
- }
- } while (rs != null);
+
} finally {
- if (rs != null) {
- rs.close();
+ if ( rs != null ) {
+ rs.close();
}
- if (statement != null) {
+ if ( statement != null ) {
statement.close();
}
}
@@ -191,21 +186,11 @@
return result;
}
- public void setContext(Cache cache) {
- this.cache = cache;
+ public void setContext(Context context) {
+ this.context = context;
}
- public Class getReturnType(Class clasz, String name) throws Throwable {
- try {
-
- return clasz.getMethod(name, null).getReturnType();
- } catch (NoSuchMethodException nsme) {
-
- throw new NoSuchMethodException("Method " + name + " not found in class " + clasz.getName() + " " + nsme.getMessage());
- }
-
- }
public void registerClass(Class clasz) {
@@ -224,22 +209,19 @@
}
}
- public Object retrieveObject(final Class clasz, Object id) throws StorageException {
-
- final InternalTransaction transaction = getTransaction();
- Persistent result = (Persistent) cache.get(id);
+ public Object retrieveObject( final Class clasz, Object id ) throws StorageException {
+
+ Persistent result = (Persistent) context.getCache().get(id);
if (result != null) {
- transaction.add(result.getMetaObject());
- return result;
+ return result;
}
- final Connection connection = getConnection();
- final MetaClass mClass = rootMetaClass.forClass(clasz);
+
+ final MetaClass mClass = context.getMetaClass( clasz );
final java.beans.PropertyDescriptor[] descriptors = mClass.getProperties();
- // TODO : ID must be in some mapping
- final String sql = "SELECT * FROM " + mClass.getName() + " WHERE ID=?";
- result = (Persistent) mClass.newInstance( id, false );
+ final String sql = "SELECT * FROM " + mClass.getName() + " WHERE "+mClass.getOIDName()+"=?";
+ result = (Persistent) mClass.newInstance( id );
final MetaObject pc = result.getMetaObject();
final Map map = pc.getProperties();
@@ -252,7 +234,7 @@
int propIndex = mClass.getPropertyIndex( name );
if (value != null) {
String property = descriptors[ propIndex ].getName();
- if (property.equals("Id")) {
+ if (name.equalsIgnoreCase(mClass.getOIDName())) {
return;
}
map.put(property, value );
@@ -265,25 +247,23 @@
};
- if (excecute(connection, sql, new Object[]{id}, rsh) == 0) {
+ if ( excecute( sql, new Object[]{id}, rsh) == 0 ) {
throw new ObjectNotFound(pc.getOID().toString(), null);
}
- transaction.add(pc);
- cache.put(id, result);
+
+ context.getCache().put(id, result);
return result;
}
public Set retrieveAll(final Class clasz) throws StorageException {
- final Connection connection = getConnection();
- final MetaClass mClass = rootMetaClass.forClass(clasz);
+ final MetaClass mClass = context.getMetaClass(clasz);
final String sql = "SELECT ID AS INTERNAL_OID, * FROM " + mClass.getName();
final Set objects = new HashSet();
- final InternalTransaction transaction = getTransaction();
-
- excecute(connection, sql, null, new QueryHandler(objects, clasz));
+
+ excecute( sql, null, new QueryHandler(objects, clasz));
return objects;
}
@@ -291,12 +271,10 @@
public Set query(final Class clasz, String proc, Object[] args) throws StorageException {
- final Connection connection = getConnection();
final Set objects = new HashSet();
- final InternalTransaction transaction = getTransaction();
proc = procedures.getProperty(proc);
- excecute(connection, proc, args, new QueryHandler(objects, clasz));
+ excecute( proc, args, new QueryHandler(objects, clasz));
return objects;
}
@@ -304,9 +282,8 @@
public void storeObject(MetaObject properties) throws StorageException {
- final Connection connection = getConnection();
final Class clasz = properties.getPersistentClass();
- final MetaClass mClass = rootMetaClass.forClass(clasz);
+ final MetaClass mClass = context.getMetaClass(clasz);
final String name = mClass.getName();
final java.beans.PropertyDescriptor[] beanProps = mClass.getProperties();
final List values = new ArrayList( beanProps.length + 1);
@@ -326,23 +303,21 @@
}
}
names.setCharAt(names.length() - 1, ' ');
- // TODO : ID must be in some mapping
- names.append("WHERE ID=?");
+ names.append("WHERE "+ mClass.getOIDName() + "=?");
values.add(properties.getOID());
final String sql = "UPDATE " + name + " SET " + names;
- if (excecute(connection, sql, values.toArray(), null) == 0) {
- throw new ObjectNotFound(properties.getOID().toString(), null);
+ if ( excecute( sql, values.toArray(), null) == 0 ) {
+ throw new ObjectNotFound(properties.getOID().toString(), null);
}
}
- // TODO : mot very meanigful here
public void close() {
try {
- Connection connection = (Connection) getTransaction().getAttribute(CONNECTION);
+ Connection connection = (Connection) getTransaction().getAttribute( CONNECTION );
if (connection != null) {
connection.close();
}
@@ -355,9 +330,8 @@
protected void createObject(MetaObject properties) throws StorageException {
- final Connection connection = getConnection();
final Class clasz = properties.getPersistentClass();
- final MetaClass mClass = rootMetaClass.forClass(clasz);
+ final MetaClass mClass = context.getMetaClass(clasz);
final String name = mClass.getName();
final Object id = properties.getOID();
final java.beans.PropertyDescriptor[] beanProps = mClass.getProperties();
@@ -382,24 +356,25 @@
params += ",?";
}
}
- //TODO : ID !!!
- final String sql = "INSERT INTO " + name + "(ID" + names + ")VALUES(?" + params + ")";
- excecute(connection, sql, values.toArray(), null);
+ final String sql = "INSERT INTO " + name +
+ "(" + mClass.getOIDName() + names +
+ ")VALUES(?" + params + ")";
+
+ excecute( sql, values.toArray(), null);
}
protected void removeObject(MetaObject obj) throws StorageException {
- final Connection connection = getConnection();
- final MetaClass mClass = rootMetaClass.forClass(obj.getPersistentClass());
+ final MetaClass mClass = context.getMetaClass(obj.getPersistentClass());
final String name = mClass.getName();
final Object id = obj.getOID();
- // TODO : ID must be in some mapping
- final String sql = "DELETE FROM " + name + " WHERE ID=?";
+ final String sql = "DELETE FROM " + name +
+ " WHERE "+ mClass.getOIDName() + "=?";
- if (excecute(connection, sql, new Object[]{id}, null) == 0) {
- throw new ObjectNotFound(obj.getOID().toString(), null);
+ if ( excecute( sql, new Object[]{id}, null) == 0 ) {
+ throw new ObjectNotFound(obj.getOID().toString(), null);
}
}
@@ -417,7 +392,6 @@
}
protected void internalRollback() throws StorageException {
-
try {
Connection connection = getConnection();
try {
@@ -453,16 +427,14 @@
}
class QueryHandler implements ResultSetHandler {
-
Map map;
Set objects;
MetaClass mClass ;
- InternalTransaction transaction = getTransaction();
java.beans.PropertyDescriptor[] descriptors;
QueryHandler(Set objects, Class clasz) {
this.objects = objects;
- this.mClass = rootMetaClass.forClass(clasz);
+ this.mClass = context.getMetaClass(clasz);
this.descriptors = mClass.getProperties();
}
@@ -470,28 +442,26 @@
try {
if (index == 1) {
- Persistent p = (Persistent) cache.get(value);
- if (p != null) {
+ Persistent p = (Persistent) context.getCache().get(value);
+ if ( p != null ) {
map = null;
objects.add(p);
- transaction.add(p.getMetaObject());
return;
}
- p = mClass.newInstance( value, false );
+ p = mClass.newInstance( value );
MetaObject pc = p.getMetaObject();
map = pc.getProperties();
objects.add(p);
- transaction.add(pc);
return;
}
- // TODO : ID must be in some mapping
- if ( name.toUpperCase().equals("ID") || map == null) {
+
+ if ( name.equalsIgnoreCase( mClass.getOIDName() ) || map == null) {
return;
}
String property = descriptors[ mClass.getPropertyIndex( name ) ].getName();
Class propType = descriptors[ mClass.getPropertyIndex( name ) ].getPropertyType();
- map.put(property, value);
+ map.put( property, value );
} catch (Throwable t) {
t.printStackTrace();
1.3 +35 -1 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/DefaultTypeConverter.java
Index: DefaultTypeConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/DefaultTypeConverter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DefaultTypeConverter.java 17 Feb 2002 11:24:08 -0000 1.2
+++ DefaultTypeConverter.java 17 Feb 2002 14:09:04 -0000 1.3
@@ -61,7 +61,7 @@
*
*@author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">
* g-froehlich@gmx.de</a>
- *@version $Id: DefaultTypeConverter.java,v 1.2 2002/02/17 11:24:08 froehlich Exp $
+ *@version $Id: DefaultTypeConverter.java,v 1.3 2002/02/17 14:09:04 baliuka Exp $
*/
public class DefaultTypeConverter implements TypeConverter {
@@ -115,5 +115,39 @@
+ clazz.getName());
}
}
+
+
+ public Object convert(Object object, Class cls) {
+
+
+ if (object == null) {
+ return null;
+ }
+
+ if ( cls.isAssignableFrom( object.getClass() )) {
+ return object;
+ }
+
+
+ if ( Number.class.isAssignableFrom( cls ) && object instanceof Number) {
+ return convertNumber((Number) object, cls);
+ }
+ if ( cls.isPrimitive() && object instanceof Number) {
+ return convertPrimitives((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));
+ }
+
+ return object;
+ }
+
+
+
}
1.2 +38 -9 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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MetaClassImpl.java 16 Feb 2002 14:26:05 -0000 1.1
+++ MetaClassImpl.java 17 Feb 2002 14:09:04 -0000 1.2
@@ -62,7 +62,7 @@
*
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- *@version $Id: MetaClassImpl.java,v 1.1 2002/02/16 14:26:05 baliuka Exp $
+ *@version $Id: MetaClassImpl.java,v 1.2 2002/02/17 14:09:04 baliuka Exp $
*/
public class MetaClassImpl implements MetaClass {
@@ -71,11 +71,12 @@
private String sqlName;
private java.util.Map properties = new java.util.Hashtable();
private java.beans.PropertyDescriptor[] descriptors;
- private TransactionManager transactionManager;
- private int oidIndex[] = {0};
+ private Context context;
+ private static OIDGenerator generator = new RandomOIDGenerator();
+
/** Creates new MetaClassImpl */
public MetaClassImpl(){
-
+
}
private MetaClassImpl(Class clasz) {
@@ -93,14 +94,25 @@
* @param index property index
* @return field name
*/
- public String getPropertyName(int index) {
+ public String getPropertyName( int index ) {
return toSQLName(descriptors[ index ].getName());
}
+ public Persistent newInstance( Object oid ){
+
+ return newInstance( nextOID(), false );
+
+ }
+
+ public Persistent newInstance(){
+
+ return newInstance( nextOID(), true );
- public Persistent newInstance(Object oid, boolean create) {
+ }
+
+ protected Persistent newInstance(Object oid, boolean create) {
- return PersistentProxy.getPersitent(clasz, oid , create , transactionManager );
+ return PersistentProxy.getPersitent(clasz, oid , create , context );
}
/**
@@ -119,8 +131,9 @@
* @param index property index
* @return type
*/
- public Class getPropertyType(int index) {
+ public Class getPropertyType( int index ) {
// TODO : must return JDBC type
+ // if Date ...
return descriptors[index].getPropertyType();
}
@@ -134,6 +147,7 @@
MetaClass mc = (MetaClass)persitentClasses.get(clasz);
if( mc == null ){
mc = new MetaClassImpl(clasz);
+ mc.setContext(context);
}
return mc;
@@ -141,7 +155,7 @@
public Persistent newInstance(Object oid, boolean create, java.lang.ClassLoader cl) {
//TODO :
- return PersistentProxy.getPersitent(clasz, oid , create , transactionManager );
+ return PersistentProxy.getPersitent(clasz, oid , create , context );
}
@@ -241,4 +255,19 @@
return descriptors;
}
+ public String getOIDName() {
+ return "ID";
+ }
+
+ public Class getOIDType() {
+ return Long.class;
+ }
+
+ public Object nextOID() {
+ return generator.getLongOID();
+ }
+
+ public void setContext( Context context ){
+ this.context = context;
+ }
}
1.4 +18 -21 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/PersistenceManager.java
Index: PersistenceManager.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/PersistenceManager.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- PersistenceManager.java 12 Feb 2002 11:38:42 -0000 1.3
+++ PersistenceManager.java 17 Feb 2002 14:09:04 -0000 1.4
@@ -62,6 +62,7 @@
import org.apache.commons.simplestore.persistence.StorageException;
import org.apache.commons.simplestore.persistence.Transaction;
import org.apache.commons.simplestore.persistence.TransactionManager;
+import org.apache.commons.simplestore.persistence.Context;
import java.math.BigInteger;
import java.util.HashMap;
@@ -70,16 +71,13 @@
/**
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- *@version $Id: PersistenceManager.java,v 1.3 2002/02/12 11:38:42 froehlich Exp $
+ *@version $Id: PersistenceManager.java,v 1.4 2002/02/17 14:09:04 baliuka Exp $
*/
public class PersistenceManager {
private static PersistenceManager pm;
- private RandomOIDGenerator mRandomOIDGenerator = new RandomOIDGenerator();
- private Storage storage;
- private TransactionManager transactionManager;
- private Cache cache = SoftRefMemoryCache.getInstance(new HashMap(), 0xFF);
+ private Context context;
/**
* Creates new PersiatenceManager
@@ -87,40 +85,39 @@
*@param storage
*@param transactionManager
*/
- protected PersistenceManager(Storage storage, TransactionManager transactionManager) {
- this.storage = storage;
- this.transactionManager = transactionManager;
+ protected PersistenceManager(Context context) {
+ this.context = context;
}
- public static PersistenceManager getPersistenceManager(Storage storage, TransactionManager transactions) {
+ public static PersistenceManager getPersistenceManager( Context context ) {
if (pm == null) {
- pm = new PersistenceManager(storage, transactions);
- storage.setContext(pm.cache);
+ pm = new PersistenceManager( context );
+
}
return pm;
}
public Transaction getTransaction() {
- return transactionManager.getTransaction();
+ return context.getTransactionManager().getTransaction();
}
- public Object getOID(Object pc) {
+ public Object getOID( Object pc ) {
return ((Persistent) pc).getOID();
}
- public Object createInstance(Class aclass) {
- Object id = mRandomOIDGenerator.getIntOID();
- Persistent p = PersistentProxy.getPersitent(aclass, id, true, transactionManager);
- cache.put(id, p);
+ public Object createInstance( Class aclass ) {
+
+ Persistent p = context.getMetaClass(aclass).newInstance();
+ context.getCache().put( p.getOID(), p );
return p;
}
- public Object findInstance(Class clasz, Object oid) throws StorageException {
- return storage.retrieveObject(clasz, oid);
+ public Object findInstance( Class clasz, Object oid ) throws StorageException {
+ return context.getStorage().retrieveObject( clasz, oid );
}
public Set findAll(Class clasz) throws StorageException {
- return storage.retrieveAll(clasz);
+ return context.getStorage().retrieveAll(clasz);
}
public void removeInstance(Object pc) {
@@ -131,7 +128,7 @@
}
public void registerClass(Class clasz) throws StorageException {
- storage.registerClass(clasz);
+ context.getStorage().registerClass(clasz);
}
}
1.5 +85 -117 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.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- PersistentProxy.java 17 Feb 2002 11:22:23 -0000 1.4
+++ PersistentProxy.java 17 Feb 2002 14:09:04 -0000 1.5
@@ -57,9 +57,10 @@
import org.apache.commons.simplestore.persistence.MetaClass;
import org.apache.commons.simplestore.persistence.MetaObject;
import org.apache.commons.simplestore.persistence.Persistent;
-import org.apache.commons.simplestore.persistence.TransactionManager;
+import org.apache.commons.simplestore.persistence.Context;
import org.apache.commons.simplestore.persistence.TypeConverter;
+
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
@@ -73,18 +74,19 @@
* 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.4 2002/02/17 11:22:23 froehlich Exp $
+ *@version $Id: PersistentProxy.java,v 1.5 2002/02/17 14:09:04 baliuka Exp $
*/
public class PersistentProxy
- implements MetaObject, InvocationHandler, Cloneable, Serializable {
-
+implements MetaObject, InvocationHandler, Cloneable, Serializable {
+
private static Map DEFAULTS = new Hashtable();
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 Map m_map = new HashMap();
private Object m_oid = null;
private boolean m_dirty = false;
@@ -92,180 +94,135 @@
private TypeConverter m_typeConverter = new DefaultTypeConverter();
private Persistent m_object;
private Class m_clazz;
- private TransactionManager m_transactionManager;
+ private Context m_context;
private boolean m_newCreated;
-
- /**
- * Creates new ValueProxy
- *
- *@param oid
- *@param newCreated
- *@param transactionManager
- *@param clazz
- */
- public PersistentProxy(Class clazz,
- Object oid,
- boolean newCreated,
- TransactionManager transactionManager) {
+
+ public PersistentProxy(Class clazz,
+ Object oid,
+ boolean newCreated,
+ Context context) {
m_oid = oid;
m_newCreated = newCreated;
- m_transactionManager = transactionManager;
+ m_context = context;
m_clazz = clazz;
+
}
-
- // 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);
+
+
+
+ public static Persistent getPersitent( Class persistent,
+ Object oid,
+ boolean newCreated,
+ Context context ) {
+
+ PersistentProxy handler = new PersistentProxy( persistent, oid,
+ newCreated, context );
Persistent p = (Persistent) Proxy.newProxyInstance(
- Thread.currentThread().getContextClassLoader(),
- new Class[]{persistent, Persistent.class}, handler);
-
+ Thread.currentThread().getContextClassLoader(),
+ new Class[]{persistent, Persistent.class},
+ handler );
handler.m_object = p;
- if (newCreated) {
+ if ( newCreated ) {
+
MetaObject mo = p.getMetaObject();
- transactionManager.getTransaction().add(mo);
+ context.getTransactionManager().getTransaction().add(mo);
}
return p;
}
-
- public void setProperty(String name, Object value) {
- Object old = m_map.put(name, value);
-
- if (old == null || !old.equals(value)) {
- m_dirty = true;
- m_transactionManager.getTransaction().add(this);
- }
- }
-
+
+
public void setDirty(boolean dirty) {
m_dirty = dirty;
m_newCreated = false;
}
-
+
public boolean isDeleted() {
return m_deleted;
}
-
+
public boolean isNew() {
return m_newCreated;
}
-
+
public MetaObject getMetaObject() {
return this;
}
-
+
public Map getProperties() {
return m_map;
}
-
+
public Object getProperty(String name) {
return m_map.get(name);
}
-
+
+ public void setProperty(String name, Object value) {
+ Object old = m_map.put(name, value);
+
+ if (old == null || !old.equals(value)) {
+ m_dirty = true;
+ m_context.getTransactionManager().getTransaction().add(this);
+ }
+ }
+
public boolean isLoaded() {
return true;
}
-
+
public Class getPersistentClass() {
return m_clazz;
}
-
+
public Object getOID() {
return m_oid;
}
-
+
public boolean isDirty() {
return m_dirty;
}
-
+
public Persistent getObject() {
return m_object;
}
-
+
+
+
public MetaClass getMetaClass() {
- throw new UnsupportedOperationException("Not implemented");
- }
-
- public Object convert(Object object, Class cls) {
- try {
- if (cls.isPrimitive()) {
- if (object == null) {
- return DEFAULTS.get(cls);
- }
-
- if (cls.isAssignableFrom(object.getClass())) {
- return object;
- }
-
- if (object instanceof Number) {
- return m_typeConverter.convertPrimitives((Number) object, cls);
- }
- }
-
- if (object == null) {
- return null;
- }
-
- if (cls.isAssignableFrom(object.getClass())) {
- return object;
- }
-
- if (cls.isAssignableFrom(Number.class) && object instanceof Number) {
- return m_typeConverter.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 UnsupportedOperationException(cls.getName() + ":" + object);
- } catch (Throwable t) {
- // TODO
- t.printStackTrace();
- throw new RuntimeException(t.getClass().getName() + ":" + t.getMessage());
- }
+ return m_context.getMetaClass( m_clazz );
}
-
+
public Object handleEquals(Object obj) {
-
+
if (obj == null) {
return new Boolean(false);
}
-
+
if (!(obj instanceof Persistent)) {
return new Boolean(false);
}
-
+
Persistent object = (Persistent) obj;
-
+
if (m_oid == null) {
return new Boolean(m_oid == m_object.getOID());
} else {
return new Boolean(m_oid.equals(m_object.getOID()));
}
}
-
+
public Object invoke(Object obj, Method method, Object[] obj2) throws Throwable {
synchronized (this) {
if (GET_META_OBJECT.equals(method)) {
return this;
} else if (TO_STRING.equals(method)) {
return m_oid + "";
+
} else if (GET_OID.equals(method)) {
return m_oid;
+
} else if (HASH_CODE.equals(method)) {
if (m_oid == null) {
+
return new Integer(0);
}
return new Integer(m_oid.hashCode());
@@ -276,12 +233,13 @@
}
}
}
-
+
public Object handleProperty(Object obj, Method method, Object[] obj2) throws 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")) {
@@ -289,22 +247,32 @@
} else {
name = method.getName().substring(2);
}
- return convert(getProperty(name), method.getReturnType());
+ Object value = getProperty(name);
+ if ( value == null && method.getReturnType().isPrimitive() ){
+
+ return DEFAULTS.get(method.getReturnType());
+
+ }
+ if( value == null ){
+ return null;
+ }
+ return m_typeConverter.convert( value , method.getReturnType() );
}
throw new IllegalStateException("pure method " + method.getName());
}
-
+
public void remove() {
m_deleted = true;
}
-
+
public Object clone() throws CloneNotSupportedException {
+
PersistentProxy cln = (PersistentProxy) super.clone();
cln.m_dirty = false;
m_map = (Map) ((HashMap) m_map).clone();
return cln;
}
-
+
public void assign(MetaObject mo) {
m_map.clear();
m_map.putAll(mo.getProperties());
@@ -315,16 +283,16 @@
m_deleted = mo.isDeleted();
m_newCreated = mo.isNew();
}
-
+
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 Byte((byte) 0));
DEFAULTS.put(short.class, new Short((short) 0));
DEFAULTS.put(int.class, new Integer(0));
1.1 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/ContextImpl.java
Index: ContextImpl.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache Cocoon" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* 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
* 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
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.simplestore.persistence.impl;
import org.apache.commons.simplestore.persistence.*;
import org.apache.commons.simplestore.cache.Cache;
/**
* Meta data
*
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
*@version $Id: ContextImpl.java,v 1.1 2002/02/17 14:09:04 baliuka Exp $
*/
public class ContextImpl implements Context {
MetaClass metaClass;
Storage storage;
Cache cache;
TransactionManager transactionManager;
/** Creates new ContextImpl */
public ContextImpl( MetaClass metaClass, Storage storage, Cache cache,TransactionManager transactionManager ) {
metaClass.setContext( this );
storage.setContext( this );
this.metaClass = metaClass;
this.storage = storage;
this.cache = cache;
this.transactionManager = transactionManager;
}
public MetaClass getMetaClass(Class clasz) {
return metaClass.forClass(clasz);
}
public Storage getStorage() {
return storage;
}
public Cache getCache() {
return cache;
}
public TransactionManager getTransactionManager() {
return transactionManager;
}
}
1.5 +6 -2 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.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- TestSample.java 11 Feb 2002 20:33:28 -0000 1.4
+++ TestSample.java 17 Feb 2002 14:09:04 -0000 1.5
@@ -58,6 +58,9 @@
import org.apache.commons.simplestore.persistence.Transaction;
import org.apache.commons.simplestore.persistence.impl.DBStorage;
import org.apache.commons.simplestore.persistence.impl.PersistenceManager;
+import org.apache.commons.simplestore.persistence.impl.ContextImpl;
+import org.apache.commons.simplestore.persistence.impl.MetaClassImpl;
+import org.apache.commons.simplestore.cache.impl.SoftRefMemoryCache;
import java.util.HashMap;
import java.util.Map;
@@ -69,7 +72,7 @@
/**
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- *@version $Id: TestSample.java,v 1.4 2002/02/11 20:33:28 froehlich Exp $
+ *@version $Id: TestSample.java,v 1.5 2002/02/17 14:09:04 baliuka Exp $
*/
public class TestSample extends TestCase {
@@ -170,7 +173,8 @@
}
DBStorage storage = new DBStorage(ds);
- pm = PersistenceManager.getPersistenceManager(storage, storage);
+ ContextImpl context = new ContextImpl(new MetaClassImpl(),storage,SoftRefMemoryCache.getInstance(new java.util.HashMap(),0xFF),storage);
+ pm = PersistenceManager.getPersistenceManager(context);
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>