KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > tools > ant > BaseJpfTask


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2006 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.tools.ant;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.Map.Entry;
36
37 import org.apache.tools.ant.BuildException;
38 import org.apache.tools.ant.DirectoryScanner;
39 import org.apache.tools.ant.Project;
40 import org.apache.tools.ant.taskdefs.MatchingTask;
41 import org.java.plugin.ObjectFactory;
42 import org.java.plugin.PathResolver;
43 import org.java.plugin.registry.Identity;
44 import org.java.plugin.registry.ManifestProcessingException;
45 import org.java.plugin.registry.PluginRegistry;
46 import org.java.plugin.registry.PluginRegistry.ManifestInfo;
47 import org.java.plugin.util.IoUtil;
48
49 /**
50  * Base class for some JPF related ant tasks.
51  * @version $Id: BaseJpfTask.java,v 1.11 2006/10/10 17:55:50 ddimon Exp $
52  */

53 public abstract class BaseJpfTask extends MatchingTask {
54     private File JavaDoc baseDir;
55     private boolean verbose;
56     private PluginRegistry registry;
57     private PathResolver pathResolver;
58     private Set JavaDoc whiteList;
59     private Set JavaDoc blackList;
60
61     /**
62      * @param aBaseDir base directory for manifest files
63      */

64     public final void setBaseDir(final File JavaDoc aBaseDir) {
65         this.baseDir = aBaseDir;
66     }
67
68     /**
69      * @param aVerbose <code>true</code> if detailed integrity check report
70      * required
71      */

72     public final void setVerbose(final boolean aVerbose) {
73         this.verbose = aVerbose;
74     }
75     
76     /**
77      * @param file while list file
78      * @throws IOException if list reading failed
79      */

80     public final void setWhiteList(final File JavaDoc file) throws IOException JavaDoc {
81         whiteList = loadList(file);
82     }
83     
84     /**
85      * @param file black list file
86      * @throws IOException if list reading failed
87      */

88     public final void setBlackList(final File JavaDoc file) throws IOException JavaDoc {
89         blackList = loadList(file);
90     }
91     
92     protected Set JavaDoc loadList(final File JavaDoc file) throws IOException JavaDoc {
93         if (file == null) {
94             return null;
95         }
96         Set JavaDoc result = new HashSet JavaDoc();
97         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
98                 new FileInputStream JavaDoc(file), "UTF-8")); //$NON-NLS-1$
99
try {
100             String JavaDoc line;
101             while ((line = reader.readLine()) != null) {
102                 line = line.trim();
103                 if (line.length() > 0) {
104                     result.add(line);
105                 }
106             }
107         } finally {
108             reader.close();
109         }
110         return result;
111     }
112     
113     protected final boolean getVerbose() {
114         return verbose;
115     }
116
117     protected final PathResolver getPathResolver() {
118         return pathResolver;
119     }
120     
121     protected final PluginRegistry getRegistry() {
122         return registry;
123     }
124     
125     protected Set JavaDoc getWhiteList() {
126         return whiteList;
127     }
128     
129     protected Set JavaDoc getBlackList() {
130         return blackList;
131     }
132
133     protected final void initRegistry(final boolean usePathResolver) {
134         if (baseDir == null) {
135             throw new BuildException("basedir attribute must be set!", //$NON-NLS-1$
136
getLocation());
137         }
138         if (!baseDir.isDirectory()) {
139             throw new BuildException("basedir " + baseDir //$NON-NLS-1$
140
+ " does not exist!", getLocation()); //$NON-NLS-1$
141
}
142         ObjectFactory objectFactory = ObjectFactory.newInstance();
143         log("Collecting manifest files..."); //$NON-NLS-1$
144
DirectoryScanner ds = super.getDirectoryScanner(baseDir);
145         registry = objectFactory.createRegistry();
146         String JavaDoc[] manifestFiles = ds.getIncludedFiles();
147         List JavaDoc manifestUrls = new LinkedList JavaDoc();
148         Map JavaDoc foldersMap = new HashMap JavaDoc();
149         for (int i = 0; i < manifestFiles.length; i++) {
150             File JavaDoc manifestFile = new File JavaDoc(baseDir, manifestFiles[i]);
151             try {
152                 //manifestUrls[i] = IoUtil.file2url(manifestFile);
153
URL JavaDoc manifestUrl = getManifestURL(manifestFile);
154                 try {
155                     if (!isManifestAccepted(manifestUrl)) {
156                         if (verbose) {
157                             log("Skipped URL: " + manifestUrl); //$NON-NLS-1$
158
}
159                         continue;
160                     }
161                 } catch (ManifestProcessingException mpe) {
162                     throw new BuildException("can't read manifest from URL " //$NON-NLS-1$
163
+ manifestUrl, mpe, getLocation());
164                 }
165                 manifestUrls.add(manifestUrl);
166                 if (verbose) {
167                     log("Added URL: " + manifestUrl); //$NON-NLS-1$
168
}
169                 if (usePathResolver) {
170                     /*foldersMap.put(manifestUrls[i],
171                             IoUtil.file2url(manifestFile.getParentFile()));*/

172                     if ("jar".equals(manifestUrl.getProtocol())) { //$NON-NLS-1$
173
foldersMap.put(manifestUrl,
174                             IoUtil.file2url(manifestFile));
175                     } else {
176                         foldersMap.put(manifestUrl,
177                             IoUtil.file2url(manifestFile.getParentFile()));
178                     }
179                 }
180             } catch (MalformedURLException JavaDoc mue) {
181                 throw new BuildException("can't create URL for file " //$NON-NLS-1$
182
+ manifestFile, mue, getLocation());
183             }
184         }
185         Map JavaDoc processedPlugins;
186         try {
187             processedPlugins = registry.register(
188                     (URL JavaDoc[]) manifestUrls.toArray(new URL JavaDoc[manifestUrls.size()]));
189         } catch (Exception JavaDoc e) {
190             throw new BuildException("can't register URLs", e, getLocation()); //$NON-NLS-1$
191
}
192         log("... successfully registered " + processedPlugins.size() //$NON-NLS-1$
193
+ " (of " + manifestUrls.size() + ") manifest files ", //$NON-NLS-1$ //$NON-NLS-2$
194
(processedPlugins.size() != manifestUrls.size())
195                     ? Project.MSG_WARN : Project.MSG_INFO);
196         if (usePathResolver) {
197             pathResolver = objectFactory.createPathResolver();
198             for (Iterator JavaDoc it = processedPlugins.entrySet().iterator();
199                     it.hasNext();) {
200                 Map.Entry JavaDoc entry = (Entry) it.next();
201                 pathResolver.registerContext((Identity) entry.getValue(),
202                         (URL JavaDoc) foldersMap.get(entry.getKey()));
203             }
204             log("PathResolver initialized"); //$NON-NLS-1$
205
}
206     }
207     
208     protected URL JavaDoc getManifestURL(final File JavaDoc file) throws MalformedURLException JavaDoc {
209         if(file.getName().endsWith(".jar") || file.getName().endsWith(".zip")) { //$NON-NLS-1$ //$NON-NLS-2$
210
URL JavaDoc url = new URL JavaDoc("jar:" + IoUtil.file2url(file).toExternalForm() //$NON-NLS-1$
211
+ "!/plugin.xml"); //$NON-NLS-1$
212
if (IoUtil.isResourceExists(url)) {
213                 return url;
214             }
215             url = new URL JavaDoc("jar:" + IoUtil.file2url(file).toExternalForm() //$NON-NLS-1$
216
+ "!/plugin-fragment.xml"); //$NON-NLS-1$
217
if (IoUtil.isResourceExists(url)) {
218                 return url;
219             }
220             url = new URL JavaDoc("jar:" + IoUtil.file2url(file).toExternalForm() //$NON-NLS-1$
221
+ "!/META-INF/plugin.xml"); //$NON-NLS-1$
222
if (IoUtil.isResourceExists(url)) {
223                 return url;
224             }
225             url = new URL JavaDoc("jar:" + IoUtil.file2url(file).toExternalForm() //$NON-NLS-1$
226
+ "!/META-INF/plugin-fragment.xml"); //$NON-NLS-1$
227
if (IoUtil.isResourceExists(url)) {
228                 return url;
229             }
230             return null;
231         }
232         return IoUtil.file2url(file);
233     }
234     
235     protected boolean isManifestAccepted(final URL JavaDoc manifestUrl)
236             throws ManifestProcessingException {
237         if ((whiteList == null) && (blackList == null)) {
238             return true;
239         }
240         ManifestInfo manifestInfo = registry.readManifestInfo(manifestUrl);
241         if (whiteList != null) {
242             if (isPluginInList(manifestInfo, whiteList)) {
243                 return true;
244             }
245         }
246         if ((blackList != null) && isPluginInList(manifestInfo, blackList)) {
247             return false;
248         }
249         return true;
250     }
251     
252     private boolean isPluginInList(final ManifestInfo manifestInfo,
253             final Set JavaDoc list) {
254         if (list.contains(manifestInfo.getId())) {
255             return true;
256         }
257         return list.contains(registry.makeUniqueId(manifestInfo.getId(),
258                 manifestInfo.getVersion()));
259     }
260 }
261
Popular Tags