KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > spds > engine > SyncSourceFactory


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.syncclient.spds.engine;
20
21 import java.lang.reflect.Method JavaDoc;
22
23 import java.util.Hashtable JavaDoc;
24 import java.util.Enumeration JavaDoc;
25
26 import sync4j.syncclient.common.logging.Logger;
27 import sync4j.syncclient.common.SimpleClassLoader;
28 import sync4j.syncclient.spds.source.SPSSyncSource;
29
30 /**
31  * This is a factory for SyncSource objects. Its istantiates the right instance
32  * given the source to be used. The it calls the setXXX() method for each
33  * property in the source definition <i>properties</i> hashtable.
34  *
35  * @author Fabio Maggi
36  * @author Stefano Fornari
37  *
38  * @version $Id: SyncSourceFactory.java,v 1.5 2005/01/19 11:18:36 fabius Exp $
39  **/

40 public class SyncSourceFactory {
41
42     private Hashtable JavaDoc syncSourcesDefinition = null ;
43     private Hashtable JavaDoc syncSources = null ;
44
45     private String JavaDoc libDir = null ;
46     private Logger logger = new Logger() ;
47
48     /**
49      * Creates a new SyncSourceFactory reading the sync source implementation
50      * classes from the given <i>libDirectory</i> and using the given
51      * <i>syncSourcesDefinition</i>.
52      *
53      * @param libDir directory where classes are looked for
54      * @param syncSourcesDefinition sources definitions
55      *
56      */

57     public SyncSourceFactory (String JavaDoc libDir, Hashtable JavaDoc syncSourcesDefinition) {
58
59         this.syncSourcesDefinition = syncSourcesDefinition;
60         this.syncSources = new Hashtable JavaDoc();
61
62         this.libDir = libDir;
63
64     }
65
66     /**
67      * @return syncSource
68      */

69     public SyncSource getSyncSource(String JavaDoc dataStoreName) {
70
71         SyncSource s = (SyncSource)syncSources.get(dataStoreName);
72
73         if (s != null) {
74             return s;
75         }
76
77         String JavaDoc syncSourceClass = null;
78         SyncSource syncSource = null;
79
80         SyncSourceDefinition def = (SyncSourceDefinition) syncSourcesDefinition.get(dataStoreName);
81
82         syncSourceClass = def.getSourceClass();
83
84         try {
85             syncSource = (SyncSource) (Class.forName(syncSourceClass)).newInstance();
86         } catch (Exception JavaDoc e) {
87             e.printStackTrace();
88
89             String JavaDoc msg = "Error loading class " +
90                          syncSourceClass +
91                          ": " +
92                          e.getMessage() ;
93
94             if (logger.isLoggable(Logger.INFO)) {
95                 logger.info(msg);
96             }
97
98             return null;
99         }
100
101         //
102
// If we loaded a SPSSyncSource, we set its classLoader property to
103
// the SimpleClassLoader instance.
104
//
105

106         if (syncSourceClass.endsWith(".SPSSyncSource")) {
107             ((SPSSyncSource)syncSource).setClassLoader(new SimpleClassLoader(this.libDir));
108         }
109
110
111         Hashtable JavaDoc properties = def.getProperties();
112
113         Enumeration JavaDoc e = properties.keys();
114
115         String JavaDoc key = null;
116         while (e.hasMoreElements()) {
117             key = (String JavaDoc)e.nextElement();
118             setProperty(syncSource, key, (String JavaDoc)properties.get(key));
119         }
120
121         syncSources.put((Object JavaDoc) dataStoreName, (Object JavaDoc) syncSource);
122
123         return syncSource;
124     }
125
126     // ---------------------------------------------------------- Private methds
127

128     private void setProperty(SyncSource source, String JavaDoc key, String JavaDoc value) {
129         String JavaDoc methodName = "";
130
131         char firstLetter = key.toUpperCase().charAt(0);
132         if (key.length() > 1) {
133             methodName = key.substring(1);
134         }
135
136         methodName = "set" + firstLetter + methodName;
137
138         Class JavaDoc sourceClass = source.getClass();
139
140         try {
141             Method JavaDoc m = sourceClass.getMethod(methodName, new Class JavaDoc[] { String JavaDoc.class });
142             m.invoke(source, new String JavaDoc[] {value});
143         } catch (Exception JavaDoc e) {
144             String JavaDoc msg = "Property " +
145                           key +
146                           " not set to " +
147                           value +
148                           ". Method " +
149                           methodName +
150                           "(String s) not found in " +
151                           sourceClass.getName() ;
152
153             if (logger.isLoggable(Logger.DEBUG)) {
154                 logger.debug(msg);
155             }
156
157         }
158     }
159 }
Popular Tags