1 15 package org.apache.hivemind.impl; 16 17 import java.util.HashMap ; 18 import java.util.Locale ; 19 import java.util.Map ; 20 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.hivemind.ClassResolver; 23 import org.apache.hivemind.ErrorHandler; 24 import org.apache.hivemind.HiveMind; 25 import org.apache.hivemind.Messages; 26 import org.apache.hivemind.internal.MessageFinder; 27 import org.apache.hivemind.internal.Module; 28 import org.apache.hivemind.internal.RegistryInfrastructure; 29 import org.apache.hivemind.internal.ServiceModelFactory; 30 import org.apache.hivemind.internal.ServicePoint; 31 import org.apache.hivemind.service.ThreadLocale; 32 import org.apache.hivemind.util.IdUtils; 33 import org.apache.hivemind.util.ToStringBuilder; 34 35 40 public final class ModuleImpl extends BaseLocatable implements Module 41 { 42 private String _moduleId; 43 44 45 private String _packageName; 46 47 private RegistryInfrastructure _registry; 48 49 private ClassResolver _resolver; 50 51 private Messages _messages; 52 53 58 private final Map _typeCache = new HashMap (); 59 60 public Object getConfiguration(String extensionPointId) 61 { 62 String qualifiedId = IdUtils.qualify(_moduleId, extensionPointId); 63 64 return _registry.getConfiguration(qualifiedId, this); 65 } 66 67 public String getModuleId() 68 { 69 return _moduleId; 70 } 71 72 73 74 public void setPackageName(String packageName) 75 { 76 _packageName = packageName; 77 } 78 79 public boolean containsService(Class serviceInterface) 80 { 81 return _registry.containsService(serviceInterface, this); 82 } 83 84 public Object getService(String serviceId, Class serviceInterface) 85 { 86 String qualifiedId = IdUtils.qualify(_moduleId, serviceId); 87 88 return _registry.getService(qualifiedId, serviceInterface, this); 89 } 90 91 public Object getService(Class serviceInterface) 92 { 93 return _registry.getService(serviceInterface, this); 94 } 95 96 public void setModuleId(String string) 97 { 98 _moduleId = string; 99 } 100 101 public void setRegistry(RegistryInfrastructure registry) 102 { 103 _registry = registry; 104 } 105 106 public void setClassResolver(ClassResolver resolver) 107 { 108 _resolver = resolver; 109 } 110 111 public ClassResolver getClassResolver() 112 { 113 return _resolver; 114 } 115 116 public synchronized Messages getMessages() 117 { 118 if (_messages == null) 119 { 120 ThreadLocale threadLocale = (ThreadLocale) _registry.getService( 121 HiveMind.THREAD_LOCALE_SERVICE, 122 ThreadLocale.class, 123 this); 124 125 MessageFinder finder = new MessageFinderImpl(getLocation().getResource()); 126 127 _messages = new ModuleMessages(finder, threadLocale); 128 } 129 130 return _messages; 131 } 132 133 public String toString() 134 { 135 ToStringBuilder builder = new ToStringBuilder(this); 136 137 builder.append("moduleId", _moduleId); 138 builder.append("classResolver", _resolver); 139 140 return builder.toString(); 141 } 142 143 public ServicePoint getServicePoint(String serviceId) 144 { 145 String qualifiedId = IdUtils.qualify(_moduleId, serviceId); 146 147 return _registry.getServicePoint(qualifiedId, this); 148 } 149 150 public ServiceModelFactory getServiceModelFactory(String name) 151 { 152 return _registry.getServiceModelFactory(name); 153 } 154 155 public Locale getLocale() 156 { 157 return _registry.getLocale(); 158 } 159 160 public ErrorHandler getErrorHandler() 161 { 162 return _registry.getErrorHander(); 163 } 164 165 public synchronized Class resolveType(String type) 166 { 167 Class result = (Class ) _typeCache.get(type); 168 169 if (result == null) 170 { 171 result = findTypeInClassResolver(type); 172 173 _typeCache.put(type, result); 174 } 175 176 return result; 177 } 178 179 private Class findTypeInClassResolver(String type) 180 { 181 Class result = _resolver.checkForClass(type); 182 183 if (result == null) 184 result = _resolver.checkForClass(_packageName + "." + type); 185 186 if (result == null) 187 throw new ApplicationRuntimeException(ImplMessages.unableToConvertType( 188 type, 189 _packageName)); 190 191 return result; 192 } 193 194 197 public RegistryInfrastructure getRegistry() 198 { 199 return _registry; 200 } 201 202 } | Popular Tags |