KickJava   Java API By Example, From Geeks To Geeks.

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


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  * PersistenceManagerFactoryImpl.java
26  *
27  * Created on March 9, 2000
28  */

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.impl;
31
32 import java.util.ResourceBundle JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import javax.sql.DataSource JavaDoc;
36
37 import com.sun.jdo.api.persistence.support.*;
38 import com.sun.jdo.spi.persistence.support.sqlstore.RuntimeVersion;
39 import com.sun.jdo.spi.persistence.utility.I18NHelper;
40 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.EJBHelper;
41
42 /**
43  *
44  * @author Marina Vatkina
45  * @version 0.1
46  */

47
48 public class PersistenceManagerFactoryImpl implements PersistenceManagerFactory
49 {
50     /**
51      * PersistenceManagerFactory properties
52      */

53     private String JavaDoc URL = null;
54     private String JavaDoc userName = null;
55     private String JavaDoc password = null;
56     private String JavaDoc driverName = null;
57     private ConnectionFactory connectionFactory = null;
58     private Object JavaDoc dataSource = null;
59     private String JavaDoc connectionFactoryName = null;
60     private String JavaDoc identifier = null;
61     private int connectionMaxPool = 0;
62     private int connectionMinPool = 0;
63     private int connectionMsInterval = 0;
64     private int connectionLoginTimeout = 0;
65     private int connectionMsWait = 0;
66     private int txIsolation = -1;
67     private PrintWriter JavaDoc connectionLogWriter;
68     private boolean optimistic = true;
69     private boolean retainValues = true;
70     private boolean nontransactionalRead = true;
71     private boolean ignoreCache = false;
72     private int queryTimeout = 0;
73     private int updateTimeout = 0;
74     private int maxPool = 0;
75     private int minPool = 0;
76     private boolean supersedeDeletedInstance = true;
77     private boolean requireCopyObjectId = true;
78     private boolean requireTrackedSCO = true;
79
80     private static final int NOT_SET = 0;
81     private static final int SET_AS_CONNECTIONFACTORY = 1;
82     private static final int SET_AS_DATASOURCE = 2;
83
84     /** flag for ConnectionFactory setup
85      */

86     private int providedConnectionFactory = 0;
87
88
89     /**
90      * Reference to the internal PersistenceManagerFactory implementation
91      * to deligate actual stuff
92      */

93     private transient SQLPersistenceManagerFactory pmFactory = null;
94
95     /**
96          * I18N message handler
97          */

98     private final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
99                                 PersistenceManagerFactoryImpl.class);
100
101     /**
102      * Creates new <code>PersistenceManagerFactoryImpl</code> without any user info
103      */

104     public PersistenceManagerFactoryImpl() {
105             EJBHelper.setPersistenceManagerFactoryDefaults(this);
106     }
107
108     /**
109      * Creates new <code>PersistenceManagerFactoryImpl</code> with user info
110      * @param URL connection URL
111      * @param userName database user
112      * @param password database user password
113      * @param driverName driver name
114      */

115     public PersistenceManagerFactoryImpl(
116             String JavaDoc URL,
117             String JavaDoc userName,
118             String JavaDoc password,
119             String JavaDoc driverName)
120     {
121             EJBHelper.setPersistenceManagerFactoryDefaults(this);
122
123         this.URL = URL;
124         this.userName = userName;
125         this.password = password;
126         this.driverName = driverName;
127
128     }
129
130     /**
131      * Sets database user
132      * @param userName database user
133      */

134     public void setConnectionUserName (String JavaDoc userName) {
135         assertNotConfigured();
136         this.userName = userName;
137     }
138
139     /**
140      * Returns database user name
141      * @return current database user name
142      */

143     public String JavaDoc getConnectionUserName()
144     {
145         if (connectionFactory != null)
146             return connectionFactory.getUserName();
147
148         return userName;
149
150     }
151
152     /**
153      * Sets database user password
154      * @param password database user password
155      */

