KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > pm > lib > SpeedoProxyManagerFactory


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27
28 package org.objectweb.speedo.pm.lib;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Properties JavaDoc;
39
40 import javax.jdo.datastore.DataStoreCache;
41 import javax.jdo.listener.InstanceLifecycleListener;
42 import javax.jdo.JDODataStoreException;
43 import javax.jdo.JDOException;
44 import javax.jdo.JDOFatalInternalException;
45 import javax.jdo.JDOUnsupportedOptionException;
46 import javax.jdo.JDOUserException;
47 import javax.jdo.PersistenceManager;
48 import javax.jdo.PersistenceManagerFactory;
49
50 import org.objectweb.fractal.api.Component;
51 import org.objectweb.fractal.api.Interface;
52 import org.objectweb.fractal.api.NoSuchInterfaceException;
53 import org.objectweb.fractal.api.control.BindingController;
54 import org.objectweb.jorm.api.PClassMapping;
55 import org.objectweb.jorm.api.PException;
56 import org.objectweb.jorm.api.PMapper;
57 import org.objectweb.perseus.cache.api.CacheEntry;
58 import org.objectweb.perseus.cache.api.CacheEntryFilter;
59 import org.objectweb.perseus.cache.api.CacheException;
60 import org.objectweb.perseus.cache.api.CacheManager;
61 import org.objectweb.perseus.cache.api.FixableCacheEntry;
62 import org.objectweb.perseus.cache.api.UnbindManager;
63 import org.objectweb.perseus.persistence.api.PersistenceException;
64 import org.objectweb.perseus.persistence.api.TransactionalPersistenceManager;
65 import org.objectweb.perseus.pool.api.Pool;
66 import org.objectweb.speedo.Speedo;
67 import org.objectweb.speedo.api.ExceptionHelper;
68 import org.objectweb.speedo.api.SpeedoProperties;
69 import org.objectweb.speedo.mapper.api.ConnectionSpecFactory;
70 import org.objectweb.speedo.mapper.api.JormFactory;
71 import org.objectweb.speedo.mim.api.SpeedoHome;
72 import org.objectweb.speedo.mim.api.SpeedoProxy;
73 import org.objectweb.speedo.pm.api.ProxyManager;
74 import org.objectweb.speedo.pm.api.ProxyManagerFactory;
75 import org.objectweb.speedo.pm.api.ProxyManagerSwitch;
76 import org.objectweb.speedo.sequence.api.SequenceManager;
77 import org.objectweb.util.monolog.api.BasicLevel;
78 import org.objectweb.util.monolog.api.Logger;
79
80 /**
81  * The SpeepoProxyManagerFactory is a factory of ProxyManager. This primitive
82  * component exports the ProxyManagerFactory interface, permitting to manage the
83  * proxy manager allocations. Due to a high cost of ProxyManager instances
84  * allocation, the unsused ProxyManager are pooled. For this reason, the
85  * SpeedoProxyManagerFactory uses a Pool of SpeedoProxyManager. The
86  * SpeedoProxyManagerFactory manages also the attachement of ProxyManager to the
87  * current thread through a ProxyManagerSwitch.
88  *
89  * @see org.objectweb.speedo.pm.api.ProxyManager
90  * @see org.objectweb.speedo.pm.api.ProxyManagerSwitch
91  * @see org.objectweb.speedo.pm.lib.SpeedoProxyManager
92  * @see org.objectweb.speedo.pm.lib.SpeedoProxyManagerFactory
93  * @see org.objectweb.speedo.pm.lib.SpeedoProxyManagerInstanciator
94  * @see org.objectweb.speedo.pm.lib.SpeedoProxyManagerSwitch
95  *
96  * @author S.Chassande-Barrioz
97  */

