KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > PersistenceManagerFactoryImpl


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: PersistenceManagerFactoryImpl.java,v 1.13 2004/01/25 22:30:38 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo;
12
13 import java.sql.Connection JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.jdo.JDOUnsupportedOptionException;
27 import javax.jdo.JDOUserException;
28 import javax.jdo.PersistenceManager;
29 import javax.jdo.PersistenceManagerFactory;
30 import javax.jdo.spi.JDOPermission;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.sql.DataSource JavaDoc;
34 import org.apache.log4j.Category;
35
36
37 /**
38  * A factory used to obtain {@link javax.jdo.PersistenceManager} instances.
39  *
40  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
41  * @version $Revision: 1.13 $
42  */

43
44 public class PersistenceManagerFactoryImpl implements PersistenceManagerFactory
45 {
46     private static final Category LOG = Category.getInstance(PersistenceManagerFactoryImpl.class);
47
48     /**
49      * The system property that selects the default value for the
50      * ValidateTables setting. This is the string
51      * "com.triactive.jdo.validateTables".
52      */

53     public static final String JavaDoc VALIDATE_TABLES_PROPERTY = "com.triactive.jdo.validateTables";
54
55     /**
56      * The system property that selects the default value for the
57      * ValidateConstraints setting. This is the string
58      * "com.triactive.jdo.validateConstraints".
59      */

60     public static final String JavaDoc VALIDATE_CONSTRAINTS_PROPERTY = "com.triactive.jdo.validateConstraints";
61
62     /**
63      * The system property that selects the default value for the
64      * AutoCreateTables setting. This is the string
65      * "com.triactive.jdo.autoCreateTables".
66      */

67     public static final String JavaDoc AUTO_CREATE_TABLES_PROPERTY = "com.triactive.jdo.autoCreateTables";
68
69     /**
70      * The system property that selects the default value for the
71      * transaction isolation level. This is the string
72      * "com.triactive.jdo.transactionIsolation".
73      */

74     public static final String JavaDoc TRANSACTION_ISOLATION_PROPERTY = "com.triactive.jdo.transactionIsolation";
75
76
77     /**
78      * This is an interface that allows setting properties on a PersistenceManagerFactory
79      * with Strings.
80      */

81     public interface PropertySetter
82     {
83         /**
84          * Set this property on the given PersistenceManagerFactoryImpl to the String value.
85          * @param pmf The PersistenceManagerFactoryImpl to set the value on.
86          * @param value The String value to set.
87          */

88         public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc value);
89     }
90
91     /**
92      * A Map mapping property name to the corresponding PropertySetter.
93      */

94     private static final Map JavaDoc PROPERTY_SETTERS = initPropertySetters();
95
96
97     private String JavaDoc driverName = null;
98     private String JavaDoc URL = null;
99     private String JavaDoc userName = null;
100     private String JavaDoc password = null;
101
102     private Object JavaDoc connectionFactory = null;
103     private String JavaDoc connectionFactoryName = null;
104
105     private Object JavaDoc connectionFactory2 = null;
106     private String JavaDoc connectionFactory2Name = null;
107
108     private boolean multithreaded = false;
109     private boolean optimistic = false;
110     private boolean retainValues = false;
111     private boolean restoreValues = false;
112     private boolean nontransactionalRead = false;
113     private boolean nontransactionalWrite = false;
114     private boolean ignoreCache = false;
115     private boolean validateTables = new Boolean JavaDoc(System.getProperty(VALIDATE_TABLES_PROPERTY, "true")).booleanValue();
116     private boolean validateConstraints = new Boolean JavaDoc(System.getProperty(VALIDATE_CONSTRAINTS_PROPERTY, "true")).booleanValue();
117     private boolean autoCreateTables = new Boolean JavaDoc(System.getProperty(AUTO_CREATE_TABLES_PROPERTY, "false")).booleanValue();
118     private int isolationLevel = Connection.TRANSACTION_SERIALIZABLE;
119
120     private int minPool = 3;
121     private int maxPool = 100;
122     private int msWait = 30000;
123
124     /**
125      * Once false, attempts to change properties above will fail (see
126      * assertConfigurable).
127      */

128     private transient boolean configurable = true;
129     private transient DataSource JavaDoc tds = null;
130     private transient DataSource JavaDoc ntds = null;
131
132     /**
133      * The set of PMs returned by this PMF that are still open. null if the
134      * PMF has been closed.
135      */

136     private transient Set JavaDoc openPMs = new HashSet JavaDoc();
137
138
139     /**
140      * Constructs a new PersistenceManagerFactoryImpl.
141      */