156     public void setConnectionPassword (String JavaDoc password) {
157         assertNotConfigured();
158         this.password = password;
159     }
160
161
162     /**
163      * Sets JDBC connection URL
164      * @param URL connection URL
165      */

166     public void setConnectionURL (String JavaDoc URL) {
167         assertNotConfigured();
168         this.URL = URL;
169     }
170
171     /**
172      * Returns connection URL
173      * @return connection URL
174      */

175     public String JavaDoc getConnectionURL()
176     {
177         if (connectionFactory != null)
178                         return connectionFactory.getURL();
179
180         return URL;
181
182     }
183
184     /**
185      * Sets JDBC driver name
186      * @param driverName JDBC driver name
187      */

188     public void setConnectionDriverName (String JavaDoc driverName) {
189         assertNotConfigured();
190         this.driverName = driverName;
191     }
192
193     /**
194      * Returns JDBC driver name
195      * @return driver name
196      */

197     public String JavaDoc getConnectionDriverName()
198     {
199         if (connectionFactory != null)
200                         return connectionFactory.getDriverName();
201         return driverName;
202
203     }
204
205     /**
206      * Sets ConnectionFactory that can be one of two types:
207      * <a HREF="ConnectionFactory.html">ConnectionFactory</a> or javax.sql.DataSource
208      * @param connectionFactory as java.lang.Object
209      */

210     public void setConnectionFactory (Object JavaDoc connectionFactory) {
211         assertNotConfigured();
212         if (connectionFactory == null) {
213             this.connectionFactory = null;
214             this.dataSource = null;
215             providedConnectionFactory = NOT_SET;
216
217         } else {
218             if (EJBHelper.isManaged() || (connectionFactory instanceof DataSource JavaDoc)) {
219                 this.dataSource = connectionFactory;
220                 providedConnectionFactory = SET_AS_DATASOURCE;
221             } else if (connectionFactory instanceof ConnectionFactory) {
222                 this.connectionFactory = (ConnectionFactory)connectionFactory;
223                 providedConnectionFactory = SET_AS_CONNECTIONFACTORY;
224             } else {
225                 throw new JDOUserException(I18NHelper.getMessage( messages,
226                                     "persistencemanagerfactoryimpl.wrongtype")); //NOI18N
227
}
228         }
229     }
230
231     /**
232      * Returns ConnectionFactory
233      * @return ConnectionFactory
234      */

235     public Object JavaDoc getConnectionFactory() {
236         if (dataSource != null)
237             return dataSource;
238
239         return connectionFactory;
240     }
241
242         /**
243          * Sets ConnectionFactory name
244          * @param connectionFactoryName ConnectionFactory name
245          */

246         public void setConnectionFactoryName (String JavaDoc connectionFactoryName)
247     {
248         assertNotConfigured();
249         this.connectionFactoryName = connectionFactoryName;
250     }
251
252     /**
253          * Returns ConnectionFactory name
254          * @return ConnectionFactoryName
255          */

256         public String JavaDoc getConnectionFactoryName ()
257     {
258         return connectionFactoryName;
259     }
260
261     /**
262      * Sets Identifier. An identifier is a string that user can use to identify
263      * the PersistenceManagerFactory in a given environment. Identifier can be
264      * particularly useful in an environment where multiple
265      * PersistenceManagerFactories are initialized in a system.
266      * @param identifier
267      */

268     public void setIdentifier(String JavaDoc identifier) {
269         assertNotConfigured();
270         this.identifier = identifier;
271     }
272
273     /**
274      * Gets Identifier. An identifier is a string that user can use to identify
275      * the PersistenceManagerFactory in a given environment. Identifier can be
276      * particularly useful in an environment where multiple
277      * PersistenceManagerFactories are initialized in a system.
278      * @return identifier
279      */

280     public String JavaDoc getIdentifier() {
281         return identifier;
282     }
283
284     /**
285          * Sets maximum number of connections in the connection pool
286          * @param MaxPool maximum number of connections
287          */

