KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > support > JDOHelper


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  * JDOHelper.java
26  *
27  * Created on April 18, 2000
28  */

29  
30 package com.sun.jdo.api.persistence.support;
31
32 import java.util.ResourceBundle JavaDoc;
33 import java.util.Collection JavaDoc;
34
35 import com.sun.jdo.spi.persistence.utility.I18NHelper;
36
37 /**
38  * @author Martin Zaun
39  * @version 0.1
40  */

41
42 /**
43  * An utility class for querying <code>PersistenceCapable</code> objects.
44  */

45 public class JDOHelper
46 {
47     /**
48      * Returns the associated PersistenceManager of an object if there is one.
49      *
50      * For transactional and persistent objects, their associated
51      * <code>PersistenceManager</code> is returned.
52      * For transient objects, <code>null</code> is returned.
53      *
54      * @param obj an <code>Object</code>
55      * @return the PersistenceManager associated with the object;
56      * <code>null</code> otherwise
57      * @see PersistenceCapable#jdoGetPersistenceManager()
58      */

59     static public PersistenceManager getPersistenceManager(Object JavaDoc obj)
60     {
61         if (obj instanceof PersistenceCapable)
62             return ((PersistenceCapable)obj).jdoGetPersistenceManager();
63         return null;
64     }
65     
66     /**
67      * Explicitly marks a field of an object as dirty if the object is
68      * persistent and transactional.
69      *
70      * Normally, PersistenceCapable classes are able to detect changes made
71      * to their fields. However, if a reference to an Array is given to a
72      * method outside the class, and the Array is modified, then the
73      * persistent object is not aware of the change. This method allows the
74      * application to notify the object that a change was made to a field.
75      * For transient objects, this method does nothing.
76      *
77      * @param obj an object
78      * @param fieldName the name of the object's field to be marked
79      * dirty
80      * @see PersistenceCapable#jdoMakeDirty(String fieldName)
81      */

82     static public void makeDirty(Object JavaDoc obj, String JavaDoc fieldName)
83     {
84         if (obj instanceof PersistenceCapable)
85             ((PersistenceCapable)obj).jdoMakeDirty(fieldName);
86     }
87     
88     /**
89      * Returns a copy of the JDO identity associated with an object.
90      *
91      * Persistent objects of PersistenceCapable classes have a JDO identity
92      * managed by the PersistenceManager. This method returns a copy of the
93      * ObjectId that represents the JDO identity of a persistent object.
94      * For transient objects, <code>null</code> is returned.
95      *<P>
96      * The ObjectId may be serialized and later restored, and used with
97      * a PersistenceManager from the same JDO implementation to locate a
98      * persistent object with the same data store identity.
99      * If the JDO identity is managed by the application, then the
100      * ObjectId may be used with a PersistenceManager from any JDO
101      * implementation that supports the PersistenceCapable class.
102      * If the JDO identity is not managed by the application or the
103      * data store, then the ObjectId returned is only valid within the
104      * current transaction.
105      *
106      * @param obj an <code>Object</code>
107      * @return a copy of the ObjectId of a persistent object;
108      * <code>null</code> if the object is transient
109      * @see PersistenceCapable#jdoGetObjectId()
110      * @see PersistenceManager#getObjectId(Object obj)
111      * @see PersistenceManager#getObjectById(Object oid)
112      */

113     static public Object JavaDoc getObjectId(Object JavaDoc obj)
114     {
115         if (obj instanceof PersistenceCapable)
116             return ((PersistenceCapable)obj).jdoGetObjectId();
117         return null;
118     }
119     
120     /**
121      * Tests whether an object is dirty.
122      *
123      * If the object have been modified, deleted, or newly
124      * made persistent in the current transaction, <code>true</code> is returned.
125      * For transient objects, <code>false</code> is returned.
126      *
127      * @param obj an <code>Object</code>
128      * @return <code>true</code> if the object has been modified
129      * in the current transaction; <code>false</code>
130      * otherwise.
131      * @see PersistenceCapable#jdoIsDirty()
132      * @see PersistenceCapable#jdoMakeDirty(String fieldName)
133      */

134     static public boolean isDirty(Object JavaDoc obj)
135     {
136         if (obj instanceof PersistenceCapable)
137             return ((PersistenceCapable)obj).jdoIsDirty();
138         return false;
139     }
140
141     /**
142      * Tests whether an object is transactional.
143      *
144      * For objects that respect transaction boundaries, <code>true</code> is
145      * returned.
146      * These objects include transient objects made transactional as a
147      * result of being the target of a makeTransactional method call; newly
148      * made persistent or deleted persistent objects; persistent objects
149      * read in data store transactions; and persistent objects modified in
150      * optimistic transactions.
151      * For non-transactional objects, <code>false</code> is returned.
152      *
153      * @param obj an <code>Object</code>
154      * @return <code>true</code> if the object is transactional;
155      * <code>false</code> otherwise.
156      * @see PersistenceCapable#jdoIsTransactional()
157      */

158     static public boolean isTransactional(Object JavaDoc obj)
159     {
160         if (obj instanceof PersistenceCapable)
161             return ((PersistenceCapable)obj).jdoIsTransactional();
162         return false;
163     }
164
165     /**
166      * Tests whether an object is persistent.
167      *
168      * For objects whose state is stored in the data store, <code>true</code>
169      * is returned.
170      * For transient objects, <code>false</code> is returned.
171      *
172      * @param obj an <code>Object</code>
173      * @return <code>true</code> if the object is persistent;
174      * <code>false</code> otherwise.
175      * @see PersistenceCapable#jdoIsPersistent()
176      * @see PersistenceManager#makePersistent(Object obj)
177      */

178     static public boolean isPersistent(Object JavaDoc obj)
179     {
180         if (obj instanceof PersistenceCapable)
181             return ((PersistenceCapable)obj).jdoIsPersistent();
182         return false;
183     }
184
185     /**
186      * Tests whether the object has been newly made persistent.
187      *
188      * For objects that have been made persistent in the current transaction,
189      * <code>true</code> is returned.
190      * For transient or objects, <code>false</code> is returned.
191      *
192      * @param obj an <code>Object</code>
193      * @return <code>true</code> if the object was made persistent
194      * in the current transaction;
195      * <code>false</code> otherwise.
196      * @see PersistenceCapable#jdoIsNew()
197      * @see PersistenceManager#makePersistent(Object obj)
198      */

199     static public boolean isNew(Object JavaDoc obj)
200     {
201         if (obj instanceof PersistenceCapable)
202             return ((PersistenceCapable)obj).jdoIsNew();
203         return false;
204     }
205
206     /**
207      * Tests whether the object has been deleted.
208      *
209      * For objects that have been deleted in the current transaction,
210      * <code>true</code> is returned.
211      * For transient objects, <code>false</code> is returned.
212      *
213      * @param obj an <code>Object</code>
214      * @return <code>true</code> if the object was deleted in the
215      * current transaction;
216      * <code>false</code> otherwise.
217      * @see PersistenceCapable#jdoIsDeleted()
218      * @see PersistenceManager#deletePersistent(Object obj)
219      */

220     static public boolean isDeleted(Object JavaDoc obj)
221     {
222         if (obj instanceof PersistenceCapable)
223             return ((PersistenceCapable)obj).jdoIsDeleted();
224         return false;
225     }
226
227     /**
228      * I18N message handler
229      */

230     private final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
231                                 "com.sun.jdo.spi.persistence.support.sqlstore.impl.Bundle", // NOI18N
232
JDOHelper.class.getClassLoader());
233
234     /**
235      * Help string
236      */

