KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jdo > JDOHelper


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 /*
18  * JDOHelper.java
19  *
20  */

21  
22 package javax.jdo;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 import java.lang.reflect.Method JavaDoc;
31 import java.lang.reflect.InvocationTargetException JavaDoc;
32
33 import java.util.Map JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 import java.security.AccessController JavaDoc;
37 import java.security.PrivilegedAction JavaDoc;
38
39 import javax.jdo.spi.I18NHelper;
40 import javax.jdo.spi.PersistenceCapable;
41 import javax.jdo.spi.StateManager; // for javadoc
42

43 import javax.naming.Context JavaDoc;
44 import javax.naming.InitialContext JavaDoc;
45 import javax.naming.NamingException JavaDoc;
46
47 import javax.rmi.PortableRemoteObject JavaDoc;
48
49
50 /**
51  * This class can be used by a JDO-aware application to call the JDO behavior
52  * of <code>PersistenceCapable</code> instances without declaring them to be
53  * <code>PersistenceCapable</code>.
54  * <P>It is also used to acquire a <code>PersistenceManagerFactory</code> via
55  * various methods.
56  * <P>This helper class defines static methods that allow a JDO-aware
57  * application to examine the runtime state of instances. For example,
58  * an application can discover whether the instance is persistent, transactional,
59  * dirty, new, deleted, or detached; and to get its associated
60  * <code>PersistenceManager</code> if it has one.
61  *
62  * @version 2.0
63  */

