You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2008/09/16 16:22:59 UTC

svn commit: r695896 - in /labs/magma/trunk/foundation-beans: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/magma/ src/main/java/org/apache/magma/beans/ src/test/ src/test/java/ src/test/java/org/...

Author: simoneg
Date: Tue Sep 16 07:22:58 2008
New Revision: 695896

URL: http://svn.apache.org/viewvc?rev=695896&view=rev
Log:
Initial import.

Added:
    labs/magma/trunk/foundation-beans/pom.xml
    labs/magma/trunk/foundation-beans/src/
    labs/magma/trunk/foundation-beans/src/main/
    labs/magma/trunk/foundation-beans/src/main/java/
    labs/magma/trunk/foundation-beans/src/main/java/org/
    labs/magma/trunk/foundation-beans/src/main/java/org/apache/
    labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/
    labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/
    labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeanData.java
    labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeanHandler.java
    labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeansAspect.aj
    labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeansHandlerAspect.aj
    labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/MagmaBean.java
    labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/MagmaBeanSupport.java
    labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/PropertyInfo.java
    labs/magma/trunk/foundation-beans/src/test/
    labs/magma/trunk/foundation-beans/src/test/java/
    labs/magma/trunk/foundation-beans/src/test/java/org/
    labs/magma/trunk/foundation-beans/src/test/java/org/apache/
    labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/
    labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/
    labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanDataTest.java
    labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanHandlerTest.java
    labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/TestBean.java

Added: labs/magma/trunk/foundation-beans/pom.xml
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/pom.xml?rev=695896&view=auto
==============================================================================
--- labs/magma/trunk/foundation-beans/pom.xml (added)
+++ labs/magma/trunk/foundation-beans/pom.xml Tue Sep 16 07:22:58 2008
@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- 
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+<project>
+	<modelVersion>4.0.0</modelVersion>
+	<parent>
+	  <groupId>org.apache.magma</groupId>
+	  <artifactId>magma-parent</artifactId>
+	  <version>1</version>
+	</parent>
+	<groupId>org.apache.magma</groupId>
+	<artifactId>foundation-beans</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	<packaging>magma</packaging>
+	<name>Magma Foundation Beans support</name>
+	<description>Basic, aspectable, beans functionalities</description>
+	<dependencies>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.4</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>commons-beanutils</groupId>
+			<artifactId>commons-beanutils</artifactId>
+			<version>1.7.0</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.magma</groupId>
+			<artifactId>foundation-basics</artifactId>
+			<version>0.0.1-SNAPSHOT</version>
+		</dependency>
+		<dependency>
+			<groupId>commons-collections</groupId>
+			<artifactId>commons-collections</artifactId>
+			<version>3.2</version>
+		</dependency>
+	</dependencies>
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.magma.tools</groupId>
+				<artifactId>maven-magma-plugin</artifactId>
+				<extensions>true</extensions>
+			</plugin>
+		</plugins>
+	</build>
+
+</project>

Added: labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeanData.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeanData.java?rev=695896&view=auto
==============================================================================
--- labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeanData.java (added)
+++ labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeanData.java Tue Sep 16 07:22:58 2008
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.beans;
+
+import org.apache.magma.basics.MagmaException;
+
+import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.beanutils.PropertyUtils;
+
+public class BeanData {
+	
+	private static Map<Class,BeanData> data = new HashMap<Class, BeanData>();
+	
+	public static BeanData getFor(Class clazz) {
+		BeanData ret = data.get(clazz);
+		if (ret == null) {
+			ret = new BeanData(clazz);
+			data.put(clazz, ret);
+		}
+		return ret;
+	}
+	
+	private Class beanClass = null;
+	
+	private Map<String, PropertyInfo> properties = new HashMap<String, PropertyInfo>();
+
+	private BeanData(Class clazz) {
+		this.beanClass = clazz;
+		BeanInfo beanInfo = null;
+		try {
+			beanInfo = Introspector.getBeanInfo(clazz);
+		} catch (IntrospectionException e) {
+			throw new IllegalStateException("Error analyzing bean " + clazz, e);
+		}
+		PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
+		for (PropertyDescriptor desc : descriptors) {
+			PropertyInfo info = new PropertyInfo();
+			info.init(desc, clazz);
+			properties.put(info.getName(), info);
+		}
+	}
+	
+	public Set<String> getPropertyNames() {
+		return Collections.unmodifiableSet(properties.keySet());
+	}
+	
+	public PropertyInfo getProperty(String name) {
+		return properties.get(name);
+	}
+	
+	public Class getBeanClass() {
+		return this.beanClass;
+	}
+
+	public PropertyInfo findProperty(Method method) {
+		String name = method.getName();
+		if (name.startsWith("set") || name.startsWith("get")) {
+			name = name.substring(3);
+		} else if (name.startsWith("is")) {
+			name = name.substring(2);
+		} else {
+			throw new MagmaException("The given method {0} is not a property setter or getter", method);
+		}
+		name = Introspector.decapitalize(name);
+		return this.getProperty(name);
+	}
+
+	
+}