98 public class SpeedoProxyManagerFactory
99         implements ProxyManagerFactory,
100         PersistenceManagerFactory,
101         BindingController {
102
103     public final static String JavaDoc MAPPER_BINDING = "mapper";
104     public final static String JavaDoc JORM_FACTORY_BINDING = "jorm-factory";
105     public final static String JavaDoc PM_POOL_BINDING = "proxy-manager-pool";
106     public final static String JavaDoc PROXY_MANAGER_SWITCH_BINDING = "proxy-manager-switch";
107     public final static String JavaDoc TPM_BINDING = "transactional-persistence-manager";
108     public final static String JavaDoc CACHE_M_BINDING = "cache-manager";
109     public final static String JavaDoc UNBIND_M_BINDING = "unbind-manager";
110     public final static String JavaDoc COMPONENT_BINDING = "component";
111     public final static String JavaDoc SEQUENCE_M_BINDING = "sequence-manager";
112     
113     private final static String JavaDoc optionArray[] = {
114             SpeedoProperties.JDO_OPTION_TRANSIENT_TRANSACTIONAL,
115             //not yet supportded JDO_OPTION_NON_TRANSACTIONAL_READ,
116
//not yet supportded ed JDO_OPTION_NON_TRANSACTIONAL_WRITE,
117
SpeedoProperties.JDO_OPTION_RETAIN_VALUES,
118             //not yet supportded JDO_OPTION_RESTORE_VALUES,
119
SpeedoProperties.JDO_OPTION_OPTIMISTIC,
120             SpeedoProperties.JDO_OPTION_APPLICATION_IDENTITY,
121             SpeedoProperties.JDO_OPTION_DATASTORE_IDENTITY,
122             SpeedoProperties.JDO_OPTION_NON_DURABLE_IDENTITY,
123             SpeedoProperties.JDO_OPTION_ARRAY_LIST,
124             SpeedoProperties.JDO_OPTION_HASH_MAP,
125             SpeedoProperties.JDO_OPTION_HASH_TABLE,
126             SpeedoProperties.JDO_OPTION_LINKED_LIST,
127             SpeedoProperties.JDO_OPTION_TREE_MAP,
128             SpeedoProperties.JDO_OPTION_TREE_SET,
129             SpeedoProperties.JDO_OPTION_VECTOR,
130             SpeedoProperties.JDO_OPTION_ARRAY,
131             SpeedoProperties.JDO_OPTION_MAP,
132             SpeedoProperties.JDO_OPTION_LIST,
133             SpeedoProperties.JDO_OPTION_NULL_COLLECTION,
134             SpeedoProperties.JDO_QUERY_JDOQL
135     };
136
137     private final static int GETMAXPOOL = 100;
138     private boolean imbricatedAuthorized = true;
139
140     /**
141      * Pool of PersistenceManager
142      */

143     private Pool managedPM = null;
144
145     /**
146      * Manages the log's traces
147      */

148     private Logger logger = null;
149
150     /**
151      * JORM object encapsulating the datastore
152      */

153     private PMapper mapper = null;
154
155     private Properties JavaDoc connectionProperties = null;
156
157     private ProxyManagerSwitch pms = null;
158
159     private Object JavaDoc _this = null;
160
161     private ConnectionSpecFactory csf = null;
162
163     private boolean started = false;
164
165     private CacheManager cacheManager = null;
166
167     private UnbindManager unbindManager = null;
168
169     private JormFactory jormFactory = null;
170     
171     private TransactionalPersistenceManager tpm = null;
172
173     private SequenceManager sequenceManager = null;
174     
175     public SpeedoProxyManagerFactory() {
176         connectionProperties = new Properties JavaDoc();
177     }
178
179     private ProxyManagerFactory getThis() {
180         if (_this == null) {
181             throw new JDODataStoreException(
182                     "Cannot provides a PersistenceManager without a reference to the component");
183         } else if (_this instanceof Component) {
184             try {
185                 _this = ((Component)_this).getFcInterface("proxy-manager-factory");
186             } catch (NoSuchInterfaceException e) {
187                 throw new JDODataStoreException(
188                         "Cannot provides a PersistenceManager ", new Exception JavaDoc[]{e});
189             }
190         }
191         return (ProxyManagerFactory) _this;
192     }
193
194     /**
195      * Verify that an instance is persistence capable.
196      * @param pc the object to test.
197      * @exception javax.jdo.JDOUserException if the object is not persistence capable.
198      */

199     private void assertIsSpeedoProxy(Object JavaDoc pc, String JavaDoc cmd) {
200         if (!(pc instanceof SpeedoProxy)) {
201             if (pc == null) {
202                 throw new JDOUserException("Null persistent object instance specified");
203             } else if (pc.getClass().isArray()
204                 || pc instanceof Collection JavaDoc) {
205                 throw new JDOUserException("You must use the " + cmd
206                     + "All method with a multivalued parameter: " + pc);
207             } else {
208                 throw new JDOUserException("The object is not a Speedoproxy, beware to : " + pc);
209             }
210         }
211     }
212
213     // IMPLEMENTATION OF THE UserBindingController INTERFACE //
214
//-------------------------------------------------------//
215

216     public String JavaDoc[] listFc() {
217         return new String JavaDoc[] {
218             PM_POOL_BINDING,
219             MAPPER_BINDING,
220             PROXY_MANAGER_SWITCH_BINDING,
221             CACHE_M_BINDING,
222             UNBIND_M_BINDING,
223             JORM_FACTORY_BINDING,
224             TPM_BINDING,
225             SEQUENCE_M_BINDING
226         };
227     }
228
229     public Object JavaDoc lookupFc(String JavaDoc s) {
230         if (PM_POOL_BINDING.equals(s))
231             return managedPM;
232         else if (MAPPER_BINDING.equals(s))
233             return mapper;
234         else if (PROXY_MANAGER_SWITCH_BINDING.equals(s))
235             return pms;
236         else if (CACHE_M_BINDING.equals(s))
237             return cacheManager;
238         else if (UNBIND_M_BINDING.equals(s))
239             return unbindManager;
240         else if (JORM_FACTORY_BINDING.equals(s))
241             return jormFactory;
242         else if (TPM_BINDING.equals(s))
243             return tpm;
244         else if (SEQUENCE_M_BINDING.equals(s))
245             return sequenceManager;
246         else
247             return null;
248     }
249
250     public void bindFc(String JavaDoc s, Object JavaDoc o) {
251         if ("logger".equals(s)) {
252             logger = (Logger) o;
253         } else if (PM_POOL_BINDING.equals(s)) {
254             managedPM = (Pool) o;
255         } else if (MAPPER_BINDING.equals(s)) {
256             mapper = (PMapper) o;
257         } else if (PROXY_MANAGER_SWITCH_BINDING.equals(s)) {
258             pms = (ProxyManagerSwitch) o;
259         } else if (CACHE_M_BINDING.equals(s)) {
260             cacheManager = (CacheManager) o;
261         } else if (UNBIND_M_BINDING.equals(s)) {
262             unbindManager = (UnbindManager) o;
263         } else if (COMPONENT_BINDING.equals(s)) {
264             _this = o;
265         } else if (JORM_FACTORY_BINDING.equals(s)) {
266             jormFactory = (JormFactory) o;
267         } else if (TPM_BINDING.equals(s)) {
268             tpm = (TransactionalPersistenceManager) o;
269         } else if (SEQUENCE_M_BINDING.equals(s)) {
270             sequenceManager = (SequenceManager) o;
271         }
272     }
273
274     public void unbindFc(String JavaDoc s) {
275         if (PM_POOL_BINDING.equals(s)) {
276             managedPM = null;
277         } else if (MAPPER_BINDING.equals(s)) {
278             mapper = null;
279         } else if (PROXY_MANAGER_SWITCH_BINDING.equals(s)) {
280             pms = null;
281         } else if (CACHE_M_BINDING.equals(s)) {
282             cacheManager = null;
283         } else if (UNBIND_M_BINDING.equals(s)) {
284             unbindManager = null;
285         } else if (JORM_FACTORY_BINDING.equals(s)) {
286             jormFactory = null;
287         } else if (TPM_BINDING.equals(s)) {
288             tpm = null;
289         } else if (SEQUENCE_M_BINDING.equals(s)) {
290             sequenceManager = null;
291         }
292     }
293
294     // IMPLEMENTATION OF THE DataStoreCache INTERFACE //
295
//------------------------------------------------//
296
public void evict(Object JavaDoc oid) {
297         try {
298             tpm.evict(null, oid, false);
299         } catch (PersistenceException e) {
300             throw new JDOException("Error during the eviction of an entry: ", e);
301         }
302     }
303     public void evictAll(Class JavaDoc clazz, boolean subClasses) {
304         final List JavaDoc classesToEvict = getClasses(clazz, subClasses);
305         try {
306             unbindManager.unbind(new CacheEntryFilter() {
307                 public boolean accept(CacheEntry ce) {
308                     Object JavaDoc o = ce.getCeObject();
309                     return o != null && classesToEvict.contains(o.getClass());
310                 }
311             }, false);
312         } catch (CacheException e) {
313             throw new JDOException("Error during the eviction of entries of the class '"
314                     + clazz.getName() + "' with"
315                     + (subClasses ? "" : "out") + " sub classes: ", e);
316         }
317     }
318     
319     private List JavaDoc getClasses(Class JavaDoc clazz, boolean subClasses) {
320         List JavaDoc classes = new ArrayList JavaDoc();
321         classes.add(clazz);
322         if (subClasses) {
323             PClassMapping[] subpcms;
324             try {
325                 subpcms = jormFactory.getPClassMapping(clazz).getSubPCMs();
326             } catch (PException e1) {
327                 return classes;
328             }
329             for (int i = 0; i < subpcms.length; i++) {
330                 ClassLoader JavaDoc cl = subpcms[i].getClass().getClassLoader();
331                 if (cl == null) {
332                     cl = getClass().getClassLoader();
333                     if (cl == null) {
334                         cl = ClassLoader.getSystemClassLoader();
335                     }
336                 }
337                 try {
338                     Class JavaDoc subclass = cl.loadClass(subpcms[i].getClassName());
339                     classes.add(subclass);
340                 } catch (ClassNotFoundException JavaDoc e) {
341                     logger.log(BasicLevel.WARN,
342                             "Impossible to load the class '"
343                             + subpcms[i].getClassName() + "': ", e);
344                     continue;
345                 }
346             }
347         }
348         return classes;
349     }
350
351     public void evictAll(Collection JavaDoc oids) {
352         try {
353             unbindManager.unbind(oids, false);
354         } catch (CacheException e) {
355             throw new JDOException("Error during the eviction of an entry: ", e);
356         }
357     }
358     public void evictAll(Object JavaDoc[] oids) {
359         evictAll(Arrays.asList(oids));
360     }
361     public void evictAll() {
362         try {
363             unbindManager.unbindUnfixed(false);
364         } catch (CacheException e) {
365             throw new JDOException("Error during the eviction of an entry: ", e);
366         }
367     }
368     
369     public void pin(Object JavaDoc oid) {
370         FixableCacheEntry ce = (FixableCacheEntry) cacheManager.lookup(oid);
371         if (ce != null && ce.getCeFixCount() == 0) {
372             try {
373                 cacheManager.fix(ce);
374             } catch (CacheException e) {
375                 logger.log(BasicLevel.WARN, "Impossible to pin the persistent class: ", e);
376             }
377         } //else TODO: pin forever an oid by creating the CacheEntry
378
}
379     public void pinAll(Class JavaDoc clazz, boolean subClasses) {
380         final List JavaDoc classesToPin = getClasses(clazz, subClasses);
381         try {
382             cacheManager.unfix(new CacheEntryFilter() {
383                 public boolean accept(CacheEntry ce) {
384                     Object JavaDoc o = ce.getCeObject();
385                     return ((FixableCacheEntry) ce).getCeFixCount() == 0
386                         && o != null && classesToPin.contains(o.getClass());
387                 }
388             });
389         } catch (CacheException e) {
390             throw new JDOException("Error during the eviction of entries of the class '"
391                     + clazz.getName() + "' with"
392                     + (subClasses ? "" : "out") + " sub classes: ", e);
393         }
394         // TODO: pin forever
395
}
396     
397     public void pinAll(Collection JavaDoc oids) {
398         for (Iterator JavaDoc iter = oids.iterator(); iter.hasNext();) {
399             pin(iter.next());
400         }
401     }
402     
403     public void pinAll(Object JavaDoc[] oids) {
404         pinAll(Arrays.asList(oids));
405     }
406     
407     public void unpin(Object JavaDoc oid) {
408         FixableCacheEntry ce = (FixableCacheEntry) cacheManager.lookup(oid);
409         if (ce != null && ce.getCeFixCount() == 0) {
410             try {
411                 cacheManager.unfix(ce);
412             } catch (CacheException e) {
413                 logger.log(BasicLevel.WARN, "Impossible to pin the persistent class: ", e);
414             }
415         }
416     }
417     public void unpinAll(Class JavaDoc clazz, boolean subClasses) {
418         final List JavaDoc classesToUnpin = getClasses(clazz, subClasses);
419         try {
420             cacheManager.unfix(new CacheEntryFilter() {
421                 public boolean accept(CacheEntry ce) {
422                     Object JavaDoc o = ce.getCeObject();
423                     return ((FixableCacheEntry) ce).getCeFixCount() == 1
424                         && o != null && classesToUnpin.contains(o.getClass());
425                 }
426             });
427         } catch (CacheException e) {
428             throw new JDOException("Error during the eviction of entries of the class '"
429                     + clazz.getName() + "' with"
430                     + (subClasses ? "" : "out") + " sub classes: ", e);
431         }
432         // TODO: unpin forever
433
}
434     public void unpinAll(Collection JavaDoc oids) {
435         for (Iterator JavaDoc iter = oids.iterator(); iter.hasNext();) {
436             unpin(iter.next());
437         }
438     }
439     public void unpinAll(Object JavaDoc[] oids) {
440         unpinAll(Arrays.asList(oids));
441     }
442
443     // IMPLEMENTATION OF THE ProxyManagerFactory INTERFACE //
444
//-----------------------------------------------------//
445

446     public ProxyManager lookup() {
447         return pms.lookup(getThis());
448     }
449
450     public void bindPM2Thread(ProxyManager pm) {
451         if (logger.isLoggable(BasicLevel.DEBUG)) {
452             logger.log(BasicLevel.DEBUG, "Bind a pm to the thread: " + pm);
453         }
454         pms.bind(pm);
455     }
456
457     public void unbindPM() {
458         if (logger.isLoggable(BasicLevel.DEBUG)) {
459             logger.log(BasicLevel.DEBUG, "Unbind the current PM from the thread");
460         }
461         pms.unbind(getThis());
462     }
463
464     public void proxyManagerClosed(ProxyManager pr) {
465         try {
466             if (logger.isLoggable(BasicLevel.DEBUG)) {
467                 logger.log(BasicLevel.DEBUG, "Unbind a PM from the thread: " + pr);
468             }
469             pms.unbind(pr);
470             managedPM.releaseResource(pr);
471         } catch (Exception JavaDoc e) {
472             throw new JDOFatalInternalException(
473                     "Impossible to release this PersistenceManager in the pool.",
474                     new Exception JavaDoc[]{ExceptionHelper.getNested(e)});
475         }
476     }
477
478     public SequenceManager getSequenceManager() {
479         return sequenceManager;
480     }
481
482     public void setSequenceManager(SequenceManager sequenceManager) {
483         this.sequenceManager = sequenceManager;
484     }
485     
486     // IMPLEMENTATION OF THE PersistentManagerFactory INTERFACE //
487
//----------------------------------------------------------//
488

489     /** Get an instance of PersistenceManager from this factory. The instance has
490      * default values for options.
491      * Invokes <code>init</code> at the first call.
492      *
493      * @return a PersistenceManager instance with default options.
494      */

495     public PersistenceManager getPersistenceManager() {
496         return getPersistenceManager(null);
497     }
498
499     public void close() {
500         //TODO: implements the close method on the PMF
501
}
502
503     /** Get an instance of PersistenceManager from this factory. The instance has
504      * default values for options. The parameters userid and password are used
505      * when obtaining datastore connections from the connection pool.
506      * Invokes <code>init</code> at the first call.
507      *
508      * @param userid the userid for the connection
509      * @param password the password for the connection
510      * @return a PersistenceManager instance with default options.
511      */

512     public PersistenceManager getPersistenceManager(String JavaDoc userid, String JavaDoc password) {
513         Object JavaDoc cs = null;
514         if (csf == null) {
515             String JavaDoc cn = connectionProperties.getProperty(Speedo.CONNECTION_SPEC_FACTORY);
516             if (cn != null && cn.length() > 0) {
517                 try {
518                     csf = (ConnectionSpecFactory) Class.forName(cn).newInstance();
519                     cs = csf.getConnectionSpec(this, userid, password);
520                 } catch (Exception JavaDoc e) {
521                     logger.log(BasicLevel.ERROR,
522                         "Impossible to instanciate the ConnectionSpecFactory: "
523                         + cn, e);
524                 }
525             }
526         } else {
527             cs = csf.getConnectionSpec(this, userid, password);
528         }
529         return getPersistenceManager(cs);
530     }
531
532     public synchronized PersistenceManager getPersistenceManager(Object JavaDoc cs) {
533         if (!started) {
534             imbricatedAuthorized = Boolean.valueOf(
535                     connectionProperties.getProperty(
536                             SpeedoProperties.IMRICATED_PM_ALLOWED, "false"))
537                             .booleanValue();
538             started = true;
539         }
540         if (imbricatedAuthorized) {
541             ProxyManager pm = pms.lookup(getThis());
542             if (pm != null) {
543                 pm.getSemaphore().P();
544                 try {
545                     if ((cs == null && pm.getConnectionSpec() == null)
546                             || (cs != null && cs.equals(pm.getConnectionSpec()))) {
547                         if (!pm.isClosed()) {
548                             logger.log(BasicLevel.INFO, "reuse the same PersistenceManager (Imbricated)");
549                             pm.addUse();
550                             return pm;
551                         }
552                     }
553                 } finally {
554                     pm.getSemaphore().V();
555                 }
556             }
557         }
558         
559         try {
560             // Gets a PM from the pool
561
Object JavaDoc pr = managedPM.getResource(null);
562             logger.log(BasicLevel.DEBUG, "get a persistenceManager from the pool");
563             Component ci = ((Interface) pr).getFcItfOwner();
564             ProxyManager pm = (ProxyManager) ci.getFcInterface("proxy-manager");
565             pm.open(cs);
566             bindPM2Thread(pm);
567             pm.addUse();
568             return pm;
569         } catch (Exception JavaDoc e) {
570             throw new JDODataStoreException(
571                     "Cannot provides a PersistenceManager ", new Exception JavaDoc[]{e});
572         }
573     }
574
575     public DataStoreCache getDataStoreCache() {
576         return this;
577     }
578
579     public boolean isClosed() {
580         //TODO: implement the isclosed method on PMF
581
return false;
582     }
583     public String JavaDoc getMapping() {
584         return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_MAPPING, "no user name defined");
585     }
586     public void setMapping(String JavaDoc arg0) {
587         connectionProperties.setProperty(SpeedoProperties.JDO_OPTION_MAPPING, arg0);
588     }
589
590     /** Set the user name for the data store connection.
591      * @param userName the user name for the data store connection.
592      */

