All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.api.jdo.JDOReplicationManager Maven / Gradle / Ivy

There is a newer version: 6.0.4
Show newest version
/**********************************************************************
Copyright (c) 2008 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:
    ...
**********************************************************************/
package org.datanucleus.api.jdo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;

import javax.jdo.Extent;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Transaction;

import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.NucleusContext;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.metadata.MetaDataManager;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;
import org.datanucleus.util.StringUtils;

/**
 * Manager to control the replication of objects from one datastore to another.
 * Allow replication of specified objects, or all objects of particular types.
 * Supports a series of properties defining the replication behaviour.
 */
public class JDOReplicationManager
{
    /** PMF for the source datastore. */
    final PersistenceManagerFactory pmfSource;

    /** PMF for the target datastore. */
    final PersistenceManagerFactory pmfTarget;

    /** Properties defining the replication process. */
    protected Properties properties = new Properties();

    /**
     * Constructor for replicating between source and target PMF.
     * @param pmf1 PMF source
     * @param pmf2 PMF target
     */
    public JDOReplicationManager(PersistenceManagerFactory pmf1, PersistenceManagerFactory pmf2)
    {
        if (pmf1 == null || pmf1.isClosed())
        {
            throw new JDOUserException(Localiser.msg("012050"));
        }
        else if (pmf2 == null || pmf2.isClosed())
        {
            throw new JDOUserException(Localiser.msg("012050"));
        }

        pmfSource = pmf1;
        pmfTarget = pmf2;

        properties.setProperty("datanucleus.replicateObjectGraph", "true");
        // TODO Implement support for these properties
        properties.setProperty("datanucleus.deleteUnknownObjects", "false");
    }

    /**
     * Method to set a property for replication.
     * @param key Property key
     * @param value Property value
     */
    public void setProperty(String key, String value)
    {
        properties.setProperty(key, value);
    }

    /**
     * Accessor for the replication properties.
     * Supported properties include
     * 
    *
  • datanucleus.replicateObjectGraph - whether we replicate the object graph from an object. * if this is set we attempt to replicate the graph from this object. Otherwise just the object * and its near neighbours.
  • *
* @return Replication properties */ public Properties getProperties() { return properties; } protected boolean getBooleanProperty(String key) { String val = properties.getProperty(key); if (val == null) { return false; } return val.equalsIgnoreCase("true"); } /** * Method to perform the replication for all objects of the specified types. * @param types Classes to replicate */ public void replicate(Class... types) { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug(Localiser.msg("012052", pmfSource, pmfTarget, StringUtils.objectArrayToString(types))); } // Check if classes are detachable NucleusContext nucleusCtxSource = ((JDOPersistenceManagerFactory)pmfSource).getNucleusContext(); MetaDataManager mmgr = nucleusCtxSource.getMetaDataManager(); ClassLoaderResolver clr = nucleusCtxSource.getClassLoaderResolver(null); for (int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy