KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > impl > PersistenceManagerWrapper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * PersistenceManagerWrapper.java
26  *
27  * Created on January 16, 2001
28  */

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.impl;
31
32 import com.sun.jdo.api.persistence.support.*;
33 import com.sun.jdo.spi.persistence.utility.I18NHelper;
34
35 import java.util.Collection JavaDoc;
36 import java.util.Properties JavaDoc;
37 import java.util.ResourceBundle JavaDoc;
38
39 /** PersistenceManagerWrapper is ....
40  */

41
42 public class PersistenceManagerWrapper implements PersistenceManager {
43
44     // Previous PersistenceManagerWrapper
45
private PersistenceManagerWrapper prev = null;
46
47     // Actual PersistenceManager
48
private PersistenceManagerImpl pm = null;
49
50     // Boolean flag that allows to use this wrapper
51
private boolean isValid = false;
52
53     /**
54      * I18N message handler
55      */

56     private final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
57             "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", // NOI18N
58
PersistenceManagerWrapper.class.getClassLoader());
59
60     // Constructed by com.sun.jdo.spi.persistence.support.sqlstore.SQLPersistenceManagerFactory
61
PersistenceManagerWrapper(PersistenceManagerImpl pm) {
62         this.pm = pm;
63         prev = pm.getCurrentWrapper();
64         pm.pushCurrentWrapper(this);
65         isValid = true;
66     }
67
68     /** A PersistenceManager instance can be used until it is closed.
69      * @return if this PersistenceManager has been closed
70      * @see #close()
71      */

72     public boolean isClosed() {
73         if (isValid) {
74             return pm.isClosed();
75         } else {
76             return true;
77         }
78     }
79
80     /** A PersistenceManager instance can be used until it is closed.
81      *
82      * <P>This method closes the PersistenceManager, which if pooled, releases it
83      * to the pool of available PersistenceManagers.
84      */

