KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > common > service > impl > ObjectFactoryImpl


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
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 WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21
22 package com.jaspersoft.jasperserver.api.common.service.impl;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Comparator JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.SortedSet JavaDoc;
32 import java.util.TreeSet JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 import com.jaspersoft.jasperserver.api.JSException;
38 import com.jaspersoft.jasperserver.api.JSExceptionWrapper;
39 import com.jaspersoft.jasperserver.api.common.service.ObjectFactory;
40
41 /**
42  * @author swood
43  *
44  */

45 public class ObjectFactoryImpl implements ObjectFactory {
46     private static final Log log = LogFactory.getLog(ObjectFactoryImpl.class);
47     
48     private final Comparator JavaDoc itfComparator = new Comparator JavaDoc() {
49         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
50             Class JavaDoc itf1 = (Class JavaDoc) o1;
51             Class JavaDoc itf2 = (Class JavaDoc) o2;
52             
53             if (itf1.equals(itf2)) {
54                 return 0;
55             } else if (itf2.isAssignableFrom(itf1)) {
56                 return -1;
57             } else if (itf1.isAssignableFrom(itf2)) {
58                 return 1;
59             } else {
60                 return itf1.getName().compareTo(itf2.getName());
61             }
62         }
63     };
64     
65     /* (non-Javadoc)
66      * @see com.jaspersoft.jasperserver.api.common.service.ObjectFactory#getImplementationClass(java.util.Map, java.lang.Class)
67      */

68
69     public Object JavaDoc newObject(Map JavaDoc classMappings, Class JavaDoc _class) {
70         Class JavaDoc implementationClass = getImplementationClass(classMappings, _class);
71         if (implementationClass == null) {
72             throw new JSException("Implementation class not found for " + _class.getName());
73         }
74         
75         try {
76             return implementationClass.newInstance();
77         } catch (InstantiationException JavaDoc e) {
78             log.error(e, e);
79             throw new JSExceptionWrapper(e);
80         } catch (IllegalAccessException JavaDoc e) {
81             log.error(e, e);
82             throw new JSExceptionWrapper(e);
83         }
84     }
85
86     
87     
88     
89     /* (non-Javadoc)
90      * @see com.jaspersoft.jasperserver.api.common.service.ObjectFactory#newObject(java.util.Map, java.lang.Class)
91      */

92
93     public Class JavaDoc getImplementationClass(Map JavaDoc classMappings, Class JavaDoc itfClass) {
94         if (classMappings == null) {
95             return null;
96         }
97         
98         //TODO cache
99
try {
100             SortedSet JavaDoc interfaces = new TreeSet JavaDoc(itfComparator);
101
102             for (Iterator JavaDoc it = classMappings.keySet().iterator(); it.hasNext();) {
103                 String JavaDoc itfName = (String JavaDoc) it.next();
104                 Class JavaDoc itf = Class.forName(itfName, true, Thread.currentThread().getContextClassLoader());
105                 if (itf.isAssignableFrom(itfClass)) {
106                     interfaces.add(itf);
107                 }
108             }
109
110             Class JavaDoc implClass = null;
111             if (!interfaces.isEmpty()) {
112                 Class JavaDoc itf = (Class JavaDoc) interfaces.iterator().next();
113                 String JavaDoc implName = (String JavaDoc) classMappings.get(itf.getName());
114                 implClass = Class.forName(implName, true, Thread.currentThread().getContextClassLoader());
115
116 // TODO Is this OK?
117
/*
118                 if (!itf.isAssignableFrom(implClass)) {
119                     throw new JSException("Implementation class " + implClass.getName() + " does not implement " + itfClass.getName());
120                 }
121 */

122             }
123
124             return implClass;
125         } catch (ClassNotFoundException JavaDoc e) {
126             log.error(e, e);
127             throw new JSExceptionWrapper(e);
128         }
129     }
130
131     /* (non-Javadoc)
132      * @see com.jaspersoft.jasperserver.api.common.service.ObjectFactory#getImplementationClassName(java.util.Map, java.lang.Class)
133      */

134     public String JavaDoc getImplementationClassName(Map JavaDoc classMappings, Class JavaDoc itfClass) {
135         Class JavaDoc _class = getImplementationClass(classMappings, itfClass);
136         if (_class == null) {
137             return null;
138         } else {
139             return _class.getName();
140         }
141     }
142
143     /* (non-Javadoc)
144      * @see com.jaspersoft.jasperserver.api.common.service.ObjectFactory#getInterface(java.util.Map, java.lang.Class)
145      */

