KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > SynchronizationStrategyBuilder


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
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;
22
23 import java.beans.Introspector JavaDoc;
24 import java.beans.PropertyDescriptor JavaDoc;
25 import java.beans.PropertyEditor JavaDoc;
26 import java.beans.PropertyEditorManager JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import net.sf.hajdbc.util.Collections;
32
33 /**
34  * @author Paul Ferraro
35  * @since 1.1
36  */

37 public class SynchronizationStrategyBuilder
38 {
39     private String JavaDoc id;
40     private String JavaDoc className;
41     private Properties JavaDoc properties;
42     
43     /**
44      * Constructs a new SynchronizationStrategyBuilder.
45      */

46     public SynchronizationStrategyBuilder()
47     {
48         // Do nothing
49
}
50     
51     /**
52      * Constructs a new SynchronizationStrategyBuilder.
53      * @param id
54      */

55     public SynchronizationStrategyBuilder(String JavaDoc id)
56     {
57         this.id = id;
58     }
59     
60     /**
61      * @return the className.
62      */

63     public String JavaDoc getClassName()
64     {
65         return this.className;
66     }
67     
68     /**
69      * @param className the className to set.
70      */

71     public void setClassName(String JavaDoc className)
72     {
73         this.className = className;
74     }
75     
76     /**
77      * @return the id.
78      */

79     public String JavaDoc getId()
80     {
81         return this.id;
82     }
83     
84     /**
85      * @param id the id to set.
86      */

87     public void setId(String JavaDoc id)
88     {
89         this.id = id;
90     }
91     
92     /**
93      * @return the properties.
94      */

95     public Properties JavaDoc getProperties()
96     {
97         return this.properties;
98     }
99     
100     /**
101      * @param properties the properties to set.
102      */

103     public void setProperties(Properties JavaDoc properties)
104     {
105         this.properties = properties;
106     }
107     
108     /**
109      * @return a SynchronizationStrategy instance
110      * @throws Exception
111      */

112     public SynchronizationStrategy buildStrategy() throws Exception JavaDoc
113     {
114         Class JavaDoc<?> strategyClass = Class.forName(this.className);
115         
116         SynchronizationStrategy strategy = strategyClass.asSubclass(SynchronizationStrategy.class).newInstance();
117         
118         PropertyDescriptor JavaDoc[] descriptors = Introspector.getBeanInfo(strategyClass).getPropertyDescriptors();
119         
120         Map JavaDoc<String JavaDoc, PropertyDescriptor JavaDoc> propertyDescriptorMap = new HashMap JavaDoc<String JavaDoc, PropertyDescriptor JavaDoc>();
121         
122         for (PropertyDescriptor JavaDoc descriptor: descriptors)
123         {
124             if (descriptor.getName().equals("class")) continue;
125             
126             propertyDescriptorMap.put(descriptor.getName(), descriptor);
127         }
128         
129         for (String JavaDoc name: Collections.cast(this.properties.keySet(), String JavaDoc.class))
130         {
131             PropertyDescriptor JavaDoc descriptor = propertyDescriptorMap.get(name);
132             
133             if (descriptor == null)
134             {
135                 throw new IllegalArgumentException JavaDoc(Messages.getMessage(Messages.INVALID_PROPERTY, name, this.getClass().getName()));
136             }
137             
138             PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(descriptor.getPropertyType());
139             
140             String JavaDoc textValue = this.properties.getProperty(name);
141             
142             try
143             {
144                 if (editor == null)
145                 {
146                     throw new Exception JavaDoc();
147                 }
148
149                 editor.setAsText(textValue);
150             }
151             catch (Exception JavaDoc e)
152             {
153                 throw new IllegalArgumentException JavaDoc(Messages.getMessage(Messages.INVALID_PROPERTY_VALUE, textValue, name, this.className));
154             }
155             
156             descriptor.getWriteMethod().invoke(strategy, editor.getValue());
157         }
158         
159         return strategy;
160     }
161     
162     /**
163      * @param id
164      * @param strategy
165      * @return a builder for this strategy
166      * @throws Exception
167      */

168     public static SynchronizationStrategyBuilder getBuilder(String JavaDoc id, SynchronizationStrategy strategy) throws Exception JavaDoc
169     {
170         SynchronizationStrategyBuilder builder = new SynchronizationStrategyBuilder();
171         
172         builder.setId(id);
173         
174         Class JavaDoc strategyClass = strategy.getClass();
175         
176         builder.setClassName(strategyClass.getName());
177         
178         Properties JavaDoc properties = new Properties JavaDoc();
179         
180         PropertyDescriptor JavaDoc[] descriptors = Introspector.getBeanInfo(strategyClass).getPropertyDescriptors();
181         
182         for (PropertyDescriptor JavaDoc descriptor: descriptors)
183         {
184             if (descriptor.getName().equals("class")) continue;
185             
186             PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(descriptor.getPropertyType());
187             
188             if (editor == null) continue;
189             
190             editor.setValue(descriptor.getReadMethod().invoke(strategy));
191             
192             properties.setProperty(descriptor.getName(), editor.getAsText());
193         }
194         
195         builder.setProperties(properties);
196         
197         return builder;
198     }
199 }
200
Popular Tags