001/* 002 * HA-JDBC: High-Availability JDBC 003 * Copyright (C) 2012 Paul Ferraro 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Lesser General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package net.sf.hajdbc.state; 019 020import java.util.concurrent.ConcurrentHashMap; 021import java.util.concurrent.ConcurrentMap; 022 023import net.sf.hajdbc.durability.DurabilityListener; 024import net.sf.hajdbc.durability.InvocationEvent; 025import net.sf.hajdbc.durability.InvokerEvent; 026import net.sf.hajdbc.tx.TransactionIdentifierFactory; 027import net.sf.hajdbc.util.Objects; 028 029/** 030 * @author Paul Ferraro 031 */ 032public class DurabilityListenerAdapter implements DurabilityListener 033{ 034 // TODO prevent memory leak 035 // Cache serialized transaction identifiers 036 private final ConcurrentMap<Object, byte[]> transactionIdentifiers = new ConcurrentHashMap<Object, byte[]>(); 037 private final SerializedDurabilityListener listener; 038 private final TransactionIdentifierFactory<Object> txIdFactory; 039 040 @SuppressWarnings("unchecked") 041 public DurabilityListenerAdapter(SerializedDurabilityListener listener, TransactionIdentifierFactory<? extends Object> txIdFactory) 042 { 043 this.listener = listener; 044 this.txIdFactory = (TransactionIdentifierFactory<Object>) txIdFactory; 045 } 046 047 /** 048 * {@inheritDoc} 049 * @see net.sf.hajdbc.durability.DurabilityListener#beforeInvocation(net.sf.hajdbc.durability.InvocationEvent) 050 */ 051 @Override 052 public void beforeInvocation(InvocationEvent event) 053 { 054 Object transactionId = event.getTransactionId(); 055 byte[] txId = this.txIdFactory.serialize(transactionId); 056 057 this.transactionIdentifiers.put(transactionId, txId); 058 this.listener.beforeInvocation(txId, (byte) event.getPhase().ordinal(), (byte) event.getExceptionType().ordinal()); 059 } 060 061 /** 062 * {@inheritDoc} 063 * @see net.sf.hajdbc.durability.DurabilityListener#afterInvocation(net.sf.hajdbc.durability.InvocationEvent) 064 */ 065 @Override 066 public void afterInvocation(InvocationEvent event) 067 { 068 this.listener.afterInvocation(this.transactionIdentifiers.remove(event.getTransactionId()), (byte) event.getPhase().ordinal()); 069 } 070 071 /** 072 * {@inheritDoc} 073 * @see net.sf.hajdbc.durability.DurabilityListener#beforeInvoker(net.sf.hajdbc.durability.InvokerEvent) 074 */ 075 @Override 076 public void beforeInvoker(InvokerEvent event) 077 { 078 this.listener.beforeInvoker(this.transactionIdentifiers.get(event.getTransactionId()), (byte) event.getPhase().ordinal(), event.getDatabaseId()); 079 } 080 081 /** 082 * {@inheritDoc} 083 * @see net.sf.hajdbc.durability.DurabilityListener#afterInvoker(net.sf.hajdbc.durability.InvokerEvent) 084 */ 085 @Override 086 public void afterInvoker(InvokerEvent event) 087 { 088 this.listener.afterInvoker(this.transactionIdentifiers.get(event.getTransactionId()), (byte) event.getPhase().ordinal(), event.getDatabaseId(), Objects.serialize(event.getResult())); 089 } 090}