288     public void setConnectionMaxPool (int MaxPool)
289     {
290         assertNotConfigured();
291         this.connectionMaxPool = MaxPool;
292     }
293
294
295         /**
296          * Returns maximum number of connections in the connection pool
297          * @return connectionMaxPool
298          */

299     public int getConnectionMaxPool ()
300     {
301         if (connectionFactory != null)
302                         return connectionFactory.getMaxPool();
303
304         return connectionMaxPool;
305     }
306
307         /**
308          * Sets minimum number of connections in the connection pool
309          * @param MinPool minimum number of connections
310          */

311
312     public void setConnectionMinPool (int MinPool)
313     {
314         assertNotConfigured();
315         this.connectionMinPool = MinPool;
316     }
317
318
319         /**
320          * Returns minimum number of connections in the connection pool
321          * @return connectionMinPool
322          */

323     public int getConnectionMinPool ()
324     {
325         if (connectionFactory != null)
326                         return connectionFactory.getMinPool();
327         return connectionMinPool;
328     }
329
330
331         /**
332          * Sets the number of milliseconds to wait for an available connection
333      * from the connection pool before throwing an exception
334          * @param MsWait number in milliseconds
335          */

336
337     public void setConnectionMsWait (int MsWait)
338     {
339         assertNotConfigured();
340         this.connectionMsWait = MsWait;
341     }
342
343
344         /**
345          * Returns the number of milliseconds to wait for an available connection
346          * from the connection pool before throwing an exception
347          * @return number in milliseconds
348          */

349     public int getConnectionMsWait ()
350     {
351         if (connectionFactory != null)
352                         return connectionFactory.getMsWait();
353
354         return connectionMsWait;
355     }
356
357         /**
358          * Returns maximum number of PersistenceManager instances in the pool
359          * @return maxPool
360          */

361     public int getMaxPool () {
362         return maxPool;
363     }
364
365
366         /**
367          * Sets maximum number of PersistenceManager instances in the pool
368          * @param MaxPool maximum number of PersistenceManager instances
369          */

370     public void setMaxPool (int MaxPool) {
371         assertNotConfigured();
372         this.maxPool = MaxPool;
373     }
374
375         /**
376          * Returns minimum number of PersistenceManager instances in the pool
377          * @return minPool
378          */

379     public int getMinPool () {
380         return minPool;
381     }
382
383
384         /**
385          * Sets minimum number of PersistenceManager instances in the pool
386          * @param MinPool minimum number of PersistenceManager instances
387          */

388     public void setMinPool (int MinPool) {
389         assertNotConfigured();
390         this.minPool = MinPool;
391     }
392
393
394
395         /**
396          * Sets the amount of time, in milliseconds, between the connection
397          * manager's attempts to get a pooled connection.
398          * @param MsInterval the interval between attempts to get a database
399      * connection, in milliseconds.
400
401          */

402     public void setConnectionMsInterval (int MsInterval)
403     {
404         assertNotConfigured();
405         this.connectionMsInterval = MsInterval;
406     }
407
408
409         /**
410          * Returns the amount of time, in milliseconds, between the connection
411          * manager's attempts to get a pooled connection.
412          * @return the length of the interval between tries in milliseconds
413          */

414     public int getConnectionMsInterval ()
415     {
416         if (connectionFactory != null)
417                         return connectionFactory.getMsInterval();
418
419         return connectionMsInterval;
420     }
421
422
423         /**
424          * Sets the number of seconds to wait for a new connection to be
425      * established to the data source
426          * @param LoginTimeout wait time in seconds
427          */

428
429     public void setConnectionLoginTimeout (int LoginTimeout)
430     {
431         assertNotConfigured();
432         this.connectionLoginTimeout = LoginTimeout;
433     }
434
435
436         /**
437          * Returns the number of seconds to wait for a new connection to be
438          * established to the data source
439          * @return wait time in seconds
440          */