593     public void setConnectionUserName(String JavaDoc userName) {
594         connectionProperties.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, userName);
595     }
596
597     /** Get the user name for the data store connection.
598      * @return the user name for the data store connection.
599      */

600     public String JavaDoc getConnectionUserName() {
601         return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_USER_NAME, "no user name defined");
602     }
603
604     /** Set the password for the data store connection.
605      * @param password the password for the data store connection.
606      */

607     public void setConnectionPassword(String JavaDoc password) {
608         connectionProperties.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD, password);
609     }
610
611     /** Get the password for the data store connection.
612      * @return password the password for the data store connection.
613      */

614     protected String JavaDoc getConnectionPassword() {
615         return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_PASSWORD);
616     }
617
618     /** Set the URL for the data store connection.
619      * @param URL the URL for the data store connection.
620      */

621     public void setConnectionURL(String JavaDoc URL) {
622         connectionProperties.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL, URL);
623     }
624
625     /** Get the URL for the data store connection.
626      * @return the URL for the data store connection.
627      */

628     public String JavaDoc getConnectionURL() {
629         return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_URL);
630     }
631
632     /** Set the driver name for the data store connection.
633      * @param driverName the driver name for the data store connection.
634      */

635     public void setConnectionDriverName(String JavaDoc driverName) {
636         if (started) {
637             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
638         }
639         connectionProperties.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME, driverName);
640     }
641
642     /** Get the driver name for the data store connection.
643      * @return the driver name for the data store connection.
644      */

