| 1 | /** |
| 2 | * Copyright (C) 2007 NetMind Consulting Bt. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | package hu.netmind.beankeeper.model.impl; |
| 20 | |
| 21 | import hu.netmind.beankeeper.service.StoreContext; |
| 22 | import org.apache.log4j.Logger; |
| 23 | import java.util.*; |
| 24 | import java.lang.reflect.*; |
| 25 | import hu.netmind.beankeeper.common.StoreException; |
| 26 | import hu.netmind.beankeeper.model.*; |
| 27 | |
| 28 | /** |
| 29 | * This type is for boxed primitive objects which need to be stored. It has |
| 30 | * a single attribute named 'value', with the type of the primitive object itself. |
| 31 | * @author Brautigam Robert |
| 32 | * @version Revision: $Revision$ |
| 33 | */ |
| 34 | public class StrictPrimitiveHandler implements StrictClassHandler |
| 35 | { |
| 36 | private static Logger logger = Logger.getLogger(StrictPrimitiveHandler.class); |
| 37 | |
| 38 | private ClassEntry sourceEntry; |
| 39 | private Map attributeTypes; |
| 40 | |
| 41 | StrictPrimitiveHandler(ClassEntry sourceEntry) |
| 42 | { |
| 43 | // Init |
| 44 | this.sourceEntry=sourceEntry; |
| 45 | attributeTypes = new HashMap(); |
| 46 | attributeTypes.put("value",sourceEntry.getSourceClass()); |
| 47 | } |
| 48 | |
| 49 | public ClassEntry getSourceEntry() |
| 50 | { |
| 51 | return sourceEntry; |
| 52 | } |
| 53 | |
| 54 | public boolean hasChanged() |
| 55 | { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | public void update() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | public Map getAttributeTypes() |
| 64 | { |
| 65 | return new HashMap(attributeTypes); |
| 66 | } |
| 67 | |
| 68 | public List getAttributeNames() |
| 69 | { |
| 70 | return new ArrayList(attributeTypes.keySet()); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Construct the primitive object with the given value. |
| 75 | * @param marshalledValues The map that contains the 'value' attribute. |
| 76 | */ |
| 77 | public Object newInstance(Map marshalledValues) |
| 78 | { |
| 79 | Class clazz = sourceEntry.getSourceClass(); |
| 80 | Object value = marshalledValues.get("value"); |
| 81 | if ( value == null ) |
| 82 | return null; |
| 83 | value = TypeUtils.getTypeValue(value,clazz); |
| 84 | logger.debug("strict primitive handler instantiates object: "+clazz+", value: "+value+" ("+value.getClass()+")"); |
| 85 | // First try the exceptional primitive types, who have no |
| 86 | // string constructor. |
| 87 | if ( (clazz.equals(char.class)) || (clazz.equals(Character.class)) ) |
| 88 | return new Character( ((Character) value).charValue() ); |
| 89 | if ( clazz.equals(Date.class) ) |
| 90 | return new Date( ((Date) value).getTime() ); |
| 91 | // Others we construct with a String constructor. Most number types |
| 92 | // can be constructed with string representations easily. |
| 93 | try |
| 94 | { |
| 95 | Constructor ctor = clazz.getConstructor( new Class[] { String.class } ); |
| 96 | return ctor.newInstance( new Object[] { value.toString() } ); |
| 97 | } catch ( Exception e ) { |
| 98 | throw new StoreException("could not instantiate primitive type: "+sourceEntry+", with values: "+marshalledValues+", using string constructor."); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Always throws exception. |
| 104 | */ |
| 105 | public Object getAttributeValue(Object obj, String attributeName) |
| 106 | { |
| 107 | if ( ! attributeName.equalsIgnoreCase("value") ) |
| 108 | throw new StoreException("primitive handler has only 'value' attribute, but queried: "+attributeName); |
| 109 | return obj; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Always returns exception. |
| 114 | */ |
| 115 | public void setAttributeValue(Object obj, String attributeName, Object value) |
| 116 | { |
| 117 | throw new StoreException("object value cannot be set, objectclass: "+ |
| 118 | obj.getClass()+" name: "+attributeName+" on primitive handler for type: "+sourceEntry); |
| 119 | } |
| 120 | |
| 121 | } |
| 122 | |
| 123 | |