1 19 package org.java.plugin.tools.ant; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStreamReader ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 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 53 public abstract class BaseJpfTask extends MatchingTask { 54 private File baseDir; 55 private boolean verbose; 56 private PluginRegistry registry; 57 private PathResolver pathResolver; 58 private Set whiteList; 59 private Set blackList; 60 61 64 public final void setBaseDir(final File aBaseDir) { 65 this.baseDir = aBaseDir; 66 } 67 68 72 public final void setVerbose(final boolean aVerbose) { 73 this.verbose = aVerbose; 74 } 75 76 80 public final void setWhiteList(final File file) throws IOException { 81 whiteList = loadList(file); 82 } 83 84 88 public final void setBlackList(final File file) throws IOException { 89 blackList = loadList(file); 90 } 91 92 protected Set loadList(final File file) throws IOException { 93 if (file == null) { 94 return null; 95 } 96 Set result = new HashSet (); 97 BufferedReader reader = new BufferedReader (new InputStreamReader ( 98 new FileInputStream (file), "UTF-8")); try { 100 String 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 getWhiteList() { 126 return whiteList; 127 } 128 129 protected Set 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!", getLocation()); 137 } 138 if (!baseDir.isDirectory()) { 139 throw new BuildException("basedir " + baseDir + " does not exist!", getLocation()); } 142 ObjectFactory objectFactory = ObjectFactory.newInstance(); 143 log("Collecting manifest files..."); DirectoryScanner ds = super.getDirectoryScanner(baseDir); 145 registry = objectFactory.createRegistry(); 146 String [] manifestFiles = ds.getIncludedFiles(); 147 List manifestUrls = new LinkedList (); 148 Map foldersMap = new HashMap (); 149 for (int i = 0; i < manifestFiles.length; i++) { 150 File manifestFile = new File (baseDir, manifestFiles[i]); 151 try { 152 URL manifestUrl = getManifestURL(manifestFile); 154 try { 155 if (!isManifestAccepted(manifestUrl)) { 156 if (verbose) { 157 log("Skipped URL: " + manifestUrl); } 159 continue; 160 } 161 } catch (ManifestProcessingException mpe) { 162 throw new BuildException("can't read manifest from URL " + manifestUrl, mpe, getLocation()); 164 } 165 manifestUrls.add(manifestUrl); 166 if (verbose) { 167 log("Added URL: " + manifestUrl); } 169 if (usePathResolver) { 170 172 if ("jar".equals(manifestUrl.getProtocol())) { foldersMap.put(manifestUrl, 174 IoUtil.file2url(manifestFile)); 175 } else { 176 foldersMap.put(manifestUrl, 177 IoUtil.file2url(manifestFile.getParentFile())); 178 } 179 } 180 } catch (MalformedURLException mue) { 181 throw new BuildException("can't create URL for file " + manifestFile, mue, getLocation()); 183 } 184 } 185 Map processedPlugins; 186 try { 187 processedPlugins = registry.register( 188 (URL []) manifestUrls.toArray(new URL [manifestUrls.size()])); 189 } catch (Exception e) { 190 throw new BuildException("can't register URLs", e, getLocation()); } 192 log("... successfully registered " + processedPlugins.size() + " (of " + manifestUrls.size() + ") manifest files ", (processedPlugins.size() != manifestUrls.size()) 195 ? Project.MSG_WARN : Project.MSG_INFO); 196 if (usePathResolver) { 197 pathResolver = objectFactory.createPathResolver(); 198 for (Iterator it = processedPlugins.entrySet().iterator(); 199 it.hasNext();) { 200 Map.Entry entry = (Entry) it.next(); 201 pathResolver.registerContext((Identity) entry.getValue(), 202 (URL ) foldersMap.get(entry.getKey())); 203 } 204 log("PathResolver initialized"); } 206 } 207 208 protected URL getManifestURL(final File file) throws MalformedURLException { 209 if(file.getName().endsWith(".jar") || file.getName().endsWith(".zip")) { URL url = new URL ("jar:" + IoUtil.file2url(file).toExternalForm() + "!/plugin.xml"); if (IoUtil.isResourceExists(url)) { 213 return url; 214 } 215 url = new URL ("jar:" + IoUtil.file2url(file).toExternalForm() + "!/plugin-fragment.xml"); if (IoUtil.isResourceExists(url)) { 218 return url; 219 } 220 url = new URL ("jar:" + IoUtil.file2url(file).toExternalForm() + "!/META-INF/plugin.xml"); if (IoUtil.isResourceExists(url)) { 223 return url; 224 } 225 url = new URL ("jar:" + IoUtil.file2url(file).toExternalForm() + "!/META-INF/plugin-fragment.xml"); 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 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 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 |