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/03/10 19:37:17 UTC
cvs commit: jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl NotNull.java MetaClassImpl.java PersistentProxy.java
baliuka 02/03/10 10:37:17
Modified: simplestore/src/java/org/apache/commons/simplestore/persistence
MetaClass.java
simplestore/src/java/org/apache/commons/simplestore/persistence/impl
MetaClassImpl.java PersistentProxy.java
Added: simplestore/src/java/org/apache/commons/simplestore/persistence
ValidationException.java Validator.java
simplestore/src/java/org/apache/commons/simplestore/persistence/impl
NotNull.java
Log:
Added validation
Revision Changes Path
1.6 +3 -1 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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- MetaClass.java 28 Feb 2002 18:28:41 -0000 1.5
+++ MetaClass.java 10 Mar 2002 18:37:17 -0000 1.6
@@ -61,7 +61,7 @@
*
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- *@version $Id: MetaClass.java,v 1.5 2002/02/28 18:28:41 baliuka Exp $
+ *@version $Id: MetaClass.java,v 1.6 2002/03/10 18:37:17 baliuka Exp $
*/
public interface MetaClass {
@@ -130,6 +130,8 @@
public Persistent newInstance(Object oid, boolean create,java.lang.ClassLoader cl);
public void setContext( Context context );
+
+ public Validator getValidator(int index);
}
1.1 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/ValidationException.java
Index: ValidationException.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;
/**
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
*@version $Id: ValidationException.java,v 1.1 2002/03/10 18:37:17 baliuka Exp $
*/
public class ValidationException extends java.lang.RuntimeException {
/** Holds value of property object. */
private Object object;
/** Holds value of property method. */
private java.lang.reflect.Method method;
/** Holds value of property value. */
private Object value;
/** Creates a new instance of ValidationException */
public ValidationException() {
}
/** Creates a new instance of ValidationException */
public ValidationException(String msg) {
super(msg);
}
/** Creates a new instance of ValidationException */
public ValidationException(Object object,java.lang.reflect.Method method,Object value,String message ) {
super(message);
this.object = object;
this.method = method;
this.value = value;
}
/** Getter for property object.
* @return Value of property object.
*/
public Object getObject() {
return this.object;
}
/** Getter for property method.
* @return Value of property method.
*/
public java.lang.reflect.Method getMethod() {
return this.method;
}
/** Getter for property value.
* @return Value of property value.
*/
public Object getValue() {
return this.value;
}
}
1.1 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/Validator.java
Index: Validator.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;
/**
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
*@version $Id: Validator.java,v 1.1 2002/03/10 18:37:17 baliuka Exp $
*/
public interface Validator {
public boolean isValid(Object value);
}
1.7 +17 -5 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.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- MetaClassImpl.java 10 Mar 2002 12:32:30 -0000 1.6
+++ MetaClassImpl.java 10 Mar 2002 18:37:17 -0000 1.7
@@ -65,7 +65,7 @@
*
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
- *@version $Id: MetaClassImpl.java,v 1.6 2002/03/10 12:32:30 baliuka Exp $
+ *@version $Id: MetaClassImpl.java,v 1.7 2002/03/10 18:37:17 baliuka Exp $
*/
public class MetaClassImpl implements MetaClass, org.apache.commons.simplestore.tools.Constants {
@@ -118,6 +118,8 @@
/** Holds value of property referencedClass. */
private Class refClass;
+ private Validator validator;
+
/** Getter for property id.
* @return Value of property id.
*/
@@ -182,14 +184,15 @@
/** Getter for property validator.
* @return Value of property validator.
*/
- public Object getValidator() {
- return null;
+ public Validator getValidator() {
+ return validator;
}
/** Setter for property validator.
* @param validator New value of property validator.
*/
- public void setValidator(Object validator) {
+ public void setValidator( Validator validator ) {
+ this.validator = validator;
}
}
@@ -488,6 +491,7 @@
MetaClassImpl currentClass ;
Field currentField;
+ Validator currentValidator;
StorageHandlerImpl( ){
@@ -523,7 +527,12 @@
}
public void start_validator(final Attributes meta) throws SAXException {
-
+ try{
+ currentValidator = (Validator)Class.forName(meta.getValue("id")).newInstance();
+ currentField.setValidator(currentValidator);
+ }catch(Exception e){
+ rethrow(e);
+ }
}
public void end_validator() throws SAXException {
@@ -593,5 +602,8 @@
), new StorageHandlerImpl());
}
+ public Validator getValidator(int index) {
+ return getField(descriptors[ index ].getName()).getValidator();
+ }
}
1.12 +44 -25 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.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- PersistentProxy.java 10 Mar 2002 12:32:30 -0000 1.11
+++ PersistentProxy.java 10 Mar 2002 18:37:17 -0000 1.12
@@ -58,6 +58,8 @@
import org.apache.commons.simplestore.persistence.MetaObject;
import org.apache.commons.simplestore.persistence.Persistent;
import org.apache.commons.simplestore.persistence.Context;
+import org.apache.commons.simplestore.persistence.Validator;
+import org.apache.commons.simplestore.persistence.ValidationException;
import org.apache.commons.simplestore.persistence.TypeConverter;
import org.apache.commons.simplestore.tools.*;
@@ -75,7 +77,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.11 2002/03/10 12:32:30 baliuka Exp $
+ *@version $Id: PersistentProxy.java,v 1.12 2002/03/10 18:37:17 baliuka Exp $
*/
public class PersistentProxy
implements MetaObject, org.apache.commons.simplestore.tools.Constants ,
@@ -88,27 +90,38 @@
private static Method TO_STRING;
private static Method GET_OID;
private static Method GET_META_OBJECT;
+ static final private int DIRTY = 8;
+ static final private int DELETED = 4;
+ static final private int NEW_CREATED = 2;
+ static final private int CLEAN = 1;
private Object m_props[] ;
private Object m_oid = null;
- private boolean m_dirty = false;
- private boolean m_deleted = false;
- private boolean m_newCreated = false;
- private boolean m_clean = false;
+ // private boolean m_dirty = false;
+ // private boolean m_deleted = false;
+ // private boolean m_newCreated = false;
+ // private boolean m_clean = false;
+ private int state = 0;
private TypeConverter m_typeConverter = new DefaultTypeConverter();
private Persistent m_object;
private Class m_clazz;
private MetaClass m_metaClass;
private Context m_context;
-
+ private void setState(boolean flag,int state){
+ this.state = flag ? this.state | state : this.state & ~state;
+ }
+ private boolean getState( int state ){
+ return ( this.state & state ) != 0;
+ }
public PersistentProxy(Class clazz,
Object oid,
boolean newCreated,
Context context) {
m_oid = oid;
- m_newCreated = newCreated;
+ setState( newCreated, NEW_CREATED );
+
m_context = context;
m_clazz = clazz;
m_metaClass = context.getMetaClass(clazz);
@@ -138,9 +151,9 @@
handler.m_object = p;
if ( newCreated ) {
- handler.m_clean = true;
+ handler.setState(true,CLEAN);
context.getTransactionManager().getTransaction().add( handler );
- handler.m_clean = false;
+ handler.setState(false,CLEAN);
}
return p;
@@ -152,16 +165,16 @@
public void setDirty(boolean dirty) {
- m_dirty = dirty;
+ setState(dirty,DIRTY);
}
public boolean isDeleted() {
- return m_deleted;
+ return getState( DELETED );
}
public boolean isNew() {
- return m_newCreated;
+ return getState( NEW_CREATED );
}
public MetaObject getMetaObject() {
@@ -198,7 +211,7 @@
}
public boolean isDirty() {
- return m_dirty;
+ return getState(DIRTY);
}
public Persistent getObject() {
@@ -290,27 +303,35 @@
public String toString(){
return
"OID=" + m_oid +
- ";dirty=" + m_dirty +
- ";newCreated=" + m_newCreated +
- ";deleted=" + m_deleted +
- ";clean=" + m_clean;
+ ";dirty=" + isDirty() +
+ ";newCreated=" + isNew() +
+ ";deleted=" + isDeleted() +
+ ";clean=" + isClean();
}
public Object handleProperty(Object obj, Method method, Object[] obj2) throws Throwable {
- if( m_clean || m_deleted )
+ if( isClean() || isDeleted() )
throw new IllegalStateException("Object properties invalidated");
int index = m_metaClass.getPropertyIndex( method );
if ( method.getReturnType() == Void.TYPE && obj2.length == 1 ) {
+
+ Validator validator = m_metaClass.getValidator(index);
+
+ if( validator != null && !validator.isValid(obj2[0]) ){
+ throw new ValidationException(obj,method,obj2[0],validator.toString());
+ }
+
+
if( obj2[0] != null && m_metaClass.isReference(method) ){
setProperty(index, ((Persistent)obj2[0]).getOID());
}else{
setProperty(index, obj2[0]);
}
- m_dirty = true;
+ setState(true,DIRTY);
return null;
} else if( obj2.length == 0 ){
Object value = getProperty(index);
@@ -333,7 +354,7 @@
public void remove() {
m_context.getTransactionManager().getTransaction().add(this);
- m_deleted = true;
+ setState( true, DELETED );
}
public Object clone() throws CloneNotSupportedException {
@@ -348,14 +369,12 @@
m_oid = mo.getOID();
m_object = mo.getObject();
m_clazz = mo.getPersistentClass();
- m_dirty = mo.isDirty();
- m_deleted = mo.isDeleted();
- m_newCreated = mo.isNew();
- m_clean = mo.isClean();
+ state = ((PersistentProxy)mo).state;
+
}
public boolean isClean() {
- return m_clean;
+ return getState(CLEAN);
}
public void handle( java.lang.reflect.Method m, java.lang.Throwable e ){
System.out.println("Error in " + m.getName());
1.1 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/persistence/impl/NotNull.java
Index: NotNull.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.Validator;
/**
*@author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
* baliuka@mwm.lt</a>
*@version $Id: NotNull.java,v 1.1 2002/03/10 18:37:17 baliuka Exp $
*/
public class NotNull implements Validator {
/** Creates a new instance of NotNull */
public NotNull() {
}
public boolean isValid(Object value) {
return value != null;
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>