Added: labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeanHandler.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeanHandler.java?rev=695896&view=auto
==============================================================================
--- labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeanHandler.java (added)
+++ labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeanHandler.java Tue Sep 16 07:22:58 2008
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.beans;
+
+import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.basics.MagmaException;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.beanutils.PropertyUtils;
+
+public class BeanHandler {
+
+	private MagmaBeanSupport bean;
+	private BeanData data;
+
+	private Map<String, Object> values = new HashMap<String, Object>();
+	private Map<String, Boolean> changed = new HashMap<String, Boolean>();
+
+	
+	public BeanHandler(MagmaBeanSupport bean) {
+		this.bean = bean;
+		this.data = bean.beanData();
+		rollback();
+	}
+	
+	public void setValue(String field, Object value) {
+		PropertyInfo property = data.getProperty(field);
+		if (property == null) throw new MagmaException("Cannot find a proprty named {0} in class {1}", field, bean.getClass().getName());
+		if (!property.isWriteable()) throw new MagmaException("Property {0} is read only in class {1}", field, bean.getClass().getName());
+		if (value != null) {
+			if (!property.getType().isPrimitive() && !property.getType().isAssignableFrom(value.getClass())) {
+				throw new MagmaException("The type {3} is invalid for property {1}.{0} which is of type {2}", field, bean.getClass().getName(), property.getType().getName(), value.getClass().getName());				
+			}
+		}
+		values.put(field, value);
+		changed.put(field, true);
+	}
+
+	public Object getValue(String field) {
+		PropertyInfo property = data.getProperty(field);
+		if (property == null) throw new MagmaException("Cannot find a proprty named {0} in class {1}", field, bean.getClass().getName());
+		if (!property.isReadable()) throw new MagmaException("Property {0} is not readable in class {1}", field, bean.getClass().getName());
+		return values.get(field);
+	}
+	
+	public void rollback() {
+		this.values.clear();
+		this.changed.clear();
+		Set<String> names = data.getPropertyNames();
+		for (String name : names) {
+			PropertyInfo property = data.getProperty(name);
+			if (property.isReadable()) {
+				Object val = null;
+				try {
+					val = PropertyUtils.getProperty(bean, name);
+				} catch (Exception e) {
+					throw new MagmaException(e, "Error while fecthing property {0} from {1}", name, bean.getClass().getName());
+				}
+				values.put(name, val);
+			}
+		}
+	}
+	
+	public void commit() {
+		Set<String> names = data.getPropertyNames();
+		for (String name : names) {
+			PropertyInfo property = data.getProperty(name);
+			if (property.isWriteable()) {
+				try {
+					PropertyUtils.setProperty(bean, name, values.get(name));
+					this.changed.remove(name);
+				} catch (Exception e) {
+					throw new MagmaException(e, "Error while setting property {0} on {1}", name, bean.getClass().getName());
+				}				
+			}
+		}
+	}
+
+	public void updated(PropertyInfo property) {
+		if (property.isReadable()) {
+			String name = property.getName();
+			Object val = null;
+			try {
+				val = PropertyUtils.getProperty(bean, name);
+			} catch (Exception e) {
+				throw new MagmaException(e, "Error while fecthing property {0} from {1}", name, bean.getClass().getName());
+			}
+			values.put(name, val);
+			changed.remove(name);
+		}
+	}
+	
+}

Added: labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeansAspect.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeansAspect.aj?rev=695896&view=auto
==============================================================================
--- labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeansAspect.aj (added)
+++ labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeansAspect.aj Tue Sep 16 07:22:58 2008
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.beans;
+
+
+public aspect BeansAspect {
+	
+	declare parents: (*..domain..*) implements MagmaBeanSupport;
+	declare parents: ((@MagmaBean *)) implements MagmaBeanSupport;
+	
+	public BeanData MagmaBeanSupport.beanData() {
+		return BeanData.getFor(this.getClass());
+	}
+
+	public BeanHandler MagmaBeanSupport.handler() {
+		return null;
+	}	
+}