441     public int getConnectionLoginTimeout ()
442     {
443         if (connectionFactory != null) {
444                         return connectionFactory.getLoginTimeout();
445 /*
446         } else if (dataSource != null) {
447                         return dataSource.getLoginTimeout();
448 */

449         } else {
450             return connectionLoginTimeout;
451         }
452     }
453
454
455     /**
456          * Sets the LogWriter to which messages should be sent
457      * @param pw LogWriter
458      */

459     public void setConnectionLogWriter(PrintWriter JavaDoc pw)
460     {
461         assertNotConfigured();
462         this.connectionLogWriter = pw;
463     }
464
465     /**
466          * Returns the LogWriter to which messages should be sent
467      * @return LogWriter
468      */

469         public PrintWriter JavaDoc getConnectionLogWriter ()
470     {
471         return connectionLogWriter;
472     }
473
474     /**
475      * Sets the number of seconds to wait for a query statement
476      * to execute in the datastore associated with this PersistenceManagerFactory.
477      * @param timeout new timout value in seconds; zero means unlimited
478      */

479     public void setQueryTimeout (int timeout)
480     {
481         assertNotConfigured();
482         this.queryTimeout = timeout;
483     }
484
485     /**
486      * Gets the number of seconds to wait for a query statement
487      * to execute in the datastore associated with this PersistenceManagerFactory.
488      * @return timout value in seconds; zero means unlimited
489      */

490     public int getQueryTimeout ()
491     {
492         return queryTimeout;
493     }
494
495     /**
496          * Sets the number of seconds to wait for an update statement
497          * to execute in the datastore associated with this PersistenceManagerFactory.
498          * @param timeout new timout value in seconds; zero means unlimited
499          */

500         public void setUpdateTimeout (int timeout)
501         {
502                 assertNotConfigured();
503                 this.updateTimeout = timeout;
504         }
505
506         /**
507          * Gets the number of seconds to wait for an update statement
508          * to execute in the datastore associated with this PersistenceManagerFactory.
509          * @return timout value in seconds; zero means unlimited
510          */

511         public int getUpdateTimeout()
512         {
513                 return updateTimeout;
514         }
515
516
517     /**
518      * Sets transaction isolation level for all connections of this PersistenceManagerFactory.
519      * All validation is done by java.sql.Connection itself, so e.g. while Oracle
520      * will not allow to set solation level to TRANSACTION_REPEATABLE_READ, this method
521      * does not have any explicit restrictions
522      *
523      * @param level - one of the java.sql.Connection.TRANSACTION_* isolation values
524      */

525     public void setConnectionTransactionIsolation (int level)
526     {
527         assertNotConfigured();
528         txIsolation = level;
529     }
530
531     /**
532      * Returns current transaction isolation level for connections of this PersistenceManagerFactory.
533      * @return the current transaction isolation mode value as java.sql.Connection.TRANSACTION_*
534      */

535     public int getConnectionTransactionIsolation ()
536     {
537         if (connectionFactory != null)
538                         return connectionFactory.getTransactionIsolation();
539
540         return txIsolation;
541     }
542
543         /**
544          * Sets the optimistic flag for all PersistenceManagers
545      * @param flag String optimistic flag
546      */

547     public void setOptimistic (String JavaDoc flag)
548     {
549         setOptimistic(Boolean.getBoolean(flag));
550     }
551
552         /**
553          * Sets the optimistic flag for all PersistenceManagers
554      * @param flag boolean optimistic flag
555      */

556     public void setOptimistic (boolean flag)
557     {
558         assertNotConfigured();
559         optimistic = flag;
560
561         // Adjust depending flags
562
if (flag)
563              nontransactionalRead = flag;
564     }
565
566     /**
567          * Returns the boolean value of the optimistic flag for all PersistenceManagers
568      * @return boolean optimistic flag
569      */