142
143     public PersistenceManagerFactoryImpl()
144     {
145         String JavaDoc tip = System.getProperty(TRANSACTION_ISOLATION_PROPERTY);
146
147         if (tip != null)
148         {
149             try
150             {
151                 setTransactionIsolation(tip);
152             }
153             catch (IllegalArgumentException JavaDoc e)
154             {
155                 LOG.warn("Invalid transaction isolation property ignored: " + TRANSACTION_ISOLATION_PROPERTY + "=" + tip);
156             }
157         }
158     }
159
160
161     /**
162      * Return a new PersistenceManagerFactoryImpl with options set according
163      * to the given Properties.
164      * @param props The Properties to initialize the PersistenceManagerFactory with.
165      * @return A PersistenceManagerFactoryImpl with options set according to the
166      * given Properties.
167      * @see javax.jdo.JDOHelper#getPersistenceManagerFactory(java.util.Properties)
168      */

169     public synchronized static PersistenceManagerFactory getPersistenceManagerFactory(Properties JavaDoc props)
170     {
171         PersistenceManagerFactoryImpl pmf = new PersistenceManagerFactoryImpl();
172  
173         // Read properties.
174
pmf.setOptions(props);
175
176         return pmf;
177     }
178
179
180     public synchronized void close()
181     {
182         if (openPMs == null)
183             return;
184
185         SecurityManager JavaDoc secmgr = System.getSecurityManager();
186         if (secmgr != null)
187             secmgr.checkPermission(JDOPermission.CLOSE_PERSISTENCE_MANAGER_FACTORY);
188
189         List JavaDoc nestedEx = new ArrayList JavaDoc();
190         Iterator JavaDoc i = openPMs.iterator();
191
192         while (i.hasNext())
193         {
194             PersistenceManager pm = (PersistenceManager)i.next();
195
196             if (pm.currentTransaction().isActive())
197                 nestedEx.add(new JDOUserException("PersistenceManager has an active transaction", pm));
198         }
199
200         if (!nestedEx.isEmpty())
201         {
202             Throwable JavaDoc[] nested = (Throwable JavaDoc[])nestedEx.toArray(new Throwable JavaDoc[nestedEx.size()]);
203             throw new JDOUserException("One or more PersistenceManagers have an active transaction", nested);
204         }
205
206         i = openPMs.iterator();
207
208         while (i.hasNext())
209             ((PersistenceManagerImpl)i.next()).forceClose();
210
211         configurable = false;
212         openPMs = null;
213     }
214
215
216     public synchronized boolean equals(Object JavaDoc obj)
217     {
218         if (obj == this)
219             return true;
220
221         if (!(obj instanceof PersistenceManagerFactoryImpl))
222             return false;
223
224         PersistenceManagerFactoryImpl pmf = (PersistenceManagerFactoryImpl)obj;
225
226         if (driverName == null) { if (pmf.driverName != null) return false; }
227         else if (!driverName.equals(pmf.driverName)) return false;
228
229         if (URL == null) { if (pmf.URL != null) return false; }
230         else if (!URL.equals(pmf.URL)) return false;
231
232         if (userName == null) { if (pmf.userName != null) return false; }
233         else if (!userName.equals(pmf.userName)) return false;
234
235         if (password == null) { if (pmf.password != null) return false; }
236         else if (!password.equals(pmf.password)) return false;
237
238         if (connectionFactory == null) { if (pmf.connectionFactory != null) return false; }
239         else if (!connectionFactory.equals(pmf.connectionFactory)) return false;
240
241         if (connectionFactoryName == null) { if (pmf.connectionFactoryName != null) return false; }
242         else if (!connectionFactoryName.equals(pmf.connectionFactoryName)) return false;
243
244         if (connectionFactory2 == null) { if (pmf.connectionFactory2 != null) return false; }
245         else if (!connectionFactory2.equals(pmf.connectionFactory2)) return false;
246
247         if (connectionFactory2Name == null) { if (pmf.connectionFactory2Name != null) return false; }
248         else if (!connectionFactory2Name.equals(pmf.connectionFactory2Name)) return false;
249
250         return multithreaded == pmf.multithreaded
251             && optimistic == pmf.optimistic
252             && retainValues == pmf.retainValues
253             && restoreValues == pmf.restoreValues
254             && nontransactionalRead == pmf.nontransactionalRead
255             && nontransactionalWrite == pmf.nontransactionalWrite
256             && ignoreCache == pmf.ignoreCache
257             && validateTables == pmf.validateTables
258             && validateConstraints == pmf.validateConstraints
259             && autoCreateTables == pmf.autoCreateTables
260             && minPool == pmf.minPool
261             && maxPool == pmf.maxPool
262             && msWait == pmf.msWait;
263     }
264
265     public synchronized int hashCode()
266     {
267         return (driverName == null ? 0 : driverName.hashCode())
268              ^ (URL == null ? 0 : URL.hashCode())
269              ^ (userName == null ? 0 : userName.hashCode())
270              ^ (password == null ? 0 : password.hashCode())
271              ^ (connectionFactory == null ? 0 : connectionFactory.hashCode())
272              ^ (connectionFactoryName == null ? 0 : connectionFactoryName.hashCode())
273              ^ (connectionFactory2 == null ? 0 : connectionFactory2.hashCode())
274              ^ (connectionFactory2Name == null ? 0 : connectionFactory2Name.hashCode())
275              ^ (multithreaded ? 1 : 0)
276              ^ (optimistic ? 1 : 0)
277              ^ (retainValues ? 1 : 0)
278              ^ (restoreValues ? 1 : 0)
279              ^ (nontransactionalRead ? 1 : 0)
280              ^ (nontransactionalWrite ? 1 : 0)
281              ^ (ignoreCache ? 1 : 0)
282              ^ (validateTables ? 1 : 0)
283              ^ (validateConstraints ? 1 : 0)
284              ^ (autoCreateTables ? 1 : 0)
285              ^ minPool
286              ^ maxPool
287              ^ msWait;
288     }
289
290
291     /**
292      * Initialize the PROPERTY_SETTERS Map.
293      * @return The PROPERTY_SETTERS Map.
294      */

