001/* 002 * HA-JDBC: High-Availability JDBC 003 * Copyright (c) 2004-2007 Paul Ferraro 004 * 005 * This library is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU Lesser General Public License as published by the 007 * Free Software Foundation; either version 2.1 of the License, or (at your 008 * option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, but WITHOUT 011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 013 * for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License 016 * along with this library; if not, write to the Free Software Foundation, 017 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 * 019 * Contact: ferraro@users.sourceforge.net 020 */ 021package net.sf.hajdbc.distributable; 022 023import java.util.Arrays; 024import java.util.Collections; 025import java.util.Set; 026import java.util.TreeSet; 027 028import net.sf.hajdbc.DatabaseCluster; 029import net.sf.hajdbc.StateManager; 030import net.sf.hajdbc.util.Strings; 031 032/** 033 * @author Paul Ferraro 034 */ 035public class QueryInitialStateCommand implements Command<Set<String>> 036{ 037 private static final long serialVersionUID = -8409746321944635265L; 038 039 /** 040 * @see net.sf.hajdbc.distributable.Command#execute(net.sf.hajdbc.DatabaseCluster, net.sf.hajdbc.StateManager) 041 */ 042 @Override 043 public <D> Set<String> execute(DatabaseCluster<D> databaseCluster, StateManager stateManager) 044 { 045 return databaseCluster.isActive() ? stateManager.getInitialState() : null; 046 } 047 048 /** 049 * Optimize transfer of result by marshalling set of strings into a string. 050 * @see net.sf.hajdbc.distributable.Command#marshalResult(java.lang.Object) 051 */ 052 @Override 053 public Object marshalResult(Set<String> set) 054 { 055 return (set != null) ? Strings.join(set, Strings.COMMA) : null; 056 } 057 058 /** 059 * Restore marshalled string into a set of strings 060 * @see net.sf.hajdbc.distributable.Command#unmarshalResult(java.lang.Object) 061 */ 062 @Override 063 public Set<String> unmarshalResult(Object object) 064 { 065 String state = (String) object; 066 067 if (state == null) return null; 068 069 if (state.length() == 0) return Collections.emptySet(); 070 071 return new TreeSet<String>(Arrays.asList(state.split(Strings.COMMA))); 072 } 073 074 /** 075 * @see java.lang.Object#toString() 076 */ 077 @Override 078 public String toString() 079 { 080 return this.getClass().getName(); 081 } 082}