645     public String JavaDoc getConnectionDriverName() {
646         return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_DRIVER_NAME);
647     }
648
649     /** Set the name for the data store connection factory.
650      * @param connectionFactoryName the name of the data store connection factory.
651      */

652     public void setConnectionFactoryName(String JavaDoc connectionFactoryName) {
653         if (started) {
654             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
655         }
656         connectionProperties.setProperty(
657                 SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY_NAME, connectionFactoryName);
658     }
659
660     /** Get the name for the data store connection factory.
661      * @return the name of the data store connection factory.
662      */

663     public String JavaDoc getConnectionFactoryName() {
664         return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY_NAME);
665     }
666
667     /** Set the data store connection factory. JDO implementations
668      * will support specific connection factories. The connection
669      * factory interfaces are not part of the JDO specification.
670      * @param connectionFactory the data store connection factory.
671      */

672     public void setConnectionFactory(Object JavaDoc connectionFactory) {
673         if (started) {
674             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
675         }
676         throw new JDOUnsupportedOptionException("Cannot set a Connection Factory, use <> instead");
677     }
678
679     /** Get the data store connection factory.
680      * @return the data store connection factory.
681      */

682     public Object JavaDoc getConnectionFactory() {
683         return mapper.getConnectionFactory();
684     }
685
686     /** Set the name for the second data store connection factory. This is
687      * needed for managed environments to get nontransactional connections for
688      * optimistic transactions.
689      * @param connectionFactoryName the name of the data store connection factory.
690      */