Added: labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeansHandlerAspect.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeansHandlerAspect.aj?rev=695896&view=auto
==============================================================================
--- labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeansHandlerAspect.aj (added)
+++ labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/BeansHandlerAspect.aj Tue Sep 16 07:22:58 2008
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.beans;
+
+import java.lang.ref.SoftReference;
+import java.lang.reflect.Method;
+
+import org.aspectj.lang.reflect.MethodSignature;
+
+public aspect BeansHandlerAspect perthis(calledHandler(MagmaBeanSupport)) {
+
+	private ThreadLocal<SoftReference<BeanHandler>> tl = new ThreadLocal<SoftReference<BeanHandler>>();
+	
+	pointcut calledHandler(MagmaBeanSupport bean) : execution(public BeanHandler MagmaBeanSupport+.handler()) && this(bean);
+	
+	Object around(MagmaBeanSupport bean) : calledHandler(bean) {
+		SoftReference<BeanHandler> reference = tl.get();
+		if (reference != null) {
+			BeanHandler handler = reference.get();
+			if (handler != null)
+				return handler;
+		}
+		BeanHandler handler = new BeanHandler(bean);
+		reference = new SoftReference<BeanHandler>(handler);
+		tl.set(reference);
+		return handler;
+	}
+	
+	pointcut calledSetter(MagmaBeanSupport bean) : execution(public void MagmaBeanSupport+.set*(..)) && !cflow(execution(* BeanHandler+.commit())) && this(bean);
+	
+	after(MagmaBeanSupport bean) returning : calledSetter(bean) {
+		Method method = ((MethodSignature)thisJoinPointStaticPart.getSignature()).getMethod();
+		BeanData data = bean.beanData();
+		PropertyInfo prop = data.findProperty(method);
+		if (prop == null) return;
+		BeanHandler handler = bean.handler();
+		handler.updated(prop);
+	}
+	
+}

Added: labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/MagmaBean.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/MagmaBean.java?rev=695896&view=auto
==============================================================================
--- labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/MagmaBean.java (added)
+++ labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/MagmaBean.java Tue Sep 16 07:22:58 2008
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.beans;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Inherited
+public @interface MagmaBean {
+
+}

Added: labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/MagmaBeanSupport.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/MagmaBeanSupport.java?rev=695896&view=auto
==============================================================================
--- labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/MagmaBeanSupport.java (added)
+++ labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/MagmaBeanSupport.java Tue Sep 16 07:22:58 2008
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.beans;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public interface MagmaBeanSupport {
+	
+	public BeanData beanData();
+	public BeanHandler handler();
+}

Added: labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/PropertyInfo.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/PropertyInfo.java?rev=695896&view=auto
==============================================================================
--- labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/PropertyInfo.java (added)
+++ labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/beans/PropertyInfo.java Tue Sep 16 07:22:58 2008
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.beans;
+
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.Collection;
+
+public class PropertyInfo implements Cloneable {
+
+	private String name;
+	private Class type;
+	private Class beanClass;
+	private boolean readable;
+	private boolean writeable;
+	
+	private boolean isCollection;
+	private Class collectionClass;
+	
+	
+	public boolean isReadable() {
+		return readable;
+	}
+	public void setReadable(boolean readable) {
+		this.readable = readable;
+	}
+	public boolean isWriteable() {
+		return writeable;
+	}
+	public void setWriteable(boolean writeable) {
+		this.writeable = writeable;
+	}
+	public String getName() {
+		return name;
+	}
+	public Class getType() {
+		return type;
+	}
+	
+	public void init(PropertyDescriptor descriptor, Class beanClass) {
+		this.beanClass = beanClass;
+		this.name = descriptor.getName();
+		this.type = descriptor.getPropertyType();
+		this.isCollection = Collection.class.isAssignableFrom(this.type);
+		if (this.isCollection && descriptor.getReadMethod() != null) {
+			Type retType = descriptor.getReadMethod().getGenericReturnType();
+			if (retType instanceof ParameterizedType) {
+				ParameterizedType parRet = (ParameterizedType) retType;
+				Type[] types = parRet.getActualTypeArguments();
+				if (types.length > 0) {
+					if (types[0] instanceof Class) {
+						this.collectionClass = (Class) types[0];
+					}
+				}
+			}
+		}
+		this.readable = descriptor.getReadMethod() != null;
+		this.writeable = descriptor.getWriteMethod() != null;
+	}
+	
+    public PropertyInfo clone() {
+    	try {
+			return (PropertyInfo) super.clone();
+		} catch (CloneNotSupportedException e) {
+			return null;
+		}
+    }
+	public boolean isCollection() {
+		return isCollection;
+	}
+	public void setCollection(boolean isCollection) {
+		this.isCollection = isCollection;
+	}
+	public Class getCollectionClass() {
+		return collectionClass;
+	}
+	public void setCollectionClass(Class collectionClass) {
+		this.collectionClass = collectionClass;
+	}
+	public Class getBeanClass() {
+		return beanClass;
+	}
+	public void setBeanClass(Class beanClass) {
+		this.beanClass = beanClass;
+	}
+    
+	
+}

