KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > idGen > IdGenAC


1 /*
2   Copyright (C) 2003 <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.idGen;
19
20 import org.aopalliance.intercept.ConstructorInvocation;
21 import org.aopalliance.intercept.MethodInvocation;
22 import org.apache.log4j.Logger;
23 import org.objectweb.jac.core.AspectComponent;
24 import org.objectweb.jac.core.Collaboration;
25 import org.objectweb.jac.core.Interaction;
26 import org.objectweb.jac.core.NameRepository;
27 import org.objectweb.jac.core.Wrapper;
28 import org.objectweb.jac.core.rtti.ClassItem;
29 import org.objectweb.jac.core.rtti.FieldItem;
30 import org.objectweb.jac.util.Log;
31
32 public class IdGenAC extends AspectComponent {
33     static Logger logger = Logger.getLogger("idgen");
34
35     public static final String JavaDoc COUNTER = "IdGenAC.COUNTER";
36     public static final String JavaDoc ID_FIELD = "IdGenAC.ID_FIELD";
37
38     public void genId(ClassItem cl, String JavaDoc counter, String JavaDoc fieldName) {
39         cl.setAttribute(COUNTER, counter);
40         cl.setAttribute(ID_FIELD, cl.getField(fieldName));
41         pointcut(
42             "ALL",
43             cl.getName(),
44             "CONSTRUCTORS",
45             IdGenWrapper.class.getName(),
46             null,
47             false);
48     }
49
50     Counters counters;
51     protected Counters getCounters() {
52         if (counters == null) {
53             if (countersName != null) {
54                 counters =
55                     (Counters) NameRepository.get().getObject(countersName);
56                 if (counters == null) {
57                     logger.error("IdGenAC: No object named " + countersName);
58                 }
59             }
60         }
61         return counters;
62     }
63
64     String JavaDoc countersName = "counters#0";
65     public void setCountersName(String JavaDoc countersName) {
66         this.countersName = countersName;
67     }
68
69     public class IdGenWrapper extends Wrapper {
70         public IdGenWrapper(AspectComponent ac) {
71             super(ac);
72         }
73
74         public Object JavaDoc genId(Interaction interaction) {
75             Object JavaDoc result = proceed(interaction);
76             logger.debug("generating id for " + interaction.wrappee);
77             ClassItem cl = interaction.getClassItem();
78             FieldItem field = (FieldItem) cl.getAttribute(ID_FIELD);
79             if (Collaboration.get().getAttribute("PersistenceAC.RESTORE")
80                 == null) {
81                 String JavaDoc counterName = (String JavaDoc) cl.getAttribute(COUNTER);
82                 Counters counters = getCounters();
83                 if (counters != null) {
84                     long id = counters.genId(counterName);
85                     logger.debug(" -> " + id);
86                     try {
87                         if (field.getType() == String JavaDoc.class)
88                             field.setThroughWriter(
89                                 interaction.wrappee,
90                                 Long.toString(id));
91                         else
92                             field.setThroughWriter(
93                                 interaction.wrappee,
94                                 new Long JavaDoc(id));
95                     } catch (IllegalAccessException JavaDoc e) {
96                         logger.error(
97                             "Failed to to set field " + field
98                                 + " for " + interaction.wrappee);
99                     }
100                 } else {
101                     logger.debug(" No counters object");
102                 }
103             } else {
104                 logger.debug(" skipping id generation for " + interaction.wrappee);
105             }
106             return result;
107         }
108
109         public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
110             throw new Exception JavaDoc("This wrapper does not support invocation interception.");
111         }
112
113         public Object JavaDoc construct(ConstructorInvocation invocation)
114             throws Throwable JavaDoc {
115             return genId((Interaction) invocation);
116         }
117     }
118 }
119
Popular Tags