1 15 package org.apache.hivemind.impl; 16 17 import java.io.IOException ; 18 import java.lang.reflect.InvocationTargetException ; 19 import java.net.URL ; 20 import java.util.ArrayList ; 21 import java.util.Enumeration ; 22 import java.util.List ; 23 import java.util.StringTokenizer ; 24 import java.util.jar.Attributes ; 25 import java.util.jar.Manifest ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.hivemind.ApplicationRuntimeException; 30 import org.apache.hivemind.ClassResolver; 31 import org.apache.hivemind.util.URLResource; 32 33 43 public class RegistryProviderAutoDetector 44 { 45 private static final Log LOG = LogFactory.getLog(RegistryProviderAutoDetector.class); 46 public static final String MANIFEST = "META-INF/MANIFEST.MF"; 47 public static final String HIVEMIND_SECTION_NAME = "hivemind"; 48 public static final String PROVIDER_ATTRIBUTE_NAME = "hivemind-providers"; 49 50 private List _providers = new ArrayList (); 51 52 public RegistryProviderAutoDetector(ClassResolver resolver) 53 { 54 processManifestFiles(resolver); 55 } 56 57 public List getProviders() 58 { 59 return _providers; 60 } 61 62 66 private void processManifestFiles(ClassResolver resolver) 67 { 68 if (LOG.isDebugEnabled()) 69 LOG.debug("Processing manifest files visible to " + resolver); 70 71 ClassLoader loader = resolver.getClassLoader(); 72 Enumeration e = null; 73 74 try 75 { 76 e = loader.getResources(MANIFEST); 77 } 78 catch (IOException ex) 79 { 80 throw new ApplicationRuntimeException(ImplMessages.unableToFindProviders(resolver, ex), 81 ex); 82 } 83 84 while (e.hasMoreElements()) 85 { 86 URL descriptorURL = (URL ) e.nextElement(); 87 88 processManifestFile(resolver, new URLResource(descriptorURL)); 89 } 90 91 } 92 93 94 100 private void processManifestFile(ClassResolver resolver, URLResource resource) 101 { 102 URL url = resource.getResourceURL(); 103 Manifest manifest; 104 try 105 { 106 manifest = new Manifest (url.openStream()); 107 } 108 catch (IOException e) 109 { 110 throw new ApplicationRuntimeException(ImplMessages.unableToReadManifest(url, e), 111 e); 112 } 113 Attributes attributes = manifest.getMainAttributes(); 115 if (attributes != null) { 116 String providers = attributes.getValue(PROVIDER_ATTRIBUTE_NAME); 117 if (providers != null) { 118 if (LOG.isDebugEnabled()) { 119 LOG.debug("Found providers '" + providers + "' defined in manifest file '" + url.toString() + "'"); 120 } 121 handleProviderAttribute(resolver, providers); 122 } 123 } 124 } 125 126 129 private void handleProviderAttribute(ClassResolver resolver, String providers) 130 { 131 StringTokenizer tokenizer = new StringTokenizer (providers, ","); 132 while (tokenizer.hasMoreTokens()) 133 { 134 String providerClassName = tokenizer.nextToken(); 135 loadProvider(resolver, providerClassName); 136 } 137 } 138 139 145 private void loadProvider(ClassResolver resolver, String providerClassName) 146 { 147 if (LOG.isDebugEnabled()) 148 LOG.debug("Loading provider " + providerClassName); 149 Object provider = null; 150 try 151 { 152 Class providerClass = resolver.findClass(providerClassName); 153 provider = providerClass.newInstance(); 154 } 155 catch (Exception e) 156 { 157 Exception cause = e; 158 if (e instanceof InvocationTargetException ) 159 { 160 cause = (InvocationTargetException ) e; 161 } 162 throw new ApplicationRuntimeException(ImplMessages.unableToCreateProvider(providerClassName, e), 163 cause); 164 } 165 if (!(provider instanceof RegistryProvider)) { 167 throw new ApplicationRuntimeException(ImplMessages.providerWrongType(providerClassName, RegistryProvider.class)); 168 } 169 170 _providers.add(provider); 171 } 172 173 } 174 | Popular Tags |