net.sf.hajdbc.sql.AbstractProxyFactory Maven / Gradle / Ivy
/*
* HA-JDBC: High-Availability JDBC
* Copyright (C) 2013 Paul Ferraro
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* 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 program. If not, see .
*/
package net.sf.hajdbc.sql;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import net.sf.hajdbc.Database;
import net.sf.hajdbc.DatabaseCluster;
import net.sf.hajdbc.ExceptionFactory;
import net.sf.hajdbc.ExceptionType;
import net.sf.hajdbc.Messages;
import net.sf.hajdbc.invocation.Invoker;
import net.sf.hajdbc.logging.Level;
import net.sf.hajdbc.logging.Logger;
import net.sf.hajdbc.logging.LoggerFactory;
/**
*
* @author Paul Ferraro
*/
public abstract class AbstractProxyFactory, TE extends Exception, T, E extends Exception> implements ProxyFactory
{
protected Logger logger = LoggerFactory.getLogger(this.getClass());
private final DatabaseCluster cluster;
private final Map map;
private final Set> children = Collections.newSetFromMap(new WeakHashMap, Boolean>());
private final Set> invokers = new HashSet>();
private final ExceptionFactory exceptionFactory;
/**
* Constructs a new proxy to a set of objects
* @param map a map of database to sql object.
* @param exceptionClass the class for exceptions thrown by this object
*/
protected AbstractProxyFactory(DatabaseCluster cluster, Map map, Class exceptionClass)
{
this.cluster = cluster;
this.map = Collections.synchronizedMap(map);
this.exceptionFactory = ExceptionType.valueOf(exceptionClass).getExceptionFactory();
}
@Override
public DatabaseCluster getDatabaseCluster()
{
return this.cluster;
}
protected T remove(D database)
{
return this.map.remove(database);
}
/**
* {@inheritDoc}
*/
@Override
public Set> entries()
{
return this.map.entrySet();
}
protected synchronized Iterable> children()
{
return this.children;
}
@Override
public synchronized void addChild(ChildProxyFactory child)
{
this.children.add(child);
}
@Override
public synchronized void removeChild(ChildProxyFactory child)
{
this.children.remove(child);
}
@Override
public synchronized final void removeChildren()
{
this.children.clear();
}
/**
* Returns the underlying SQL object for the specified database.
* If the sql object does not exist (this might be the case if the database was newly activated), it will be created from the stored operation.
* Any recorded operations are also executed. If the object could not be created, or if any of the executed operations failed, then the specified database is deactivated.
* @param database a database descriptor.
* @return an underlying SQL object
*/
@Override
public T get(D database)
{
synchronized (this.map)
{
T object = this.map.get(database);
if (object == null)
{
try
{
object = this.create(database);
this.replay(database, object);
this.map.put(database, object);
}
catch (Throwable e)
{
if (!this.map.isEmpty() && this.cluster.deactivate(database, this.cluster.getStateManager()))
{
this.logger.log(Level.WARN, e, Messages.SQL_OBJECT_INIT_FAILED.getMessage(), this.getClass().getName(), database);
}
}
}
return object;
}
}
protected abstract T create(D database) throws TE;
@Override
public void record(Invoker invoker)
{
// Record only the last invocation of a given set*(...) method
synchronized (this.invokers)
{
this.invokers.remove(invoker);
this.invokers.add(invoker);
}
}
/**
* @throws E
*/
@Override
public void replay(D database, T object) throws E
{
synchronized (this.invokers)
{
for (Invoker invoker: this.invokers)
{
this.logger.log(Level.TRACE, "Replaying {1}.{2} against database {0}", database, object.getClass().getName(), invoker);
try
{
invoker.invoke(database, object);
}
catch (Throwable e)
{
this.exceptionFactory.createException(e);
}
}
}
}
/**
* {@inheritDoc}
*/
@Override
public final ExceptionFactory getExceptionFactory()
{
return this.exceptionFactory;
}
}