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.xa;
019
020import javax.sql.XADataSource;
021import javax.xml.bind.annotation.XmlElement;
022import javax.xml.bind.annotation.XmlRootElement;
023import javax.xml.bind.annotation.XmlType;
024
025import net.sf.hajdbc.sql.AbstractDatabaseClusterConfiguration;
026import net.sf.hajdbc.tx.TransactionIdentifierFactory;
027import net.sf.hajdbc.tx.UUIDTransactionIdentifierFactory;
028
029@XmlRootElement(name = "ha-jdbc")
030@XmlType(name = "databaseClusterConfiguration")
031public class XADataSourceDatabaseClusterConfiguration extends AbstractDatabaseClusterConfiguration<XADataSource, XADataSourceDatabase>
032{
033        private static final long serialVersionUID = 6548016448539963613L;
034        
035        @XmlElement(name = "cluster", required = true)
036        private XADataSourceNestedConfiguration configuration = new XADataSourceNestedConfiguration();
037
038        @Override
039        protected AbstractDatabaseClusterConfiguration.NestedConfiguration<XADataSource, XADataSourceDatabase> getNestedConfiguration()
040        {
041                return this.configuration;
042        }
043
044        /**
045         * {@inheritDoc}
046         * @see net.sf.hajdbc.sql.AbstractDatabaseClusterConfiguration#getTransactionIdentifierFactory()
047         */
048        @Override
049        public TransactionIdentifierFactory<? extends Object> getTransactionIdentifierFactory()
050        {
051                return new UUIDTransactionIdentifierFactory();
052        }
053
054        @XmlType(name = "nestedConfiguration")
055        static class XADataSourceNestedConfiguration extends AbstractDatabaseClusterConfiguration.NestedConfiguration<XADataSource, XADataSourceDatabase>
056        {
057                private static final long serialVersionUID = 8096563929212126538L;
058
059                @XmlElement(name = "database")
060                private XADataSourceDatabase[] getDatabases()
061                {
062                        return this.getDatabaseMap().values().toArray(new XADataSourceDatabase[this.getDatabaseMap().size()]);
063                }
064                
065                @SuppressWarnings("unused")
066                private void setDatabases(XADataSourceDatabase[] databases)
067                {
068                        for (XADataSourceDatabase database: databases)
069                        {
070                                this.getDatabaseMap().put(database.getId(), database);
071                        }
072                }
073
074                @Override
075                public XADataSourceDatabase createDatabase(String id)
076                {
077                        XADataSourceDatabase database = new XADataSourceDatabase();
078                        database.setId(id);
079                        return database;
080                }
081        }
082}