Added: labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanDataTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanDataTest.java?rev=695896&view=auto
==============================================================================
--- labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanDataTest.java (added)
+++ labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanDataTest.java Tue Sep 16 07:22:58 2008
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.beans;
+
+import static org.junit.Assert.*;
+
+import java.util.Set;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class BeanDataTest {
+
+	private BeanData bd;
+
+	@Before
+	public void setUp() throws Exception {
+		TestBean tb = new TestBean();
+		bd = tb.beanData();
+	}
+
+	@Test
+	public void testBeanData() {
+		BeanData data = BeanData.getFor(TestBean.class);
+		assertEquals(data.getPropertyNames(), bd.getPropertyNames());
+	}
+
+	@Test
+	public void testGetPropertyNames() {
+		Set<String> propertyNames = bd.getPropertyNames();
+		
+		assertTrue(propertyNames.contains("privString"));
+		assertTrue(propertyNames.contains("class"));
+		assertEquals(5, propertyNames.size());
+	}
+
+	@Test
+	public void testGetProperty() {
+		PropertyInfo property = bd.getProperty("privString");
+		assertTrue(property.isReadable());
+		assertTrue(property.isWriteable());
+		assertEquals("privString", property.getName());
+		assertEquals(String.class, property.getType());
+	}
+
+	@Test
+	public void testGetBeanClass() {
+		assertEquals(TestBean.class, bd.getBeanClass());
+	}
+	
+	@Test
+	public void collections() throws Exception {
+		PropertyInfo property = bd.getProperty("AList");
+		assertTrue(property.isCollection());
+		assertNotNull(property.getCollectionClass());
+		assertEquals(String.class, property.getCollectionClass());
+	}
+
+}

