1 11 12 package org.eclipse.jdt.apt.core.internal; 13 14 import java.io.BufferedReader ; 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.io.InputStreamReader ; 19 import java.util.LinkedHashMap ; 20 import java.util.Map ; 21 import java.util.jar.JarEntry ; 22 import java.util.jar.JarFile ; 23 24 import org.eclipse.jdt.apt.core.internal.util.FactoryContainer; 25 26 31 public abstract class JarFactoryContainer extends FactoryContainer 32 { 33 34 37 public abstract File getJarFile(); 38 39 @Override 40 public boolean exists() { 41 try { 42 final File jarFile = getJarFile(); 43 if(jarFile == null) 44 return false; 45 return getJarFile().exists(); 46 } catch (SecurityException e) { 47 return false; 48 } 49 } 50 51 @Override 52 protected Map <String , String > loadFactoryNames() throws IOException { 53 return getServiceClassnamesFromJar( getJarFile() ); 54 } 55 56 70 protected static Map <String , String > getServiceClassnamesFromJar(File jar) throws IOException 71 { 72 Map <String , String > classNames = new LinkedHashMap <String , String >(); 73 JarFile jarFile = null; 74 try { 75 jarFile = new JarFile (jar); 76 77 for (String serviceName : AUTOLOAD_SERVICES) { 78 String providerName = "META-INF/services/" + serviceName; JarEntry provider = jarFile.getJarEntry(providerName); 81 if (provider == null) { 82 continue; 83 } 84 InputStream is = jarFile.getInputStream(provider); 86 readServiceProvider(is, serviceName, classNames); 87 } 88 } 89 finally { 90 try {if (jarFile != null) jarFile.close();} catch (IOException ioe) {} 91 } 92 return classNames; 93 } 94 95 101 protected static void readServiceProvider(InputStream is, String serviceName, Map <String , String > classNames) throws IOException { 102 BufferedReader rd = null; 103 try { 104 rd = new BufferedReader (new InputStreamReader (is, "UTF-8")); for (String line = rd.readLine(); line != null; line = rd.readLine()) { 106 int iComment = line.indexOf('#'); 108 if (iComment >= 0) { 109 line = line.substring(0, iComment); 110 } 111 final String [] tokens = line.split("\\s", 2); if (tokens[0].length() > 0) { 114 classNames.put(tokens[0], serviceName); 115 } 116 } 117 rd.close(); 118 } 119 finally { 120 if (rd != null) try {rd.close();} catch (IOException ioe) {} 121 } 122 } 123 124 125 private static final String [] AUTOLOAD_SERVICES = { 126 AptPlugin.JAVA5_FACTORY_NAME, 127 AptPlugin.JAVA6_FACTORY_NAME 128 }; 129 130 } 131 132 | Popular Tags |