KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > metainf > Service


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.metainf;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.PrintStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.jar.Attributes JavaDoc;
34 import java.util.jar.JarEntry JavaDoc;
35 import java.util.jar.JarFile JavaDoc;
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 /**
51  * Representation of a record in META-INF/services.
52  *
53  * @author pzajac
54  */

55 final class Service {
56     
57     private final String JavaDoc codebase;
58     private final String JavaDoc fileName;
59     private final List JavaDoc classes;
60     
61     static final String JavaDoc META_INF_SERVICES = "META-INF/services";
62     
63     /** Creates a new instance of Service */
64     public Service(String JavaDoc codebase,String JavaDoc fileName,List JavaDoc classes) {
65         this.codebase = codebase;
66         this.fileName = fileName;
67         this.classes = classes;
68     }
69     
70     static Service createService(String JavaDoc codebase,String JavaDoc fileName, InputStream JavaDoc jarIs) throws IOException JavaDoc {
71         List JavaDoc list = new ArrayList JavaDoc();
72         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(jarIs));
73         String JavaDoc 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 JavaDoc /*Service*/ readServices(File JavaDoc jarFile) {
84         List JavaDoc /*Service */ services = new ArrayList JavaDoc();
85         try {
86             JarFile JavaDoc jar = new JarFile JavaDoc(jarFile);
87             Attributes JavaDoc attrs = jar.getManifest().getMainAttributes();
88             String JavaDoc codebase = (String JavaDoc) attrs.getValue("OpenIDE-Module"); // NOI18N
89
Enumeration JavaDoc /*JarEntry*/entries = jar.entries();
90             while (entries.hasMoreElements()) {
91                 JarEntry JavaDoc entry = (JarEntry JavaDoc) entries.nextElement();
92                 if (entry.getName().startsWith(META_INF_SERVICES)) {
93                     String JavaDoc name = entry.getName().substring(META_INF_SERVICES.length() + 1).trim();
94                     if (!name.equals("")) { // NOI18N
95
InputStream JavaDoc 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 JavaDoc ioe) {
105             ErrorManager.getDefault().notify(ErrorManager.ERROR,ioe);
106         }
107         return services;
108     }
109     
110     static List JavaDoc /*Service*/ getOnlyProjectServices(NbModuleProject project) {
111         List JavaDoc /*Service */ services = new ArrayList JavaDoc();
112         try {
113             FileObject mIServicesFolder = null;
114             mIServicesFolder = SUtil.getServicesFolder(project,false);
115             // get META-INF.services folder
116
if (mIServicesFolder != null) {
117                 String JavaDoc 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 JavaDoc 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 JavaDoc ioe) {
131             ErrorManager.getDefault().notify(ErrorManager.ERROR,ioe);
132         }
133         return services;
134     }
135     
136     public String JavaDoc getCodebase() {
137         return codebase;
138     }
139
140     public String JavaDoc getFileName() {
141         return fileName;
142     }
143
144     public List JavaDoc getClasses() {
145         return classes;
146     }
147
148     public boolean containsClass(String JavaDoc name) {
149         return classes.indexOf(name) != -1;
150     }
151     
152     public void removeClass(String JavaDoc name) {
153         classes.remove(name);
154     }
155     
156     private static Set JavaDoc<File JavaDoc> getJars(NbModuleProject p) throws IOException JavaDoc {
157         if (p == null) {
158             // testing
159
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 JavaDoc suiteDir = suiteProv.getSuiteDirectory();
168                 if (suiteDir == null || !suiteDir.isDirectory()) {
169                     throw new IOException JavaDoc("Could not locate suite for " + p); // NOI18N
170
}
171                 Project suite = ProjectManager.getDefault().findProject(FileUtil.toFileObject(suiteDir));
172                 if (!(suite instanceof SuiteProject)) {
173                     throw new IOException JavaDoc("Could not load suite for " + p + " from " + suiteDir); // NOI18N
174
}
175                 return LayerUtils.getPlatformJarsForSuiteComponentProject(p,(SuiteProject)suite);
176             } else if (type == NbModuleProvider.NETBEANS_ORG) {
177                 /// Is it really correct?
178
// [TODO]
179
return LayerUtils.getPlatformJarsForStandaloneProject(p);
180             } else {
181                 throw new AssertionError JavaDoc(type);
182             }
183         }
184     }
185     
186     static List JavaDoc <Service> getPlatfromServices(NbModuleProject p) throws IOException JavaDoc {
187         NbModuleProvider.NbModuleType type = Util.getModuleType(p);
188         List JavaDoc<Service> services = new ArrayList JavaDoc<Service>();
189         if (type == NbModuleProvider.NETBEANS_ORG) {
190             // special case fro nborg modules
191
Set JavaDoc<NbModuleProject> projects = LayerUtils.getProjectsForNetBeansOrgProject(p);
192             Iterator JavaDoc it = projects.iterator();
193             while (it.hasNext()) {
194                 services.addAll(getOnlyProjectServices((NbModuleProject)it.next()));
195             }
196         } else {
197             Set JavaDoc<File JavaDoc> jars = getJars(p);
198             for (File JavaDoc jarFile : jars) {
199                 services.addAll(readServices(jarFile));
200             }
201         }
202         return services;
203     }
204
205     void removeClass(String JavaDoc className,NbModuleProject project) {
206         String JavaDoc 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 JavaDoc ps = new PrintStream JavaDoc(serviceFo.getOutputStream(lock));
230                     for (Iterator JavaDoc it = classes.iterator() ; it.hasNext() ; ) {
231                         Object JavaDoc object = it.next();
232                         ps.println(object);
233                     }
234                     ps.close();
235                 } finally {
236                     lock.releaseLock();
237                 }
238             } else {
239                 // no service, remove file
240
serviceFo.delete();
241             }
242         } catch (IOException JavaDoc ioe) {
243             ErrorManager.getDefault().notify(ioe);
244         }
245     }
246     
247 }
248
Popular Tags