691     public void setConnectionFactory2Name(String JavaDoc connectionFactoryName) {
692         if (started) {
693             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
694         }
695         connectionProperties.setProperty(SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY2_NAME, connectionFactoryName);
696     }
697
698     /** Get the name for the second data store connection factory. This is
699      * needed for managed environments to get nontransactional connections for
700      * optimistic transactions.
701      * @return the name of the data store connection factory.
702      */

703     public String JavaDoc getConnectionFactory2Name() {
704         return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_CONNECTION_FACTORY2_NAME);
705     }
706
707     /** Set the second data store connection factory. This is
708      * needed for managed environments to get nontransactional connections for
709      * optimistic transactions. JDO implementations
710      * will support specific connection factories. The connection
711      * factory interfaces are not part of the JDO specification.
712      * @param connectionfactory the data store connection factory.
713      */

714     public void setConnectionFactory2(Object JavaDoc connectionfactory) {
715         if (started) {
716             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
717         }
718         throw new JDOUnsupportedOptionException(
719                 "Cannot set a second Connection Factory");
720     }
721
722     /** Get the second data store connection factory. This is
723      * needed for managed environments to get nontransactional connections for
724      * optimistic transactions.
725      * @return the data store connection factory.
726      */

727     public Object JavaDoc getConnectionFactory2() {
728         return mapper.getConnectionFactory();
729     }
730
731     /** Set the default Multithreaded setting for all PersistenceManager instances
732      * obtained from this factory.
733      *
734      * @param flag the default Multithreaded setting.
735      */

