001: /*
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * Release: 1.0
021: *
022: * Created on 15 mars 2004
023: * @author fmillevi@yahoo.com
024: *
025: */
026: package org.objectweb.speedo.j2eedo.common;
027:
028: import javax.jdo.JDOException;
029: import javax.jdo.PersistenceManager;
030: import javax.jdo.PersistenceManagerFactory;
031:
032: import org.objectweb.speedo.j2eedo.bo.PollsSynchronizations;
033: import org.objectweb.util.monolog.Monolog;
034: import org.objectweb.util.monolog.api.BasicLevel;
035: import org.objectweb.util.monolog.api.Logger;
036: import org.objectweb.util.monolog.api.LoggerFactory;
037:
038: /**
039: * This class is used to handle the JDO persistence manager and it's factory
040: * @author fmillevi@yahoo.com
041: */
042: public class PMHolder {
043: private PersistenceManagerFactory pmf = null;
044: private PersistenceManager pm = null;
045: private int countGetPM = 0;
046:
047: static Logger logger = Monolog.initialize().getLogger(
048: PMHolder.class.getName());
049:
050: /**
051: * Persistence manager holder's constructor
052: * @param newPMF is the persistence manager factory
053: */
054: public PMHolder(PersistenceManagerFactory newPMF) {
055: this .pmf = newPMF;
056: this .countGetPM = 0;
057: }
058:
059: public PersistenceManagerFactory getPersistenceManagerFactory() {
060: return pmf;
061: }
062:
063: /**
064: * Returns the persistence manager.<p>When the persistence manager is allready
065: * not null, the existing PM is returned.</p>
066: */
067: public synchronized PersistenceManager getPersistenceManager() {
068: this .countGetPM++;
069: if (null != this .pm && this .pm.isClosed()) {
070: logger.log(BasicLevel.WARN, "PersistenceManager is closed");
071: this .countGetPM = 1;
072: this .pm = null;
073: }
074: if (null == this .pm) {
075: try {
076: logger
077: .log(BasicLevel.DEBUG,
078: "Get PersistenceManager");
079: this .pm = this .pmf.getPersistenceManager();
080: } catch (NullPointerException e) {
081: throw e;
082: } catch (JDOException e) {
083: throw e;
084: }
085: }
086: logger.log(BasicLevel.DEBUG, "Returned persistenceManager : "
087: + pm);
088: return this .pm;
089: }
090:
091: /**
092: * Close the persistence manager.<p>When the persistence manager is allready
093: * close nothing is done.</p><p>When the PM has bo be closed, this method launchs
094: * the {@link org.objectweb.speedo.j2eedo.bo.PollsSynchronizations polls
095: * synchronization process} when needed (it means when the PM userObject
096: * is not null.</p>
097: * <i>Remark:</i> The getPersistenceManager can be called more than 1 time during
098: * each action. This object keep updated a counter in order to know when
099: * it necessary to close the PM.
100: */
101: public void closePersistenceManager() {
102: this .countGetPM--;
103: if (pm != null && this .countGetPM == 0) {
104: logger.log(BasicLevel.DEBUG, "Close PersistenceManager : "
105: + pm);
106: if (!pm.isClosed()) {
107: //verify if some pool must be updated
108: if (pm.getUserObject() != null) {
109: ((PollsSynchronizations) this .pm.getUserObject())
110: .afterCompletion(-1);
111: }
112: pm.close();
113: } else {
114: logger.log(BasicLevel.WARN,
115: "PersistenceManager already closed : " + pm);
116: }
117: pm = null;
118: }
119: }
120: }
|