| 1 | /** |
| 2 | * Copyright (C) 2006 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 java.util.HashMap; |
| 22 | import java.util.Map; |
| 23 | import java.util.List; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Iterator; |
| 26 | import java.lang.reflect.Field; |
| 27 | import java.lang.reflect.Modifier; |
| 28 | import org.apache.log4j.Logger; |
| 29 | import java.util.Date; |
| 30 | import hu.netmind.beankeeper.common.StoreException; |
| 31 | import hu.netmind.beankeeper.model.*; |
| 32 | |
| 33 | /** |
| 34 | * This strict handler works on member fields of the given class. |
| 35 | * @author Brautigam Robert |
| 36 | * @version Revision: $Revision$ |
| 37 | */ |
| 38 | public class StrictStaticHandler implements StrictClassHandler |
| 39 | { |
| 40 | private static Logger logger = Logger.getLogger(StrictStaticHandler.class); |
| 41 | |
| 42 | private ClassEntry sourceEntry; |
| 43 | private HashMap attributeTypes; |
| 44 | private HashMap attributes; |
| 45 | |
| 46 | StrictStaticHandler(ClassEntry sourceEntry) |
| 47 | { |
| 48 | // Init |
| 49 | this.sourceEntry=sourceEntry; |
| 50 | attributes = new HashMap(); |
| 51 | attributeTypes = new HashMap(); |
| 52 | // If this class is a dynamic class, it has no static attributes! |
| 53 | if ( sourceEntry.getDynamicName() != null ) |
| 54 | return; |
| 55 | // Add this class' attribute to sets |
| 56 | Field[] fields = sourceEntry.getSourceClass().getDeclaredFields(); |
| 57 | for ( int i=0; i<fields.length; i++ ) |
| 58 | { |
| 59 | if ( (! Modifier.isTransient(fields[i].getModifiers())) && |
| 60 | (! Modifier.isStatic(fields[i].getModifiers())) |
| 61 | ) |
| 62 | { |
| 63 | String attributeName = fields[i].getName().toLowerCase(); |
| 64 | fields[i].setAccessible(true); |
| 65 | attributes.put(attributeName,fields[i]); |
| 66 | attributeTypes.put(attributeName,fields[i].getType()); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public static boolean hasStaticAttributes(ClassEntry entry) |
| 72 | { |
| 73 | StrictStaticHandler tmpHandler = new StrictStaticHandler(entry); |
| 74 | List attributeNames = tmpHandler.getAttributeNames(); |
| 75 | for ( int i=0; i<attributeNames.size(); i++ ) |
| 76 | if ( ! ((String) attributeNames.get(i)).startsWith("persistence") ) |
| 77 | return true; |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | public ClassEntry getSourceEntry() |
| 82 | { |
| 83 | return sourceEntry; |
| 84 | } |
| 85 | |
| 86 | public Map getAttributeTypes() |
| 87 | { |
| 88 | return new HashMap(attributeTypes); |
| 89 | } |
| 90 | |
| 91 | public List getAttributeNames() |
| 92 | { |
| 93 | return new ArrayList(attributeTypes.keySet()); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the attribute value from a given object of this class and |
| 98 | * from given attribute. |
| 99 | */ |
| 100 | public Object getAttributeValue(Object obj, String attributeName) |
| 101 | { |
| 102 | Field field = (Field) attributes.get(attributeName.toLowerCase()); |
| 103 | if ( field == null ) |
| 104 | return null; |
| 105 | try |
| 106 | { |
| 107 | return field.get(obj); |
| 108 | } catch ( Throwable e ) { |
| 109 | throw new StoreException("object value cannot be get, name: "+attributeName,e); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Set an object as value into object given. |
| 115 | */ |
| 116 | public void setAttributeValue(Object obj, String attributeName, Object value) |
| 117 | { |
| 118 | Field field = (Field) attributes.get(attributeName.toLowerCase()); |
| 119 | if ( field == null ) |
| 120 | return; |
| 121 | Class attrClass = (Class) attributeTypes.get(attributeName.toLowerCase()); |
| 122 | try |
| 123 | { |
| 124 | field.set(obj,TypeUtils.getTypeValue(value,attrClass)); |
| 125 | } catch ( Throwable e ) { |
| 126 | Class valueClass = null; |
| 127 | if ( value != null ) |
| 128 | valueClass = value.getClass(); |
| 129 | throw new StoreException("object value cannot be set, objectclass: "+ |
| 130 | obj.getClass()+" name: "+attributeName+" (class: "+attrClass+", value: "+value+" (class: "+valueClass+"))",e); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | public boolean hasChanged() |
| 135 | { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | public void update() |
| 140 | { |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |