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.sync;
022:
023: import java.beans.Introspector;
024: import java.beans.PropertyDescriptor;
025: import java.beans.PropertyEditor;
026: import java.beans.PropertyEditorManager;
027: import java.util.HashMap;
028: import java.util.Map;
029: import java.util.Properties;
030:
031: import net.sf.hajdbc.Messages;
032: import net.sf.hajdbc.SynchronizationStrategy;
033:
034: /**
035: * @author Paul Ferraro
036: * @since 1.1
037: */
038: public class SynchronizationStrategyBuilder {
039: private static final String CLASS = "class"; //$NON-NLS-1$
040:
041: private String id;
042: private Class<? extends SynchronizationStrategy> targetClass;
043: private Properties properties;
044:
045: /**
046: * Constructs a new SynchronizationStrategyBuilder.
047: */
048: public SynchronizationStrategyBuilder() {
049: // Do nothing
050: }
051:
052: /**
053: * @return the unique identifier for this synchronization strategy
054: */
055: public String getId() {
056: return this .id;
057: }
058:
059: /**
060: * @return a SynchronizationStrategy instance
061: * @throws Exception
062: */
063: public SynchronizationStrategy buildStrategy() throws Exception {
064: SynchronizationStrategy strategy = this .targetClass.asSubclass(
065: SynchronizationStrategy.class).newInstance();
066:
067: PropertyDescriptor[] descriptors = Introspector.getBeanInfo(
068: this .targetClass).getPropertyDescriptors();
069:
070: Map<String, PropertyDescriptor> propertyDescriptorMap = new HashMap<String, PropertyDescriptor>();
071:
072: for (PropertyDescriptor descriptor : descriptors) {
073: // Prevent Object.getClass() from being read as a property
074: if (descriptor.getName().equals(CLASS))
075: continue;
076:
077: propertyDescriptorMap.put(descriptor.getName(), descriptor);
078: }
079:
080: for (Object key : this .properties.keySet()) {
081: String name = (String) key;
082:
083: PropertyDescriptor descriptor = propertyDescriptorMap
084: .get(name);
085:
086: if (descriptor == null) {
087: throw new IllegalArgumentException(Messages.getMessage(
088: Messages.INVALID_PROPERTY, name, this
089: .getClass().getName()));
090: }
091:
092: PropertyEditor editor = PropertyEditorManager
093: .findEditor(descriptor.getPropertyType());
094:
095: String textValue = this .properties.getProperty(name);
096:
097: try {
098: if (editor == null) {
099: throw new Exception();
100: }
101:
102: editor.setAsText(textValue);
103: } catch (Exception e) {
104: throw new IllegalArgumentException(Messages.getMessage(
105: Messages.INVALID_PROPERTY_VALUE, textValue,
106: name, this .targetClass.getName()));
107: }
108:
109: descriptor.getWriteMethod().invoke(strategy,
110: editor.getValue());
111: }
112:
113: return strategy;
114: }
115:
116: /**
117: * @param id
118: * @param strategy
119: * @return a builder for this strategy
120: * @throws Exception
121: */
122: public static SynchronizationStrategyBuilder getBuilder(String id,
123: SynchronizationStrategy strategy) throws Exception {
124: SynchronizationStrategyBuilder builder = new SynchronizationStrategyBuilder();
125:
126: builder.id = id;
127:
128: Class<? extends SynchronizationStrategy> strategyClass = strategy
129: .getClass();
130:
131: builder.targetClass = strategyClass;
132:
133: builder.properties = new Properties();
134:
135: PropertyDescriptor[] descriptors = Introspector.getBeanInfo(
136: strategyClass).getPropertyDescriptors();
137:
138: for (PropertyDescriptor descriptor : descriptors) {
139: // Prevent Object.getClass() from being written as a property
140: if (descriptor.getName().equals(CLASS))
141: continue;
142:
143: PropertyEditor editor = PropertyEditorManager
144: .findEditor(descriptor.getPropertyType());
145:
146: if (editor == null)
147: continue;
148:
149: editor
150: .setValue(descriptor.getReadMethod().invoke(
151: strategy));
152:
153: builder.properties.setProperty(descriptor.getName(), editor
154: .getAsText());
155: }
156:
157: return builder;
158: }
159: }
|