295     private static final Map JavaDoc initPropertySetters()
296     {
297         final Map JavaDoc map = new HashMap JavaDoc();
298         map.put("javax.jdo.option.Optimistic",
299                 new PropertySetter()
300                 {
301                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setOptimistic(Boolean.valueOf(s).booleanValue()); }
302                 });
303         map.put("javax.jdo.option.RetainValues",
304                 new PropertySetter()
305                 {
306                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setRetainValues(Boolean.valueOf(s).booleanValue()); }
307                 });
308         map.put("javax.jdo.option.RestoreValues",
309                 new PropertySetter()
310                 {
311                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setRestoreValues(Boolean.valueOf(s).booleanValue()); }
312                 });
313         map.put("javax.jdo.option.IgnoreCache",
314                 new PropertySetter()
315                 {
316                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setIgnoreCache(Boolean.valueOf(s).booleanValue()); }
317                 });
318         map.put("javax.jdo.option.NontransactionalRead",
319                 new PropertySetter()
320                 {
321                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setNontransactionalRead(Boolean.valueOf(s).booleanValue()); }
322                 });
323         map.put("javax.jdo.option.NontransactionalWrite",
324                 new PropertySetter()
325                 {
326                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setNontransactionalWrite(Boolean.valueOf(s).booleanValue()); }
327                 });
328         map.put("javax.jdo.option.Multithreaded",
329                 new PropertySetter()
330                 {
331                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setMultithreaded(Boolean.valueOf(s).booleanValue()); }
332                 });
333         map.put("javax.jdo.option.ConnectionUserName",
334                 new PropertySetter()
335                 {
336                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setConnectionUserName(s); }
337                 });
338         map.put("javax.jdo.option.ConnectionPassword",
339                 new PropertySetter()
340                 {
341                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setConnectionPassword(s); }
342                 });
343         map.put("javax.jdo.option.ConnectionDriverName",
344                 new PropertySetter()
345                 {
346                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setConnectionDriverName(s); }
347                 });
348         map.put("javax.jdo.option.ConnectionURL",
349                 new PropertySetter()
350                 {
351                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setConnectionURL(s); }
352                 });
353         map.put("javax.jdo.option.ConnectionFactoryName",
354                 new PropertySetter()
355                 {
356                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setConnectionFactoryName(s); }
357                 });
358         map.put("javax.jdo.option.ConnectionFactory2Name",
359                 new PropertySetter()
360                 {
361                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setConnectionFactory2Name(s); }
362                 });
363         map.put(VALIDATE_TABLES_PROPERTY,
364                 new PropertySetter()
365                 {
366                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setValidateTables(Boolean.valueOf(s).booleanValue()); }
367                 });
368         map.put(VALIDATE_CONSTRAINTS_PROPERTY,
369                 new PropertySetter()
370                 {
371                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setValidateConstraints(Boolean.valueOf(s).booleanValue()); }
372                 });
373         map.put(AUTO_CREATE_TABLES_PROPERTY,
374                 new PropertySetter()
375                 {
376                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setAutoCreateTables(Boolean.valueOf(s).booleanValue()); }
377                 });
378         map.put(TRANSACTION_ISOLATION_PROPERTY,
379                 new PropertySetter()
380                 {
381                   public void set(PersistenceManagerFactoryImpl pmf, String JavaDoc s) { pmf.setTransactionIsolation(s); }
382                 });
383         return map;
384     }
385
386
387     /**
388      * Asserts that a change to a configuration property is allowed.
389      */