237     static final String JavaDoc null_instance = "null"; //NOI18N
238

239     /**
240      * Prints the object.
241      * If object is not NULL and is not deleted, calls toString() method
242      * on the object.
243      * Does not allow to access fields of the deleted object
244      */

245     static public String JavaDoc printObject(Object JavaDoc o) {
246         if (o==null)
247                 return null_instance;
248         else if (isDeleted(o))
249                 return I18NHelper.getMessage(messages, "jdohelper.deleted_instance", //NOI18N
250
o.getClass().getName());
251         else
252                 return o.toString();
253   }
254
255    /** Returns the class loader for the class of the object.
256    * If object is an instance of the java.util.Collection or
257    * an Array it is recursively checked for the class loader
258    * of its elements.
259    *
260    * @param obj the object to get the class loader for
261    * @return the class loader that loaded the class or interface
262    * represented by this object.
263    */

264    static private ClassLoader JavaDoc getObjectClassLoader(Object JavaDoc obj) {
265     Class JavaDoc clazz = obj.getClass();
266
267     if (obj instanceof Collection JavaDoc) {
268         return getCollectionClassLoader((Collection JavaDoc)obj);
269     } else if (clazz.isArray()) {
270         return getArrayClassLoader((Object JavaDoc[])obj);
271     } else {
272         return clazz.getClassLoader();
273     }
274    }
275
276    /** Returns the class loader for the elements of the collection.
277    * If element is itself an instance of the java.util.Collection or
278    * an Array it is recursively checked for its class loader.
279    *
280    * @param col collection of objects to get the class loader for
281    * @return the class loader that loaded the class or interface
282    * represented by its elements.
283    */

284    static private ClassLoader JavaDoc getCollectionClassLoader (Collection JavaDoc col) {
285     Object JavaDoc[] arr = col.toArray();
286     return getArrayClassLoader(arr);
287    }
288
289    /** Returns the first not null class loader for the elements of the
290    * object array.
291    * If element is itself an instance of the java.util.Collection or
292    * an Array it is recursively checked for its class loader.
293    *
294    * @param arr array of objects to get the class loader for
295    * @return the class loader that loaded the class or interface
296    * represented by its elements.
297    */

298    static private ClassLoader JavaDoc getArrayClassLoader (Object JavaDoc[] arr) {
299     ClassLoader JavaDoc cl = null;
300
301     for (int i = 0; i < arr.length; i++) {
302         cl = getObjectClassLoader(arr[i]);
303         if (cl != null) {
304         break;
305         }
306     }
307     return cl;
308    }
309 }
310
Popular Tags