146     public Class JavaDoc getInterface(Map JavaDoc classMappings, Class JavaDoc implClass) {
147         if (classMappings == null) {
148             return null;
149         }
150         
151         //TODO cache
152
try {
153             Class JavaDoc interfaceClass = null;
154             for (Iterator JavaDoc it = classMappings.entrySet().iterator(); it.hasNext();) {
155                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
156                 String JavaDoc itfName = (String JavaDoc) entry.getKey();
157                 String JavaDoc implName = (String JavaDoc) entry.getValue();
158                 Class JavaDoc itf = Class.forName(itfName, true, Thread.currentThread().getContextClassLoader());
159                 
160                 /*
161                  * We have some classes coming from Hibernate that are proxies.
162                  * These have a class name of <class name>$$ blah blah.
163                  * Match the first part of the class name to handle these.
164                  */

165                 if (implClass.getName().startsWith(implName, 0)) {
166                     // TODO maybe we should do this check too. It makes sense, but
167
// does not work.
168

169                     // && implClass.isAssignableFrom(itf)
170
interfaceClass = itf;
171                     break;
172                 }
173             }
174
175             return interfaceClass;
176         } catch (ClassNotFoundException JavaDoc e) {
177             log.error(e, e);
178             throw new JSExceptionWrapper(e);
179         }
180     }
181
182     /* (non-Javadoc)
183      * @see com.jaspersoft.jasperserver.api.common.service.ObjectFactory#getInterfaceName(java.util.Map, java.lang.Class)
184      */

185     public String JavaDoc getInterfaceName(Map JavaDoc classMappings, Class JavaDoc implClass) {
186         Class JavaDoc _class = getInterface(classMappings, implClass);
187         if (_class == null) {
188             return null;
189         } else {
190             return _class.getName();
191         }
192     }
193
194
195
196
197     /* (non-Javadoc)
198      * @see com.jaspersoft.jasperserver.api.common.service.ObjectFactory#getImplementationClass(java.util.Map, java.lang.String)
199      */

200     public Class JavaDoc getImplementationClass(Map JavaDoc classMappings, String JavaDoc id) {
201         if (classMappings == null) {
202             return null;
203         }
204         try {
205             
206             String JavaDoc implName = (String JavaDoc) classMappings.get(id);
207             Class JavaDoc implClass = Class.forName(implName, true, Thread.currentThread().getContextClassLoader());
208
209             return implClass;
210         } catch (ClassNotFoundException JavaDoc e) {
211             log.error(e, e);
212             throw new JSExceptionWrapper(e);
213         }
214     }
215
216
217
218
219     /* (non-Javadoc)
220      * @see com.jaspersoft.jasperserver.api.common.service.ObjectFactory#getImplementationClassName(java.util.Map, java.lang.String)
221      */

222     public String JavaDoc getImplementationClassName(Map JavaDoc classMappings, String JavaDoc id) {
223         return (String JavaDoc) classMappings.get(id);
224     }
225
226
227
228
229     /* (non-Javadoc)
230      * @see com.jaspersoft.jasperserver.api.common.service.ObjectFactory#getIdForClass(java.util.Map, java.lang.Class)
231      */

232     public String JavaDoc getIdForClass(Map JavaDoc classMappings, Class JavaDoc _class) {
233         if (classMappings == null) {
234             return null;
235         }
236         
237         for (Iterator JavaDoc it = classMappings.entrySet().iterator(); it.hasNext();) {
238             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
239             String JavaDoc id = (String JavaDoc) entry.getKey();
240             String JavaDoc implName = (String JavaDoc) entry.getValue();
241             
242             /*
243              * We have some classes coming from Hibernate that are proxies.
244              * These have a class name of <class name>$$ blah blah.
245              * Match the first part of the class name to handle these.
246              */

247             if (_class.getName().startsWith(implName, 0)) {
248                 // TODO maybe we should do this check too. It makes sense, but
249
// does not work.
250

251                 // && implClass.isAssignableFrom(itf)
252
return id;
253             }
254         }
255
256         return null;
257     }
258
259
260
261
262     /* (non-Javadoc)
263      * @see com.jaspersoft.jasperserver.api.common.service.ObjectFactory#newObject(java.util.Map, java.lang.String)
264      */

265     public Object JavaDoc newObject(Map JavaDoc classMappings, String JavaDoc id) {
266         Class JavaDoc implementationClass = getImplementationClass(classMappings, id);
267         if (implementationClass == null) {
268             throw new JSException("Implementation class not found for " + id);
269         }
270         
271         try {
272             return implementationClass.newInstance();
273         } catch (InstantiationException JavaDoc e) {
274             log.error(e, e);
275             throw new JSExceptionWrapper(e);
276         } catch (IllegalAccessException JavaDoc e) {
277             log.error(e, e);
278             throw new JSExceptionWrapper(e);
279         }
280     }
281
282
283
284
285     /* (non-Javadoc)
286      * @see com.jaspersoft.jasperserver.api.common.service.ObjectFactory#getKeys(java.util.Map)
287      */

288     public List JavaDoc getKeys(Map JavaDoc classMappings) {
289         List JavaDoc l = new ArrayList JavaDoc(classMappings.keySet());
290         Collections.sort(l);
291         return l;
292     }
293
294 }
295
Popular Tags