Package org.objectweb.speedo.pm.lib

Source Code of org.objectweb.speedo.pm.lib.POManagerSwitchImpl

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

import org.objectweb.speedo.pm.api.POManagerItf;
import org.objectweb.speedo.pm.api.POManagerFactoryItf;
import org.objectweb.speedo.pm.api.POManagerSwitchItf;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;

/**
* This class is an implementation of the POManagerSwitchItf based on the use
* of a ThreadLocal field. This field contains an instance of POManagerItf of an
* ArrayList of POManagerItf. A POManagerSwitch is a fractal component without
* bindings.
*
* @author S.Chassande-Barrioz
*/
public class POManagerSwitchImpl implements POManagerSwitchItf {

    protected ThreadLocal pms = new ThreadLocal();

    public final static String BIND_ERROR_MSG = "Impossible to associated to POManagerItf linked to the same PersistenceManagerFactory in the same context (thread)";

    // IMPLEMENTATION OF THE POManagerSwitchItf INTERFACE //
    //----------------------------------------------------//

    /**
     * @param pmf is persistent manager factory which manages the returned
     * po manager.
     * @return the POManagerItf managed by the given persistence manager factory
     * and bound to current the context, or the null value if there is no
     * POManagerItf.
     */
    public POManagerItf lookup(POManagerFactoryItf pmf) {
        Object o = pms.get();
        if (o == null) {
            return null;
        } else if (o instanceof POManagerItf) {
            if (((POManagerItf) o).getPOManagerFactory() == pmf)
                return (POManagerItf) o;
        } else {
            for (Iterator it = ((List) o).iterator(); it.hasNext();) {
                POManagerItf pm = (POManagerItf) it.next();
                if (pm.getPOManagerFactory() == pmf)
                    return pm;
            }
        }
        return null;
    }

    /**
     * It assignes a POManagerItf to the current context.
     * @param pm is the POManagerItf
     */
    public void bind(POManagerItf pm) {
        Object o = pms.get();
        if (o == null) {
            pms.set(pm);
        } else if (o instanceof POManagerItf) {
            // There is one POManagerItf, then check if the old and the new
            // po manager are managed by the same PMF.
            POManagerItf pm1 = (POManagerItf) o;
            if (pm1.getPOManagerFactory()
                    == pm.getPOManagerFactory()) {
                pms.set(pm);
            } else {
                // Put the old and the new in a List
                ArrayList al = new ArrayList(3);
                al.add(pm1);
                al.add(pm);
                pms.set(al);
            }
        } else {
            // The element is a list of POManagerItf instances, then check if
            // there is a po manager linked to the same PersistenceManagerFactory
            List l = (List) o;
            for (Iterator it = l.iterator(); it.hasNext();) {
                if (((POManagerItf) it.next()).getPOManagerFactory()
                        == pm.getPOManagerFactory()) {
                    it.remove();
                }
            }
            //No element with the same pmf, then add the po manager
            ((List) o).add(pm);
        }
    }

    /**
     * It clears the list of POManagerItf for the current context.
     */
    public void clear() {
        pms.set(null);
    }

    /**
     * It clears a POManagerItf for the current context.
     */
    public boolean unbind(POManagerItf pm) {
        Object o = pms.get();
        if (o == null) {
      return false;
    } else if (o instanceof POManagerItf) {
            if (o == pm) {
                pms.set(null);
                return true;
            }
        } else if (o instanceof Collection) {
            return ((Collection) o).remove(pm);
        }
        return false;
    }

  public boolean unbind(POManagerFactoryItf pmf) {
    Object o = pms.get();
    if (o == null) {
      return false;
    } else if (o instanceof POManagerItf) {
      if (((POManagerItf) o).getPOManagerFactory() == pmf) {
        pms.set(null);
        return true;
      }
    } else { // The element is a list of POManagerItf instances
            for (Iterator it = ((List) o).iterator(); it.hasNext();) {
                if (((POManagerItf) it.next()).getPOManagerFactory()
                        == pmf) {
                    it.remove();
          return true;
                }
            }
        }
    return false;
  }

    /**
     * @return all POManagerItf instances bound with the current context
     */
    public Collection entries() {
        Object o = pms.get();
        if (o instanceof POManagerItf)
            return Collections.singletonList(o);
        else
            return (Collection) o;
    }
}
TOP

Related Classes of org.objectweb.speedo.pm.lib.POManagerSwitchImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.