001: package org.conform;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ObjectInputStream;
005: import java.io.Serializable;
006: import java.util.*;
007:
008: public class Beans implements Serializable, Cloneable, BeanMetaProvider {
009:
010: private Map beansMap = new HashMap();
011:
012: public Collection getBeans() {
013: return beansMap.values();
014: }
015:
016: public void setBeans(Collection beans) {
017: beansMap.clear();
018: for (Iterator iterator = beans.iterator(); iterator.hasNext();) {
019: BeanMeta bean = (BeanMeta) iterator.next();
020: beansMap.put(bean.getName(), bean);
021: }
022: }
023:
024: public void addBeanMeta(BeanMeta bean) {
025: beansMap.put(bean.getName(), bean);
026: }
027:
028: public void removeBeanMeta(BeanMeta bean) {
029: beansMap.remove(bean.getName());
030: }
031:
032: public BeanMeta getBeanMeta(Class type) {
033: if (type == null)
034: return null;
035: BeanMeta bean = (BeanMeta) beansMap.get(type.getName());
036: if (bean == null) {
037: bean = new BeanMeta(type);
038: addBeanMeta(bean);
039: return bean;
040: }
041: return bean;
042: }
043:
044: public void clearCache() {
045: beansMap.clear();
046: }
047:
048: public boolean containsBeanMeta(Class type) {
049: if (type == null)
050: return false;
051: if (beansMap.containsKey(type.getName()))
052: return true;
053: return false;
054: }
055:
056: public boolean containsBeanMeta(String name) {
057: if (name == null)
058: return false;
059: if (beansMap.containsKey(name))
060: return true;
061: return false;
062: }
063:
064: public Beans copy() {
065: byte[] bytes = org.apache.commons.lang.SerializationUtils
066: .serialize(this );
067: ByteArrayInputStream in = new ByteArrayInputStream(bytes);
068: try {
069: ObjectInputStream oin = new ObjectInputStream(in);
070: return (Beans) oin.readObject();
071: } catch (Exception e) {
072: throw new RuntimeException(e);
073: }
074: }
075:
076: public Object clone() {
077: try {
078: Map map = new HashMap();
079: Beans beans = (Beans) super .clone();
080: for (Iterator iterator = beansMap.values().iterator(); iterator
081: .hasNext();) {
082: BeanMeta beanMeta = (BeanMeta) iterator.next();
083: BeanMeta clone = (BeanMeta) beanMeta.clone();
084: map.put(clone.getName(), clone);
085:
086: for (int i = 0; i < clone.getProperties().length; i++) {
087: PropertyMeta property = (PropertyMeta) clone
088: .getProperties()[i];
089: BeanMeta relationBean = property.getRelationBean();
090: PropertyMeta relationProperty = property
091: .getRelationProperty();
092: if (relationBean != null) {
093: relationBean = (BeanMeta) map.get(relationBean
094: .getName());
095: property.setRelationBean(relationBean);
096: property
097: .setRelationProperty(relationBean
098: .getProperty(relationProperty
099: .getName()));
100: }
101: }
102:
103: }
104: beans.beansMap = map;
105: return beans;
106: } catch (CloneNotSupportedException e) {
107: throw new RuntimeException(e);
108: }
109: }
110: }
|