01: /*
02: * HA-JDBC: High-Availability JDBC
03: * Copyright (c) 2004-2007 Paul Ferraro
04: *
05: * This library is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU Lesser General Public License as published by the
07: * Free Software Foundation; either version 2.1 of the License, or (at your
08: * option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13: * for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public License
16: * along with this library; if not, write to the Free Software Foundation,
17: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: * Contact: ferraro@users.sourceforge.net
20: */
21: package net.sf.hajdbc.distributable;
22:
23: import java.util.Arrays;
24: import java.util.Collections;
25: import java.util.Set;
26: import java.util.TreeSet;
27:
28: import net.sf.hajdbc.DatabaseCluster;
29: import net.sf.hajdbc.StateManager;
30: import net.sf.hajdbc.util.Strings;
31:
32: /**
33: * @author Paul Ferraro
34: */
35: public class QueryInitialStateCommand implements Command<Set<String>> {
36: private static final long serialVersionUID = -8409746321944635265L;
37:
38: /**
39: * @see net.sf.hajdbc.distributable.Command#execute(net.sf.hajdbc.DatabaseCluster, net.sf.hajdbc.StateManager)
40: */
41: @Override
42: public <D> Set<String> execute(DatabaseCluster<D> databaseCluster,
43: StateManager stateManager) {
44: return databaseCluster.isActive() ? stateManager
45: .getInitialState() : null;
46: }
47:
48: /**
49: * Optimize transfer of result by marshalling set of strings into a string.
50: * @see net.sf.hajdbc.distributable.Command#marshalResult(java.lang.Object)
51: */
52: @Override
53: public Object marshalResult(Set<String> set) {
54: return (set != null) ? Strings.join(set, Strings.COMMA) : null;
55: }
56:
57: /**
58: * Restore marshalled string into a set of strings
59: * @see net.sf.hajdbc.distributable.Command#unmarshalResult(java.lang.Object)
60: */
61: @Override
62: public Set<String> unmarshalResult(Object object) {
63: String state = (String) object;
64:
65: if (state == null)
66: return null;
67:
68: if (state.length() == 0)
69: return Collections.emptySet();
70:
71: return new TreeSet<String>(Arrays.asList(state
72: .split(Strings.COMMA)));
73: }
74:
75: /**
76: * @see java.lang.Object#toString()
77: */
78: @Override
79: public String toString() {
80: return this.getClass().getName();
81: }
82: }
|