KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import org.aopalliance.intercept.ConstructorInvocation;
23 import org.aopalliance.intercept.MethodInvocation;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.core.AspectComponent;
26 import org.objectweb.jac.core.Interaction;
27 import org.objectweb.jac.core.Wrappee;
28 import org.objectweb.jac.core.Wrapping;
29 import org.objectweb.jac.core.rtti.ClassRepository;
30 import org.objectweb.jac.core.rtti.CollectionItem;
31 import org.objectweb.jac.core.rtti.MethodItem;
32 import org.objectweb.jac.util.ExtBoolean;
33 import org.objectweb.jac.util.Log;
34
35 /**
36  * A wrapper for the Map interface.
37  */

38 public class MapWrapper extends CollectionWrapper {
39     static Logger logger = Logger.getLogger("persistence");
40
41     public MapWrapper(AspectComponent ac,
42                       Object JavaDoc substance,
43                       CollectionItem collection,
44                       boolean isLoaded)
45     {
46         super(ac, substance, collection, isLoaded);
47     }
48
49     protected void doLoad(Wrappee wrappee) throws Exception JavaDoc {
50         OID oid = getOID(wrappee);
51         Map JavaDoc map = oid.getStorage().getMap(oid);
52         // Keep the number of dynamic invocations to the minimum
53
//HashMap normalizedMap = new HashMap(map.size());
54
Iterator JavaDoc i = map.entrySet().iterator();
55         Object JavaDoc[] params = new Object JavaDoc[2];
56         MethodItem put = cr.getClass(wrappee).getMethod("put");
57         while (i.hasNext()) {
58             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
59             try {
60                 /*
61                   normalizedMap.put(
62                     normalizeOutput(entry.getKey()),
63                     normalizeOutput(entry.getValue()));
64                 */

65                 params[0] = normalizeOutput(entry.getKey());
66                 params[1] = normalizeOutput(entry.getValue());
67                 Wrapping.invokeOrg(wrappee, put, params);
68             } catch (NoSuchOIDError e) {
69                 logger.error(
70                     "MapWrapper.doLoad("
71                         + oid + "): skipping entry with unknown OID " + entry);
72             }
73         }
74         /*
75         attrdef(ATTR_ADDED, "true");
76         Wrapping.invokeOrg(wrappee, "putAll", new Object[] { normalizedMap });
77         attrdef(ATTR_ADDED, null);
78         */

79     }
80
81     public Object JavaDoc containsKey(Interaction interaction) throws Exception JavaDoc {
82         touch();
83         if (isLoaded) {
84             return interaction.proceed();
85         } else {
86             OID oid = getOID(interaction.wrappee);
87             return ExtBoolean.valueOf(
88                 oid.getStorage().mapContainsKey(
89                     oid,
90                     normalizeInput(interaction.args[0])));
91         }
92     }
93
94     public Object JavaDoc containsValue(Interaction interaction) throws Exception JavaDoc {
95         touch();
96         if (isLoaded) {
97             return interaction.proceed();
98         } else {
99             OID oid = getOID(interaction.wrappee);
100             return ExtBoolean.valueOf(
101                 oid.getStorage().mapContainsValue(
102                     oid,
103                     normalizeInput(interaction.args[0])));
104         }
105     }
106
107     public Object JavaDoc put(Interaction interaction) throws Exception JavaDoc {
108         touch();
109         Object JavaDoc ret = null;
110         if (isLoaded)
111             interaction.proceed();
112         OID oid = getOID(interaction.wrappee);
113         ret =
114             normalizeOutput(
115                 oid.getStorage().putInMap(
116                     oid,
117                     normalizeInput(interaction.args[0]),
118                     normalizeInput(interaction.args[1])));
119         return ret;
120     }
121
122     public Object JavaDoc get(Interaction interaction) throws Exception JavaDoc {
123         touch();
124         if (isLoaded) {
125             return interaction.proceed();
126         } else {
127             OID oid = getOID(interaction.wrappee);
128             return normalizeOutput(
129                 oid.getStorage().getFromMap(
130                     oid,
131                     normalizeInput(interaction.args[0])));
132         }
133     }
134
135     public Object JavaDoc remove(Interaction interaction) throws Exception JavaDoc {
136         touch();
137         Object JavaDoc result1 = null;
138         boolean proceeded = false;
139         if (isLoaded) {
140             result1 = interaction.proceed();
141             proceeded = true;
142         }
143         OID oid = getOID(interaction.wrappee);
144         Object JavaDoc result2 =
145             oid.getStorage().removeFromMap(
146                 oid,
147                 normalizeInput(interaction.args[0]));
148         if (proceeded)
149             return result1;
150         else
151             return normalizeOutput(result2);
152     }
153
154     /**
155      * Remove all instances from the collection
156      */

157     public Object JavaDoc clear(Interaction interaction) throws Exception JavaDoc {
158         touch();
159         Object JavaDoc result = interaction.proceed();
160         OID oid = getOID(interaction.wrappee);
161         oid.getStorage().clearMap(oid);
162         return result;
163     }
164
165     protected long getCollectionSize(OID oid) throws Exception JavaDoc {
166         return oid.getStorage().getMapSize(oid);
167     }
168
169     public Object JavaDoc iterator(Interaction interaction) {
170         touch();
171         return new MapIterator(interaction.wrappee);
172     }
173
174     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
175         String JavaDoc name = invocation.getMethod().getName();
176         Interaction interaction = (Interaction) invocation;
177         if (name.equals("keySet")) {
178             load(interaction.wrappee);
179             return interaction.proceed();
180         } else if (name.equals("entrySet")) {
181             load(interaction.wrappee);
182             return interaction.proceed();
183         } else if (name.equals("values")) {
184             load(interaction.wrappee);
185             return interaction.proceed();
186         } else if (name.equals("isEmpty")) {
187             return isEmpty(interaction);
188         } else if (name.equals("size")) {
189             return size(interaction);
190         } else if (name.equals("containsKey")) {
191             return containsKey(interaction);
192         } else if (name.equals("containsValue")) {
193             return containsValue(interaction);
194         } else if (name.equals("clear")) {
195             return clear(interaction);
196         } else if (name.equals("put")) {
197             return put(interaction);
198         } else if (name.equals("remove")) {
199             return remove(interaction);
200         } else if (name.equals("get")) {
201             return get(interaction);
202         } else if (name.equals("clone")) {
203             load(interaction.wrappee);
204             return interaction.proceed();
205         } else {
206             logger.error("MapWrapper: don't know what to do with method "+name);
207         }
208         return interaction.proceed();
209     }
210
211 }
212
Popular Tags