390
391     private void assertConfigurable()
392     {
393         if (!configurable)
394             throw new JDOUserException("Configuration changes not allowed after PersistenceManagers have been generated");
395     }
396
397
398     /**
399      * Asserts that the PMF is still open.
400      */

401
402     private void assertNotClosed()
403     {
404         if (openPMs == null)
405             throw new JDOUserException("PersistenceManagerFactory has been closed");
406     }
407
408
409     /**
410      * Looks up a DataSource object in JNDI.
411      *
412      * @param name The JNDI name of the DataSource.
413      *
414      * @return The DataSource object.
415      *
416      * @exception ConnectionFactoryNotFoundException
417      * If a JNDI lookup failure occurs.
418      * @exception UnsupportedConnectionFactoryException
419      * If the object is not a javax.sql.DataSource.
420      */

421
422     private DataSource JavaDoc lookupDataSource(String JavaDoc name)
423     {
424         Object JavaDoc obj;
425
426         try
427         {
428             obj = new InitialContext JavaDoc().lookup(name);
429         }
430         catch (NamingException JavaDoc e)
431         {
432             throw new ConnectionFactoryNotFoundException(name, e);
433         }
434
435         if (!(obj instanceof DataSource JavaDoc))
436             throw new UnsupportedConnectionFactoryException(obj);
437
438         return (DataSource JavaDoc)obj;
439     }
440
441
442     /**
443      * Freezes the current configuration.
444      *
445      * @exception UnsupportedConnectionFactoryException
446      * If the configured connection factory is not a javax.sql.DataSource.
447      */

448
449     private void freezeConfiguration()
450     {
451         if (configurable)
452         {
453             if (connectionFactory != null)
454             {
455                 if (!(connectionFactory instanceof DataSource JavaDoc))
456                     throw new UnsupportedConnectionFactoryException(connectionFactory);
457
458                 tds = (DataSource JavaDoc)connectionFactory;
459             }
460             else if (connectionFactoryName != null)
461                 tds = lookupDataSource(connectionFactoryName);
462             else
463                 tds = new DriverManagerDataSource(driverName, URL);
464
465             if (connectionFactory2 != null)
466             {
467                 if (!(connectionFactory2 instanceof DataSource JavaDoc))
468                     throw new UnsupportedConnectionFactoryException(connectionFactory2);
469
470                 ntds = (DataSource JavaDoc)connectionFactory2;
471             }
472             else if (connectionFactory2Name != null)
473                 ntds = lookupDataSource(connectionFactory2Name);
474             else
475                 ntds = tds;
476
477             configurable = false;
478         }
479     }
480
481
482     /**
483      * Set the options for this PersistenceManagerFactory based on the given Properties.
484      * @param prop The Properties to set the options from.
485      */

486     private void setOptions(Properties JavaDoc props)
487     {
488         for (Enumeration JavaDoc e=props.propertyNames(); e.hasMoreElements(); )
489         {
490             String JavaDoc prop = (String JavaDoc)e.nextElement();
491
492             PropertySetter setter = (PropertySetter)PROPERTY_SETTERS.get(prop);
493             if (null != prop && null != setter)
494             {
495                 String JavaDoc value = props.getProperty(prop);
496                 setter.set(this, value);
497             }
498         }
499     }
500
501
502     /**
503      * Returns the data source to be used for transactional access.
504      *
505      * @return The data source to be used for transactional access.
506      */

507
508     public synchronized DataSource JavaDoc getTransactionalDataSource()
509     {
510         freezeConfiguration();
511
512         return tds;
513     }
514
515
516     /**
517      * Returns the data source to be used for non-transactional access.
518      *
519      * @return The data source to be used for non-transactional access.
520      */

521
522     public synchronized DataSource JavaDoc getNontransactionalDataSource()
523     {
524         freezeConfiguration();
525
526         return ntds;
527     }
528
529
530     /**
531      * Get an instance of <tt>PersistenceManager</tt> from this factory. The
532      * instance has default values for options.
533      *
534      * <p>After the first use of getPersistenceManager, no "set" methods will
535      * succeed.
536      *
537      * @return a <tt>PersistenceManager</tt> instance with default options.
538      */

539
540     public synchronized PersistenceManager getPersistenceManager()
541     {
542         return getPersistenceManager(userName, password);
543     }
544
545
546     /**
547      * Get an instance of <tt>PersistenceManager</tt> from this factory. The
548      * instance has default values for options. The parameters userid and
549      * password are used when obtaining datastore connections from the
550      * connection pool.
551      *
552      * <p>After the first use of getPersistenceManager, no "set" methods will
553      * succeed.
554      *
555      * @param userName the user name for the connection
556      * @param password the password for the connection
557      *
558      * @return a <tt>PersistenceManager</tt> instance with default options.
559      */

