1 package net.sf.crispy.server; 2 3 import java.util.Hashtable; 4 import java.util.Map; 5 6 import net.sf.crispy.InvocationException; 7 8 /** 9 * 10 * Container for the service implementation. This is a mapping from service-interface plus 11 * a key (e.g. user key or sessionId) to the service-implementation. 12 * The service instance exist multiple (per key). They service implementation must not be Thread safe. 13 * 14 * @author Linke 15 * 16 * @since 1.1.0 17 */ 18 public class MultiServiceContainerImpl { 19 20 private Map services = new Hashtable(); 21 private Map registerServices = new Hashtable(); 22 private ServiceCreator serviceCreator = null; 23 24 25 public void setServiceCreator(ServiceCreator pvServiceCreator) { 26 serviceCreator = pvServiceCreator; 27 } 28 public ServiceCreator getServiceCreator() { 29 if (serviceCreator == null) { 30 serviceCreator = new ServiceCreatorDefaultImpl(); 31 } 32 return serviceCreator; 33 } 34 35 public void registerService(String pvServiceInterfaceName, String pvServiceImplName) { 36 registerServices.put(pvServiceInterfaceName, pvServiceImplName); 37 } 38 39 public String getRegisterServiceByServiceInterfaceName(String pvServiceInterfaceName) { 40 return (String) registerServices.get(pvServiceInterfaceName); 41 } 42 43 public String removeRegisterServiceByServiceInterfaceName(String pvServiceInterfaceName) { 44 return (String) registerServices.remove(pvServiceInterfaceName); 45 } 46 47 public int getRegisterServiceSize() { 48 return registerServices.size(); 49 } 50 51 /** 52 * Find the service impl. 53 * 54 * @param pvKey Is the key in a map. 55 * @param pvServiceInterfaceName Is the key for the service impl in a map. 56 * @return The service impl or <code>null</code>. 57 */ 58 public Object getService(Object pvKey, String pvServiceInterfaceName) throws Exception { 59 Map lvServiceMap = (Map) services.get(pvKey); 60 if (lvServiceMap != null) { 61 Object lvObject = lvServiceMap.get(pvServiceInterfaceName); 62 if (lvObject != null) { 63 return lvObject; 64 } else { 65 return getServiceImplAndRegisterThisServiceImpl(pvServiceInterfaceName, lvServiceMap); 66 } 67 } else { 68 lvServiceMap = new Hashtable(); 69 Object lvServiceImpl = getServiceImplAndRegisterThisServiceImpl(pvServiceInterfaceName, lvServiceMap); 70 services.put(pvKey, lvServiceMap); 71 return lvServiceImpl; 72 } 73 } 74 75 private Object getServiceImplAndRegisterThisServiceImpl(final String pvServiceInterfaceName, final Map pvServiceMap) throws Exception { 76 String lvServiceImplName = getRegisterServiceByServiceInterfaceName(pvServiceInterfaceName); 77 if (lvServiceImplName == null) { 78 throw new InvocationException("With the name: " + pvServiceInterfaceName + " is no service register."); 79 } else { 80 Object lvServiceImpl = getServiceCreator().createService(pvServiceInterfaceName, lvServiceImplName); 81 pvServiceMap.put(pvServiceInterfaceName, lvServiceImpl); 82 return lvServiceImpl; 83 } 84 } 85 86 87 /** 88 * Remove the service from the container. 89 * 90 * @param pvKey Is the key in a map. 91 * @param pvServiceInterfaceName Is the key for the service impl in a map. 92 */ 93 public void removeService(Object pvKey, String pvServiceInterfaceName) { 94 Map lvServiceMap = (Map) services.get(pvKey); 95 if (lvServiceMap != null) { 96 lvServiceMap.remove(pvServiceInterfaceName); 97 if (lvServiceMap.size() == 0) { 98 services.remove(pvKey); 99 } 100 } 101 } 102 103 /** 104 * Remove all service by a key. 105 * 106 * @param pvKey The key to removed services. 107 */ 108 public void removeAllServices(Object pvKey) { 109 services.remove(pvKey); 110 } 111 112 /** 113 * Get the size of services for a key. 114 * @param pvKey search key 115 * @return size of services for a key 116 */ 117 public int getServiceSize(Object pvKey) { 118 Map lvServiceMap = (Map) services.get(pvKey); 119 if (lvServiceMap != null) { 120 return lvServiceMap.size(); 121 } else { 122 return 0; 123 } 124 } 125 126 /** 127 * Size of services in the container. 128 * @return Size of services. 129 */ 130 public int getKeySize() { 131 return services.size(); 132 } 133 134 135 136 }