001: /**
002: * Copyright (C) 2006 NetMind Consulting Bt.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 3 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package hu.netmind.persistence;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.List;
022: import java.util.Vector;
023: import java.util.Iterator;
024: import java.lang.reflect.Field;
025: import java.lang.reflect.Modifier;
026: import org.apache.log4j.Logger;
027: import java.util.Date;
028:
029: /**
030: * Information and transformations on a given class. This class
031: * implements reflecting of classes, and type knowledge.
032: * @author Brautigam Robert
033: * @version Revision: $Revision$
034: */
035: public class StrictStaticHandler implements StrictClassHandler {
036: private static Logger logger = Logger
037: .getLogger(StrictStaticHandler.class);
038:
039: protected StoreContext context;
040: protected ClassEntry sourceEntry;
041: protected HashMap attributeTypes;
042: private HashMap attributes;
043:
044: StrictStaticHandler(StoreContext context, ClassEntry sourceEntry) {
045: this .context = context;
046: // Init
047: this .sourceEntry = sourceEntry;
048: attributes = new HashMap();
049: attributeTypes = new HashMap();
050: // If this class is a dynamic class, it has no static attributes!
051: if (sourceEntry.getDynamicName() != null)
052: return;
053: // Add this class' attribute to sets
054: Field[] fields = sourceEntry.getSourceClass()
055: .getDeclaredFields();
056: for (int i = 0; i < fields.length; i++) {
057: if ((!Modifier.isTransient(fields[i].getModifiers()))
058: && (!Modifier.isStatic(fields[i].getModifiers()))) {
059: String attributeName = fields[i].getName()
060: .toLowerCase();
061: fields[i].setAccessible(true);
062: attributes.put(attributeName, fields[i]);
063: attributeTypes.put(attributeName, fields[i].getType());
064: }
065: }
066: }
067:
068: public static boolean hasStaticAttributes(ClassEntry entry) {
069: StrictStaticHandler tmpHandler = new StrictStaticHandler(null,
070: entry);
071: List attributeNames = tmpHandler.getAttributeNames();
072: for (int i = 0; i < attributeNames.size(); i++)
073: if (!((String) attributeNames.get(i))
074: .startsWith("persistence"))
075: return true;
076: return false;
077: }
078:
079: public ClassEntry getSourceEntry() {
080: return sourceEntry;
081: }
082:
083: public boolean hasChanged() {
084: return false;
085: }
086:
087: public Map getAttributeTypes() {
088: return attributeTypes;
089: }
090:
091: public List getAttributeNames() {
092: return new Vector(attributeTypes.keySet());
093: }
094:
095: /**
096: * Get the attribute value from a given object of this class and
097: * from given attribute.
098: */
099: public Object getAttributeValue(Object obj, String attributeName) {
100: Field field = (Field) attributes.get(attributeName
101: .toLowerCase());
102: if (field == null)
103: return null;
104: try {
105: return field.get(obj);
106: } catch (Throwable e) {
107: throw new StoreException(
108: "object value cannot be get, name: "
109: + attributeName, e);
110: }
111: }
112:
113: /**
114: * Return whether the given attribute name is a static attribute.
115: */
116: protected boolean isStatic(String attributeName) {
117: return attributes.get(attributeName.toLowerCase()) != null;
118: }
119:
120: /**
121: * Set an object as value into object given.
122: */
123: public void setAttributeValue(Object obj, String attributeName,
124: Object value) {
125: Field field = (Field) attributes.get(attributeName
126: .toLowerCase());
127: if (field == null)
128: return;
129: Class attrClass = (Class) attributeTypes.get(attributeName
130: .toLowerCase());
131: try {
132: field
133: .set(obj, ObjectTracker.getTypeValue(value,
134: attrClass));
135: } catch (Throwable e) {
136: Class valueClass = null;
137: if (value != null)
138: valueClass = value.getClass();
139: throw new StoreException(
140: "object value cannot be set, objectclass: "
141: + obj.getClass() + " name: "
142: + attributeName + " (class: " + attrClass
143: + ", value: " + value + " (class: "
144: + valueClass + "))", e);
145: }
146: }
147:
148: }
|