85     public void close() {
86         if (isValid) {
87             isValid = false;
88             pm.popCurrentWrapper(prev);
89             pm = null;
90             prev = null;
91         } else {
92             throw new JDOUserException(I18NHelper.getMessage(messages,
93                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
94
}
95     }
96
97     /** There is exactly one Transaction associated with a PersistenceManager.
98      * @return the Transaction associated with this
99      * PersistenceManager.
100      */

101     public Transaction currentTransaction() {
102         if (isValid) {
103             return pm.currentTransaction();
104         } else {
105             throw new JDOUserException(I18NHelper.getMessage(messages,
106                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
107
}
108     }
109
110     /** Create a new Query with no elements.
111      * @return a new Query instance with no elements.
112      */

113     public Query newQuery() {
114         if (isValid) {
115             return pm.newQuery();
116         } else {
117             throw new JDOUserException(I18NHelper.getMessage(messages,
118                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
119
}
120     }
121
122     /** Create a new Query using elements from another Query. The other Query
123      * must have been created by the same JDO implementation. It might be active
124      * in a different PersistenceManager or might have been serialized and
125      * restored.
126      * @return the new Query
127      * @param compiled another Query from the same JDO implementation
128      */

129     public Query newQuery(Object JavaDoc compiled) {
130         if (isValid) {
131             return pm.newQuery(compiled);
132         } else {
133             throw new JDOUserException(I18NHelper.getMessage(messages,
134                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
135
}
136     }
137
138     /** Create a new Query specifying the Class of the results.
139      * @param cls the Class of the results
140      * @return the new Query
141      */

142     public Query newQuery(Class JavaDoc cls) {
143         if (isValid) {
144             return pm.newQuery(cls);
145         } else {
146             throw new JDOUserException(I18NHelper.getMessage(messages,
147                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
148
}
149     }
150
151     /** Create a new Query with the Class of the results and candidate Collection.
152      * specified.
153      * @param cls the Class of results
154      * @param cln the Collection of candidate instances
155      * @return the new Query
156      */

157     public Query newQuery(Class JavaDoc cls, Collection JavaDoc cln) {
158         if (isValid) {
159             return pm.newQuery(cls, cln);
160         } else {
161             throw new JDOUserException(I18NHelper.getMessage(messages,
162                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
163
}
164     }
165
166     /** Create a new Query with the Class of the results and Filter.
167      * specified.
168      * @param cls the Class of results
169      * @param filter the Filter for candidate instances
170      * @return the new Query
171      */

172     public Query newQuery(Class JavaDoc cls, String JavaDoc filter) {
173         if (isValid) {
174             return pm.newQuery(cls, filter);
175         } else {
176             throw new JDOUserException(I18NHelper.getMessage(messages,
177                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
178
}
179     }
180
181     /** Create a new Query with the Class of the results, candidate Collection,
182      * and Filter.
183      * @param cls the Class of results
184      * @param cln the Collection of candidate instances
185      * @param filter the Filter for candidate instances
186      * @return the new Query
187      */

188     public Query newQuery(Class JavaDoc cls, Collection JavaDoc cln, String JavaDoc filter) {
189         if (isValid) {
190             return pm.newQuery(cls, cln, filter);
191         } else {
192             throw new JDOUserException(I18NHelper.getMessage(messages,
193                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
194
}
195     }
196
197     /** The PersistenceManager may manage a collection of instances in the data
198      * store based on the class of the instances. This method returns a
199      * Collection of instances in the data store that might be iterated or
200      * given to a Query as the Collection of candidate instances.
201      * @param persistenceCapableClass Class of instances
202      * @param subclasses whether to include instances of subclasses
203      * @return a Collection of instances
204      * @see Query
205      */

206     public Collection JavaDoc getExtent(Class JavaDoc persistenceCapableClass, boolean subclasses) {
207         if (isValid) {
208             return pm.getExtent(persistenceCapableClass, subclasses);
209         } else {
210             throw new JDOUserException(I18NHelper.getMessage(messages,
211                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
212
}
213     }
214
215     /** This method locates a persistent instance in the cache of instances
216      * managed by this PersistenceManager. If an instance with the same ObjectId
217      * is found it is returned. Otherwise, a new instance is created and
218      * associated with the ObjectId.
219      *
220      * <P>If the instance does not exist in the data store, then this method will
221      * not fail. However, a request to access fields of the instance will
222      * throw an exception.
223      * @param oid an ObjectId
224      * @return the PersistenceCapable instance with the specified
225      * ObjectId
226      */

227     public Object JavaDoc getObjectById(Object JavaDoc oid) {
228         if (isValid) {
229             return pm.getObjectById(oid);
230         } else {
231             throw new JDOUserException(I18NHelper.getMessage(messages,
232                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
233
}
234     }
235
236     /** This method locates a persistent instance in the cache of instances
237      * managed by this <code>PersistenceManager</code>.
238      * The <code>getObjectById</code> method attempts
239      * to find an instance in the cache with the specified JDO identity.
240      * The <code>oid</code> parameter object might have been returned by an earlier call
241      * to <code>getObjectId</code> or might have been constructed by the application.
242      * <P>If the <code>PersistenceManager</code> is unable to resolve the <code>oid</code> parameter
243      * to an ObjectId instance, then it throws a <code>JDOUserException</code>.
244      * <P>If the <code>validate</code> flag is <code>false</code>, and there is already an instance in the
245      * cache with the same JDO identity as the <code>oid</code> parameter, then this method
246      * returns it. There is no change made to the state of the returned
247      * instance.
248      * <P>If there is not an instance already in the cache with the same JDO
249      * identity as the <code>oid</code> parameter, then this method creates an instance
250      * with the specified JDO identity and returns it. If there is no
251      * transaction in progress, the returned instance will be hollow or
252      * persistent-nontransactional, at the choice of the implementation.
253      * <P>If there is a transaction in progress, the returned instance will
254      * be hollow, persistent-nontransactional, or persistent-clean, at the
255      * choice of the implementation.
256      * @see #getObjectId(Object pc)
257      * @return the <code>PersistenceCapable</code> instance with the specified ObjectId
258      * @param oid an ObjectId
259      * @param validate if the existence of the instance is to be validated
260      */

261     public Object JavaDoc getObjectById (Object JavaDoc oid, boolean validate) {
262         if (isValid) {
263             return pm.getObjectById(oid, validate);
264         } else {
265             throw new JDOUserException(I18NHelper.getMessage(messages,
266                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
267
}
268     }
269
270     /** The ObjectId returned by this method represents the JDO identity of
271      * the instance. The ObjectId is a copy (clone) of the internal state
272      * of the instance, and changing it does not affect the JDO identity of
273      * the instance.
274      * @param pc the PersistenceCapable instance
275      * @return the ObjectId of the instance
276      */

277     public Object JavaDoc getObjectId(Object JavaDoc pc) {
278         if (isValid) {
279             return pm.getObjectId(pc);
280         } else {
281             throw new JDOUserException(I18NHelper.getMessage(messages,
282                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
283
}
284     }
285
286     /** This method is used to get a PersistenceCapable instance
287      * representing the same data store object as the parameter, that is valid
288      * for this PersistenceManager.
289      * @param pc a PersistenceCapable instance
290      * @return the PersistenceCapable instance representing the
291      * same data store object
292      */

293     public Object JavaDoc getTransactionalInstance(Object JavaDoc pc) {
294         if (isValid) {
295             return pm.getTransactionalInstance(pc);
296         } else {
297             throw new JDOUserException(I18NHelper.getMessage(messages,
298                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
299
}
300     }
301
302     /** Make the transient instance persistent in this PersistenceManager.
303      * This method must be called in an active transaction.
304      * The PersistenceManager assigns an ObjectId to the instance and
305      * transitions it to persistent-new.
306      * The instance will be managed in the Extent associated with its Class.
307      * The instance will be put into the data store at commit.
308      * @param pc a transient instance of a Class that implements
309      * PersistenceCapable
310      */

311     public void makePersistent(Object JavaDoc pc) {
312         if (isValid) {
313             pm.makePersistent(pc);
314         } else {
315             throw new JDOUserException(I18NHelper.getMessage(messages,
316                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
317
}
318     }
319
320     /** Make an array of instances persistent.
321      * @param pcs an array of transient instances
322      * @see #makePersistent(Object pc)
323      */

324     public void makePersistent(Object JavaDoc[] pcs) {
325         if (isValid) {
326             pm.makePersistent(pcs);
327         } else {
328             throw new JDOUserException(I18NHelper.getMessage(messages,
329                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
330
}
331     }
332
333     /** Make a Collection of instances persistent.
334      * @param pcs a Collection of transient instances
335      * @see #makePersistent(Object pc)
336      */

337     public void makePersistent(Collection JavaDoc pcs) {
338         if (isValid) {
339             pm.makePersistent(pcs);
340         } else {
341             throw new JDOUserException(I18NHelper.getMessage(messages,
342                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
343
}
344     }
345
346     /** Delete the persistent instance from the data store.
347      * This method must be called in an active transaction.
348      * The data store object will be removed at commit.
349      * Unlike makePersistent, which makes the closure of the instance persistent,
350      * the closure of the instance is not deleted from the data store.
351      * This method has no effect if the instance is already deleted in the
352      * current transaction.
353      * This method throws an exception if the instance is transient or is managed by another
354      * PersistenceManager.
355      *
356      * @param pc a persistent instance
357      */

358     public void deletePersistent(Object JavaDoc pc) {
359         if (isValid) {
360             pm.deletePersistent(pc);
361         } else {
362             throw new JDOUserException(I18NHelper.getMessage(messages,
363                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
364
}
365     }
366
367     /** Delete an array of instances from the data store.
368      * @param pcs a Collection of persistent instances
369      * @see #deletePersistent(Object pc)
370      */

371     public void deletePersistent(Object JavaDoc[] pcs) {
372         if (isValid) {
373             pm.deletePersistent(pcs);
374         } else {
375             throw new JDOUserException(I18NHelper.getMessage(messages,
376                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
377
}
378     }
379
380     /** Delete a Collection of instances from the data store.
381      * @param pcs a Collection of persistent instances
382      * @see #deletePersistent(Object pc)
383      */

384     public void deletePersistent(Collection JavaDoc pcs) {
385         if (isValid) {
386             pm.deletePersistent(pcs);
387         } else {
388             throw new JDOUserException(I18NHelper.getMessage(messages,
389                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
390
}
391     }
392
393     /** This method returns the PersistenceManagerFactory used to create
394      * this PersistenceManager. It returns null if this instance was
395      * created via a constructor.
396      * @return the PersistenceManagerFactory that created
397      * this PersistenceManager
398      */

399     public PersistenceManagerFactory getPersistenceManagerFactory() {
400         if (isValid) {
401             return pm.getPersistenceManagerFactory();
402         } else {
403             throw new JDOUserException(I18NHelper.getMessage(messages,
404                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
405
}
406     }
407
408     /** The application can manage the PersistenceManager instances
409      * more easily by having an application object associated with each
410      * PersistenceManager instance.
411      * @param o the user instance to be remembered by the PersistenceManager
412      * @see #getUserObject
413      */

414     public void setUserObject(Object JavaDoc o) {
415         if (isValid) {
416             pm.setUserObject(o);
417         } else {
418             throw new JDOUserException(I18NHelper.getMessage(messages,
419                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
420
}
421     }
422
423     /** The application can manage the PersistenceManager instances
424      * more easily by having an application object associated with each
425      * PersistenceManager instance.
426      * @return the user object associated with this PersistenceManager
427      * @see #setUserObject
428      */

429     public Object JavaDoc getUserObject() {
430         if (isValid) {
431             return pm.getUserObject();
432         } else {
433             throw new JDOUserException(I18NHelper.getMessage(messages,
434                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
435
}
436     }
437
438     /** The JDO vendor might store certain non-operational properties and
439      * make those properties available to applications (for troubleshooting).
440      *
441      * <P>Standard properties include:
442      * <li>VendorName</li>
443      * <li>VersionNumber</li>
444      * @return the Properties of this PersistenceManager
445      */

446     public Properties JavaDoc getProperties() {
447         if (isValid) {
448             return pm.getProperties();
449         } else {
450             throw new JDOUserException(I18NHelper.getMessage(messages,
451                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
452
}
453     }
454
455     /**
456      * Returns the boolean value of the supersedeDeletedInstance flag
457      * for this PersistenceManager. If set to true, deleted instances are
458      * allowed to be replaced with persistent-new instances with the equal
459      * Object Id.
460      * @return boolean supersedeDeletedInstance flag
461      */

462     public boolean getSupersedeDeletedInstance () {
463         if (isValid) {
464             return pm.getSupersedeDeletedInstance();
465         } else {
466             throw new JDOUserException(I18NHelper.getMessage(messages,
467                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
468
}
469     }
470
471
472     /**
473      * Sets the supersedeDeletedInstance flag for this PersistenceManager.
474      * @param flag boolean supersedeDeletedInstance flag
475      */

476     public void setSupersedeDeletedInstance (boolean flag) {
477         if (isValid) {
478             pm.setSupersedeDeletedInstance(flag);
479         } else {
480             throw new JDOUserException(I18NHelper.getMessage(messages,
481                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
482
}
483     }
484
485     /**
486      * Returns the boolean value of the requireCopyObjectId flag
487      * for this PersistenceManager. If set to false, the PersistenceManager
488      * does not create a copy of an ObjectId for <code>PersistenceManager.getObjectId(Object pc)</code>
489      * and <code>PersistenceManager.getObjectById(Object oid)</code> requests.
490      *
491      * @see PersistenceManager#getObjectId(Object pc)
492      * @see PersistenceManager#getObjectById(Object oid)
493      * @return boolean requireCopyObjectId flag
494      */

495     public boolean getRequireCopyObjectId() {
496         if (isValid) {
497             return pm.getRequireCopyObjectId();
498         } else {
499             throw new JDOUserException(I18NHelper.getMessage(messages,
500                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
501
}
502     }
503   
504   
505     /**
506      * Sets the requireCopyObjectId flag for this PersistenceManager.
507      * If set to false, the PersistenceManager will not create a copy of
508      * an ObjectId for <code>PersistenceManager.getObjectId(Object pc)</code>
509      * and <code>PersistenceManager.getObjectById(Object oid)</code> requests.
510      *
511      * @see PersistenceManager#getObjectId(Object pc)
512      * @see PersistenceManager#getObjectById(Object oid)
513      * @param flag boolean requireCopyObjectId flag
514      */

515     public void setRequireCopyObjectId (boolean flag) {
516         if (isValid) {
517             pm.setRequireCopyObjectId(flag);
518         } else {
519             throw new JDOUserException(I18NHelper.getMessage(messages,
520                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
521
}
522     }
523
524     /**
525      * Returns the boolean value of the requireTrackedSCO flag
526      * for this PersistenceManager. If set to false, the PersistenceManager
527      * will not create tracked SCO instances for
528      * new persistent instances at commit with retainValues set to true
529      * and while retrieving data from a datastore.
530      *
531      * @return boolean requireTrackedSCO flag
532      */

533     public boolean getRequireTrackedSCO() {
534         if (isValid) {
535             return pm.getRequireTrackedSCO();
536         } else {
537             throw new JDOUserException(I18NHelper.getMessage(messages,
538                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
539
}
540     }
541
542     /**
543      * Sets the requireTrackedSCO flag for this PersistenceManager.
544      * If set to false, the PersistenceManager will not create tracked
545      * SCO instances for new persistent instances at commit with retainValues
546      * set to true and while retrieving data from a datastore.
547      *
548      * @param flag boolean requireTrackedSCO flag
549      */

550     public void setRequireTrackedSCO (boolean flag) {
551         if (isValid) {
552             pm.setRequireTrackedSCO(flag);
553         } else {
554             throw new JDOUserException(I18NHelper.getMessage(messages,
555                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
556
}
557     }
558
559
560     /** In order for the application to construct instance of the ObjectId class
561      * it needs to know the class being used by the JDO implementation.
562      * @param cls the PersistenceCapable Class
563      * @return the Class of the ObjectId of the parameter
564      */

565     public Class JavaDoc getObjectIdClass(Class JavaDoc cls) {
566         if (isValid) {
567             return pm.getObjectIdClass(cls);
568         } else {
569             throw new JDOUserException(I18NHelper.getMessage(messages,
570                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
571
}
572     }
573
574
575     /**
576      * Returns a new Second Class Object instance of the type specified,
577      * with the owner and field name to notify upon changes to the value
578      * of any of its fields. If a collection class is created, then the
579      * class does not restrict the element types, and allows nulls to be added as elements.
580      *
581      * @param type Class of the new SCO instance
582      * @param owner the owner to notify upon changes
583      * @param fieldName the field to notify upon changes
584      * @return the object of the class type
585      */

586     public Object JavaDoc newSCOInstance(Class JavaDoc type, Object JavaDoc owner, String JavaDoc fieldName) {
587         if (isValid) {
588             return pm.newSCOInstance(type, owner, fieldName);
589         } else {
590             throw new JDOUserException(I18NHelper.getMessage(messages,
591                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
592
}
593     }
594
595
596     /**
597      * Returns a new Collection instance of the type specified, with the
598      * owner and field name to notify upon changes to the value of any of its fields.
599      * The collection class restricts the element types allowed to the elementType or
600      * instances assignable to the elementType, and allows nulls to be added as
601      * elements based on the setting of allowNulls. The Collection has an initial size
602      * as specified by the initialSize parameter.
603      *
604      * @param type Class of the new SCO instance
605      * @param owner the owner to notify upon changes
606      * @param fieldName the field to notify upon changes
607      * @param elementType the element types allowed
608      * @param allowNulls true if allowed
609      * @param initialSize initial size of the Collection
610      * @return the object of the class type
611      */

612     public Object JavaDoc newCollectionInstance(Class JavaDoc type, Object JavaDoc owner, String JavaDoc fieldName,
613                                         Class JavaDoc elementType, boolean allowNulls, int initialSize) {
614         if (isValid) {
615             return pm.newCollectionInstance(type, owner, fieldName, elementType, allowNulls, initialSize);
616         } else {
617             throw new JDOUserException(I18NHelper.getMessage(messages,
618                     "jdo.persistencemanagerwrapper.invalidpm"));// NOI18N
619
}
620     }
621
622
623     public PersistenceManager getPersistenceManager() {
624         return (PersistenceManager) pm;
625     }
626 }
627
Popular Tags