560
561     public synchronized PersistenceManager getPersistenceManager(String JavaDoc userName, String JavaDoc password)
562     {
563         assertNotClosed();
564         freezeConfiguration();
565
566         PersistenceManagerImpl pm = new PersistenceManagerImpl(this, userName, password);
567
568         openPMs.add(pm);
569
570         return pm;
571     }
572
573
574     synchronized void pmClosed(PersistenceManagerImpl pm)
575     {
576         if (openPMs != null)
577             openPMs.remove(pm);
578     }
579
580
581     /**
582      * Set the user name for the data store connection.
583      * @param userName the user name for the data store connection.
584      */

585
586     public synchronized void setConnectionUserName(String JavaDoc userName)
587     {
588         assertConfigurable();
589         this.userName = userName;
590     }
591
592
593     /**
594      * Get the user name for the data store connection.
595      * @return the user name for the data store connection.
596      */

597
598     public String JavaDoc getConnectionUserName()
599     {
600         return userName;
601     }
602
603
604     /**
605      * Set the password for the data store connection.
606      * @param password the password for the data store connection.
607      */

608
609     public synchronized void setConnectionPassword(String JavaDoc password)
610     {
611         assertConfigurable();
612         this.password = password;
613     }
614
615
616     /**
617      * Set the URL for the data store connection.
618      * @param URL the URL for the data store connection.
619      */

620
621     public synchronized void setConnectionURL(String JavaDoc URL)
622     {
623         assertConfigurable();
624         this.URL = URL;
625     }
626
627
628     /**
629      * Get the URL for the data store connection.
630      * @return the URL for the data store connection.
631      */

632
633     public String JavaDoc getConnectionURL()
634     {
635         return URL;
636     }
637
638
639     /**
640      * Set the driver name for the data store connection.
641      * @param driverName the driver name for the data store connection.
642      */

643
644     public synchronized void setConnectionDriverName(String JavaDoc driverName)
645     {
646         assertConfigurable();
647         this.driverName = driverName;
648     }
649
650
651     /**
652      * Get the driver name for the data store connection.
653      * @return the driver name for the data store connection.
654      */

655
656     public String JavaDoc getConnectionDriverName()
657     {
658         return driverName;
659     }
660
661
662     /**
663      * Set the name for the data store connection factory.
664      * @param connectionFactoryName the name of the data store connection factory.
665      */

666
667     public synchronized void setConnectionFactoryName(String JavaDoc connectionFactoryName)
668     {
669         assertConfigurable();
670         this.connectionFactoryName = connectionFactoryName;
671     }
672
673
674     /**
675      * Get the name for the data store connection factory.
676      * @return the name of the data store connection factory.
677      */

678
679     public String JavaDoc getConnectionFactoryName()
680     {
681         return connectionFactoryName;
682     }
683
684
685     /**
686      * Set the data store connection factory. JDO implementations
687      * will support specific connection factories. The connection
688      * factory interfaces are not part of the JDO specification.
689      * @param connectionFactory the data store connection factory.
690      */

691
692     public synchronized void setConnectionFactory(Object JavaDoc connectionFactory)
693     {
694         assertConfigurable();
695         this.connectionFactory = connectionFactory;
696     }
697
698
699     /**
700      * Get the data store connection factory.
701      * @return the data store connection factory.
702      */

703
704     public Object JavaDoc getConnectionFactory()
705     {
706         return connectionFactory;
707     }
708
709
710     /**
711      * Set the name for the second data store connection factory. This is
712      * needed for managed environments to get nontransactional connections for
713      * optimistic transactions.
714      * @param connectionFactory2Name the name of the data store connection factory.
715      */

716
717     public synchronized void setConnectionFactory2Name(String JavaDoc connectionFactory2Name)
718     {
719         assertConfigurable();
720         this.connectionFactory2Name = connectionFactory2Name;
721     }
722
723
724     /**
725      * Get the name for the second data store connection factory. This is
726      * needed for managed environments to get nontransactional connections for
727      * optimistic transactions.
728      * @return the name of the data store connection factory.
729      */

730
731     public String JavaDoc getConnectionFactory2Name()
732     {
733         return connectionFactory2Name;
734     }
735
736
737     /**
738      * Set the second data store connection factory. This is
739      * needed for managed environments to get nontransactional connections for
740      * optimistic transactions. JDO implementations
741      * will support specific connection factories. The connection
742      * factory interfaces are not part of the JDO specification.
743      * @param connectionFactory2 the data store connection factory.
744      */

745
746     public synchronized void setConnectionFactory2(Object JavaDoc connectionFactory2)
747     {
748         assertConfigurable();
749         this.connectionFactory2 = connectionFactory2;
750     }
751
752
753     /**
754      * Get the second data store connection factory. This is
755      * needed for managed environments to get nontransactional connections for
756      * optimistic transactions.
757      * @return the data store connection factory.
758      */

