001 /* 002 Copyright (C) 2001 Renaud Pawlak 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program 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 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 017 018 package org.objectweb.jac.aspects.transaction; 019 020 021 022 import java.util.Iterator; 023 import java.util.Vector; 024 import org.apache.log4j.Logger; 025 import org.objectweb.jac.core.Wrappee; 026 import org.objectweb.jac.core.rtti.ClassItem; 027 import org.objectweb.jac.core.rtti.ClassRepository; 028 import org.objectweb.jac.core.rtti.CollectionItem; 029 import org.objectweb.jac.core.rtti.FieldItem; 030 import org.objectweb.jac.util.Log; 031 032 public class Merging { 033 034 static Logger logger = Logger.getLogger("transaction"); 035 036 public static void merge(Wrappee receptor, 037 Wrappee original, 038 Wrappee modified) throws Exception { 039 040 logger.debug("merging "+modified+" with "+receptor); 041 042 Vector peerChangedFields = diffFields(receptor,original); 043 044 Vector changedFields = diffFields(original,modified); 045 046 Iterator it = changedFields.iterator(); 047 while(it.hasNext()) { 048 FieldItem field = (FieldItem)it.next(); 049 if( peerChangedFields.contains( field ) ) { 050 throw new RuntimeException("concurrent modification during transaction on " 051 +field); 052 } 053 if( field instanceof CollectionItem ) { 054 // merge the collections 055 } else { 056 // we use the setter so that the other aspects are 057 // warned of the change 058 field.setThroughWriter(receptor,field.get(modified)); 059 } 060 } 061 } 062 063 public static Vector diffFields(Wrappee o1, Wrappee o2) { 064 065 Vector result = new Vector(); 066 ClassItem cli = ClassRepository.get().getClass(o1.getClass()); 067 FieldItem[] fields = cli.getPrimitiveFields(); 068 069 // diff the primitive fields 070 for( int i=0; i<fields.length; i++ ) { 071 if(!fields[i].get(o1) 072 .equals(fields[i].get(o2))) { 073 logger.debug(fields[i]+" differs after transaction"); 074 result.add(fields[i]); 075 } 076 } 077 078 CollectionItem[] collections = cli.getCollections(); 079 080 // diff the collections (to be implemeted) 081 /*for( int i=0; i<collections.length; i++ ) { 082 if(!collections[i].getActualCollection(o1) 083 .equals(collections[i].getActualCollection(o2))) { 084 result.add(fields[i]); 085 } 086 }*/ 087 088 return result; 089 090 } 091 092 } 093 094