001: /**
002: * Copyright (C) 2007 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 org.apache.log4j.Logger;
020: import java.util.*;
021: import java.lang.reflect.*;
022:
023: /**
024: * Null handler is a class handler for interfaces and reserved classes.
025: * It contains no attributes, so all methods return default values
026: * statically.
027: * @author Brautigam Robert
028: * @version Revision: $Revision$
029: */
030: public class StrictPrimitiveHandler implements StrictClassHandler {
031: private static Logger logger = Logger
032: .getLogger(StrictPrimitiveHandler.class);
033:
034: private ClassEntry sourceEntry;
035: private Map attributeTypes;
036:
037: StrictPrimitiveHandler(ClassEntry sourceEntry) {
038: // Init
039: this .sourceEntry = sourceEntry;
040: attributeTypes = new HashMap();
041: attributeTypes.put("value", sourceEntry.getSourceClass());
042: }
043:
044: public ClassEntry getSourceEntry() {
045: return sourceEntry;
046: }
047:
048: public boolean hasChanged() {
049: return false;
050: }
051:
052: public Map getAttributeTypes() {
053: return new HashMap(attributeTypes);
054: }
055:
056: public List getAttributeNames() {
057: return new Vector(attributeTypes.keySet());
058: }
059:
060: /**
061: * Construct the primitive object with the given value.
062: * @param marshalledValues The map that contains the 'value' attribute.
063: */
064: public Object newInstance(Map marshalledValues) {
065: Class clazz = sourceEntry.getSourceClass();
066: Object value = marshalledValues.get("value");
067: if (value == null)
068: return null;
069: value = ObjectTracker.getTypeValue(value, clazz);
070: logger.debug("strict primitive handler instantiates object: "
071: + clazz + ", value: " + value + " (" + value.getClass()
072: + ")");
073: // First try the exceptional primitive types, who have no
074: // string constructor.
075: if ((clazz.equals(char.class))
076: || (clazz.equals(Character.class)))
077: return new Character(((Character) value).charValue());
078: if (clazz.equals(Date.class))
079: return new Date(((Date) value).getTime());
080: // Others we construct with a String constructor. Most number types
081: // can be constructed with string representations easily.
082: try {
083: Constructor ctor = clazz
084: .getConstructor(new Class[] { String.class });
085: return ctor.newInstance(new Object[] { value.toString() });
086: } catch (Exception e) {
087: throw new StoreException(
088: "could not instantiate primitive type: "
089: + sourceEntry + ", with values: "
090: + marshalledValues
091: + ", using string constructor.");
092: }
093: }
094:
095: /**
096: * Always throws exception.
097: */
098: public Object getAttributeValue(Object obj, String attributeName) {
099: if (!attributeName.equalsIgnoreCase("value"))
100: throw new StoreException(
101: "primitive handler has only 'value' attribute, but queried: "
102: + attributeName);
103: return obj;
104: }
105:
106: /**
107: * Always returns exception.
108: */
109: public void setAttributeValue(Object obj, String attributeName,
110: Object value) {
111: throw new StoreException(
112: "object value cannot be set, objectclass: "
113: + obj.getClass() + " name: " + attributeName
114: + " on primitive handler for type: "
115: + sourceEntry);
116: }
117:
118: }
|