759
760     public Object JavaDoc getConnectionFactory2()
761     {
762         return connectionFactory2;
763     }
764
765
766     /**
767      * Set the default Multithreaded setting for all <tt>PersistenceManager</tt> instances
768      * obtained from this factory.
769      *
770      * @param flag the default Multithreaded setting.
771      */

772
773     public synchronized void setMultithreaded(boolean flag)
774     {
775         assertConfigurable();
776
777         multithreaded = flag;
778     }
779
780
781     /**
782      * Get the default Multithreaded setting for all <tt>PersistenceManager</tt> instances
783      * obtained from this factory.
784      *
785      * @return the default Multithreaded setting.
786      */

787
788     public boolean getMultithreaded()
789     {
790         return multithreaded;
791     }
792
793
794     /**
795      * Set the default Optimistic setting for all <tt>PersistenceManager</tt> instances
796      * obtained from this factory.
797      *
798      * @param flag the default Optimistic setting.
799      */

800
801     public synchronized void setOptimistic(boolean flag)
802     {
803         assertConfigurable();
804
805         optimistic = flag;
806
807         if (flag)
808              nontransactionalRead = flag;
809     }
810
811
812     /**
813      * Get the default Optimistic setting for all <tt>PersistenceManager</tt> instances
814      * obtained from this factory.
815      *
816      * @return the default Optimistic setting.
817      */

818
819     public boolean getOptimistic()
820     {
821         return optimistic;
822     }
823
824
825     /**
826      * Set the default RetainValues setting for all <tt>PersistenceManager</tt> instances
827      * obtained from this factory.
828      *
829      * @param flag the default RetainValues setting.
830      */

831
832     public synchronized void setRetainValues(boolean flag)
833     {
834         assertConfigurable();
835         retainValues = flag;
836
837         if (flag)
838             nontransactionalRead = flag;
839     }
840
841
842     /**
843      * Get the default RetainValues setting for all <tt>PersistenceManager</tt> instances
844      * obtained from this factory.
845      *
846      * @return the default RetainValues setting.
847      */

848
849     public boolean getRetainValues()
850     {
851         return retainValues;
852     }
853
854
855     /**
856      * Set the default RestoreValues setting for all <tt>PersistenceManager</tt> instances
857      * obtained from this factory.
858      *
859      * @param flag the default RestoreValues setting.
860      */

861
862     public synchronized void setRestoreValues(boolean flag)
863     {
864         assertConfigurable();
865         restoreValues = flag;
866     }
867
868
869     /**
870      * Get the default RestoreValues setting for all <tt>PersistenceManager</tt> instances
871      * obtained from this factory.
872      *
873      * @return the default RestoreValues setting.
874      */

875
876     public boolean getRestoreValues()
877     {
878         return restoreValues;
879     }
880
881
882     /**
883      * Set the default NontransactionalRead setting for all <tt>PersistenceManager</tt> instances
884      * obtained from this factory.
885      *
886      * @param flag the default NontransactionalRead setting.
887      */

888
889     public synchronized void setNontransactionalRead(boolean flag)
890     {
891         assertConfigurable();
892         nontransactionalRead = flag;
893     }
894
895
896     /**
897      * Get the default NontransactionalRead setting for all <tt>PersistenceManager</tt> instances
898      * obtained from this factory.
899      *
900      * @return the default NontransactionalRead setting.
901      */

902
903     public boolean getNontransactionalRead()
904     {
905         return nontransactionalRead;
906     }
907
908
909     /**
910      * Set the default NontransactionalWrite setting for all <tt>PersistenceManager</tt> instances
911      * obtained from this factory.
912      *
913      * @param flag the default NontransactionalWrite setting.
914      */

915
916     public synchronized void setNontransactionalWrite(boolean flag)
917     {
918         assertConfigurable();
919         nontransactionalWrite = flag;
920     }
921
922
923     /**
924      * Get the default NontransactionalWrite setting for all <tt>PersistenceManager</tt> instances
925      * obtained from this factory.
926      *
927      * @return the default NontransactionalWrite setting.
928      */

929
930     public boolean getNontransactionalWrite()
931     {
932         return nontransactionalWrite;
933     }
934
935
936     /**
937      * Set the default IgnoreCache setting for all <tt>PersistenceManager</tt> instances
938      * obtained from this factory.
939      *
940      * @param flag the default IgnoreCache setting.
941      */

942
943     public synchronized void setIgnoreCache(boolean flag)
944     {
945         assertConfigurable();
946         ignoreCache = flag;
947     }
948
949
950     /**
951      * Get the default IgnoreCache setting for all <tt>PersistenceManager</tt> instances
952      * obtained from this factory.
953      *
954      * @return the default IngoreCache setting.
955      */