64 public class JDOHelper extends Object JavaDoc {
65       
66     /** The Internationalization message helper.
67      */

68     private final static I18NHelper msg = I18NHelper.getInstance ("javax.jdo.Bundle"); //NOI18N
69

70     /** Return the associated <code>PersistenceManager</code> if there is one.
71      * Transactional and persistent instances return the associated
72      * <code>PersistenceManager</code>.
73      *
74      * <P>Transient non-transactional instances and instances of classes
75      * that do not implement <code>PersistenceCapable</code> return <code>null</code>.
76      * @see PersistenceCapable#jdoGetPersistenceManager()
77      * @param pc the <code>PersistenceCapable</code> instance.
78      * @return the <code>PersistenceManager</code> associated with the parameter instance.
79      */

80      public static PersistenceManager getPersistenceManager(Object JavaDoc pc) {
81         return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoGetPersistenceManager():null;
82       }
83     
84     /** Explicitly mark the parameter instance and field dirty.
85      * Normally, <code>PersistenceCapable</code> classes are able to detect changes made
86      * to their fields. However, if a reference to an array is given to a
87      * method outside the class, and the array is modified, then the
88      * persistent instance is not aware of the change. This API allows the
89      * application to notify the instance that a change was made to a field.
90      *
91      * <P>Transient instances and instances of classes
92      * that do not implement <code>PersistenceCapable</code> ignore this method.
93      * @see PersistenceCapable#jdoMakeDirty(String fieldName)
94      * @param pc the <code>PersistenceCapable</code> instance.
95      * @param fieldName the name of the field to be marked dirty.
96      */

97     public static void makeDirty(Object JavaDoc pc, String JavaDoc fieldName) {
98      if (pc instanceof PersistenceCapable)
99       ((PersistenceCapable)pc).jdoMakeDirty(fieldName);
100     }
101     
102     /** Return a copy of the JDO identity associated with the parameter instance.
103      *
104      * <P>Persistent instances of <code>PersistenceCapable</code> classes have a JDO identity
105      * managed by the <code>PersistenceManager</code>. This method returns a copy of the
106      * ObjectId that represents the JDO identity.
107      *
108      * <P>Transient instances and instances of classes
109      * that do not implement <code>PersistenceCapable</code> return <code>null</code>.
110      *
111      * <P>The ObjectId may be serialized
112      * and later restored, and used with a <code>PersistenceManager</code> from the same JDO
113      * implementation to locate a persistent instance with the same data store
114      * identity.
115      *
116      * <P>If the JDO identity is managed by the application, then the ObjectId may
117      * be used with a <code>PersistenceManager</code> from any JDO implementation that supports
118      * the <code>PersistenceCapable</code> class.
119      *
120      * <P>If the JDO identity is not managed by the application or the data store,
121      * then the ObjectId returned is only valid within the current transaction.
122      *<P>
123      * @see PersistenceManager#getObjectId(Object pc)
124      * @see PersistenceCapable#jdoGetObjectId()
125      * @see PersistenceManager#getObjectById(Object oid, boolean validate)
126      * @param pc the PersistenceCapable instance.
127      * @return a copy of the ObjectId of the parameter instance as of the beginning of the transaction.
128      */

129     public static Object JavaDoc getObjectId(Object JavaDoc pc) {
130       return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoGetObjectId():null;
131     }
132     
133     /** Return a copy of the JDO identity associated with the parameter instance.
134      *
135      * @see PersistenceCapable#jdoGetTransactionalObjectId()
136      * @see PersistenceManager#getObjectById(Object oid, boolean validate)
137      * @param pc the <code>PersistenceCapable</code> instance.
138      * @return a copy of the ObjectId of the parameter instance as modified in this transaction.
139      */

140     public static Object JavaDoc getTransactionalObjectId(Object JavaDoc pc) {
141       return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoGetTransactionalObjectId():null;
142     }
143     
144     /**
145      * Return the version of the instance.
146      * @since 2.0
147      * @param pc the instance
148      * @return the version of the instance
149      */

150     public static Object JavaDoc getVersion (Object JavaDoc pc) {
151       return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoGetVersion():null;
152     }
153     /** Tests whether the parameter instance is dirty.
154      *
155      * Instances that have been modified, deleted, or newly
156      * made persistent in the current transaction return <code>true</code>.
157      *
158      *<P>Transient instances and instances of classes
159      * that do not implement <code>PersistenceCapable</code> return <code>false</code>.
160      *<P>
161      * @see StateManager#makeDirty(PersistenceCapable pc, String fieldName)
162      * @see PersistenceCapable#jdoIsDirty()
163      * @param pc the <code>PersistenceCapable</code> instance.
164      * @return <code>true</code> if the parameter instance has been modified in the current transaction.
165      */

166     public static boolean isDirty(Object JavaDoc pc) {
167       return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsDirty():false;
168     }
169
170     /** Tests whether the parameter instance is transactional.
171      *
172      * Instances whose state is associated with the current transaction
173      * return true.
174      *
175      *<P>Transient instances and instances of classes
176      * that do not implement <code>PersistenceCapable</code> return <code>false</code>.
177      * @see PersistenceCapable#jdoIsTransactional()
178      * @param pc the <code>PersistenceCapable</code> instance.
179      * @return <code>true</code> if the parameter instance is transactional.
180      */

181     public static boolean isTransactional(Object JavaDoc pc) {
182       return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsTransactional():false;
183     }
184
185     /** Tests whether the parameter instance is persistent.
186      *
187      * Instances that represent persistent objects in the data store
188      * return <code>true</code>.
189      *
190      *<P>Transient instances and instances of classes
191      * that do not implement <code>PersistenceCapable</code> return <code>false</code>.
192      *<P>
193      * @see PersistenceManager#makePersistent(Object pc)
194      * @see PersistenceCapable#jdoIsPersistent()
195      * @param pc the <code>PersistenceCapable</code> instance.
196      * @return <code>true</code> if the parameter instance is persistent.
197      */

198     public static boolean isPersistent(Object JavaDoc pc) {
199       return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsPersistent():false;
200     }
201
202     /** Tests whether the parameter instance has been newly made persistent.
203      *
204      * Instances that have been made persistent in the current transaction
205      * return <code>true</code>.
206      *
207      *<P>Transient instances and instances of classes
208      * that do not implement <code>PersistenceCapable</code> return <code>false</code>.
209      *<P>
210      * @see PersistenceManager#makePersistent(Object pc)
211      * @see PersistenceCapable#jdoIsNew()
212      * @param pc the <code>PersistenceCapable</code> instance.
213      * @return <code>true</code> if the parameter instance was made persistent
214      * in the current transaction.
215      */

216     public static boolean isNew(Object JavaDoc pc) {
217       return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsNew():false;
218     }
219
220     /** Tests whether the parameter instance has been deleted.
221      *
222      * Instances that have been deleted in the current transaction return <code>true</code>.
223      *
224      *<P>Transient instances and instances of classes
225      * that do not implement <code>PersistenceCapable</code> return <code>false</code>.
226      *<P>
227      * @see PersistenceManager#deletePersistent(Object pc)
228      * @see PersistenceCapable#jdoIsDeleted()
229      * @param pc the <code>PersistenceCapable</code> instance.
230      * @return <code>true</code> if the parameter instance was deleted
231      * in the current transaction.
232      */

233     public static boolean isDeleted(Object JavaDoc pc) {
234       return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsDeleted():false;
235     }
236     
237     /**
238      * Tests whether the parameter instance has been detached.
239      *
240      * Instances that have been detached return true.
241      *
242      * <P>Transient instances return false.
243      * <P>
244      * @see PersistenceCapable#jdoIsDetached()
245      * @return <code>true</code> if this instance is detached.
246      * @since 2.0
247      * @param pc the instance
248      */

249     public static boolean isDetached(Object JavaDoc pc) {
250         return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsDetached():false;
251     }
252     
253     /** Get a <code>PersistenceManagerFactory</code> based on a <code>Properties</code> instance, using
254      * the current thread's context class loader to locate the
255      * <code>PersistenceManagerFactory</code> class.
256      * @return the <code>PersistenceManagerFactory</code>.
257      * @param props a <code>Properties</code> instance with properties of the <code>PersistenceManagerFactory</code>.
258      * @see #getPersistenceManagerFactory(Map,ClassLoader)
259      */

260     public static PersistenceManagerFactory getPersistenceManagerFactory
261             (Map JavaDoc props) {
262         ClassLoader JavaDoc cl = getContextClassLoader();
263         return getPersistenceManagerFactory (props, cl);
264     }
265     
266     /**
267      * Get a <code>PersistenceManagerFactory</code> based on a <code>Properties</code>
268      * instance and a class loader.
269      * The following are standard key values:
270      * <BR><code>"javax.jdo.PersistenceManagerFactoryClass"
271      * <BR>"javax.jdo.option.Optimistic",
272      * <BR>"javax.jdo.option.RetainValues",
273      * <BR>"javax.jdo.option.RestoreValues",
274      * <BR>"javax.jdo.option.IgnoreCache",
275      * <BR>"javax.jdo.option.NontransactionalRead",
276      * <BR>"javax.jdo.option.NontransactionalWrite",
277      * <BR>"javax.jdo.option.Multithreaded",
278      * <BR>"javax.jdo.option.ConnectionUserName",
279      * <BR>"javax.jdo.option.ConnectionPassword",
280      * <BR>"javax.jdo.option.ConnectionURL",
281      * <BR>"javax.jdo.option.ConnectionFactoryName",
282      * <BR>"javax.jdo.option.ConnectionFactory2Name",
283      * <BR>"javax.jdo.option.Mapping",
284      * <BR>"javax.jdo.mapping.Catalog",
285      * <BR>"javax.jdo.mapping.Schema".
286      * </code><P>JDO implementations
287      * are permitted to define key values of their own. Any key values not
288      * recognized by the implementation must be ignored. Key values that are
289      * recognized but not supported by an implementation must result in a
290      * <code>JDOFatalUserException</code> thrown by the method.
291      * <P>The returned <code>PersistenceManagerFactory</code> is not configurable (the
292      * <code>set<I>XXX</I></code> methods will throw an exception).
293      * <P>JDO implementations might manage a map of instantiated
294      * <code>PersistenceManagerFactory</code> instances based on specified property key
295      * values, and return a previously instantiated <code>PersistenceManagerFactory</code>
296      * instance. In this case, the properties of the returned
297      * instance must exactly match the requested properties.
298      * @return the <code>PersistenceManagerFactory</code>.
299      * @param props a <code>Properties</code> instance with properties of the <code>PersistenceManagerFactory</code>.
300      * @param cl the class loader to use to load the <code>PersistenceManagerFactory</code> class
301      */

302     public static PersistenceManagerFactory getPersistenceManagerFactory
303             (Map JavaDoc props, ClassLoader JavaDoc cl) {
304         String JavaDoc pmfClassName = (String JavaDoc) props.get ("javax.jdo.PersistenceManagerFactoryClass"); //NOI18N
305
if (pmfClassName == null) {
306             throw new JDOFatalUserException (msg.msg("EXC_GetPMFNoClassNameProperty")); // NOI18N
307
}
308         try {
309             Class JavaDoc pmfClass = cl.loadClass (pmfClassName);
310             Method JavaDoc pmfMethod = pmfClass.getMethod("getPersistenceManagerFactory", //NOI18N
311
new Class JavaDoc[] {Map JavaDoc.class});
312             return (PersistenceManagerFactory) pmfMethod.invoke (null, new Object JavaDoc[] {props});
313         } catch (ClassNotFoundException JavaDoc cnfe) {
314             throw new JDOFatalUserException (msg.msg("EXC_GetPMFClassNotFound", pmfClassName), cnfe); //NOI18N
315
} catch (IllegalAccessException JavaDoc iae) {
316             throw new JDOFatalUserException (msg.msg("EXC_GetPMFIllegalAccess", pmfClassName), iae); //NOI18N
317
} catch (NoSuchMethodException JavaDoc nsme) {
318             throw new JDOFatalInternalException (msg.msg("EXC_GetPMFNoSuchMethod"), nsme); //NOI18N
319
} catch (InvocationTargetException JavaDoc ite) {
320             Throwable JavaDoc nested = ite.getTargetException();
321             if (nested instanceof JDOException) {
322                 throw (JDOException)nested;
323             } else throw new JDOFatalInternalException (msg.msg("EXC_GetPMFUnexpectedException"), ite); //NOI18N
324
} catch (NullPointerException JavaDoc e) {
325             throw new JDOFatalInternalException (msg.msg("EXC_GetPMFNullPointerException", pmfClassName), e); //NOI18N
326
} catch (ClassCastException JavaDoc e) {
327             throw new JDOFatalInternalException (msg.msg("EXC_GetPMFClassCastException", pmfClassName), e); //NOI18N
328
} catch (Exception JavaDoc e) {
329             throw new JDOFatalInternalException (msg.msg("EXC_GetPMFUnexpectedException"), e); //NOI18N
330
}
331     }
332     
333     /**
334      * Returns a {@link PersistenceManagerFactory} configured based
335      * on the properties stored in the resource at
336      * <code>propsResource</code>. This method is equivalent to
337      * invoking {@link
338      * #getPersistenceManagerFactory(String,ClassLoader)} with
339      * <code>Thread.currentThread().getContextClassLoader()</code> as
340      * the <code>loader</code> argument.
341      * @since 2.0
342      * @param propsResource the resource containing the Properties
343      * @return the PersistenceManagerFactory
344      */

345     public static PersistenceManagerFactory getPersistenceManagerFactory
346         (String JavaDoc propsResource) {
347         return getPersistenceManagerFactory (propsResource,
348             getContextClassLoader());
349     }
350
351     /**
352      * Returns a {@link PersistenceManagerFactory} configured based
353      * on the properties stored in the resource at
354      * <code>propsResource</code>. Loads the resource via
355      * <code>loader</code>, and creates a {@link
356      * PersistenceManagerFactory} with <code>loader</code>. Any
357      * <code>IOException</code>s thrown during resource loading will
358      * be wrapped in a {@link JDOFatalUserException}.
359      * @since 2.0
360      * @param propsResource the resource containing the Properties
361      * @param loader the class loader to use to load both the propsResource and the <code>PersistenceManagerFactory</code> class
362      * @return the PersistenceManagerFactory
363      */

364     public static PersistenceManagerFactory getPersistenceManagerFactory
365         (String JavaDoc propsResource, ClassLoader JavaDoc loader) {
366         return getPersistenceManagerFactory(propsResource, loader, loader);
367     }
368         
369     /**
370      * Returns a {@link PersistenceManagerFactory} configured based
371      * on the properties stored in the resource at
372      * <code>propsResource</code>. Loads the Properties via
373      * <code>propsLoader</code>, and creates a {@link
374      * PersistenceManagerFactory} with <code>pmfLoader</code>. Any
375      * <code>IOException</code>s thrown during resource loading will
376      * be wrapped in a {@link JDOFatalUserException}.
377      * @since 2.0
378      * @param propsResource the resource containing the Properties
379      * @param propsLoader the class loader to use to load the propsResource
380      * @param pmfLoader the class loader to use to load the <code>PersistenceManagerFactory</code> class
381      * @return the PersistenceManagerFactory
382      */

383     public static PersistenceManagerFactory getPersistenceManagerFactory
384         (String JavaDoc propsResource, ClassLoader JavaDoc propsLoader, ClassLoader JavaDoc pmfLoader) {
385         
386         if (propsResource == null)
387             throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullResource")); //NOI18N
388
if (propsLoader == null)
389             throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullPropsLoader")); //NOI18N
390
if (pmfLoader == null)
391             throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullPMFLoader")); //NOI18N
392

393         Properties JavaDoc props = new Properties JavaDoc ();
394         InputStream JavaDoc in = null;
395         try {
396             in = propsLoader.getResourceAsStream (propsResource);
397             if (in == null)
398                 throw new JDOFatalUserException
399                     (msg.msg ("EXC_GetPMFNoResource", propsResource, propsLoader)); //NOI18N
400
props.load (in);
401         } catch (IOException JavaDoc ioe) {
402             throw new JDOFatalUserException
403                 (msg.msg ("EXC_GetPMFIOExceptionRsrc", propsResource), ioe); //NOI18N
404
}
405         finally {
406             if (in != null)
407                 try {
408                     in.close ();
409                 } catch (IOException JavaDoc ioe) { }
410         }
411
412         return getPersistenceManagerFactory (props, pmfLoader);
413     }
414
415
416     /**
417      * Returns a {@link PersistenceManagerFactory} configured based
418      * on the properties stored in the file at
419      * <code>propsFile</code>. This method is equivalent to
420      * invoking {@link
421      * #getPersistenceManagerFactory(File,ClassLoader)} with
422      * <code>Thread.currentThread().getContextClassLoader()</code> as
423      * the <code>loader</code> argument.
424      * @since 2.0
425      * @param propsFile the file containing the Properties
426      * @return the PersistenceManagerFactory
427      */

428     public static PersistenceManagerFactory getPersistenceManagerFactory
429         (File JavaDoc propsFile) {
430         return getPersistenceManagerFactory (propsFile,
431             getContextClassLoader());
432     }
433
434     /**
435      * Returns a {@link PersistenceManagerFactory} configured based
436      * on the properties stored in the file at
437      * <code>propsFile</code>. Creates a {@link
438      * PersistenceManagerFactory} with <code>loader</code>. Any
439      * <code>IOException</code>s or
440      * <code>FileNotFoundException</code>s thrown during resource
441      * loading will be wrapped in a {@link JDOFatalUserException}.
442      * @since 2.0
443      * @param propsFile the file containing the Properties
444      * @param loader the class loader to use to load the <code>PersistenceManagerFactory</code> class
445      * @return the PersistenceManagerFactory
446      */

447     public static PersistenceManagerFactory getPersistenceManagerFactory
448         (File JavaDoc propsFile, ClassLoader JavaDoc loader) {
449         if (propsFile == null)
450             throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullFile")); //NOI18N
451
if (loader == null)
452             throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullLoader")); //NOI18N
453
Properties JavaDoc props = new Properties JavaDoc ();
454         InputStream JavaDoc in = null;
455         try {
456             in = new FileInputStream JavaDoc (propsFile);
457             props.load (in);
458         } catch (FileNotFoundException JavaDoc fnfe) {
459             throw new JDOFatalUserException
460                 (msg.msg ("EXC_GetPMFNoFile", propsFile, loader), fnfe); //NOI18N
461
} catch (IOException JavaDoc ioe) {
462             throw new JDOFatalUserException
463                 (msg.msg ("EXC_GetPMFIOExceptionFile", propsFile), ioe); //NOI18N
464
} finally {
465             if (in != null)
466                 try {
467                     in.close ();
468                 } catch (IOException JavaDoc ioe) { }
469         }
470         return getPersistenceManagerFactory (props, loader);
471     }
472
473     /**
474      * Returns a {@link PersistenceManagerFactory} at the JNDI
475      * location specified by <code>jndiLocation</code> in the context
476      * <code>context</code>. If <code>context</code> is
477      * <code>null</code>, <code>new InitialContext()</code> will be
478      * used. This method is equivalent to invoking {@link
479      * #getPersistenceManagerFactory(String,Context,ClassLoader)}
480      * with <code>Thread.currentThread().getContextClassLoader()</code> as
481      * the <code>loader</code> argument.
482      * @since 2.0
483      * @param jndiLocation the JNDI location containing the PersistenceManagerFactory
484      * @param context the context in which to find the named PersistenceManagerFactory
485      * @return the PersistenceManagerFactory
486      */

487     public static PersistenceManagerFactory getPersistenceManagerFactory
488         (String JavaDoc jndiLocation, Context JavaDoc context) {
489         return getPersistenceManagerFactory (jndiLocation, context,
490             getContextClassLoader());
491     }
492
493
494     /**
495      * Returns a {@link PersistenceManagerFactory} at the JNDI
496      * location specified by <code>jndiLocation</code> in the context
497      * <code>context</code>. If <code>context</code> is
498      * <code>null</code>, <code>new InitialContext()</code> will be
499      * used. Creates a {@link PersistenceManagerFactory} with
500      * <code>loader</code>. Any <code>NamingException</code>s thrown
501      * will be wrapped in a {@link JDOFatalUserException}.
502      * @since 2.0
503      * @param jndiLocation the JNDI location containing the PersistenceManagerFactory
504      * @param context the context in which to find the named PersistenceManagerFactory
505      * @param loader the class loader to use to load the <code>PersistenceManagerFactory</code> class
506      * @return the PersistenceManagerFactory
507      */

508     public static PersistenceManagerFactory getPersistenceManagerFactory
509         (String JavaDoc jndiLocation, Context JavaDoc context, ClassLoader JavaDoc loader) {
510         if (jndiLocation == null)
511             throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullJndiLoc")); //NOI18N
512
if (loader == null)
513             throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullLoader")); //NOI18N
514
try {
515             if (context == null)
516                 context = new InitialContext JavaDoc ();
517
518             Object JavaDoc o = context.lookup (jndiLocation);
519             return (PersistenceManagerFactory) PortableRemoteObject.narrow
520                 (o, PersistenceManagerFactory.class);
521         } catch (NamingException JavaDoc ne) {
522             throw new JDOFatalUserException
523                 (msg.msg ("EXC_GetPMFNamingException", jndiLocation, loader), ne); //NOI18N
524
}
525     }
526     
527     /**
528      * Returns a {@link PersistenceManagerFactory} configured based
529      * on the Properties stored in the input stream at
530      * <code>stream</code>. This method is equivalent to
531      * invoking {@link
532      * #getPersistenceManagerFactory(InputStream,ClassLoader)} with
533      * <code>Thread.currentThread().getContextClassLoader()</code> as
534      * the <code>loader</code> argument.
535      * @since 2.0
536      * @param stream the stream containing the Properties
537      * @return the PersistenceManagerFactory
538      */

539     public static PersistenceManagerFactory getPersistenceManagerFactory
540         (InputStream JavaDoc stream) {
541         return getPersistenceManagerFactory (stream,
542             getContextClassLoader());
543     }
544
545     /**
546      * Returns a {@link PersistenceManagerFactory} configured based
547      * on the Properties stored in the input stream at
548      * <code>stream</code>. Creates a {@link
549      * PersistenceManagerFactory} with <code>loader</code>. Any
550      * <code>IOException</code>s thrown during resource
551      * loading will be wrapped in a {@link JDOFatalUserException}.
552      * @since 2.0
553      * @param stream the stream containing the Properties
554      * @param loader the class loader to use to load the <code>PersistenceManagerFactory</code> class
555      * @return the PersistenceManagerFactory
556      */

557     public static PersistenceManagerFactory getPersistenceManagerFactory
558         (InputStream JavaDoc stream, ClassLoader JavaDoc loader) {
559         if (stream == null)
560             throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullStream")); //NOI18N
561
if (loader == null)
562             throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullLoader")); //NOI18N
563
Properties JavaDoc props = new Properties JavaDoc ();
564         try {
565             props.load (stream);
566         } catch (IOException JavaDoc ioe) {
567             throw new JDOFatalUserException
568                 (msg.msg ("EXC_GetPMFIOExceptionStream"), ioe); //NOI18N
569
}
570         return getPersistenceManagerFactory (props, loader);
571     }
572
573     /** Get the context class loader associated with the current thread.
574      * This is done in a doPrivileged block because it is a secure method.
575      * @return the current thread's context class loader.
576      * @since 2.0
577      */

578     private static ClassLoader JavaDoc getContextClassLoader() {
579         return (ClassLoader JavaDoc)AccessController.doPrivileged(
580             new PrivilegedAction JavaDoc () {
581                 public Object JavaDoc run () {
582                     return Thread.currentThread().getContextClassLoader();
583                 }
584             }
585         );
586     }
587 }
588
Popular Tags