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: */
021: package net.sf.hajdbc.local;
022:
023: import java.util.ArrayList;
024: import java.util.Arrays;
025: import java.util.Collections;
026: import java.util.List;
027: import java.util.Set;
028: import java.util.TreeSet;
029: import java.util.prefs.BackingStoreException;
030: import java.util.prefs.Preferences;
031:
032: import net.sf.hajdbc.Database;
033: import net.sf.hajdbc.DatabaseCluster;
034: import net.sf.hajdbc.Messages;
035: import net.sf.hajdbc.StateManager;
036: import net.sf.hajdbc.util.Strings;
037:
038: import org.slf4j.Logger;
039: import org.slf4j.LoggerFactory;
040:
041: /**
042: * @author Paul Ferraro
043: */
044: public class LocalStateManager implements StateManager {
045: private static Preferences preferences = Preferences
046: .userNodeForPackage(LocalStateManager.class);
047: private static Logger logger = LoggerFactory
048: .getLogger(LocalStateManager.class);
049:
050: private DatabaseCluster<?> databaseCluster;
051:
052: /**
053: * @param databaseCluster
054: */
055: public LocalStateManager(DatabaseCluster<?> databaseCluster) {
056: this .databaseCluster = databaseCluster;
057: }
058:
059: /**
060: * @see net.sf.hajdbc.StateManager#getInitialState()
061: */
062: @Override
063: public Set<String> getInitialState() {
064: String state = preferences.get(this .statePreferenceKey(), null);
065:
066: if (state == null) {
067: logger.info(Messages
068: .getMessage(Messages.INITIAL_CLUSTER_STATE_NONE));
069:
070: return null;
071: }
072:
073: Set<String> databaseSet = Collections.emptySet();
074:
075: if (state.length() > 0) {
076: databaseSet = new TreeSet<String>(Arrays.asList(state
077: .split(Strings.COMMA)));
078: }
079:
080: logger.info(Messages.getMessage(
081: Messages.INITIAL_CLUSTER_STATE_LOCAL, databaseSet));
082:
083: return databaseSet;
084: }
085:
086: /**
087: * @see net.sf.hajdbc.StateManager#add(java.lang.String)
088: */
089: @Override
090: public void add(String databaseId) {
091: this .storeState();
092: }
093:
094: /**
095: * @see net.sf.hajdbc.StateManager#remove(java.lang.String)
096: */
097: @Override
098: public void remove(String databaseId) {
099: this .storeState();
100: }
101:
102: private void storeState() {
103: List<String> databaseList = new ArrayList<String>();
104:
105: for (Database<?> database : this .databaseCluster.getBalancer()
106: .all()) {
107: databaseList.add(database.getId());
108: }
109:
110: preferences.put(this .statePreferenceKey(), Strings.join(
111: databaseList, Strings.COMMA));
112:
113: try {
114: preferences.flush();
115: } catch (BackingStoreException e) {
116: throw new RuntimeException(Messages.getMessage(
117: Messages.CLUSTER_STATE_STORE_FAILED,
118: this .databaseCluster), e);
119: }
120: }
121:
122: /**
123: * @see net.sf.hajdbc.StateManager#start()
124: */
125: @Override
126: public void start() throws Exception {
127: preferences.sync();
128: }
129:
130: /**
131: * @see net.sf.hajdbc.StateManager#stop()
132: */
133: @Override
134: public void stop() {
135: try {
136: preferences.sync();
137: } catch (BackingStoreException e) {
138: logger.warn(e.getMessage(), e);
139: }
140: }
141:
142: private String statePreferenceKey() {
143: return this .databaseCluster.getId();
144: }
145:
146: /**
147: * @see net.sf.hajdbc.StateManager#isMembershipEmpty()
148: */
149: @Override
150: public boolean isMembershipEmpty() {
151: return false;
152: }
153: }
|