956
957     public boolean getIgnoreCache()
958     {
959         return ignoreCache;
960     }
961
962
963     /**
964      * Set the MaxPool setting for the <tt>PersistenceManager</tt>
965      * pool for this factory.
966      * @param maxPool the MaxPool setting.
967      */

968
969     public synchronized void setMaxPool(int maxPool)
970     {
971         assertConfigurable();
972         this.maxPool = maxPool;
973     }
974
975
976     /**
977      * Get the MaxPool setting for the <tt>PersistenceManager</tt>
978      * pool for this factory.
979      * @return the MaxPool setting.
980      */

981
982     public int getMaxPool()
983     {
984         return maxPool;
985     }
986
987
988     /**
989      * Set the MinPool setting for the <tt>PersistenceManager</tt>
990      * pool for this factory.
991      * @param minPool the MinPool setting.
992      */

993
994     public synchronized void setMinPool(int minPool)
995     {
996         assertConfigurable();
997         this.minPool = minPool;
998     }
999
1000
1001    /**
1002     * Get the MinPool setting for the <tt>PersistenceManager</tt>
1003     * pool for this factory.
1004     * @return the MinPool setting.
1005     */

1006
1007    public int getMinPool()
1008    {
1009        return minPool;
1010    }
1011
1012
1013    /**
1014     * Set the MsWait setting for the <tt>PersistenceManager</tt>
1015     * pool for this factory.
1016     * @param msWait the MsWait setting.
1017     */

1018
1019    public synchronized void setMsWait(int msWait)
1020    {
1021        assertConfigurable();
1022        this.msWait = msWait;
1023    }
1024
1025
1026    /**
1027     * Get the MsWait setting for the <tt>PersistenceManager</tt>
1028     * pool for this factory.
1029     * @return the MsWait setting.
1030     */

1031
1032    public int getMsWait()
1033    {
1034        return msWait;
1035    }
1036
1037
1038    /**
1039     * Set the default ValidateTables setting for all
1040     * <tt>PersistenceManager</tt> instances obtained from this factory.
1041     *
1042     * @param flag the default ValidateTables setting.
1043     */

1044
1045    public synchronized void setValidateTables(boolean flag)
1046    {
1047        assertConfigurable();
1048        validateTables = flag;
1049    }
1050
1051
1052    /**
1053     * Get the default ValidateTables setting for all
1054     * <tt>PersistenceManager</tt> instances obtained from this factory.
1055     *
1056     * @return the default ValidateTables setting.
1057     */

1058
1059    public boolean getValidateTables()
1060    {
1061        return validateTables;
1062    }
1063
1064
1065    /**
1066     * Set the default ValidateConstraints setting for all
1067     * <tt>PersistenceManager</tt> instances obtained from this factory.
1068     *
1069     * @param flag the default ValidateConstraints setting.
1070     */

1071
1072    public synchronized void setValidateConstraints(boolean flag)
1073    {
1074        assertConfigurable();
1075        validateConstraints = flag;
1076    }
1077
1078
1079    /**
1080     * Get the default ValidateConstraints setting for all
1081     * <tt>PersistenceManager</tt> instances obtained from this factory.
1082     *
1083     * @return the default ValidateConstraints setting.
1084     */

1085
1086    public boolean getValidateConstraints()
1087    {
1088        return validateConstraints;
1089    }
1090
1091
1092    /**
1093     * Set the default AutoCreateTables setting for all
1094     * <tt>PersistenceManager</tt> instances obtained from this factory.
1095     *
1096     * @param flag the default AutoCreateTables setting.
1097     */

1098
1099    public synchronized void setAutoCreateTables(boolean flag)
1100    {
1101        assertConfigurable();
1102        autoCreateTables = flag;
1103    }
1104
1105
1106    /**
1107     * Get the default AutoCreateTables setting for all
1108     * <tt>PersistenceManager</tt> instances obtained from this factory.
1109     *
1110     * @return the default AutoCreateTables setting.
1111     */

1112
1113    public boolean getAutoCreateTables()
1114    {
1115        return autoCreateTables;
1116    }
1117
1118
1119    /**
1120     * Set the default transaction isolation level for all
1121     * <tt>PersistenceManager</tt> instances obtained from this factory.
1122     *
1123     * @param isolationLevelName
1124     * One of the values "read uncommitted", "read committed", "repeatable
1125     * read", or "serializable". The name is case-insensitive, and spaces
1126     * and underscores are equivalent.
1127     *
1128     */

