KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.log4j.Logger;
21 import org.objectweb.jac.core.AspectComponent;
22 import org.objectweb.jac.core.Interaction;
23 import org.objectweb.jac.core.Wrappee;
24 import org.objectweb.jac.core.Wrapping;
25 import org.objectweb.jac.core.rtti.CollectionItem;
26 import org.objectweb.jac.core.rtti.RttiAC;
27 import org.objectweb.jac.util.ExtArrays;
28 import org.objectweb.jac.util.ExtBoolean;
29
30 /**
31  * Base class for collection wrappers
32  */

33
34 public abstract class CollectionWrapper extends AbstractPersistenceWrapper {
35     static Logger logger = Logger.getLogger("persistence");
36
37     boolean isLoaded = false;
38     CollectionItem collection;
39     Object JavaDoc substance;
40
41     public CollectionWrapper(AspectComponent ac,
42                              Object JavaDoc substance,
43                              CollectionItem collection,
44                              boolean isLoaded)
45     {
46         super(ac);
47         this.collection = collection;
48         this.substance = substance;
49         this.isLoaded = isLoaded;
50     }
51
52     boolean cache=false;
53
54     /**
55      * Load the whole collection if it is not already loaded
56      */

57     public synchronized void load(Wrappee wrappee) throws Exception JavaDoc {
58         if (!isLoaded) {
59             logger.debug("loading collection "+getOID(wrappee)+" - "+wrappee);
60             doLoad(wrappee);
61             isLoaded = true;
62         }
63     }
64
65     public boolean isLoaded() {
66         return isLoaded;
67     }
68
69     /**
70      * Unload the collection.
71      */

72     public synchronized void unload(Wrappee wrappee) {
73         logger.debug(getOID(wrappee)+".unload...");
74         isLoaded = false;
75         Wrapping.invokeOrg(wrappee,"clear",ExtArrays.emptyObjectArray);
76     }
77
78     /**
79      * Really load the whole collection. This is an abstract method
80      * must be overriden by subclasses.
81      */

82     protected abstract void doLoad(Wrappee wrappee) throws Exception JavaDoc;
83
84     /**
85      * Remove all instances from the collection
86      */

87     public abstract Object JavaDoc clear(Interaction interaction) throws Exception JavaDoc;
88
89     public Object JavaDoc preload(Interaction interaction) throws Exception JavaDoc {
90         logger.debug(getOID(interaction.wrappee)+
91                   ".preload for "+interaction.method);
92         try {
93             load(interaction.wrappee);
94         } catch (Exception JavaDoc e) {
95             logger.warn("Failed to preload collection for "+interaction,e);
96         }
97         return proceed(interaction);
98     }
99    
100     public synchronized Object JavaDoc size(Interaction interaction) throws Exception JavaDoc {
101         if (!isLoaded) {
102             long size = getCollectionSize(getOID(interaction.wrappee));
103             // If the collection isEmpty, we can consider it is loaded
104
// even if it not (since it's empty)
105
if (size==0)
106                 isLoaded = true;
107             return new Integer JavaDoc(new Long JavaDoc(size).intValue());
108         } else {
109             return proceed(interaction);
110         }
111     }
112
113     protected abstract long getCollectionSize(OID oid) throws Exception JavaDoc;
114
115     public synchronized Object JavaDoc isEmpty(Interaction interaction) throws Exception JavaDoc {
116         if (!isLoaded) {
117             boolean result = getCollectionSize(getOID(interaction.wrappee))==0;
118             // If the collection isEmpty, we can consider it is loaded
119
// even if it not (since it's empty)
120
if (result)
121                 isLoaded = true;
122             return ExtBoolean.valueOf(result);
123         } else {
124             return proceed(interaction);
125         }
126     }
127
128     // The last time the wrapped object was used
129
long useDate = System.currentTimeMillis();
130
131     //public Object memorizeUseDate(Interaction i) {
132
// useDate = System.currentTimeMillis());
133
// return proceed(i);
134
//}
135

136     public long getUseDate(Wrappee wrappee) {
137         return useDate;
138     }
139
140     /**
141      * Sets useDate to current time
142      */

143     protected void touch() {
144         useDate = System.currentTimeMillis();
145     }
146
147     public abstract Object JavaDoc iterator(Interaction interaction);
148
149     public boolean isCache() {
150         return cache;
151     }
152
153     public void setCache(boolean b) {
154         cache = b;
155     }
156
157     protected Object JavaDoc convert(Object JavaDoc value, Object JavaDoc wrappee) throws Exception JavaDoc {
158         if (value==null) {
159             return null;
160         } else {
161             Class JavaDoc collType = (Class JavaDoc)collection.getComponentType().getDelegate();
162             if (!collType.isAssignableFrom(value.getClass())) {
163                 Object JavaDoc converted = RttiAC.convert(value,collType);
164                 if (converted == value)
165                     logger.warn(
166                         "Failed to convert "+value+" into "+collType.getName()+
167                         " for collection "+substance+"["+getOID((Wrappee)substance)+"]."+
168                         collection.getName());
169                 return converted;
170             } else {
171                 return value;
172             }
173         }
174     }
175 }
176
Popular Tags