KickJava   Java API By Example, From Geeks To Geeks.

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


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.Comparator JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.SortedSet JavaDoc;
28 import java.util.TreeSet JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.springframework.context.ApplicationContext;
33 import org.springframework.context.ApplicationContextAware;
34
35 import com.jaspersoft.jasperserver.api.JSException;
36 import com.jaspersoft.jasperserver.api.JSExceptionWrapper;
37 import com.jaspersoft.jasperserver.api.common.service.BeanForInterfaceFactory;
38
39 /**
40  * @author swood
41  *
42  */

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

72     public String JavaDoc getBeanName(Map JavaDoc interfaceToBeanMappings, Class JavaDoc itfClass) {
73         if (interfaceToBeanMappings == null) {
74             return null;
75         }
76         
77         //TODO cache
78
try {
79             SortedSet JavaDoc interfaces = new TreeSet JavaDoc(itfComparator);
80
81             for (Iterator JavaDoc it = interfaceToBeanMappings.keySet().iterator(); it.hasNext();) {
82                 String JavaDoc itfName = (String JavaDoc) it.next();
83                 Class JavaDoc itf = Class.forName(itfName, true, Thread.currentThread().getContextClassLoader());
84                 if (itf.isAssignableFrom(itfClass)) {
85                     interfaces.add(itf);
86                 }
87             }
88
89             if (!interfaces.isEmpty()) {
90                 Class JavaDoc itf = (Class JavaDoc) interfaces.iterator().next();
91                 return (String JavaDoc) interfaceToBeanMappings.get(itf.getName());
92             }
93             return null;
94         } catch (ClassNotFoundException JavaDoc e) {
95             log.error(e, e);
96             throw new JSExceptionWrapper(e);
97         }
98     }
99
100     /* (non-Javadoc)
101      * @see com.jaspersoft.jasperserver.api.common.service.BeanForInterfaceFactory#getBeanName(java.util.Map, java.lang.Class)
102      */

103     public Object JavaDoc getBean(Map JavaDoc classToBeanMappings, Class JavaDoc _class) {
104         
105         String JavaDoc beanName = getBeanName(classToBeanMappings, _class);
106         
107         /*
108          * TODO Leave this to the caller to handle?
109          */

110         if (beanName == null) {
111             throw new JSException("No bean name found for interface: " + _class.getName());
112         }
113         
114         Object JavaDoc bean = ctx.getBean(beanName);
115         
116         /*
117          * TODO Leave this to the caller to handle?
118          */

119         if (bean == null) {
120             throw new JSException("No bean with name: " + beanName);
121         }
122         
123         return bean;
124     }
125
126 }
127
Popular Tags