1 13 package info.magnolia.cms.util; 14 15 import info.magnolia.cms.core.SystemProperty; 16 17 import java.util.HashMap ; 18 import java.util.Map ; 19 import java.util.Properties ; 20 21 import org.apache.commons.beanutils.ConstructorUtils; 22 import org.apache.commons.discovery.tools.DiscoverClass; 23 import org.apache.commons.discovery.tools.DiscoverSingleton; 24 import org.apache.commons.lang.StringUtils; 25 import org.slf4j.Logger; 26 27 28 32 public class FactoryUtil { 33 34 public interface InstanceFactory { 35 public Object newInstance(); 36 } 37 38 41 protected static Logger log; 43 protected static DiscoverClass discovery = new DiscoverClass(); 44 45 48 protected static Map instances = new HashMap (); 49 50 53 protected static Map factories = new HashMap (); 54 55 private FactoryUtil() { 56 57 } 58 59 62 public static Object getInstance(Class interf) { 63 return newInstance(interf); 64 } 65 66 public static Object newInstance(Class interf) { 67 try { 68 if (factories.containsKey(interf)) { 69 return ((InstanceFactory) factories.get(interf)).newInstance(); 70 } 71 return discovery.newInstance(interf, SystemProperty.getProperties(), interf.getName()); 73 } 74 catch (Exception e) { 75 log.error("can't instantiate an implementation of this class [" + interf.getName() + "]"); 76 } 77 return null; 78 } 79 80 83 public static Object getInstanceWithoutDiscovery(String className, Object [] args) { 84 return newInstanceWithoutDiscovery(className, args); 85 } 86 87 93 public static Object newInstanceWithoutDiscovery(String className, Object [] args) { 94 95 if (StringUtils.isEmpty(className)) { 96 return null; 97 } 98 99 Class clazz; 100 try { 101 clazz = ClassUtil.classForName(className); 102 } 103 catch (ClassNotFoundException e) { 104 log.error("can't find class: " + className, e); 105 return null; 106 } 107 try { 108 return ConstructorUtils.invokeConstructor(clazz, args); 109 } 110 catch (Exception e) { 111 log.error("can't instantiate: " + className, e); 112 } 113 return null; 114 } 115 116 119 public static Object getInstanceWithoutDiscovery(String className) { 120 return newInstanceWithoutDiscovery(className); 121 } 122 123 public static Object newInstanceWithoutDiscovery(String className) { 124 return newInstanceWithoutDiscovery(className, new Object []{}); 125 } 126 127 public static Object getSingleton(Class interf) { 128 Object instance = instances.get(interf); 129 if (instance == null) { 130 if (factories.containsKey(interf)) { 131 instance = ((InstanceFactory) factories.get(interf)).newInstance(); 132 } else { 133 instance = DiscoverSingleton.find(interf, SystemProperty.getProperties(), interf.getName()); 134 } 135 instances.put(interf, instance); 136 } 137 return instance; 138 } 139 140 public static void setDefaultImplementation(Class interf, Class impl) { 141 setDefaultImplementation(interf, impl.getName()); 142 } 143 144 148 public static void setDefaultImplementation(Class interf, String impl) { 149 if(!SystemProperty.getProperties().containsKey(interf.getName())){ 150 setImplementation(interf, impl); 151 } 152 } 153 154 public static void setImplementation(Class interf, Class impl) { 155 setDefaultImplementation(interf, impl.getName()); 156 } 157 158 162 public static void setImplementation(Class interf, String impl) { 163 SystemProperty.getProperties().setProperty(interf.getName(), impl); 164 } 165 166 167 173 public static void setInstance(Class interf, Object instance) { 174 instances.put(interf, instance); 175 } 176 177 183 public static void setInstanceFactory(Class interf, InstanceFactory factory) { 184 factories.put(interf, factory); 185 } 186 187 public static void clear() { 188 factories.clear(); 189 instances.clear(); 190 DiscoverSingleton.release(); 191 } 192 193 } 194 | Popular Tags |