KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > genclass > GenClass


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; 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.speedo.genclass;
19
20 import org.objectweb.speedo.genclass.api.SpeedoGenClassProxy;
21 import org.objectweb.speedo.mim.api.LifeCycle;
22 import org.objectweb.speedo.mim.api.SpeedoHome;
23 import org.objectweb.speedo.mim.api.SpeedoAccessor;
24 import org.objectweb.speedo.mim.api.SpeedoProxy;
25 import org.objectweb.speedo.genclass.api.SpeedoGenClassListener;
26 import org.objectweb.speedo.api.Debug;
27 import org.objectweb.jorm.util.api.Loggable;
28 import org.objectweb.jorm.api.PBinding;
29 import org.objectweb.jorm.api.PException;
30 import org.objectweb.jorm.api.PClassMapping;
31 import org.objectweb.jorm.api.PAccessor;
32 import org.objectweb.jorm.api.PIndexedElem;
33 import org.objectweb.jorm.api.PBindingCtrl;
34 import org.objectweb.jorm.type.api.PType;
35 import org.objectweb.jorm.naming.api.PName;
36 import org.objectweb.perseus.cache.api.UnFixProtocolException;
37 import org.objectweb.util.monolog.api.Logger;
38 import org.objectweb.util.monolog.api.BasicLevel;
39 import org.objectweb.util.monolog.api.LoggerFactory;
40
41 import javax.jdo.PersistenceManager;
42 import javax.jdo.JDOUnsupportedOptionException;
43 import javax.jdo.JDOException;
44 import javax.jdo.spi.PersistenceCapable;
45 import javax.jdo.spi.StateManager;
46 import java.util.Collection JavaDoc;
47 import java.util.Collections JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.ArrayList JavaDoc;
51
52 /**
53  * This class is a basic implementation of the SpeedoGenClassProxy interface.
54  * It uses a delegate PBinding.
55  *
56  * @author S.Chassande-Barrioz
57  */

