KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > transaction > Merging


1 /*
2   Copyright (C) 2001 Renaud Pawlak
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program 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
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.transaction;
19
20
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Vector JavaDoc;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.core.Wrappee;
26 import org.objectweb.jac.core.rtti.ClassItem;
27 import org.objectweb.jac.core.rtti.ClassRepository;
28 import org.objectweb.jac.core.rtti.CollectionItem;
29 import org.objectweb.jac.core.rtti.FieldItem;
30 import org.objectweb.jac.util.Log;
31
32 public class Merging {
33
34     static Logger logger = Logger.getLogger("transaction");
35
36     public static void merge(Wrappee receptor,
37                              Wrappee original,
38                              Wrappee modified) throws Exception JavaDoc {
39
40         logger.debug("merging "+modified+" with "+receptor);
41       
42         Vector JavaDoc peerChangedFields = diffFields(receptor,original);
43       
44         Vector JavaDoc changedFields = diffFields(original,modified);
45
46         Iterator JavaDoc it = changedFields.iterator();
47         while(it.hasNext()) {
48             FieldItem field = (FieldItem)it.next();
49             if( peerChangedFields.contains( field ) ) {
50                 throw new RuntimeException JavaDoc("concurrent modification during transaction on "
51                                            +field);
52             }
53             if( field instanceof CollectionItem ) {
54                 // merge the collections
55
} else {
56                 // we use the setter so that the other aspects are
57
// warned of the change
58
field.setThroughWriter(receptor,field.get(modified));
59             }
60         }
61     }
62
63     public static Vector JavaDoc diffFields(Wrappee o1, Wrappee o2) {
64       
65         Vector JavaDoc result = new Vector JavaDoc();
66         ClassItem cli = ClassRepository.get().getClass(o1.getClass());
67         FieldItem[] fields = cli.getPrimitiveFields();
68       
69         // diff the primitive fields
70
for( int i=0; i<fields.length; i++ ) {
71             if(!fields[i].get(o1)
72                .equals(fields[i].get(o2))) {
73                 logger.debug(fields[i]+" differs after transaction");
74                 result.add(fields[i]);
75             }
76         }
77
78         CollectionItem[] collections = cli.getCollections();
79
80         // diff the collections (to be implemeted)
81
/*for( int i=0; i<collections.length; i++ ) {
82           if(!collections[i].getActualCollection(o1)
83           .equals(collections[i].getActualCollection(o2))) {
84           result.add(fields[i]);
85           }
86           }*/

87
88         return result;
89      
90     }
91
92 }
93
94
95
Popular Tags