KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > persistence > SetWrapper


1 /*
2   Copyright (C) 2001-2003 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.persistence;
19
20 import java.util.List JavaDoc;
21 import org.aopalliance.intercept.ConstructorInvocation;
22 import org.aopalliance.intercept.MethodInvocation;
23 import org.apache.log4j.Logger;
24 import org.objectweb.jac.core.AspectComponent;
25 import org.objectweb.jac.core.Interaction;
26 import org.objectweb.jac.core.Wrappee;
27 import org.objectweb.jac.core.Wrapping;
28 import org.objectweb.jac.core.rtti.ClassRepository;
29 import org.objectweb.jac.core.rtti.CollectionItem;
30 import org.objectweb.jac.core.rtti.MethodItem;
31 import org.objectweb.jac.util.ExtBoolean;
32
33 /**
34  * A wrapper for the Set interface.
35  */

36 public class SetWrapper extends CollectionWrapper {
37     static Logger logger = Logger.getLogger("persistence");
38
39     public SetWrapper(AspectComponent ac,
40                       Object JavaDoc substance,
41                       CollectionItem collection,
42                       boolean isLoaded)
43     {
44         super(ac, substance, collection, isLoaded);
45     }
46
47     protected void doLoad(Wrappee wrappee) throws Exception JavaDoc {
48         logger.debug( "do load " + wrappee.getClass());
49         // Keep the number of dynamic invocations to the minimum
50
OID oid = getOID(wrappee);
51         List JavaDoc list = oid.getStorage().getSet(oid);
52         MethodItem add = cr.getClass(wrappee).getMethod("add(java.lang.Object)");
53         Object JavaDoc[] params = new Object JavaDoc[1];
54         for (int i = 0; i < list.size(); i++) {
55             try {
56                 //list.set(i, normalizeOutput(list.get(i)));
57
params[0] = convert(normalizeOutput(list.get(i)),wrappee);
58                 Wrapping.invokeOrg(wrappee, add, params);
59             } catch (NoSuchOIDError e) {
60                 logger.error(
61                     "SetWrapper.doLoad("
62                     + oid + "): "+collection.getName()
63                     + ": skipping object at pos "+i
64                     +" with unknown OID "+list.get(i));
65                 list.set(i, null);
66             } catch (Exception JavaDoc e) {
67                 logger.error(
68                     "SetWrapper.doLoad("
69                     + oid + "): "+collection.getName()
70                     +"skipping object at pos "+i
71                     +" because of exception",e);
72                 list.set(i, null);
73             }
74         }
75         /*
76         attrdef(ATTR_ADDED, "true");
77         Wrapping.invokeOrg(wrappee, "addAll", new Object[] { list });
78         attrdef(ATTR_ADDED, null);
79         */

80     }
81
82     public Object JavaDoc contains(Interaction interaction) throws Exception JavaDoc {
83         touch();
84         if (isLoaded) {
85             return interaction.proceed();
86         } else {
87             OID oid = getOID(interaction.wrappee);
88             return ExtBoolean.valueOf(
89                 oid.getStorage().setContains(
90                     oid,
91                     normalizeInput(interaction.args[0])));
92         }
93     }
94
95     public boolean add(Interaction interaction) throws Exception JavaDoc {
96         touch();
97         if (interaction.args[0] != null)
98             logger.debug(
99                 "adding "
100                     + interaction.args[0].getClass().getName()
101                     + " to set");
102         if (isLoaded)
103             interaction.proceed();
104         OID oid = getOID(interaction.wrappee);
105         return oid.getStorage().addToSet(
106             oid,
107             normalizeInput(interaction.args[0]));
108     }
109
110     public Object JavaDoc remove(Interaction interaction) throws Exception JavaDoc {
111         touch();
112         boolean result1 = false;
113         if (isLoaded) {
114             result1 = ((Boolean JavaDoc) interaction.proceed()).booleanValue();
115         }
116         OID oid = getOID(interaction.wrappee);
117         boolean result2 =
118             oid.getStorage().removeFromSet(
119                 oid,
120                 normalizeInput(interaction.args[0]));
121         if (isLoaded) {
122             if (result1 != result2)
123                 throw new Error JavaDoc("SetWrapper.remove result1 != result2");
124             return ExtBoolean.valueOf(result1);
125         } else {
126             return ExtBoolean.valueOf(result2);
127         }
128     }
129
130     /**
131      * Remove all instances from the collection
132      */

133     public Object JavaDoc clear(Interaction interaction) throws Exception JavaDoc {
134         touch();
135         Object JavaDoc result = interaction.proceed();
136         OID oid = getOID(interaction.wrappee);
137         oid.getStorage().clearSet(oid);
138         return result;
139     }
140
141     protected long getCollectionSize(OID oid) throws Exception JavaDoc {
142         return oid.getStorage().getSetSize(oid);
143     }
144
145     public Object JavaDoc iterator(Interaction interaction) {
146         touch();
147         return new SetIterator(interaction.wrappee);
148     }
149
150     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
151         String JavaDoc name = invocation.getMethod().getName();
152         Interaction interaction = (Interaction) invocation;
153         if (name.equals("iterator")) {
154             load(interaction.wrappee);
155             return interaction.proceed();
156         } else if (name.equals("isEmpty")) {
157             return isEmpty(interaction);
158         } else if (name.equals("size")) {
159             return size(interaction);
160         } else if (name.equals("clear")) {
161             return clear(interaction);
162         } else if (name.equals("add")) {
163             return new Boolean JavaDoc(add(interaction));
164         } else if (name.equals("remove")) {
165             return remove(interaction);
166         } else if (name.equals("contains")) {
167             return contains(interaction);
168         } else if (name.equals("toArray")) {
169             load(interaction.wrappee);
170             return interaction.proceed();
171         } else if (name.equals("clone")) {
172             load(interaction.wrappee);
173             return interaction.proceed();
174         } else {
175             logger.error("SetWrapper: don't know what to do with method "+name);
176         }
177         return interaction.proceed();
178     }
179
180 }
181
Popular Tags