KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > j2eedo > common > PMHolder


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  * Release: 1.0
21  *
22  * Created on 15 mars 2004
23  * @author fmillevi@yahoo.com
24  *
25  */

26 package org.objectweb.speedo.j2eedo.common;
27
28 import javax.jdo.JDOException;
29 import javax.jdo.PersistenceManager;
30 import javax.jdo.PersistenceManagerFactory;
31
32 import org.objectweb.speedo.j2eedo.bo.PollsSynchronizations;
33 import org.objectweb.util.monolog.Monolog;
34 import org.objectweb.util.monolog.api.BasicLevel;
35 import org.objectweb.util.monolog.api.Logger;
36 import org.objectweb.util.monolog.api.LoggerFactory;
37
38 /**
39  * This class is used to handle the JDO persistence manager and it's factory
40  * @author fmillevi@yahoo.com
41  */

42 public class PMHolder {
43     private PersistenceManagerFactory pmf = null;
44     private PersistenceManager pm = null;
45     private int countGetPM = 0;
46
47     static Logger logger = null;
48     static {
49         LoggerFactory lf = Monolog.initialize();
50         logger = lf.getLogger(PMHolder.class.getName());
51     }
52
53     /**
54      * Persistence manager holder's constructor
55      * @param newPMF is the persistence manager factory
56      */

57     public PMHolder(PersistenceManagerFactory newPMF){
58         this.pmf = newPMF;
59         this.countGetPM = 0;
60     }
61
62     public PersistenceManagerFactory getPersistenceManagerFactory() {
63         return pmf;
64     }
65     
66     /**
67      * Returns the persistence manager.<p>When the persistence manager is allready
68      * not null, the existing PM is returned.</p>
69      */

70     public synchronized PersistenceManager getPersistenceManager() {
71         this.countGetPM ++;
72         if (null != this.pm && this.pm.isClosed()) {
73             logger.log(BasicLevel.WARN, "PersistenceManager is closed");
74             this.countGetPM = 1;
75             this.pm = null;
76         }
77         if (null == this.pm) {
78             try {
79                 logger.log(BasicLevel.DEBUG, "Get PersistenceManager");
80                 this.pm = this.pmf.getPersistenceManager();
81             } catch (NullPointerException JavaDoc e) {
82                 throw e;
83             } catch (JDOException e) {
84                 throw e;
85             }
86         }
87         logger.log(BasicLevel.DEBUG, "Returned persistenceManager : " + pm);
88         return this.pm;
89     }
90
91     /**
92      * Close the persistence manager.<p>When the persistence manager is allready
93      * close nothing is done.</p><p>When the PM has bo be closed, this method launchs
94      * the {@link org.objectweb.speedo.j2eedo.bo.PollsSynchronizations polls
95      * synchronization process} when needed (it means when the PM userObject
96      * is not null.</p>
97      * <i>Remark:</i> The getPersistenceManager can be called more than 1 time during
98      * each action. This object keep updated a counter in order to know when
99      * it necessary to close the PM.
100      */

101     public void closePersistenceManager() {
102         this.countGetPM --;
103         if (null != this.pm && this.countGetPM == 0) {
104             logger.log(BasicLevel.DEBUG, "Close PersistenceManager : " + this.pm);
105             if (!this.pm.isClosed()) {
106                 //verify if some pool must be updated
107
if (null != this.pm.getUserObject()) {
108                     ((PollsSynchronizations)this.pm.getUserObject()).afterCompletion(-1);
109                 }
110                 this.pm.close();
111             } else {
112                 logger.log(BasicLevel.WARN, "PersistenceManager allready closed : " + this.pm);
113             }
114             this.pm = null;
115         }
116     }
117 }
Popular Tags