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

org.datacleaner.connection.UsageAwareDatastore Maven / Gradle / Ivy

The newest version!
/**
 * DataCleaner (community edition)
 * Copyright (C) 2014 Free Software Foundation, Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program 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 distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.datacleaner.connection;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.List;

import org.apache.metamodel.DataContext;
import org.apache.metamodel.util.BaseObject;
import org.datacleaner.util.ReadObjectBuilder;
import org.datacleaner.util.ReadObjectBuilder.Moved;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Abstract datastore implementation that uses a shared
 * UsageAwareDataContextProvider when posssible.
 *
 * @see UsageAwareDatastoreConnection
 */
public abstract class UsageAwareDatastore extends BaseObject implements Datastore {

    private static final long serialVersionUID = 1L;

    private static final Logger logger = LoggerFactory.getLogger(UsageAwareDatastore.class);
    @Moved
    private final String _name;
    private transient volatile Reference> _datastoreConnectionRef;
    private transient volatile UsageAwareDatastoreConnection _datastoreConnection = null;
    private String _description;

    public UsageAwareDatastore(final String name) {
        _name = name;
    }

    private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
        ReadObjectBuilder.create(this, UsageAwareDatastore.class).readObject(stream);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getName() {
        return _name;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final String getDescription() {
        return _description;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final void setDescription(final String description) {
        _description = description;
    }

    protected Reference> getDataContextProviderRef() {
        return _datastoreConnectionRef;
    }

    protected void setDataContextProviderRef(final Reference> dataContextProviderRef) {
        _datastoreConnectionRef = dataContextProviderRef;
    }

    protected void setDataContextProvider(final UsageAwareDatastoreConnection dataContextProvider) {
        _datastoreConnection = dataContextProvider;
    }

    /**
     * {@inheritDoc}
     */
    private synchronized UsageAwareDatastoreConnection getDatastoreConnection() {
        if (_datastoreConnection != null) {
            return _datastoreConnection;
        }

        UsageAwareDatastoreConnection datastoreConnection;
        if (_datastoreConnectionRef != null) {
            datastoreConnection = _datastoreConnectionRef.get();
            if (datastoreConnection != null && datastoreConnection.requestUsage()) {
                // reuse existing data context provider
                logger.debug("Reusing existing DatastoreConnection: {}", datastoreConnection);
                return datastoreConnection;
            }
        }

        datastoreConnection = createDatastoreConnection();
        if (datastoreConnection == null) {
            throw new IllegalStateException("createDatastoreConnection() returned null");
        }
        _datastoreConnectionRef = new WeakReference<>(datastoreConnection);

        return datastoreConnection;
    }

    @Override
    public DatastoreConnection openConnection() {
        final UsageAwareDatastoreConnection connection = getDatastoreConnection();
        if (connection instanceof UpdateableDatastoreConnection) {
            return new UpdateableDatastoreConnectionLease((UpdateableDatastoreConnection) connection);
        } else {
            return new DatastoreConnectionLease(connection);
        }
    }

    protected abstract UsageAwareDatastoreConnection createDatastoreConnection();

    @Override
    protected void decorateIdentity(final List identifiers) {
        identifiers.add(getName());
        identifiers.add(_description);
    }

    /**
     * Gets whether the datacontext provider is already open / ready or if it
     * has to be created.
     *
     * @return a boolean indicating if the datacontext provider is open
     */
    public final boolean isDatastoreConnectionOpen() {
        if (_datastoreConnectionRef == null) {
            return false;
        }
        final UsageAwareDatastoreConnection dataContextProvider = _datastoreConnectionRef.get();
        return isDataContextProviderOpen(dataContextProvider);
    }

    private boolean isDataContextProviderOpen(final UsageAwareDatastoreConnection dataContextProvider) {
        return dataContextProvider != null && !dataContextProvider.isClosed();
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + "[name=" + getName() + "]";
    }
}