570     public boolean getOptimistic ()
571     {
572         return optimistic;
573     }
574
575     /**
576          * Sets the RetainValues flag for all PersistenceManagers
577          * @param flag String RetainValues flag
578          */

579         public void setRetainValues (String JavaDoc flag)
580         {
581                 setRetainValues(Boolean.getBoolean(flag));
582         }
583
584     /**
585          * Sets flag that will not cause the eviction of persistent instances after transaction completion.
586      * @param flag boolean flag passed
587          */

588     public void setRetainValues (boolean flag)
589     {
590         assertNotConfigured();
591         retainValues = flag;
592
593         // Adjust depending flags
594
if (flag) {
595                         nontransactionalRead = flag;
596         }
597     }
598
599         /**
600          * Returns the boolean value for the flag that will not cause the eviction of persistent
601      * instances after transaction completion.
602          * @return boolean setting for the flag
603          */

604     public boolean getRetainValues ()
605     {
606         return retainValues;
607     }
608
609         /**
610          * Sets the NontransactionalRead flag for all PersistenceManagers
611          * @param flag String NontransactionalRead flag
612          */

613         public void setNontransactionalRead (String JavaDoc flag)
614         {
615                 setNontransactionalRead(Boolean.getBoolean(flag));
616         }
617
618         /**
619          * Sets the flag that allows non-transactional instances to be managed in the cache.
620          * @param flag boolean flag passed
621          */

622     public void setNontransactionalRead (boolean flag)
623     {
624         assertNotConfigured();
625         nontransactionalRead = flag;
626
627         // Adjust depending flags
628
if (flag == false)
629         {
630             retainValues = flag;
631             optimistic = flag;
632         }
633     }
634
635         /**
636          * Returns the boolean value for the flag that allows non-transactional instances
637      * to be managed in the cache.
638          * @return boolean setting for the flag
639          */

640     public boolean getNontransactionalRead ()
641     {
642         return nontransactionalRead;
643     }
644
645         /**
646          * Sets the IgnoreCache flag for all PersistenceManagers
647          * @param flag String IgnoreCache flag
648          */

649         public void setIgnoreCache (String JavaDoc flag)
650         {
651                 setIgnoreCache(Boolean.getBoolean(flag));
652         }
653
654     /**
655          * Sets the flag that allows the user to request that queries be optimized to return
656      * approximate results by ignoring changed values in the cache.
657          * @param flag boolean flag passed
658          */

659     public void setIgnoreCache (boolean flag)
660     {
661         assertNotConfigured();
662         throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
663                             "persistencemanagerfactoryimpl.notsupported")); //NOI18N
664
//ignoreCache = flag;
665
}
666
667     /**
668          * Returns the boolean value for the flag that allows the user to request that queries
669      * be optimized to return approximate results by ignoring changed values in the cache.
670      * @return boolean setting for the flag
671          */

672     public boolean getIgnoreCache ()
673     {
674         return ignoreCache;
675     }
676
677
678         /**
679          * Returns non-operational properties to be available to the application via a Properties instance.
680          * @return Properties object
681          */

682     public Properties JavaDoc getProperties ()
683     {
684         if (pmFactory != null)
685         {
686             return pmFactory.getProperties();
687         }
688         return RuntimeVersion.getVendorProperties(
689             "/com/sun/jdo/spi/persistence/support/sqlstore/sys.properties"); //NOI18N
690
}
691
692
693
694     /**
695      * Creates new <a HREF="PersistenceManager.html">PersistenceManager</a> without extra info
696      * @return the persistence manager
697      */

698     public PersistenceManager getPersistenceManager() {
699         return getPersistenceManager(null, null);
700     }
701
702     /**
703      * Returns the boolean value of the supersedeDeletedInstance flag
704      * for all PersistenceManagers. If set to true, deleted instances are
705      * allowed to be replaced with persistent-new instances with the equal
706      * Object Id.
707      * @return boolean supersedeDeletedInstance flag
708      */

