001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.hivemind.impl; 016 017 import java.util.HashMap; 018 import java.util.Locale; 019 import java.util.Map; 020 021 import org.apache.hivemind.ApplicationRuntimeException; 022 import org.apache.hivemind.ClassResolver; 023 import org.apache.hivemind.ErrorHandler; 024 import org.apache.hivemind.HiveMind; 025 import org.apache.hivemind.Messages; 026 import org.apache.hivemind.internal.MessageFinder; 027 import org.apache.hivemind.internal.Module; 028 import org.apache.hivemind.internal.RegistryInfrastructure; 029 import org.apache.hivemind.internal.ServiceModelFactory; 030 import org.apache.hivemind.internal.ServicePoint; 031 import org.apache.hivemind.service.ThreadLocale; 032 import org.apache.hivemind.util.IdUtils; 033 import org.apache.hivemind.util.ToStringBuilder; 034 035 /** 036 * Implementation of {@link org.apache.hivemind.internal.Module}. 037 * 038 * @author Howard Lewis Ship 039 */ 040 public final class ModuleImpl extends BaseLocatable implements Module 041 { 042 private String _moduleId; 043 044 /** @since 1.1 */ 045 private String _packageName; 046 047 private RegistryInfrastructure _registry; 048 049 private ClassResolver _resolver; 050 051 private Messages _messages; 052 053 /** 054 * Map from (partial) class name to Class. Related to performance bug HIVEMIND-162. 055 * 056 * @since 1.1.1 057 */ 058 private final Map _typeCache = new HashMap(); 059 060 public Object getConfiguration(String extensionPointId) 061 { 062 String qualifiedId = IdUtils.qualify(_moduleId, extensionPointId); 063 064 return _registry.getConfiguration(qualifiedId, this); 065 } 066 067 public String getModuleId() 068 { 069 return _moduleId; 070 } 071 072 /** @since 1.1 */ 073 074 public void setPackageName(String packageName) 075 { 076 _packageName = packageName; 077 } 078 079 public boolean containsService(Class serviceInterface) 080 { 081 return _registry.containsService(serviceInterface, this); 082 } 083 084 public Object getService(String serviceId, Class serviceInterface) 085 { 086 String qualifiedId = IdUtils.qualify(_moduleId, serviceId); 087 088 return _registry.getService(qualifiedId, serviceInterface, this); 089 } 090 091 public Object getService(Class serviceInterface) 092 { 093 return _registry.getService(serviceInterface, this); 094 } 095 096 public void setModuleId(String string) 097 { 098 _moduleId = string; 099 } 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 /** 195 * @see org.apache.hivemind.internal.Module#getRegistry() 196 */ 197 public RegistryInfrastructure getRegistry() 198 { 199 return _registry; 200 } 201 202 }