1 21 22 package com.jaspersoft.jasperserver.api.common.service.impl; 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.Comparator ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.SortedSet ; 32 import java.util.TreeSet ; 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 45 public class ObjectFactoryImpl implements ObjectFactory { 46 private static final Log log = LogFactory.getLog(ObjectFactoryImpl.class); 47 48 private final Comparator itfComparator = new Comparator () { 49 public int compare(Object o1, Object o2) { 50 Class itf1 = (Class ) o1; 51 Class itf2 = (Class ) 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 68 69 public Object newObject(Map classMappings, Class _class) { 70 Class 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 e) { 78 log.error(e, e); 79 throw new JSExceptionWrapper(e); 80 } catch (IllegalAccessException e) { 81 log.error(e, e); 82 throw new JSExceptionWrapper(e); 83 } 84 } 85 86 87 88 89 92 93 public Class getImplementationClass(Map classMappings, Class itfClass) { 94 if (classMappings == null) { 95 return null; 96 } 97 98 try { 100 SortedSet interfaces = new TreeSet (itfComparator); 101 102 for (Iterator it = classMappings.keySet().iterator(); it.hasNext();) { 103 String itfName = (String ) it.next(); 104 Class itf = Class.forName(itfName, true, Thread.currentThread().getContextClassLoader()); 105 if (itf.isAssignableFrom(itfClass)) { 106 interfaces.add(itf); 107 } 108 } 109 110 Class implClass = null; 111 if (!interfaces.isEmpty()) { 112 Class itf = (Class ) interfaces.iterator().next(); 113 String implName = (String ) classMappings.get(itf.getName()); 114 implClass = Class.forName(implName, true, Thread.currentThread().getContextClassLoader()); 115 116 122 } 123 124 return implClass; 125 } catch (ClassNotFoundException e) { 126 log.error(e, e); 127 throw new JSExceptionWrapper(e); 128 } 129 } 130 131 134 public String getImplementationClassName(Map classMappings, Class itfClass) { 135 Class _class = getImplementationClass(classMappings, itfClass); 136 if (_class == null) { 137 return null; 138 } else { 139 return _class.getName(); 140 } 141 } 142 143 146 public Class getInterface(Map classMappings, Class implClass) { 147 if (classMappings == null) { 148 return null; 149 } 150 151 try { 153 Class interfaceClass = null; 154 for (Iterator it = classMappings.entrySet().iterator(); it.hasNext();) { 155 Map.Entry entry = (Map.Entry ) it.next(); 156 String itfName = (String ) entry.getKey(); 157 String implName = (String ) entry.getValue(); 158 Class itf = Class.forName(itfName, true, Thread.currentThread().getContextClassLoader()); 159 160 165 if (implClass.getName().startsWith(implName, 0)) { 166 169 interfaceClass = itf; 171 break; 172 } 173 } 174 175 return interfaceClass; 176 } catch (ClassNotFoundException e) { 177 log.error(e, e); 178 throw new JSExceptionWrapper(e); 179 } 180 } 181 182 185 public String getInterfaceName(Map classMappings, Class implClass) { 186 Class _class = getInterface(classMappings, implClass); 187 if (_class == null) { 188 return null; 189 } else { 190 return _class.getName(); 191 } 192 } 193 194 195 196 197 200 public Class getImplementationClass(Map classMappings, String id) { 201 if (classMappings == null) { 202 return null; 203 } 204 try { 205 206 String implName = (String ) classMappings.get(id); 207 Class implClass = Class.forName(implName, true, Thread.currentThread().getContextClassLoader()); 208 209 return implClass; 210 } catch (ClassNotFoundException e) { 211 log.error(e, e); 212 throw new JSExceptionWrapper(e); 213 } 214 } 215 216 217 218 219 222 public String getImplementationClassName(Map classMappings, String id) { 223 return (String ) classMappings.get(id); 224 } 225 226 227 228 229 232 public String getIdForClass(Map classMappings, Class _class) { 233 if (classMappings == null) { 234 return null; 235 } 236 237 for (Iterator it = classMappings.entrySet().iterator(); it.hasNext();) { 238 Map.Entry entry = (Map.Entry ) it.next(); 239 String id = (String ) entry.getKey(); 240 String implName = (String ) entry.getValue(); 241 242 247 if (_class.getName().startsWith(implName, 0)) { 248 251 return id; 253 } 254 } 255 256 return null; 257 } 258 259 260 261 262 265 public Object newObject(Map classMappings, String id) { 266 Class 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 e) { 274 log.error(e, e); 275 throw new JSExceptionWrapper(e); 276 } catch (IllegalAccessException e) { 277 log.error(e, e); 278 throw new JSExceptionWrapper(e); 279 } 280 } 281 282 283 284 285 288 public List getKeys(Map classMappings) { 289 List l = new ArrayList (classMappings.keySet()); 290 Collections.sort(l); 291 return l; 292 } 293 294 } 295 | Popular Tags |