KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > timestamp > TimestampAC


1 /*
2   Copyright (C) 2004 Laurent Martelli <laurent@aopsys.com>
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.timestamp;
19
20 import java.util.Iterator JavaDoc;
21 import org.aopalliance.intercept.ConstructorInvocation;
22 import org.aopalliance.intercept.MethodInvocation;
23 import org.apache.log4j.Logger;
24 import org.objectweb.*;
25 import org.objectweb.jac.core.AspectComponent;
26 import org.objectweb.jac.core.Interaction;
27 import org.objectweb.jac.core.NameRepository;
28 import org.objectweb.jac.core.rtti.ClassItem;
29 import org.objectweb.jac.core.rtti.ClassRepository;
30 import org.objectweb.jac.core.rtti.CollectionItem;
31 import org.objectweb.jac.core.rtti.FieldItem;
32 import org.objectweb.jac.core.rtti.RttiAC;
33 import org.objectweb.jac.util.ExtBoolean;
34
35 public class TimestampAC extends AspectComponent implements TimestampConf {
36     static final Logger logger = Logger.getLogger("timestamp");
37     
38     public static final String JavaDoc FOLLOW = "TimestampAC.FOLLOW";
39
40     public void declareTimestampedClasses(String JavaDoc classExpr,
41                                           String JavaDoc wrappeeExpr,
42                                           String JavaDoc repositoryName)
43     {
44         pointcut("ALL",classExpr,"MODIFIERS",
45                  new Wrapper(this,repositoryName),null);
46     }
47
48     public void followLink(FieldItem link, boolean follow) {
49         logger.info("Setting follow on "+link);
50         link.setAttribute(FOLLOW,ExtBoolean.valueOf(follow));
51     }
52
53     public static class Wrapper extends org.objectweb.jac.core.Wrapper {
54         public Wrapper(AspectComponent ac, String JavaDoc stampsName) {
55             super(ac);
56             this.stampsName = stampsName;
57         }
58
59         String JavaDoc stampsName;
60         Timestamps stamps;
61
62         void setStamps() {
63             stamps = (Timestamps)NameRepository.get().getObject(stampsName);
64         }
65
66         public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
67             Object JavaDoc res = invocation.proceed();
68             setStamps();
69             if (stamps!=null) {
70                 Interaction interaction = (Interaction)invocation;
71                 touch(interaction.wrappee,interaction.getClassItem());
72             }
73             return res;
74         }
75
76         void touch(Object JavaDoc object, ClassItem cl) {
77             logger.info("touch "+object);
78             stamps.touch(object);
79             if (cl==null) {
80                 cl = ClassRepository.get().getClass(object);
81             }
82             FieldItem[] fields = cl.getFields();
83             for (int i=0; i<fields.length; i++) {
84                 FieldItem field= fields[i];
85                 logger.debug("Testing field "+field);
86                 boolean followed = false;
87                 if (field.isReference()) {
88                     FieldItem opposite = (FieldItem)field.getOppositeRole();
89                     if (opposite!=null && opposite.isAggregation()) {
90                         logger.debug(" opposite is aggregation");
91                         followed = true;
92                         Object JavaDoc touched = fields[i].getThroughAccessor(object);
93                         if (touched!=null)
94                             touch(touched,null);
95                     }
96                 }
97
98                 if (!followed && field.getBoolean(FOLLOW, false)) {
99                     logger.debug(" must be followed");
100                     if (field instanceof CollectionItem) {
101                         Iterator JavaDoc it = ((CollectionItem)field).getActualCollectionThroughAccessor(object).iterator();
102                         while (it.hasNext()) {
103                             Object JavaDoc touched = it.next();
104                             if (touched!=null)
105                                 touch(touched,null);
106                             else
107                                 logger.debug(" Not touching null value in "+field.getLongName());
108                         }
109                     } else {
110                         Object JavaDoc touched = field.getThroughAccessor(object);
111                         if (touched!=null)
112                             touch(touched,null);
113                         else
114                             logger.info(" Not touching null value "+field.getLongName());
115                     }
116                 }
117             }
118         }
119
120         public Object JavaDoc construct(ConstructorInvocation invocation) throws Throwable JavaDoc {
121             return invocation.proceed();
122         }
123     }
124 }
125
Popular Tags