736     public void setMultithreaded(boolean flag) {
737         if (started) {
738             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
739         }
740         connectionProperties.setProperty(
741                 SpeedoProperties.JDO_OPTION_MULTITREADED, new Boolean JavaDoc(flag).toString());
742     }
743
744     /** Get the default Multithreaded setting for all PersistenceManager instances
745      * obtained from this factory.
746      *
747      * @return the default Multithreaded setting.
748      */

749     public boolean getMultithreaded() {
750         return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_MULTITREADED, "").equals("true");
751     }
752
753     /** Set the default Optimistic setting for all PersistenceManager instances
754      * obtained from this factory.
755      *
756      * @param flag the default Optimistic setting.
757      */

758     public void setOptimistic(boolean flag) {
759         if (started) {
760             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
761         }
762         connectionProperties.setProperty(
763                 SpeedoProperties.JDO_OPTION_OPTIMISTIC, new Boolean JavaDoc(flag).toString());
764     }
765
766     /** Get the default Optimistic setting for all PersistenceManager instances
767      * obtained from this factory.
768      *
769      * @return the default Optimistic setting.
770      */

771     public boolean getOptimistic() {
772         return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_OPTIMISTIC,"").equals("true");
773     }
774
775     /** Set the default RetainValues setting for all PersistenceManager instances
776      * obtained from this factory.
777      *
778      * @param flag the default RetainValues setting.
779      */

