1 19 20 package org.netbeans.modules.apisupport.project.metainf; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.io.PrintStream ; 28 import java.util.ArrayList ; 29 import java.util.Enumeration ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Set ; 33 import java.util.jar.Attributes ; 34 import java.util.jar.JarEntry ; 35 import java.util.jar.JarFile ; 36 import org.netbeans.api.project.Project; 37 import org.netbeans.api.project.ProjectManager; 38 import org.netbeans.api.queries.VisibilityQuery; 39 import org.netbeans.modules.apisupport.project.NbModuleProject; 40 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider; 41 import org.netbeans.modules.apisupport.project.SuiteProvider; 42 import org.netbeans.modules.apisupport.project.Util; 43 import org.netbeans.modules.apisupport.project.layers.LayerUtils; 44 import org.netbeans.modules.apisupport.project.suite.SuiteProject; 45 import org.openide.ErrorManager; 46 import org.openide.filesystems.FileLock; 47 import org.openide.filesystems.FileObject; 48 import org.openide.filesystems.FileUtil; 49 50 55 final class Service { 56 57 private final String codebase; 58 private final String fileName; 59 private final List classes; 60 61 static final String META_INF_SERVICES = "META-INF/services"; 62 63 64 public Service(String codebase,String fileName,List classes) { 65 this.codebase = codebase; 66 this.fileName = fileName; 67 this.classes = classes; 68 } 69 70 static Service createService(String codebase,String fileName, InputStream jarIs) throws IOException { 71 List list = new ArrayList (); 72 BufferedReader reader = new BufferedReader (new InputStreamReader (jarIs)); 73 String line = null; 74 while ((line = reader.readLine())!= null) { 75 line = line.trim(); 76 if (!line.startsWith("#") && line.length() != 0) { 77 list.add(line); 78 } 79 } 80 return new Service(codebase,fileName,list); 81 } 82 83 static List readServices(File jarFile) { 84 List services = new ArrayList (); 85 try { 86 JarFile jar = new JarFile (jarFile); 87 Attributes attrs = jar.getManifest().getMainAttributes(); 88 String codebase = (String ) attrs.getValue("OpenIDE-Module"); Enumeration entries = jar.entries(); 90 while (entries.hasMoreElements()) { 91 JarEntry entry = (JarEntry ) entries.nextElement(); 92 if (entry.getName().startsWith(META_INF_SERVICES)) { 93 String name = entry.getName().substring(META_INF_SERVICES.length() + 1).trim(); 94 if (!name.equals("")) { InputStream is = jar.getInputStream(entry); 96 try { 97 services.add(createService(codebase,name.intern(),is)); 98 } finally { 99 is.close(); 100 } 101 } 102 } 103 } 104 } catch (IOException ioe) { 105 ErrorManager.getDefault().notify(ErrorManager.ERROR,ioe); 106 } 107 return services; 108 } 109 110 static List getOnlyProjectServices(NbModuleProject project) { 111 List services = new ArrayList (); 112 try { 113 FileObject mIServicesFolder = null; 114 mIServicesFolder = SUtil.getServicesFolder(project,false); 115 if (mIServicesFolder != null) { 117 String codebase = project.getCodeNameBase(); 118 FileObject servicesFOs [] = mIServicesFolder.getChildren(); 119 for (int foIt = 0 ; foIt < servicesFOs.length ; foIt++ ) { 120 if (servicesFOs[foIt].isData() && VisibilityQuery.getDefault().isVisible(servicesFOs[foIt])) { 121 InputStream is = servicesFOs[foIt].getInputStream(); 122 try { 123 services.add(createService(codebase,servicesFOs[foIt].getNameExt(),is)); 124 } finally { 125 is.close(); 126 } 127 } 128 } 129 } 130 } catch (IOException ioe) { 131 ErrorManager.getDefault().notify(ErrorManager.ERROR,ioe); 132 } 133 return services; 134 } 135 136 public String getCodebase() { 137 return codebase; 138 } 139 140 public String getFileName() { 141 return fileName; 142 } 143 144 public List getClasses() { 145 return classes; 146 } 147 148 public boolean containsClass(String name) { 149 return classes.indexOf(name) != -1; 150 } 151 152 public void removeClass(String name) { 153 classes.remove(name); 154 } 155 156 private static Set <File > getJars(NbModuleProject p) throws IOException { 157 if (p == null) { 158 return SUtil.getPlatformJars(); 160 } else { 161 NbModuleProvider.NbModuleType type = ((NbModuleProvider) p.getLookup().lookup(NbModuleProvider.class)).getModuleType(); 162 if (type == NbModuleProvider.STANDALONE) { 163 return LayerUtils.getPlatformJarsForStandaloneProject(p); 164 } else if (type == NbModuleProvider.SUITE_COMPONENT) { 165 SuiteProvider suiteProv = (SuiteProvider) p.getLookup().lookup(SuiteProvider.class); 166 assert suiteProv != null : p; 167 File suiteDir = suiteProv.getSuiteDirectory(); 168 if (suiteDir == null || !suiteDir.isDirectory()) { 169 throw new IOException ("Could not locate suite for " + p); } 171 Project suite = ProjectManager.getDefault().findProject(FileUtil.toFileObject(suiteDir)); 172 if (!(suite instanceof SuiteProject)) { 173 throw new IOException ("Could not load suite for " + p + " from " + suiteDir); } 175 return LayerUtils.getPlatformJarsForSuiteComponentProject(p,(SuiteProject)suite); 176 } else if (type == NbModuleProvider.NETBEANS_ORG) { 177 return LayerUtils.getPlatformJarsForStandaloneProject(p); 180 } else { 181 throw new AssertionError (type); 182 } 183 } 184 } 185 186 static List <Service> getPlatfromServices(NbModuleProject p) throws IOException { 187 NbModuleProvider.NbModuleType type = Util.getModuleType(p); 188 List <Service> services = new ArrayList <Service>(); 189 if (type == NbModuleProvider.NETBEANS_ORG) { 190 Set <NbModuleProject> projects = LayerUtils.getProjectsForNetBeansOrgProject(p); 192 Iterator it = projects.iterator(); 193 while (it.hasNext()) { 194 services.addAll(getOnlyProjectServices((NbModuleProject)it.next())); 195 } 196 } else { 197 Set <File > jars = getJars(p); 198 for (File jarFile : jars) { 199 services.addAll(readServices(jarFile)); 200 } 201 } 202 return services; 203 } 204 205 void removeClass(String className,NbModuleProject project) { 206 String removedClass = "-" + className; 207 removedClass = removedClass.intern(); 208 if (containsClass(className)) { 209 removeClass(className); 210 } else if (containsClass(removedClass)) { 211 removeClass(removedClass); 212 } else { 213 classes.add(removedClass); 214 } 215 write(project); 216 } 217 218 void write(NbModuleProject project) { 219 try { 220 FileObject mIServicesFolder = null; 221 mIServicesFolder = SUtil.getServicesFolder(project,true); 222 FileObject serviceFo = mIServicesFolder.getFileObject(getFileName()); 223 if (classes.size() > 0) { 224 if (serviceFo == null) { 225 serviceFo = mIServicesFolder.createData(getFileName()); 226 } 227 FileLock lock = serviceFo.lock(); 228 try { 229 PrintStream ps = new PrintStream (serviceFo.getOutputStream(lock)); 230 for (Iterator it = classes.iterator() ; it.hasNext() ; ) { 231 Object object = it.next(); 232 ps.println(object); 233 } 234 ps.close(); 235 } finally { 236 lock.releaseLock(); 237 } 238 } else { 239 serviceFo.delete(); 241 } 242 } catch (IOException ioe) { 243 ErrorManager.getDefault().notify(ioe); 244 } 245 } 246 247 } 248 | Popular Tags |