709     public boolean getSupersedeDeletedInstance () {
710         return supersedeDeletedInstance;
711     }
712
713
714     /**
715      * Sets the supersedeDeletedInstance flag for all PersistenceManagers.
716      * @param flag boolean supersedeDeletedInstance flag
717      */

718     public void setSupersedeDeletedInstance (boolean flag) {
719         assertNotConfigured();
720         supersedeDeletedInstance = flag;
721     }
722
723     /**
724       * Returns the default value of the requireCopyObjectId flag
725       * for this PersistenceManagerFactoryImpl. If set to false, the PersistenceManager
726       * will not create a copy of an ObjectId for <code>PersistenceManager.getObjectId(Object pc)</code>
727       * and <code>PersistenceManager.getObjectById(Object oid)</code> requests.
728       *
729       * @see PersistenceManager#getObjectId(Object pc)
730       * @see PersistenceManager#getObjectById(Object oid)
731       * @return boolean requireCopyObjectId flag
732       */

733      public boolean getRequireCopyObjectId() {
734         return requireCopyObjectId;
735     }
736
737
738      /**
739       * Sets the default value of the requireCopyObjectId flag.
740       * If set to false, by default a PersistenceManager will not create a copy of
741       * an ObjectId for <code>PersistenceManager.getObjectId(Object pc)</code>
742       * and <code>PersistenceManager.getObjectById(Object oid)</code> requests.
743       *
744       * @see PersistenceManager#getObjectId(Object pc)
745       * @see PersistenceManager#getObjectById(Object oid)
746       * @param flag boolean requireCopyObjectId flag
747       */

748      public void setRequireCopyObjectId (boolean flag) {
749         assertNotConfigured();
750         requireCopyObjectId = flag;
751     }
752
753     /**
754      * Returns the boolean value of the requireTrackedSCO flag.
755      * If set to false, by default the PersistenceManager will not create
756      * tracked SCO instances for new persistent instances at commit with
757      * retainValues set to true and while retrieving data from a datastore.
758      *
759      * @return boolean requireTrackedSCO flag
760      */

761     public boolean getRequireTrackedSCO() {
762         return requireTrackedSCO;
763     }
764
765     /**
766      * Sets the requireTrackedSCO flag for this PersistenceManagerFactory.
767      * If set to false, by default the PersistenceManager will not create tracked
768      * SCO instances for new persistent instances at commit with retainValues set to true
769      * and while retrieving data from a datastore.
770      *
771      * @param flag boolean requireTrackedSCO flag
772      */

773     public void setRequireTrackedSCO (boolean flag) {
774         assertNotConfigured();
775         requireTrackedSCO = flag;
776     }
777
778
779   /**
780    * Creates new <a HREF="PersistenceManager.html">PersistenceManager</a> with specific
781    * username and password. Used to call ConnectionFactory.getConnection(String, String)
782    * @param username datasource user
783    * @param passwd datasource user password
784    * @return the persistence manager
785    */

