1 15 package org.apache.hivemind.lib.factory; 16 17 import java.lang.reflect.Constructor ; 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.ErrorLog; 25 import org.apache.hivemind.HiveMind; 26 import org.apache.hivemind.impl.BaseLocatable; 27 import org.apache.hivemind.lib.BeanFactory; 28 29 34 public class BeanFactoryImpl extends BaseLocatable implements BeanFactory 35 { 36 private ErrorLog _errorLog; 37 38 private Class _vendType; 39 40 private Map _contributions = new HashMap (); 41 42 private Map _cache = new HashMap (); 43 44 private boolean _defaultCacheable; 45 46 public BeanFactoryImpl(ErrorLog errorLog, Class vendType, List contributions, 47 boolean defaultCacheable) 48 { 49 _errorLog = errorLog; 50 _vendType = vendType; 51 _defaultCacheable = defaultCacheable; 52 53 processContributions(contributions); 54 } 55 56 public boolean contains(String locator) 57 { 58 int commax = locator.indexOf(','); 59 60 String name = commax < 0 ? locator.trim() : locator.substring(0, commax); 61 62 return _contributions.containsKey(name); 63 } 64 65 private void processContributions(List list) 66 { 67 Iterator i = list.iterator(); 68 69 while (i.hasNext()) 70 { 71 BeanFactoryContribution c = (BeanFactoryContribution) i.next(); 72 73 Class beanClass = c.getBeanClass(); 74 75 if (beanClass.isInterface() || beanClass.isArray() || beanClass.isPrimitive()) 76 { 77 _errorLog.error(FactoryMessages.invalidContributionClass(c), c.getLocation(), null); 78 continue; 79 } 80 81 if (!_vendType.isAssignableFrom(beanClass)) 82 { 83 _errorLog.error(FactoryMessages.wrongContributionType(c, _vendType), c 84 .getLocation(), null); 85 continue; 86 } 87 88 _contributions.put(c.getName(), c); 89 } 90 } 91 92 public synchronized Object get(String locator) 93 { 94 Object result = _cache.get(locator); 95 96 if (result == null) 97 result = create(locator); 98 99 return result; 100 } 101 102 104 private Object create(String locator) 105 { 106 int commax = locator.indexOf(','); 107 108 String name = commax < 0 ? locator.trim() : locator.substring(0, commax); 109 String initializer = commax < 0 ? null : locator.substring(commax + 1).trim(); 110 111 BeanFactoryContribution c = (BeanFactoryContribution) _contributions.get(name); 112 113 if (c == null) 114 throw new ApplicationRuntimeException(FactoryMessages.unknownContribution(name)); 115 116 Object result = construct(c, initializer); 117 118 if (c.getStoreResultInCache(_defaultCacheable)) 119 _cache.put(locator, result); 120 121 return result; 122 } 123 124 private Object construct(BeanFactoryContribution contribution, String initializer) 125 { 126 Class beanClass = contribution.getBeanClass(); 127 128 try 129 { 130 if (HiveMind.isBlank(initializer)) 131 return beanClass.newInstance(); 132 133 Constructor c = beanClass.getConstructor(new Class [] 134 { String .class }); 135 136 return c.newInstance(new Object [] 137 { initializer }); 138 } 139 catch (Exception ex) 140 { 141 throw new ApplicationRuntimeException(FactoryMessages 142 .unableToInstantiate(beanClass, ex), contribution.getLocation(), ex); 143 144 } 145 } 146 147 } | Popular Tags |