com.tangosol.persistence.SafePersistenceWrappers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of coherence Show documentation
Show all versions of coherence Show documentation
Oracle Coherence Community Edition
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package com.tangosol.persistence;
import com.oracle.coherence.common.base.Collector;
import com.oracle.coherence.common.base.Continuation;
import com.oracle.coherence.persistence.PersistenceEnvironment;
import com.oracle.coherence.persistence.PersistenceManager;
import com.oracle.coherence.persistence.PersistentStore;
import com.tangosol.io.ReadBuffer;
import com.tangosol.io.WriteBuffer;
import com.tangosol.util.NullImplementation;
import com.tangosol.util.NullImplementation.NullPersistenceEnvironment;
import com.tangosol.util.NullImplementation.NullPersistenceManager;
import com.tangosol.util.NullImplementation.NullPersistentStore;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* SafePersistenceWrappers provides safe wrapper implementations of persistence
* layer interfaces that can be used to consolidate error handling across the
* various persistence layers.
*
* @since Coherence 12.1.3
* @author rhl/jh 2013.07.18
*/
public class SafePersistenceWrappers
{
// ----- static helpers -------------------------------------------------
/**
* Return the underlying (non-"Safe") PersistenceEnvironment.
*
* @param env a PersistenceEnvironment
* @param the type of a raw, environment specific object representation
*
* @return the underlying PersistenceEnvironment implementation
*/
public static PersistenceEnvironment unwrap(PersistenceEnvironment env)
{
while (env instanceof SafePersistenceEnvironment)
{
env = ((SafePersistenceEnvironment) env).getEnvironment();
}
return env;
}
/**
* Return the underlying (non-"Safe") PersistenceManager.
*
* @param mgr a PersistenceManager
* @param the type of a raw, environment specific object representation
*
* @return the underlying PersistenceManager implementation
*/
public static PersistenceManager unwrap(PersistenceManager mgr)
{
while (mgr instanceof SafePersistenceManager)
{
mgr = ((SafePersistenceManager) mgr).getManager();
}
return mgr;
}
/**
* Return the underlying (non-"Safe") PersistentStore.
*
* @param store a PersistentStore
* @param the type of a raw, environment specific object representation
*
* @return the underlying PersistentStore implementation
*/
public static PersistentStore unwrap(PersistentStore store)
{
while (store instanceof SafePersistentStore)
{
store = ((SafePersistentStore) store).getStore();
}
return store;
}
/**
* Return a simple FailureContinuationFactory that uses the specified failure
* continuation to handle failures at any layer of the persistence
* implementation.
*
* @param cont the failure continuation
* @param the type of a raw, environment specific object representation
* @param the type of a Throwable failure to protect
*
* @return a simple FailureContinuationFactory that uses a single common
* failure continuation
*/
protected static FailureContinuationFactory getSimpleFactory(final Continuation super T> cont)
{
return new FailureContinuationFactory()
{
public Continuation super T> getEnvironmentContinuation(PersistenceEnvironment env)
{
return cont;
}
public Continuation super T> getManagerContinuation(PersistenceManager mgr)
{
return cont;
}
public Continuation super T> getStoreContinuation(PersistentStore store)
{
return cont;
}
};
}
// ----- inner class: SafePersistenceEnvironment ------------------------
/**
* SafePersistenceEnvironment is a wrapper PersistenceEnvironment implementation
* which protects all operations on the underlying environment (and any
* PersistenceManger or PersistentStore instances opened through this environment)
* from unexpected failures, delegating the exception handling to a failure
* {@link Continuation}.
*
* @param the type of a raw, environment specific object representation
* @param the type of a Throwable failure to protect
*/
public static class SafePersistenceEnvironment
extends NullPersistenceEnvironment
{
// ----- constructors -----------------------------------------------
/**
* Construct a PersistenceEnvironment backed by the specified environment.
*
* @param env the underlying PersistenceEnvironment
*/
public SafePersistenceEnvironment(PersistenceEnvironment env)
{
this(env, DEFAULT_FACTORY);
}
/**
* Construct a PersistenceEnvironment backed by the specified environment.
*
* @param env the underlying PersistenceEnvironment
* @param cont the failure continuation to use to handle unexpected exceptions
*/
public SafePersistenceEnvironment(PersistenceEnvironment env, Continuation super Throwable> cont)
{
this(env, getSimpleFactory(cont));
}
/**
* Construct a PersistenceEnvironment backed by the specified environment.
*
* @param env the underlying PersistenceEnvironment
* @param factory the failure continuation factory to use to create handlers
* for unexpected exceptions
*/
public SafePersistenceEnvironment(PersistenceEnvironment env, FailureContinuationFactory factory)
{
f_env = env;
f_factoryCont = factory;
f_contFailure = factory.getEnvironmentContinuation(env);
}
// ----- accessors --------------------------------------------------
/**
* Return the underlying PersistenceEnvironment.
*
* @return the underlying PersistenceEnvironment
*/
public PersistenceEnvironment getEnvironment()
{
return f_env;
}
// ----- internal methods -------------------------------------------
/**
* Called to handle an unexpected exception.
*
* @param t the Throwable
*/
protected void onException(T t)
{
f_contFailure.proceed(t);
}
/**
* Wrap the specified manager in a SafePersistenceManager implementation.
*
* @param mgr the underlying PersistenceManger
*
* @return a "safe" PersistenceManger or null if the specified
* manager is null
*/
protected PersistenceManager wrap(PersistenceManager mgr)
{
return mgr == null ? null : new SafePersistenceManager<>(mgr, f_factoryCont);
}
// ----- PersistenceEnvironment methods -----------------------------
/**
* {@inheritDoc}
*/
public PersistenceManager openBackup()
{
try
{
return wrap(getEnvironment().openBackup());
}
catch (Throwable t)
{
onException((T) t);
}
return NullImplementation.getPersistenceManager();
}
/**
* {@inheritDoc}
*/
public PersistenceManager openEvents()
{
try
{
return wrap(getEnvironment().openEvents());
}
catch (Throwable t)
{
onException((T) t);
}
return NullImplementation.getPersistenceManager();
}
/**
* {@inheritDoc}
*/
public PersistenceManager openActive()
{
try
{
return wrap(getEnvironment().openActive());
}
catch (Throwable t)
{
onException((T) t);
}
return NullImplementation.getPersistenceManager();
}
/**
* {@inheritDoc}
*/
public PersistenceManager openSnapshot(String sSnapshot)
{
try
{
return wrap(getEnvironment().openSnapshot(sSnapshot));
}
catch (Throwable t)
{
onException((T) t);
}
return NullImplementation.getPersistenceManager();
}
/**
* {@inheritDoc}
*/
public PersistenceManager createSnapshot(String sSnapshot, PersistenceManager manager)
{
try
{
return wrap(getEnvironment().createSnapshot(sSnapshot, unwrap(manager)));
}
catch (Throwable t)
{
onException((T) t);
}
return NullImplementation.getPersistenceManager();
}
/**
* {@inheritDoc}
*/
public boolean removeSnapshot(String sSnapshot)
{
try
{
return getEnvironment().removeSnapshot(sSnapshot);
}
catch (Throwable t)
{
onException((T) t);
}
return super.removeSnapshot(sSnapshot);
}
/**
* {@inheritDoc}
*/
public String[] listSnapshots()
{
try
{
return getEnvironment().listSnapshots();
}
catch (Throwable t)
{
onException((T) t);
}
return super.listSnapshots();
}
/**
* {@inheritDoc}
*/
public void release()
{
try
{
getEnvironment().release();
}
catch (Throwable t)
{
onException((T) t);
}
}
// ----- Object methods ---------------------------------------------
/**
* Return a human readable description of this SafePersistenceEnvironment.
*
* @return a human readable description
*/
@Override
public String toString()
{
return "Safe" + String.valueOf(getEnvironment());
}
// ----- data members -----------------------------------------------
/**
* The underlying PersistenceEnvironment.
*/
protected final PersistenceEnvironment f_env;
/**
* The failure continuation for this environment.
*/
protected final Continuation super T> f_contFailure;
/**
* The FailureContinuationFactory.
*/
protected final FailureContinuationFactory f_factoryCont;
}
// ----- inner class: SafePersistenceManager ----------------------------
/**
* SafePersistenceManager is a wrapper PersistenceManager implementation which
* protects all operations on the underlying manager (and any PersistentStore
* instances opened by the manager) from unexpected failures, delegating
* the exception handling to a failure {@link Continuation}.
*
* @param the type of a raw, environment specific object representation
* @param the type of a Throwable failure to protect
*/
public static class SafePersistenceManager
extends NullPersistenceManager
{
// ----- constructors -----------------------------------------------
/**
* Construct a SafePersistenceManager backed by the specified manager.
*
* @param mgr the underlying PersistenceManager
*/
public SafePersistenceManager(PersistenceManager mgr)
{
this(mgr, DEFAULT_FACTORY);
}
/**
* Construct a SafePersistenceManager backed by the specified manager.
*
* @param mgr the underlying PersistenceManager
* @param cont the failure continuation to use to handle unexpected exceptions
*/
public SafePersistenceManager(PersistenceManager mgr, Continuation super Throwable> cont)
{
this(mgr, getSimpleFactory(cont));
}
/**
* Construct a PersistenceManager backed by the specified manager.
*
* @param mgr the underlying PersistenceManager
* @param factory the failure continuation factory to use to create handlers
* for unexpected exceptions
*/
public SafePersistenceManager(PersistenceManager mgr, FailureContinuationFactory factory)
{
f_manager = mgr;
f_factoryCont = factory;
f_contFailure = factory.getManagerContinuation(mgr);
}
// ----- accessors --------------------------------------------------
/**
* Return the underlying PersistenceManager.
*
* @return the underlying PersistenceManager
*/
public PersistenceManager getManager()
{
return f_manager;
}
// ----- internal methods -------------------------------------------
/**
* Called to handle an unexpected exception.
*
* @param t the Throwable
*/
public void onException(T t)
{
f_contFailure.proceed(t);
}
/**
* Wrap the specified store in a SafePersistentStore implementation.
*
* @param store the underlying PersistentStore
*
* @return a "safe" PersistenceManger or null if the specified store
* is null
*/
protected PersistentStore wrap(PersistentStore store)
{
return store == null ? null : new SafePersistentStore<>(store, f_factoryCont);
}
// ----- PersistenceManager methods ---------------------------------
/**
* {@inheritDoc}
*/
public String getName()
{
try
{
return getManager().getName();
}
catch (Throwable t)
{
onException((T) t);
}
return super.getName();
}
/**
* {@inheritDoc}
*/
public PersistentStore createStore(String sId)
{
try
{
return wrap(getManager().createStore(sId));
}
catch (Throwable t)
{
onException((T) t);
}
return null;
}
/**
* {@inheritDoc}
*/
public PersistentStore open(String sId, PersistentStore store)
{
try
{
return wrap(getManager().open(sId, store));
}
catch (Throwable t)
{
onException((T) t);
}
return super.open(sId, store);
}
/**
* {@inheritDoc}
*/
public PersistentStore open(String sId, PersistentStore store, Collector