1129
1130    private synchronized void setTransactionIsolation(String JavaDoc isolationLevelName)
1131    {
1132        assertConfigurable();
1133
1134        String JavaDoc iln = isolationLevelName.trim().replace(' ', '_').toUpperCase();
1135
1136        if (iln.equals("READ_UNCOMMITTED"))
1137            isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED;
1138        else if (iln.equals("READ_COMMITTED"))
1139            isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
1140        else if (iln.equals("REPEATABLE_READ"))
1141            isolationLevel = Connection.TRANSACTION_REPEATABLE_READ;
1142        else if (iln.equals("SERIALIZABLE"))
1143            isolationLevel = Connection.TRANSACTION_SERIALIZABLE;
1144        else
1145            throw new IllegalArgumentException JavaDoc("Illegal isolation level: " + isolationLevelName);
1146    }
1147
1148
1149    /**
1150     * Set the default transaction isolation level for all
1151     * <tt>PersistenceManager</tt> instances obtained from this factory.
1152     *
1153     * @param isolationLevel the default transaction isolation level.
1154     */

1155
1156    public synchronized void setTransactionIsolation(int isolationLevel)
1157    {
1158        assertConfigurable();
1159
1160        switch (isolationLevel)
1161        {
1162            case Connection.TRANSACTION_READ_UNCOMMITTED:
1163            case Connection.TRANSACTION_READ_COMMITTED:
1164            case Connection.TRANSACTION_REPEATABLE_READ:
1165            case Connection.TRANSACTION_SERIALIZABLE:
1166                break;
1167
1168            default:
1169                throw new IllegalArgumentException JavaDoc("Illegal isolation level: " + isolationLevel);
1170        }
1171
1172        this.isolationLevel = isolationLevel;
1173    }
1174
1175
1176    /**
1177     * Get the default transaction isolation level for all
1178     * <tt>PersistenceManager</tt> instances obtained from this factory.
1179     *
1180     * @return the default transaction isolation level.
1181     */

1182
1183    public int getTransactionIsolation()
1184    {
1185        return isolationLevel;
1186    }
1187
1188
1189    /**
1190     * Return non-configurable properties of this PersistenceManagerFactory.
1191     * Properties with keys VendorName and VersionNumber are required. Other
1192     * keys are optional.
1193     * @return the non-configurable properties of this
1194     * PersistenceManagerFactory.
1195     */

1196
1197    public Properties JavaDoc getProperties()
1198    {
1199        Properties JavaDoc props = new Properties JavaDoc();
1200
1201        props.setProperty("VendorName", "TriActive");
1202        props.setProperty("VersionNumber", "$Name: TJDO_2_1 $");
1203
1204        return props;
1205    }
1206
1207
1208    /**
1209     * The application can determine from the results of this method which
1210     * optional features, and which query languages are supported by the JDO
1211     * implementation.
1212     *
1213     * <p>Each supported JDO optional feature is represented by a String with
1214     * one of the following values:
1215     *
1216     * <pre><blockquote>
1217     * javax.jdo.option.TransientTransactional
1218     * javax.jdo.option.NontransactionalRead
1219     * javax.jdo.option.NontransactionalWrite
1220     * javax.jdo.option.RetainValues
1221     * javax.jdo.option.Optimistic
1222     * javax.jdo.option.ApplicationIdentity
1223     * javax.jdo.option.DatastoreIdentity
1224     * javax.jdo.option.NonDatastoreIdentity
1225     * javax.jdo.option.ArrayList
1226     * javax.jdo.option.HashMap
1227     * javax.jdo.option.Hashtable
1228     * javax.jdo.option.LinkedList
1229     * javax.jdo.option.TreeMap
1230     * javax.jdo.option.TreeSet
1231     * javax.jdo.option.Vector
1232     * javax.jdo.option.Map
1233     * javax.jdo.option.List
1234     * javax.jdo.option.Array
1235     * javax.jdo.option.NullCollection
1236     * </blockquote></pre>
1237     *
1238     * The standard JDO query language is represented by a String:
1239     *
1240     * <pre><blockquote>
1241     * javax.jdo.query.JDOQL
1242     * </blockquote></pre>
1243     *
1244     * @return A Collection of String representing the supported options.
1245     */

1246
1247    public Collection JavaDoc supportedOptions()
1248    {
1249        return Collections.unmodifiableList(Arrays.asList(optionArray));
1250    }
1251
1252    private static final String JavaDoc[] optionArray =
1253    {
1254        "javax.jdo.option.TransientTransactional",
1255        "javax.jdo.option.NontransactionalRead",
1256        "javax.jdo.option.RetainValues",
1257        "javax.jdo.option.DatastoreIdentity",
1258        "javax.jdo.option.NonDatastoreIdentity",
1259        "javax.jdo.option.HashMap",
1260        "javax.jdo.option.Map",
1261        "javax.jdo.query.JDOQL",
1262        "javax.jdo.query.TJDOSQL"
1263    };
1264}
1265
Popular Tags