Added: labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanHandlerTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanHandlerTest.java?rev=695896&view=auto
==============================================================================
--- labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanHandlerTest.java (added)
+++ labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/BeanHandlerTest.java Tue Sep 16 07:22:58 2008
@@ -0,0 +1,186 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.beans;
+
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.magma.basics.MagmaException;
+
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class BeanHandlerTest {
+
+	@Test
+	public void testRetrieve() throws Exception {
+		final TestBean b = new TestBean();
+		BeanHandler h = b.handler();
+		assertNotNull(h);
+		BeanHandler h2 = b.handler();
+		assertNotNull(h2);
+		assertSame(h, h2);
+		
+		// Different threads receive different handlers
+		final List<BeanHandler> h3 = new ArrayList<BeanHandler>();
+		Thread t = new Thread() {
+			@Override
+			public void run() {
+				h3.add(b.handler());
+			}
+		};
+		t.start();
+		while (h3.size() == 0) {
+			Thread.sleep(100);
+		}
+		assertNotSame(h, h3.get(0));
+		
+		// Now check that the BeanHandler does not remains stale
+		int code = System.identityHashCode(h);
+		h2 = null;
+		h = null;
+		// use all ram
+		boolean allocate = true;
+		byte[][] buff = new byte[10000][];
+		int i = 0;
+		while (allocate && i < 10000) {
+			try {
+				buff[i] = new byte[65535];
+			} catch (OutOfMemoryError e) {
+				buff = null;
+				System.gc();
+				Thread.sleep(500);
+				allocate = false;
+			}
+			i++;
+		}
+		if (allocate) return;
+		System.gc();
+		Thread.sleep(500);
+		h2 = b.handler();
+		assertTrue(code != System.identityHashCode(h2));
+	}
+	
+	@Test
+	public void testGetters() throws Exception {
+		TestBean b = new TestBean();
+		b.setPrivString("test");
+		BeanHandler h = b.handler();
+		Object val = h.getValue("privString");
+		assertNotNull(val);
+		assertTrue(val instanceof String);
+		assertEquals((String)val, "test");
+		h = b.handler();
+		val = h.getValue("privString");
+		assertNotNull(val);
+		assertTrue(val instanceof String);
+		assertEquals((String)val, "test");		
+	}
+	
+	@Test
+	public void testSetters() throws Exception {
+		TestBean b = new TestBean();
+		BeanHandler h = b.handler();
+		h.setValue("privString", "test");
+		Object val = h.getValue("privString");
+		assertNotNull(val);
+		assertTrue(val instanceof String);
+		assertEquals((String)val, "test");
+		
+		assertNull(b.getPrivString());
+		h.commit();
+		val = b.getPrivString();
+		assertNotNull(val);
+		assertEquals((String)val, "test");
+	}
+	
+	@Test
+	public void rollback() throws Exception {
+		TestBean b = new TestBean();
+		b.setPrivString("test");
+		BeanHandler h = b.handler();
+		h.setValue("privString", "wrong");
+		Object val = h.getValue("privString");
+		assertNotNull(val);
+		assertTrue(val instanceof String);
+		assertEquals((String)val, "wrong");
+		assertEquals("test", b.getPrivString());
+		
+		h.rollback();
+		assertEquals("test", b.getPrivString());
+		val = h.getValue("privString");
+		assertNotNull(val);
+		assertTrue(val instanceof String);
+		assertEquals("test", (String)val);		
+	}
+	
+	@Test(expected=MagmaException.class)
+	public void unexistentGet() {
+		TestBean b = new TestBean();
+		BeanHandler h = b.handler();
+		h.getValue("doesnotexist");
+	}
+	
+	@Test(expected=MagmaException.class)
+	public void unexistentSet() {
+		TestBean b = new TestBean();
+		BeanHandler h = b.handler();
+		h.setValue("doesnotexist", "test");
+	}
+	
+	@Test(expected=MagmaException.class)
+	public void nonReadableGet() {
+		TestBean b = new TestBean();
+		BeanHandler h = b.handler();
+		h.getValue("writeOnly");
+		h.setValue("doesnotexist", "test");
+	}
+	
+	@Test(expected=MagmaException.class)
+	public void nonWriteableSet() {
+		TestBean b = new TestBean();
+		BeanHandler h = b.handler();
+		h.setValue("readOnly", "test");
+	}
+	
+	@Test(expected=MagmaException.class)
+	public void wrongTypeSet() {
+		TestBean b = new TestBean();
+		BeanHandler h = b.handler();
+		h.setValue("privString", 2);
+	}
+	
+	@Test
+	public void selfUpdate() throws Exception {
+		TestBean b = new TestBean();
+		b.setPrivString("test");
+		BeanHandler h = b.handler();
+		Object val = h.getValue("privString");
+		assertNotNull(val);
+		assertTrue(val instanceof String);
+		assertEquals((String)val, "test");
+
+		b.setPrivString("other");
+		val = h.getValue("privString");
+		assertNotNull(val);
+		assertTrue(val instanceof String);
+		assertEquals((String)val, "other");		
+	}
+	
+}

Added: labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/TestBean.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/TestBean.java?rev=695896&view=auto
==============================================================================
--- labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/TestBean.java (added)
+++ labs/magma/trunk/foundation-beans/src/test/java/org/apache/magma/beans/TestBean.java Tue Sep 16 07:22:58 2008
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.beans;
+
+import java.util.List;
+
+import org.junit.Ignore;
+
+@Ignore("This is not a test")
+@MagmaBean
+public class TestBean {
+
+	private String privString;
+	private List<String> aList;
+	
+	public String getPrivString() {
+		return privString;
+	}
+	public void setPrivString(String privString) {
+		this.privString = privString;
+	}
+	
+	public void setWriteOnly(String val) {
+	}
+	
+	public String getReadOnly() {
+		return null;
+	}
+	public List<String> getAList() {
+		return aList;
+	}
+	public void setAList(List<String> list) {
+		aList = list;
+	}
+	
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org