1 25 package org.ofbiz.entity.datasource; 26 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 import org.ofbiz.base.util.Debug; 31 import org.ofbiz.entity.config.DatasourceInfo; 32 import org.ofbiz.entity.config.EntityConfigUtil; 33 34 41 public class GenericHelperFactory { 42 43 public static final String module = GenericHelperFactory.class.getName(); 44 45 protected static Map helperCache = new HashMap (); 47 48 public static GenericHelper getHelper(String helperName) { 49 GenericHelper helper = (GenericHelper) helperCache.get(helperName); 50 51 if (helper == null) { 53 synchronized (GenericHelperFactory.class) { 54 helper = (GenericHelper) helperCache.get(helperName); 56 if (helper == null) { 57 try { 58 DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName); 59 60 if (datasourceInfo == null) { 61 throw new IllegalStateException ("Could not find datasource definition with name " + helperName); 62 } 63 String helperClassName = datasourceInfo.helperClass; 64 Class helperClass = null; 65 66 if (helperClassName != null && helperClassName.length() > 0) { 67 try { 68 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 69 helperClass = loader.loadClass(helperClassName); 70 } catch (ClassNotFoundException e) { 71 Debug.logWarning(e, module); 72 throw new IllegalStateException ("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage()); 73 } 74 } 75 76 Class [] paramTypes = new Class [] {String .class}; 77 Object [] params = new Object [] {helperName}; 78 79 java.lang.reflect.Constructor helperConstructor = null; 80 81 if (helperClass != null) { 82 try { 83 helperConstructor = helperClass.getConstructor(paramTypes); 84 } catch (NoSuchMethodException e) { 85 Debug.logWarning(e, module); 86 throw new IllegalStateException ("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage()); 87 } 88 } 89 try { 90 helper = (GenericHelper) helperConstructor.newInstance(params); 91 } catch (IllegalAccessException e) { 92 Debug.logWarning(e, module); 93 throw new IllegalStateException ("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage()); 94 } catch (InstantiationException e) { 95 Debug.logWarning(e, module); 96 throw new IllegalStateException ("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage()); 97 } catch (java.lang.reflect.InvocationTargetException e) { 98 Debug.logWarning(e, module); 99 throw new IllegalStateException ("Error loading GenericHelper class \"" + helperClassName + "\": " + e.getMessage()); 100 } 101 102 if (helper != null) 103 helperCache.put(helperName, helper); 104 } catch (SecurityException e) { 105 Debug.logError(e, module); 106 throw new IllegalStateException ("Error loading GenericHelper class: " + e.toString()); 107 } 108 } 109 } 110 } 111 return helper; 112 } 113 } 114 | Popular Tags |