786   public PersistenceManager getPersistenceManager (String JavaDoc username, String JavaDoc passwd){
787         synchronized (this) {
788
789         if (pmFactory == null) {
790             // Nothing there yet. Check and create
791
if (providedConnectionFactory == NOT_SET) {
792
793                 if (connectionFactoryName == null) {
794
795                 // Validate that MsWait/MsInterval are correct
796
assertConnectionWait();
797
798                 // only PMFactory was configured
799
// Create a default ConnectionFactoryImpl and
800
// set all the parameters. With 1st connection
801
// which happens during configuration od SqlStore
802
// the actaul connectionManager will be created
803
connectionFactory = new ConnectionFactoryImpl();
804
805                 connectionFactory.setURL(URL);
806                 connectionFactory.setUserName(userName);
807                 connectionFactory.setPassword(password);
808                                 connectionFactory.setDriverName(driverName);
809                                 connectionFactory.setMinPool(connectionMinPool);
810                                 connectionFactory.setMaxPool(connectionMaxPool);
811
812                 // MsWait MUST be set BEFORE MsInterval
813
connectionFactory.setMsWait(this.connectionMsWait);
814                 connectionFactory.setMsInterval(this.connectionMsInterval);
815                 connectionFactory.setLogWriter(this.connectionLogWriter);
816                 connectionFactory.setLoginTimeout(this.connectionLoginTimeout);
817                 if (txIsolation > 0)
818                     connectionFactory.setTransactionIsolation(txIsolation);
819                 } else {
820                 // Do JNDI lookup
821
try {
822                     javax.naming.InitialContext JavaDoc ctx =
823                         (javax.naming.InitialContext JavaDoc) Class.forName("javax.naming.InitialContext").newInstance(); //NOI18N
824
Object JavaDoc o = ctx.lookup(connectionFactoryName);
825                     if (EJBHelper.isManaged() || (o instanceof DataSource JavaDoc))
826                                         dataSource = o;
827                     else if (o instanceof ConnectionFactory)
828                                         connectionFactory = (ConnectionFactory) o;
829                     else
830                         throw new JDOUserException(I18NHelper.getMessage(
831                             messages,
832                                                 "persistencemanagerfactoryimpl.wrongtype")); //NOI18N
833

834                 } catch (JDOException e) {
835                     throw e; // rethrow it.
836

837                 } catch (ClassNotFoundException JavaDoc e) {
838                     throw new JDOUserException(I18NHelper.getMessage(messages,
839                                                 "persistencemanagerfactoryimpl.initialcontext")); //NOI18N
840

841                 } catch (Exception JavaDoc e) {
842                     throw new JDOUserException(I18NHelper.getMessage(messages,
843                                         "persistencemanagerfactoryimpl.lookup"), e); //NOI18N
844
}
845                 }
846             }
847
848             //If identifier is not yet set, set it to name of connection factory
849
if(getIdentifier() == null) {
850                 setIdentifier(getConnectionFactoryName());
851             }
852             // create new
853
pmFactory = new SQLPersistenceManagerFactory(this);
854                 // Check EJBHelper
855
pmFactory =
856                             (SQLPersistenceManagerFactory)EJBHelper.replaceInternalPersistenceManagerFactory(pmFactory);
857
858             }
859         } // end synchronized (this)
860

861         // Should not be called
862
if (username != null && connectionFactory != null) {
863             throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
864                 "persistencemanagerfactoryimpl.notsupported")); //NOI18N
865
}
866
867         return pmFactory.getPersistenceManager(username, passwd);
868
869     }
870
871         /**
872          * Sets default value of a known boolean property.
873          *
874          * @param name the name of the property to be set.
875          * @param value the default boolean value.
876          */

877         public void setBooleanProperty(String JavaDoc name, boolean value) {
878            // These if-else statements will be replaced by the JDO implementation...
879
if (name.equals("optimistic")) { // NOI18N
880
setOptimistic(value);
881
882            } else if (name.equals("retainValues")) { // NOI18N
883
setRetainValues(value);
884
885            } else if (name.equals("nontransactionalRead")) { // NOI18N
886
setNontransactionalRead(value);
887
888            } else if (name.equals("ignoreCache")) { // NOI18N
889
setIgnoreCache(value);
890
891            } else if (name.equals("supersedeDeletedInstance")) { // NOI18N
892
setSupersedeDeletedInstance(value);
893
894            } else if (name.equals("requireCopyObjectId")) { // NOI18N
895
setRequireCopyObjectId(value);
896
897            } else if (name.equals("requireTrackedSCO")) { // NOI18N
898
setRequireTrackedSCO(value);
899
900            } // else ignore it.
901

902     }
903
904         /**
905          * Determines whether obj is a PersistenceManagerFactoryImpl with the same configuration
906          *
907          * @param obj The possibly null object to check.
908          * @return true if obj is equal to this PersistenceManagerFactoryImpl; false otherwise.
909          */