58 public abstract class GenClass
59         implements SpeedoGenClassProxy, Loggable, PBindingCtrl {
60
61     /**
62      * This fields indicates if the proxy is active (attached to at least a
63      * Proxy Manager)
64      */

65     protected boolean jdoIsActive = false;
66
67     /**
68      * is the PBinding which permits to store/load the data into/from the support
69      */

70     protected PBindingCtrl pbinding;
71
72     /**
73      * The logger used to trace this generic class
74      */

75     public Logger logger;
76
77     /**
78      * is the name of the mapper inside which the generic class is persistent
79      */

80     protected String JavaDoc mapperName;
81
82     /**
83      * is the name of the project name inside which the generic class is used.
84      */

85     protected String JavaDoc projectName;
86
87     /**
88      * is the type of the generic class
89      */

90     protected PType ptype;
91
92     /**
93      * The linked field associated to this gen class
94      */

95     protected String JavaDoc linkedField = null;
96
97     /**
98      * Is the PName which
99      */

100     protected Object JavaDoc pnameHints = null;
101
102     /**
103      * Id of the field that references this gen class instance
104      */

105     protected int gcid;
106
107     /**
108      * List of SpeedoGenClassListener
109      */

110     protected List JavaDoc listeners;
111
112     protected long age;
113
114     protected int fix = 0;
115     
116     protected Object JavaDoc encodedPName;
117     
118     public void init(PType _ptype,
119                     String JavaDoc _linkedField,
120                     Object JavaDoc _pnameHints,
121                     Logger _logger) {
122         this.pnameHints = _pnameHints;
123         this.ptype = _ptype;
124         this.linkedField = _linkedField;
125         this.logger = _logger;
126    }
127
128     public void jdoSetPNameHints(Object JavaDoc hints) {
129         pnameHints = hints;
130     }
131
132     public boolean elementIsReference() {
133         return ptype.getNestedPType().getTypeCode() == PType.TYPECODE_REFERENCE;
134     }
135
136     public void speedoSetGcId (int _gcid) {
137         this.gcid = _gcid;
138     }
139
140     public void speedoAddGenClassListener (SpeedoGenClassListener l) {
141         if (listeners == null) {
142             listeners = new ArrayList JavaDoc();
143             listeners.add(l);
144         } else {
145             for(int i=0; i<listeners.size(); i++) {
146                 if (listeners.get(i) == l) {
147                     return;
148                 }
149             }
150             listeners.add(l);
151         }
152     }
153
154     public void speedoAdd(Object JavaDoc elem, int gcFieldNumber) {
155         throw new JDOException("Not supported in tuple class");
156     }
157
158     public boolean speedoAdd(Object JavaDoc elemToAdd, Object JavaDoc hints) {
159         GenClassAccessor gca = (GenClassAccessor) getSpeedoHome().writeIntention(this, null);
160         return gca.speedoAdd(elemToAdd, hints);
161     }
162
163     public boolean speedoRemove(Object JavaDoc elemToRemove, Object JavaDoc hints) {
164         GenClassAccessor gca = (GenClassAccessor) getSpeedoHome().writeIntention(this, null);
165         return gca.speedoRemove(elemToRemove, hints);
166     }
167
168     public void fireSpeedoElementAdded (Object JavaDoc elem) {
169         if (Debug.ON) {
170             logger.log(BasicLevel.DEBUG, "fireSpeedoElementAdded(): listner: "
171                 + (listeners == null ? "no listener" : "" + listeners.size()));
172         }
173         if (listeners == null) {
174             return;
175         }
176         Iterator JavaDoc i = listeners.iterator();
177         while (i.hasNext()) {
178             SpeedoGenClassListener l = (SpeedoGenClassListener)i.next();
179             l.speedoElementAdded(elem, gcid);
180         }
181     }
182
183     public void fireSpeedoElementRemoved (Object JavaDoc elem) {
184         if (Debug.ON) {
185             logger.log(BasicLevel.DEBUG, "fireSpeedoElementRemoved(): listner: "
186                 + (listeners == null ? "no listener" : "" + listeners.size()));
187         }
188         if (listeners == null) {
189             return;
190         }
191         Iterator JavaDoc i = listeners.iterator();
192         while (i.hasNext()) {
193             SpeedoGenClassListener l = (SpeedoGenClassListener)i.next();
194             l.speedoElementRemoved(elem, gcid);
195         }
196     }
197
198     public Object JavaDoc getDataIdentifier(Object JavaDoc o) {
199         if (o instanceof SpeedoProxy) {
200             if (!((SpeedoProxy) o).jdoIsActive()) {
201                 ((SpeedoGenClassHome) getSpeedoHome())
202                     .makePersistent(null, (SpeedoProxy) o, this);
203             }
204             return ((SpeedoProxy) o).getCeIdentifier();
205         } else {
206             return o;
207         }
208     }
209     public Collection JavaDoc getDataIdentifiers(Collection JavaDoc os) {
210         if (os == null) {
211             return null;
212         }
213         if (os.size() == 0) {
214             return Collections.EMPTY_LIST;
215         }
216         ArrayList JavaDoc al = new ArrayList JavaDoc();
217         for(Iterator JavaDoc it = os.iterator(); it.hasNext();) {
218             al.add(getDataIdentifier(it.next()));
219         }
220         return al;
221     }
222     
223     // ABSTRACT METHODS //
224
// -----------------//
225

226     public abstract SpeedoAccessor createAccessor();
227
228     public abstract SpeedoAccessor getReferenceAccessor();
229
230     public abstract void setReferenceAccessor(SpeedoAccessor refAcc);
231
232     // IMPLEMENTATION OF THE PBindingCtrl INTERFACE //
233
// ---------------------------------------------//
234

235     public void bind(PName name) throws PException {
236         pbinding.bind(name);
237     }
238
239     public boolean exist(Object JavaDoc o) throws PException {
240         return pbinding.exist(o);
241     }
242
243     public PName export(Object JavaDoc o) throws PException {
244         return pbinding.export(o);
245     }
246
247     public PName export(Object JavaDoc o, Object JavaDoc o1) throws PException {
248         return pbinding.export(o, o1);
249     }
250
251     public PClassMapping getPClassMapping() {
252         return pbinding.getPClassMapping();
253     }
254
255     public PName getPName() {
256         return pbinding.getPName();
257     }
258
259     public byte getStatus() {
260         return pbinding.getStatus();
261     }
262
263     public void setPName(PName pName) {
264         pbinding.setPName(pName);
265     }
266
267     public void setStatus(byte b) {
268         pbinding.setStatus(b);
269     }
270
271     public void init(PClassMapping mapping) throws PException {
272         pbinding.init(mapping);
273     }
274
275     public void read(Object JavaDoc o, PAccessor accessor) throws PException {
276         pbinding.read(o, accessor);
277     }
278
279     public void read(Object JavaDoc o, PAccessor pAccessor, Object JavaDoc o1) throws PException {
280         pbinding.read(o, pAccessor, o1);
281     }
282
283     public void unbind() throws PException {
284         pbinding.unbind();
285     }
286
287     public void unexport(Object JavaDoc o) throws PException {
288         pbinding.unexport(o);
289     }
290
291     public void write(Object JavaDoc o, PAccessor accessor) throws PException {
292         if (logger != null)
293             logger.log(BasicLevel.DEBUG, "Writing the gen class: " + linkedField);
294         pbinding.write(o, accessor);
295         boolean retainDeltaForMerge = ((SpeedoHome) pbinding.getPClassMapping()).isFieldLockingLevel();
296         GenClassAccessor gca = (GenClassAccessor) accessor;
297         if (retainDeltaForMerge && gca.deltaForMerge == null) {
298             gca.deltaForMerge = new ArrayList JavaDoc();
299         }
300         for(Iterator JavaDoc i = gca.elements.iterator();i.hasNext();) {
301             GenClassElement gce = (GenClassElement) i.next();
302             if (retainDeltaForMerge
303                     && gce.getElemStatus() != PIndexedElem.ELEM_MODIFIED) {
304                 gce.retainStatusForMerge();
305                 gca.deltaForMerge.add(gce);
306             }
307             if (gce.getElemStatus() != PIndexedElem.ELEM_DELETED) {
308                 gce.setStatus(PIndexedElem.ELEM_UNMODIFIED);
309             } else {
310                 // remove the element
311
i.remove();
312             }
313         }
314     }
315
316
317     // IMPLEMENTATION OF THE PersistentCapable INTERFACE //
318
//---------------------------------------------------//
319
// Speedo implements partialy the javax.jdo.spi package. Then only the
320
// minimal method are implemented. The others throw a
321
// JDOUnsupportedOptionException exception.
322

323     public PersistenceManager jdoGetPersistenceManager() {
324         return (jdoIsActive ? getSpeedoHome().getProxyManagerFactory().lookup() : null);
325     }
326
327     public void jdoReplaceFlags() {
328         throw new JDOUnsupportedOptionException();
329     }
330
331     public PersistenceCapable jdoNewInstance(StateManager sm) {
332         throw new JDOUnsupportedOptionException();
333     }
334
335     public PersistenceCapable jdoNewInstance(StateManager sm, Object JavaDoc oid) {
336         throw new JDOUnsupportedOptionException();
337     }
338
339     public Object JavaDoc jdoNewObjectIdInstance(Object JavaDoc arg0) {
340         throw new JDOUnsupportedOptionException();
341     }
342     public Object JavaDoc jdoNewObjectIdInstance() {
343         throw new JDOUnsupportedOptionException();
344     }
345
346     public Object JavaDoc jdoGetObjectId() {
347         return getPName();
348     }
349
350     public Object JavaDoc jdoGetVersion() {
351         return getPName();
352     }
353
354     public Object JavaDoc jdoGetTransactionalObjectId() {
355         return getPName();
356     }
357
358     public void jdoReplaceField(int fieldNumber) {
359         throw new JDOUnsupportedOptionException();
360     }
361
362     public void jdoReplaceFields(int[] fieldNumbers) {
363         throw new JDOUnsupportedOptionException();
364     }
365
366     public void jdoProvideField(int fieldNumber) {
367         throw new JDOUnsupportedOptionException();
368     }
369
370     public void jdoProvideFields(int[] fieldNumbers) {
371         throw new JDOUnsupportedOptionException();
372     }
373
374     public void jdoCopyFields(Object JavaDoc pc, int[] fieldNumbers) {
375         throw new JDOUnsupportedOptionException();
376     }
377
378     public void jdoMakeDirty(String JavaDoc fieldName) {
379         // no fields => does nothing
380
}
381
382     public boolean jdoIsDirty() {
383         SpeedoAccessor sa = getSpeedoAccessor();
384         return sa != null
385                 && !LifeCycle.isTransient(sa.jdoGetStatus())
386                 && LifeCycle.isDirty(sa.jdoGetStatus());
387     }
388
389     public boolean jdoIsDetached() {
390         //TODO: IMPLEMENT JDO 2
391
return false;
392     }
393     public void jdoReplaceObjectId(Object JavaDoc arg0) {
394         //TODO: IMPLEMENT JDO 2
395
}
396     public boolean jdoIsTransactional() {
397         SpeedoAccessor sa = getSpeedoAccessor();
398         return sa != null
399                 && !LifeCycle.isTransient(sa.jdoGetStatus())
400                 && LifeCycle.isTransactional(sa.jdoGetStatus());
401     }
402
403     public boolean jdoIsPersistent() {
404         SpeedoAccessor sa = getSpeedoAccessor();
405         return sa == null || LifeCycle.isPersistent(sa.jdoGetStatus());
406     }
407
408     public boolean jdoIsNew() {
409         SpeedoAccessor sa = getSpeedoAccessor();
410         return sa != null && LifeCycle.isNew(sa.jdoGetStatus());
411     }
412
413     public boolean jdoIsDeleted() {
414         SpeedoAccessor sa = getSpeedoAccessor();
415         return sa != null && LifeCycle.isDeleted(sa.jdoGetStatus());
416     }
417
418     public void jdoReplaceStateManager(StateManager sm) {
419         // nothing to do, No State manager is used
420
}
421
422     public Object JavaDoc jdoNewObjectIdInstance(String JavaDoc s) {
423         return null;
424     }
425
426     public void jdoCopyKeyFieldsToObjectId(Object JavaDoc o) {
427         throw new JDOUnsupportedOptionException();
428     }
429
430     public void jdoCopyKeyFieldsToObjectId(PersistenceCapable.ObjectIdFieldSupplier objectIdFieldSupplier, Object JavaDoc o) {
431         throw new JDOUnsupportedOptionException();
432     }
433
434     public void jdoCopyKeyFieldsFromObjectId(PersistenceCapable.ObjectIdFieldConsumer objectIdFieldConsumer, Object JavaDoc o) {
435         throw new JDOUnsupportedOptionException();
436     }
437
438
439     // IMPLEMENTATION OF THE SpeedoGenClassProxy INTERFACE //
440
//-----------------------------------------------------//
441

442     /**
443      * Assignes the PType of the generic class.
444      * @param _ptype of the generic class
445      */

446     public void jdoSetPType(PType _ptype) {
447         this.ptype = _ptype;
448     }
449
450     /**
451      * @return the PType of the generic class.
452      */

453     public PType jdoGetPType() {
454         return ptype;
455     }
456
457     /**
458      * assignes the PBinding to use to load/store the generic class
459      * Call this method with null value means that the generic is no more
460      * persistent.
461      * @param pb is the PBinding.
462      */

463     public void jdoSetPBinding(PBinding pb) {
464         pbinding = (PBindingCtrl) pb;
465     }
466
467     /**
468      * @return the pbinding associated to the generic class. The value is if the
469      * generic class is not persistent.
470      */

471     public PBinding jdoGetPBinding() {
472         return pbinding;
473     }
474
475     /**
476      * @return a string which represents the fully path of the persistent field
477      * of the class with wich the generic class is persistent.
478      * If the linked fiekf is the 'f1' provided by the class 'com.foo.Bar'
479      * then the returned string will be 'com.foo.Bar/f1'
480      */

481     public String JavaDoc jdoGetGenClassId() {
482         return linkedField;
483     }
484
485     /**
486      * Assignes the linked field name for this generic class.
487      * @param lf is tring which represents the fully path of the persistent
488      * field of the class with wich the generic class is persistent.
489      * If the linked fiekf is the 'f1' provided by the class 'com.foo.Bar'
490      * then the expected string is 'com.foo.Bar/f1'
491      */

492     public void jdoSetLinkedField(String JavaDoc lf) {
493         linkedField = lf;
494     }
495
496     /**
497      * Assignes the new value of the generic class (Collection, Map or Array
498      * or ...).
499      */

500     public void setElements(Object JavaDoc o) {
501         ((GenClassAccessor) getSpeedoHome().writeIntention(this, null)).setElements(o);
502     }
503
504     // IMPLEMENTATION OF THE SpeedoProxy INTERFACE //
505
//---------------------------------------------//
506

507     public void copyAccessor(SpeedoAccessor src, SpeedoAccessor dest) {
508         ((GenClassAccessor) dest).loadFieldsFromAccessor(src);
509     }
510
511     public Object JavaDoc jdoGetPNameHints() {
512         return pnameHints;
513     }
514
515     public boolean jdoIsActive() {
516         return jdoIsActive;
517     }
518
519     public void jdoIsActive(boolean newvalue) {
520         jdoIsActive = newvalue;
521     }
522     public SpeedoAccessor getSpeedoAccessor() {
523         if (!jdoIsActive) {
524             return getReferenceAccessor();
525         }
526         return getSpeedoHome().getSpeedoAccessor(this);
527     }
528
529     public Collection JavaDoc getSpeedoAccessors() {
530         return null;
531     }
532
533     public void removeSpeedoAccessor(SpeedoAccessor sa) {
534     }
535
536     public String JavaDoc jdoGetJdoFileName() {
537         return null;
538     }
539
540     public void setEncodedPName(Object JavaDoc _encodedPName) {
541         this.encodedPName = _encodedPName;
542     }
543
544     public Object JavaDoc getEncodedPName() {
545         return encodedPName;
546     }
547     
548     public SpeedoHome getSpeedoHome() {
549         return (SpeedoHome) getPClassMapping();
550     }
551     
552     // IMPLEMENTATION OF THE CacheEntry INTERFACE //
553
//-------------------------------------------//
554

555     public Object JavaDoc getCeObject() {
556         return this;
557     }
558
559     public Object JavaDoc getCeIdentifier() {
560         return getPName();
561     }
562
563
564     // IMPLEMENTATION OF THE FixableCacheEntry INTERFACE //
565
//---------------------------------------------------//
566

567     public void fixCe() {
568         fix++;
569     }
570
571     public void unfixCe() throws UnFixProtocolException {
572         fix--;
573     }
574
575     public int getCeFixCount() {
576         return fix;
577     }
578
579     // IMPLEMENTATION OF THE ReplaceableCacheEntry INTERFACE //
580
//-------------------------------------------------------//
581

582     public long getCeAge() {
583         return age;
584     }
585
586     public void setCeAge(long l) {
587         age = l;
588     }
589
590     // ------------------------------------------------------------------------
591
// IMPLEMENTATION OF THE Loggable INTERFACE
592
// ------------------------------------------------------------------------
593

594     public void setLogger(Logger _logger) {
595         logger = _logger;
596     }
597
598     public void setLoggerFactory(LoggerFactory loggerfactory) {
599         logger = loggerfactory.getLogger(this.getClass().getName());
600     }
601
602     public Logger getLogger() {
603         return logger;
604     }
605
606     public LoggerFactory getLoggerFactory() {
607         return null;
608     }
609 }
610
Popular Tags