You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by fr...@apache.org on 2002/02/17 12:22:24 UTC
cvs commit: jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl DefaultTypeConverter.java PersistentProxy.java
froehlich 02/02/17 03:22:23
Modified: simplestore/src/java/org/apache/commons/simplestore/persistence/impl
PersistentProxy.java
Added: simplestore/src/java/org/apache/commons/simplestore/persistence/impl
DefaultTypeConverter.java
Log:
added TypeConverter interface + default implementation
Revision Changes Path
1.4 +151 -215 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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- PersistentProxy.java 16 Feb 2002 14:26:05 -0000 1.3
+++ PersistentProxy.java 17 Feb 2002 11:22:23 -0000 1.4
@@ -53,13 +53,14 @@
* <http://www.apache.org/>.
*/
package org.apache.commons.simplestore.persistence.impl;
-import java.io.Serializable;
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.TypeConverter;
+import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
@@ -70,147 +71,164 @@
/**
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- *@version $Id: PersistentProxy.java,v 1.3 2002/02/16 14:26:05 baliuka Exp $
+ *@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 $
*/
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 static Map defaults = new Hashtable();
-
- Map map = new HashMap();
-
- Object oid = null;
- Persistent object;
- Class pClass;
- TransactionManager transactionManager;
- boolean dirty = false;
- boolean deleted = false;
- boolean newCreated;
-
-
+
+ private Map m_map = new HashMap();
+ private Object m_oid = null;
+ private boolean m_dirty = false;
+ private boolean m_deleted = false;
+ private TypeConverter m_typeConverter = new DefaultTypeConverter();
+ private Persistent m_object;
+ private Class m_clazz;
+ private TransactionManager m_transactionManager;
+ private boolean m_newCreated;
+
/**
* Creates new ValueProxy
*
- *@param pClass
*@param oid
*@param newCreated
*@param transactionManager
+ *@param clazz
*/
- public PersistentProxy(Class pClass, Object oid, boolean newCreated, TransactionManager transactionManager) {
-
- this.oid = oid;
- this.newCreated = newCreated;
- this.transactionManager = transactionManager;
- this.pClass = pClass;
-
+ public PersistentProxy(Class clazz,
+ Object oid,
+ boolean newCreated,
+ TransactionManager transactionManager) {
+ m_oid = oid;
+ m_newCreated = newCreated;
+ m_transactionManager = transactionManager;
+ 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,
+ TransactionManager transactionManager) {
+
+ PersistentProxy handler = new PersistentProxy(persistent,
+ oid,
+ newCreated,
+ transactionManager);
Persistent p = (Persistent) Proxy.newProxyInstance(
- Thread.currentThread().getContextClassLoader(), new Class[]{persistent, Persistent.class}, handler);
- handler.object = p;
+ Thread.currentThread().getContextClassLoader(),
+ new Class[]{persistent, Persistent.class}, handler);
+
+ handler.m_object = p;
if (newCreated) {
-
MetaObject mo = p.getMetaObject();
transactionManager.getTransaction().add(mo);
}
return p;
}
- // 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)) {
- return new Short(number.shortValue());
- } else if (cls.equals(Integer.class)) {
- return new Integer(number.intValue());
- } else if (cls.equals(Long.class)) {
- return new Long(number.longValue());
- } else if (cls.equals(Float.class)) {
- return new Float(number.floatValue());
- } else if (cls.equals(Double.class)) {
- return new Double(number.doubleValue());
- } else if (cls.equals(Boolean.class)) {
- return new Boolean(number.intValue() != 0);
- } else if (cls.equals(Character.TYPE)) {
- return new Character(number.toString().charAt(0));
- } else {
- throw new UnsupportedOperationException("Number class = " + number.getClass().getName() + " Target Class " + cls.getName());
+
+ 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);
}
-
}
- // 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)) {
- return new Short(number.shortValue());
- } else if (cls.equals(Integer.TYPE)) {
- return new Integer(number.intValue());
- } else if (cls.equals(Long.TYPE)) {
- return new Long(number.longValue());
- } else if (cls.equals(Float.TYPE)) {
- return new Float(number.floatValue());
- } else if (cls.equals(Double.TYPE)) {
- return new Double(number.doubleValue());
- } else if (cls.equals(Boolean.TYPE)) {
- return new Boolean(number.intValue() != 0);
- } else if (cls.equals(Character.TYPE)) {
- return new Character(number.toString().charAt(0));
- } else {
- throw new UnsupportedOperationException("Number class = " + number.getClass().getName() + " Target Class " + cls.getName());
- }
-
+
+ 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;
}
-
- static Object convert(Object object, Class cls) {
+
+ public Object getProperty(String name) {
+ return m_map.get(name);
+ }
+
+ 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);
+ return DEFAULTS.get(cls);
}
-
+
if (cls.isAssignableFrom(object.getClass())) {
return object;
}
-
+
if (object instanceof Number) {
- return convertPrimityve((Number) object, cls);
+ 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 convertNumber((Number) object, cls);
+ 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
@@ -218,185 +236,103 @@
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 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 MetaClass getMetaClass() {
- throw new UnsupportedOperationException("Not implemented");
- }
-
+
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 (oid == null) {
-
- return new Boolean(oid == object.getOID());
+
+ if (m_oid == null) {
+ return new Boolean(m_oid == m_object.getOID());
} else {
-
- return new Boolean(oid.equals(object.getOID()));
+ 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 oid + "";
+ return m_oid + "";
} else if (GET_OID.equals(method)) {
-
- return oid;
+ return m_oid;
} else if (HASH_CODE.equals(method)) {
-
- if (oid == null) {
+ if (m_oid == null) {
return new Integer(0);
}
- return new Integer(oid.hashCode());
+ return new Integer(m_oid.hashCode());
} else if (EQUALS.equals(method)) {
-
return handleEquals(obj2[0]);
} else {
-
return handleProperty(obj, method, obj2);
}
-
}
}
-
-
+
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")) {
name = method.getName().substring(3);
} else {
name = method.getName().substring(2);
}
-
- return convert( getProperty(name) ,method.getReturnType() );
-
+ return convert(getProperty(name), method.getReturnType());
}
-
-
-
-
throw new IllegalStateException("pure method " + method.getName());
}
-
+
public void remove() {
- deleted = true;
+ m_deleted = true;
}
-
+
public Object clone() throws CloneNotSupportedException {
PersistentProxy cln = (PersistentProxy) super.clone();
- cln.dirty = false;
- map = (Map) ((HashMap) map).clone();
+ cln.m_dirty = false;
+ m_map = (Map) ((HashMap) m_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();
+ m_map.clear();
+ m_map.putAll(mo.getProperties());
+ m_oid = mo.getOID();
+ m_object = mo.getObject();
+ m_clazz = mo.getPersistentClass();
+ m_dirty = mo.isDirty();
+ 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));
- defaults.put(long.class, new Long(0));
- defaults.put(float.class, new Float(0));
- defaults.put(double.class, new Double(0));
- defaults.put(char.class, new Character('\u0000'));
- defaults.put(boolean.class, new Boolean(false));
-
+
+ DEFAULTS.put(byte.class, new Byte((byte) 0));
+ DEFAULTS.put(short.class, new Short((short) 0));
+ DEFAULTS.put(int.class, new Integer(0));
+ DEFAULTS.put(long.class, new Long(0));
+ DEFAULTS.put(float.class, new Float(0));
+ DEFAULTS.put(double.class, new Double(0));
+ DEFAULTS.put(char.class, new Character('\u0000'));
+ DEFAULTS.put(boolean.class, new Boolean(false));
} catch (Exception e) {
e.printStackTrace();
throw new Error(e.getMessage());
1.1 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/DefaultTypeConverter.java
Index: DefaultTypeConverter.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.TypeConverter;
/**
* Default implementation of the type converter
*
*@author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">
* baliuka@mwm.lt</a>
*@version $Id: DefaultTypeConverter.java,v 1.1 2002/02/17 11:22:23 froehlich Exp $
*/
public class DefaultTypeConverter implements TypeConverter {
public Object convertNumber(Number number, Class clazz) {
if (clazz.equals(Byte.class)) {
return new Byte(number.byteValue());
} else if (clazz.equals(Short.class)) {
return new Short(number.shortValue());
} else if (clazz.equals(Integer.class)) {
return new Integer(number.intValue());
} else if (clazz.equals(Long.class)) {
return new Long(number.longValue());
} else if (clazz.equals(Float.class)) {
return new Float(number.floatValue());
} else if (clazz.equals(Double.class)) {
return new Double(number.doubleValue());
} else if (clazz.equals(Boolean.class)) {
return new Boolean(number.intValue() != 0);
} else if (clazz.equals(Character.TYPE)) {
return new Character(number.toString().charAt(0));
} else {
throw new UnsupportedOperationException("Number class = "
+ number.getClass().getName()
+ " Target Class "
+ clazz.getName());
}
}
public Object convertPrimitives(Number number, Class clazz) {
if (clazz.equals(Byte.TYPE)) {
return new Byte(number.byteValue());
} else if (clazz.equals(Short.TYPE)) {
return new Short(number.shortValue());
} else if (clazz.equals(Integer.TYPE)) {
return new Integer(number.intValue());
} else if (clazz.equals(Long.TYPE)) {
return new Long(number.longValue());
} else if (clazz.equals(Float.TYPE)) {
return new Float(number.floatValue());
} else if (clazz.equals(Double.TYPE)) {
return new Double(number.doubleValue());
} else if (clazz.equals(Boolean.TYPE)) {
return new Boolean(number.intValue() != 0);
} else if (clazz.equals(Character.TYPE)) {
return new Character(number.toString().charAt(0));
} else {
throw new UnsupportedOperationException("Number class = "
+ number.getClass().getName()
+ " Target Class "
+ clazz.getName());
}
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>