910     public boolean equals(Object JavaDoc obj) {
911             if ((obj == null) || !(obj instanceof PersistenceManagerFactoryImpl)) {
912             return false;
913         }
914             PersistenceManagerFactoryImpl pmf = (PersistenceManagerFactoryImpl)obj;
915
916         if (pmf.providedConnectionFactory == this.providedConnectionFactory) {
917             if (pmf.providedConnectionFactory == SET_AS_CONNECTIONFACTORY) {
918                 return (pmf.connectionFactory.equals(this.connectionFactory) &&
919                                        equalBooleanProperties(pmf));
920
921             } else if (pmf.providedConnectionFactory == SET_AS_DATASOURCE) {
922                 return (pmf.dataSource.equals(this.dataSource) &&
923                                        equalBooleanProperties(pmf));
924
925             } else if (pmf.connectionFactoryName != null) {
926                 return (pmf.connectionFactoryName.equals(this.connectionFactoryName) &&
927                                        equalBooleanProperties(pmf));
928
929             }
930             return (pmf.URL.equals(this.URL) && pmf.userName.equals(this.userName) &&
931                 pmf.password.equals(this.password) &&
932                 pmf.driverName.equals(this.driverName) &&
933                                        equalBooleanProperties(pmf));
934         }
935         return false;
936         }
937
938         /**
939          * Computes the hash code of this PersistenceManagerFactory.
940          *
941          * @return A hash code of the owning PersistenceManagerFactory as an int.
942          */

943         public int hashCode() {
944                 if (providedConnectionFactory == SET_AS_CONNECTIONFACTORY) {
945             return connectionFactory.hashCode();
946                 } else if (providedConnectionFactory == SET_AS_DATASOURCE) {
947             return dataSource.hashCode();
948         } else if (connectionFactoryName != null) {
949             return connectionFactoryName.hashCode();
950                 }
951         return URL.hashCode() + userName.hashCode() + password.hashCode() + driverName.hashCode();
952         }
953
954     /**
955      * INTERNAL
956      * Asserts that change to the property is allowed
957      */

958     private void assertNotConfigured() {
959         if ( pmFactory != null) {
960             throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
961                                 "persistencemanagerfactoryimpl.configured")); //NOI18N
962
}
963     }
964
965
966     /**
967      * INTERNAL
968      * Asserts that MsWait and MsInterval are properly configured
969      */

970     private void assertConnectionWait() {
971         if ( connectionMsWait < 0 )
972         {
973             throw new JDOUserException(I18NHelper.getMessage(messages,
974                                              "connection.connectionmanager.mswaitvalue")); // NOI18N
975
}
976         else if ( connectionMsInterval < 0 ||
977             connectionMsInterval > connectionMsWait ||
978             (connectionMsWait > 0 && connectionMsInterval == 0) )
979         {
980                 throw new JDOUserException(I18NHelper.getMessage(messages,
981                                              "connection.connectionmanager.msintervalvalue")); // NOI18N
982
}
983     }
984
985         /**
986          * Compares boolean setting on 2 PersistenceManagerFactory instances.
987          *
988          * @param pmf the PersistenceManagerFactory instance to compare with this instance.
989          */

990         private boolean equalBooleanProperties(PersistenceManagerFactory pmf) {
991             return (pmf.getOptimistic() == optimistic &&
992                        pmf.getRetainValues() == retainValues &&
993                        pmf.getNontransactionalRead() == nontransactionalRead &&
994                        pmf.getIgnoreCache() == ignoreCache &&
995                        pmf.getSupersedeDeletedInstance() == supersedeDeletedInstance &&
996                        pmf.getRequireCopyObjectId() == requireCopyObjectId &&
997                        pmf.getRequireTrackedSCO() == requireTrackedSCO
998                    );
999
1000        }
1001}
1002
Popular Tags