1 15 package org.apache.tapestry.services.impl; 16 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 import org.apache.hivemind.ApplicationRuntimeException; 23 import org.apache.hivemind.ErrorLog; 24 import org.apache.tapestry.engine.IEngineService; 25 import org.apache.tapestry.services.ServiceMap; 26 27 33 public class ServiceMapImpl implements ServiceMap, EngineServiceSource 34 { 35 38 private List _applicationServices; 39 40 43 private List _factoryServices; 44 45 private ErrorLog _errorLog; 46 47 50 private Map _services; 51 52 56 57 private Map _proxies = new HashMap (); 58 59 public void initializeService() 60 { 61 Map factoryMap = buildServiceMap(_factoryServices); 62 Map applicationMap = buildServiceMap(_applicationServices); 63 64 67 factoryMap.putAll(applicationMap); 68 69 _services = factoryMap; 70 } 71 72 private Map buildServiceMap(List services) 73 { 74 Map result = new HashMap (); 75 76 Iterator i = services.iterator(); 77 while (i.hasNext()) 78 { 79 EngineServiceContribution contribution = (EngineServiceContribution) i.next(); 80 String name = contribution.getName(); 81 82 EngineServiceContribution existing = (EngineServiceContribution) result.get(name); 83 84 if (existing != null) 85 { 86 _errorLog.error( 87 ImplMessages.dupeService(name, existing), 88 existing.getLocation(), 89 null); 90 continue; 91 } 92 93 result.put(name, contribution); 94 } 95 96 return result; 97 } 98 99 public synchronized IEngineService getService(String name) 100 { 101 IEngineService result = (IEngineService) _proxies.get(name); 102 103 if (result == null) 104 { 105 result = buildProxy(name); 106 _proxies.put(name, result); 107 } 108 109 return result; 110 } 111 112 115 116 public IEngineService resolveEngineService(String name) 117 { 118 EngineServiceContribution contribution = (EngineServiceContribution) _services.get(name); 119 120 if (contribution == null) 121 throw new ApplicationRuntimeException(ImplMessages.noSuchService(name)); 122 123 IEngineService service = contribution.getService(); 124 String serviceName = service.getName(); 125 126 if (!serviceName.equals(name)) 127 throw new ApplicationRuntimeException(ImplMessages.serviceNameMismatch( 128 service, 129 name, 130 serviceName), contribution.getLocation(), null); 131 132 return service; 133 } 134 135 private IEngineService buildProxy(String name) 136 { 137 if (!_services.containsKey(name)) 138 throw new ApplicationRuntimeException(ImplMessages.noSuchService(name)); 139 140 EngineServiceOuterProxy outer = new EngineServiceOuterProxy(name); 141 142 EngineServiceInnerProxy inner = new EngineServiceInnerProxy(name, outer, this); 143 144 outer.installDelegate(inner); 145 146 return outer; 147 } 148 149 public void setApplicationServices(List applicationServices) 150 { 151 _applicationServices = applicationServices; 152 } 153 154 public void setFactoryServices(List factoryServices) 155 { 156 _factoryServices = factoryServices; 157 } 158 159 public void setErrorLog(ErrorLog errorLog) 160 { 161 _errorLog = errorLog; 162 } 163 } | Popular Tags |