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.simple;
019
020import java.util.HashMap;
021import java.util.Map;
022import java.util.Set;
023import java.util.concurrent.ConcurrentHashMap;
024import java.util.concurrent.CopyOnWriteArraySet;
025
026import net.sf.hajdbc.durability.InvocationEvent;
027import net.sf.hajdbc.durability.InvokerEvent;
028import net.sf.hajdbc.state.DatabaseEvent;
029import net.sf.hajdbc.state.StateManager;
030
031public class SimpleStateManager implements StateManager
032{
033        private final Map<InvocationEvent, Map<String, InvokerEvent>> invocations = new ConcurrentHashMap<InvocationEvent, Map<String, InvokerEvent>>();
034        private final Set<String> activeDatabases = new CopyOnWriteArraySet<String>();
035
036        @Override
037        public Set<String> getActiveDatabases()
038        {
039                return this.activeDatabases;
040        }
041
042        @Override
043        public Map<InvocationEvent, Map<String, InvokerEvent>> recover()
044        {
045                return this.invocations;
046        }
047
048        @Override
049        public void setActiveDatabases(Set<String> databases)
050        {
051                this.activeDatabases.retainAll(databases);
052                this.activeDatabases.addAll(databases);
053        }
054
055        @Override
056        public void activated(DatabaseEvent event)
057        {
058                this.activeDatabases.add(event.getSource());
059        }
060
061        @Override
062        public void deactivated(DatabaseEvent event)
063        {
064                this.activeDatabases.remove(event.getSource());
065        }
066
067        @Override
068        public void afterInvocation(InvocationEvent event)
069        {
070                this.invocations.remove(event);
071        }
072
073        @Override
074        public void beforeInvocation(InvocationEvent event)
075        {
076                this.invocations.put(event, new HashMap<String, InvokerEvent>());
077        }
078
079        @Override
080        public void afterInvoker(InvokerEvent event)
081        {
082                this.invocations.get(new InvocationEventAdapter(event)).remove(event.getDatabaseId());
083        }
084
085        @Override
086        public void beforeInvoker(InvokerEvent event)
087        {
088                this.invocations.get(new InvocationEventAdapter(event)).put(event.getDatabaseId(), event);
089        }
090
091        @Override
092        public boolean isEnabled()
093        {
094                return true;
095        }
096
097        @Override
098        public void start()
099        {
100        }
101
102        @Override
103        public void stop()
104        {
105        }
106}