780     public void setRetainValues(boolean flag) {
781         if (started) {
782             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
783         }
784         connectionProperties.setProperty(
785                 SpeedoProperties.JDO_OPTION_RETAIN_VALUES, new Boolean JavaDoc(flag).toString());
786     }
787
788     /** Get the default RetainValues setting for all PersistenceManager instances
789      * obtained from this factory.
790      *
791      * @return the default RetainValues setting.
792      */

793     public boolean getRetainValues() {
794         return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_RETAIN_VALUES, "").equals("true");
795     }
796
797     /** Set the default NontransactionalRead setting for all PersistenceManager instances
798      * obtained from this factory.
799      *
800      * @param flag the default NontransactionalRead setting.
801      */

802     public void setNontransactionalRead(boolean flag) {
803         if (started) {
804             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
805         }
806         connectionProperties.setProperty(
807                 SpeedoProperties.JDO_OPTION_NON_TRANSACTIONAL_READ, new Boolean JavaDoc(flag).toString());
808     }
809
810     /** Get the default NontransactionalRead setting for all PersistenceManager instances
811      * obtained from this factory.
812      *
813      * @return the default NontransactionalRead setting.
814      */

815     public boolean getNontransactionalRead() {
816         return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_NON_TRANSACTIONAL_READ).equals("true");
817     }
818
819     /** Set the default NontransactionalWrite setting for all PersistenceManager instances
820      * obtained from this factory.
821      *
822      * @param flag the default NontransactionalWrite setting.
823      */

824     public void setNontransactionalWrite(boolean flag) {
825         if (started) {
826             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
827         }
828         connectionProperties.setProperty(
829                 SpeedoProperties.JDO_OPTION_NON_TRANSACTIONAL_WRITE, new Boolean JavaDoc(flag).toString());
830     }
831
832     /** Get the default NontransactionalWrite setting for all PersistenceManager instances
833      * obtained from this factory.
834      *
835      * @return the default NontransactionalWrite setting.
836      */

837     public boolean getNontransactionalWrite() {
838         return connectionProperties.getProperty(SpeedoProperties.JDO_OPTION_NON_TRANSACTIONAL_WRITE).equals("true");
839     }
840
841     /** Set the default IgnoreCache setting for all PersistenceManager instances
842      * obtained from this factory.
843      *
844      * @param flag the default IgnoreCache setting.
845      */

846     public void setIgnoreCache(boolean flag) {
847         if (started) {
848             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
849         }
850         connectionProperties.setProperty(
851                 SpeedoProperties.JDO_OPTION_IGNORE_CACHE, new Boolean JavaDoc(flag).toString());
852     }
853
854     /** Get the default IgnoreCache setting for all PersistenceManager instances
855      * obtained from this factory.
856      *
857      * @return the default IngoreCache setting.
858      */

859     public boolean getIgnoreCache() {
860         return "true".equalsIgnoreCase(connectionProperties
861                 .getProperty(SpeedoProperties.JDO_OPTION_IGNORE_CACHE));
862     }
863
864     /** Get the MaxPool setting for the PersistenceManager
865      * pool for this factory.
866      * @return the MaxPool setting.
867      */

868     public int getMaxPool() {
869         String JavaDoc v = connectionProperties.getProperty(SpeedoProperties.PM_POOL_MAX);
870         if (v != null && v.length()>0) {
871             return Integer.parseInt(v);
872         } else {
873             return GETMAXPOOL;
874         }
875     }
876
877     /** Set the MaxPool setting for the PersistenceManager
878      * pool for this factory.
879      * @param maxPool the MaxPool setting.
880      */

881     public void setMaxPool(int maxPool) {
882         if (started) {
883             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
884         }
885         connectionProperties.setProperty(SpeedoProperties.PM_POOL_MAX, Integer.toString(maxPool));
886     }
887
888     /** Get the MinPool setting for the PersistenceManager
889      * pool for this factory.
890      * @return the MinPool setting.
891      */

892     public int getMinPool() {
893         return Integer.parseInt(connectionProperties.getProperty(SpeedoProperties.PM_POOL_MIN));
894     }
895
896     /** Set the MinPool setting for the PersistenceManager
897      * pool for this factory.
898      * @param minPool the MinPool setting.
899      */

900     public void setMinPool(int minPool) {
901         if (started) {
902             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
903         }
904         connectionProperties.setProperty(SpeedoProperties.PM_POOL_MIN, Integer.toString(minPool));
905     }
906
907     /** Get the MsWait setting for the PersistenceManager
908      * pool for this factory.
909      * @return the MsWait setting.
910      */

911     public int getMsWait() {
912         return Integer.parseInt(connectionProperties.getProperty(SpeedoProperties.PM_POOL_TIMEOUT));
913     }
914
915     /** Set the MsWait setting for the PersistenceManager
916      * pool for this factory.
917      * @param msWait the MsWait setting.
918      */

