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.sql;
019
020import java.lang.reflect.Method;
021import java.util.Collections;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import net.sf.hajdbc.durability.Durability;
027import net.sf.hajdbc.durability.Durability.Phase;
028import net.sf.hajdbc.util.SimpleStaticRegistry;
029import net.sf.hajdbc.util.SimpleStaticRegistry.ExceptionMessageFactory;
030import net.sf.hajdbc.util.StaticRegistry;
031
032/**
033 * @author Paul Ferraro
034 */
035public class DurabilityPhaseRegistry implements StaticRegistry<Method, Durability.Phase>, ExceptionMessageFactory<Method>
036{
037        private final StaticRegistry<Method, Durability.Phase> registry;
038        
039        public DurabilityPhaseRegistry(List<Method> commitMethods, List<Method> rollbackMethods)
040        {
041                this(Collections.<Method>emptyList(), commitMethods, rollbackMethods, Collections.<Method>emptyList());
042        }
043
044        public DurabilityPhaseRegistry(List<Method> prepareMethods, List<Method> commitMethods, List<Method> rollbackMethods, List<Method> forgetMethods)
045        {
046                Map<Method, Durability.Phase> map = new HashMap<Method, Durability.Phase>();
047                for (Method method: prepareMethods)
048                {
049                        map.put(method, Durability.Phase.PREPARE);
050                }
051                for (Method method: commitMethods)
052                {
053                        map.put(method, Durability.Phase.COMMIT);
054                }
055                for (Method method: rollbackMethods)
056                {
057                        map.put(method, Durability.Phase.ROLLBACK);
058                }
059                for (Method method: forgetMethods)
060                {
061                        map.put(method, Durability.Phase.FORGET);
062                }
063                this.registry = new SimpleStaticRegistry<Method, Durability.Phase>(this, map);
064        }
065
066        /**
067         * {@inheritDoc}
068         * @see net.sf.hajdbc.util.StaticRegistry#get(java.lang.Object)
069         */
070        @Override
071        public Phase get(Method method)
072        {
073                return this.registry.get(method);
074        }
075
076        /**
077         * {@inheritDoc}
078         * @see net.sf.hajdbc.util.SimpleStaticRegistry.ExceptionMessageFactory#createMessage(java.lang.Object)
079         */
080        @Override
081        public String createMessage(Method method)
082        {
083                return String.format("%s.%s(...) has no associated durability phase.", method.getClass().getName(), method.getName());
084        }
085}