919     public void setMsWait(int msWait) {
920         if (started) {
921             throw new JDOUserException("It is forbidden to modify property after a getPersistenceManager() call");
922         }
923         connectionProperties.setProperty(SpeedoProperties.PM_POOL_TIMEOUT, Integer.toString(msWait));
924     }
925
926     /** Return non-configurable properties of this PersistenceManagerFactory.
927      * Properties with keys VendorName and VersionNumber are required. Other
928      * keys are optional.
929      * @return the non-configurable properties of this
930      * PersistenceManagerFactory.
931      */

932     public Properties JavaDoc getProperties() {
933         return connectionProperties;
934     }
935
936     /** The application can determine from the results of this
937      * method which optional features, and which query languages
938      * are supported by the JDO implementation.
939      * <P>Each supported JDO optional feature is represented by a
940      * String with one of the following values:
941      *
942      * <P>javax.jdo.option.TransientTransactional
943      * <P>javax.jdo.option.NontransactionalRead
944      * <P>javax.jdo.option.NontransactionalWrite
945      * <P>javax.jdo.option.RetainValues
946      * <P>javax.jdo.option.Optimistic
947      * <P>javax.jdo.option.ApplicationIdentity
948      * <P>javax.jdo.option.DatastoreIdentity
949      * <P>javax.jdo.option.NonDatastoreIdentity
950      * <P>javax.jdo.option.ArrayList
951      * <P>javax.jdo.option.HashMap
952      * <P>javax.jdo.option.Hashtable
953      * <P>javax.jdo.option.LinkedList
954      * <P>javax.jdo.option.TreeMap
955      * <P>javax.jdo.option.TreeSet
956      * <P>javax.jdo.option.Vector
957      * <P>javax.jdo.option.Map
958      * <P>javax.jdo.option.List
959      * <P>javax.jdo.option.Array
960      * <P>javax.jdo.option.NullCollection
961      *
962      *<P>The standard JDO query language is represented by a String:
963      *<P>javax.jdo.query.JDOQL
964      * @return the List of String representing the supported Options
965      */

966     public Collection JavaDoc supportedOptions() {
967         return Collections.unmodifiableList(Arrays.asList(optionArray));
968     }
969
970     public void setRestoreValues(boolean b) {
971     }
972
973     public boolean getRestoreValues() {
974         return false;
975     }
976
977     private Map JavaDoc class2listeners;
978     private Map JavaDoc listener2classes;
979     
980     public synchronized void addInstanceLifecycleListener(
981             InstanceLifecycleListener l,
982             Class JavaDoc[] classes) {
983         //Register the listener
984
if (class2listeners == null) {
985             class2listeners = new HashMap JavaDoc();
986             listener2classes = new HashMap JavaDoc();
987         }
988         List JavaDoc classesCol = (List JavaDoc) listener2classes.get(l);
989         if (classesCol == null) {
990             classesCol = new ArrayList JavaDoc();
991             listener2classes.put(l, classesCol);
992         }
993         classesCol.addAll(Arrays.asList(classes));
994         
995         for (int i = 0; i < classes.length; i++) {
996             List JavaDoc listenersCol = (List JavaDoc) class2listeners.get(classes[i]);
997             if (listenersCol == null) {
998                 listenersCol = new ArrayList JavaDoc();
999                 class2listeners.put(classes[i], listenersCol);
1000            }
1001            listenersCol.add(l);
1002            SpeedoHome sh = null;
1003            try {
1004                sh = (SpeedoHome) jormFactory.getPClassMapping(classes[i]);
1005            } catch (PException e) {
1006            }
1007            if (sh == null) {
1008                logger.log(BasicLevel.WARN, "Class '" + classes[i].getName()
1009                        + "' is not recongnized as a persistent class. The listener will not receiver event, but it is registerd.");
1010            } else {
1011                sh.addInstanceLifeCycleListener(l);
1012            }
1013        }
1014    }
1015
1016    public synchronized void removeInstanceLifecycleListener(InstanceLifecycleListener l) {
1017        List JavaDoc classes = (List JavaDoc) listener2classes.remove(l);
1018        if (classes != null) {
1019            for (Iterator JavaDoc iter = classes.iterator(); iter.hasNext();) {
1020                Class JavaDoc clazz = (Class JavaDoc) iter.next();
1021                List JavaDoc listenersCol = (List JavaDoc) class2listeners.get(clazz);
1022                if (listenersCol != null) {
1023                    if (listenersCol.remove(l)) {
1024                        SpeedoHome sh = null;
1025                        try {
1026                            sh = (SpeedoHome) jormFactory.getPClassMapping(clazz);
1027                        } catch (PException e) {
1028                        }
1029                        if (sh != null) {
1030                            sh.removeInstanceLifeCycleListener(l);
1031                        }
1032                        
1033                    }
1034                }
1035            }
1036        }
1037    }
1038    
1039    // IMPLEMENTATION OF THE PoolProvider INTERFACE //
1040
//----------------------------------------------//
1041

1042    /**
1043     * @return the owned <code>Pool</code>
1044     */

1045    public Pool getPool() {
1046        return managedPM;
1047    }
1048
1049}
1050
Popular Tags