⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basejpftask.java

📁 java插件系统源码
💻 JAVA
字号:
/*****************************************************************************
 * Java Plug-in Framework (JPF)
 * Copyright (C) 2004-2006 Dmitry Olshansky
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *****************************************************************************/
package org.java.plugin.tools.ant;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.java.plugin.ObjectFactory;
import org.java.plugin.PathResolver;
import org.java.plugin.registry.Identity;
import org.java.plugin.registry.ManifestProcessingException;
import org.java.plugin.registry.PluginRegistry;
import org.java.plugin.registry.PluginRegistry.ManifestInfo;
import org.java.plugin.util.IoUtil;

/**
 * Base class for some JPF related ant tasks.
 * @version $Id: BaseJpfTask.java,v 1.11 2006/10/10 17:55:50 ddimon Exp $
 */
public abstract class BaseJpfTask extends MatchingTask {
    private File baseDir;
    private boolean verbose;
    private PluginRegistry registry;
    private PathResolver pathResolver;
    private Set whiteList;
    private Set blackList;

    /**
     * @param aBaseDir base directory for manifest files
     */
    public final void setBaseDir(final File aBaseDir) {
        this.baseDir = aBaseDir;
    }

    /**
     * @param aVerbose <code>true</code> if detailed integrity check report
     *                required
     */
    public final void setVerbose(final boolean aVerbose) {
        this.verbose = aVerbose;
    }
    
    /**
     * @param file while list file
     * @throws IOException if list reading failed
     */
    public final void setWhiteList(final File file) throws IOException {
        whiteList = loadList(file);
    }
    
    /**
     * @param file black list file
     * @throws IOException if list reading failed
     */
    public final void setBlackList(final File file) throws IOException {
        blackList = loadList(file);
    }
    
    protected Set loadList(final File file) throws IOException {
        if (file == null) {
            return null;
        }
        Set result = new HashSet();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(file), "UTF-8")); //$NON-NLS-1$
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.length() > 0) {
                    result.add(line);
                }
            }
        } finally {
            reader.close();
        }
        return result;
    }
    
    protected final boolean getVerbose() {
        return verbose;
    }

    protected final PathResolver getPathResolver() {
        return pathResolver;
    }
    
    protected final PluginRegistry getRegistry() {
        return registry;
    }
    
    protected Set getWhiteList() {
        return whiteList;
    }
    
    protected Set getBlackList() {
        return blackList;
    }

    protected final void initRegistry(final boolean usePathResolver) {
        if (baseDir == null) {
            throw new BuildException("basedir attribute must be set!", //$NON-NLS-1$
                    getLocation());
        }
        if (!baseDir.isDirectory()) {
            throw new BuildException("basedir " + baseDir //$NON-NLS-1$
                    + " does not exist!", getLocation()); //$NON-NLS-1$
        }
        ObjectFactory objectFactory = ObjectFactory.newInstance();
        log("Collecting manifest files..."); //$NON-NLS-1$
        DirectoryScanner ds = super.getDirectoryScanner(baseDir);
        registry = objectFactory.createRegistry();
        String[] manifestFiles = ds.getIncludedFiles();
        List manifestUrls = new LinkedList();
        Map foldersMap = new HashMap();
        for (int i = 0; i < manifestFiles.length; i++) {
            File manifestFile = new File(baseDir, manifestFiles[i]);
            try {
                //manifestUrls[i] = IoUtil.file2url(manifestFile);
                URL manifestUrl = getManifestURL(manifestFile);
                try {
                    if (!isManifestAccepted(manifestUrl)) {
                        if (verbose) {
                            log("Skipped URL: " + manifestUrl); //$NON-NLS-1$
                        }
                        continue;
                    }
                } catch (ManifestProcessingException mpe) {
                    throw new BuildException("can't read manifest from URL " //$NON-NLS-1$
                            + manifestUrl, mpe, getLocation());
                }
                manifestUrls.add(manifestUrl);
                if (verbose) {
                    log("Added URL: " + manifestUrl); //$NON-NLS-1$
                }
                if (usePathResolver) {
                    /*foldersMap.put(manifestUrls[i],
                            IoUtil.file2url(manifestFile.getParentFile()));*/
                    if ("jar".equals(manifestUrl.getProtocol())) { //$NON-NLS-1$
                        foldersMap.put(manifestUrl,
                            IoUtil.file2url(manifestFile));
                    } else {
                        foldersMap.put(manifestUrl,
                            IoUtil.file2url(manifestFile.getParentFile()));
                    }
                }
            } catch (MalformedURLException mue) {
                throw new BuildException("can't create URL for file " //$NON-NLS-1$
                        + manifestFile, mue, getLocation());
            }
        }
        Map processedPlugins;
        try {
            processedPlugins = registry.register(
                    (URL[]) manifestUrls.toArray(new URL[manifestUrls.size()]));
        } catch (Exception e) {
            throw new BuildException("can't register URLs", e, getLocation()); //$NON-NLS-1$
        }
        log("... successfully registered " + processedPlugins.size() //$NON-NLS-1$
                + " (of " + manifestUrls.size() + ") manifest files ", //$NON-NLS-1$ //$NON-NLS-2$
                (processedPlugins.size() != manifestUrls.size())
                    ? Project.MSG_WARN : Project.MSG_INFO);
        if (usePathResolver) {
            pathResolver = objectFactory.createPathResolver();
            for (Iterator it = processedPlugins.entrySet().iterator();
                    it.hasNext();) {
                Map.Entry entry = (Entry) it.next();
                pathResolver.registerContext((Identity) entry.getValue(),
                        (URL) foldersMap.get(entry.getKey()));
            }
            log("PathResolver initialized"); //$NON-NLS-1$
        }
    }
    
    protected URL getManifestURL(final File file) throws MalformedURLException {
        if(file.getName().endsWith(".jar") || file.getName().endsWith(".zip")) { //$NON-NLS-1$ //$NON-NLS-2$
            URL url = new URL("jar:" + IoUtil.file2url(file).toExternalForm() //$NON-NLS-1$
            + "!/plugin.xml"); //$NON-NLS-1$
            if (IoUtil.isResourceExists(url)) {
                return url;
            }
            url = new URL("jar:" + IoUtil.file2url(file).toExternalForm() //$NON-NLS-1$
                    + "!/plugin-fragment.xml"); //$NON-NLS-1$
            if (IoUtil.isResourceExists(url)) {
                return url;
            }
            url = new URL("jar:" + IoUtil.file2url(file).toExternalForm() //$NON-NLS-1$
                    + "!/META-INF/plugin.xml"); //$NON-NLS-1$
            if (IoUtil.isResourceExists(url)) {
                return url;
            }
            url = new URL("jar:" + IoUtil.file2url(file).toExternalForm() //$NON-NLS-1$
                    + "!/META-INF/plugin-fragment.xml"); //$NON-NLS-1$
            if (IoUtil.isResourceExists(url)) {
                return url;
            }
            return null;
        }
        return IoUtil.file2url(file);
    }
    
    protected boolean isManifestAccepted(final URL manifestUrl)
            throws ManifestProcessingException {
        if ((whiteList == null) && (blackList == null)) {
            return true;
        }
        ManifestInfo manifestInfo = registry.readManifestInfo(manifestUrl);
        if (whiteList != null) {
            if (isPluginInList(manifestInfo, whiteList)) {
                return true;
            }
        }
        if ((blackList != null) && isPluginInList(manifestInfo, blackList)) {
            return false;
        }
        return true;
    }
    
    private boolean isPluginInList(final ManifestInfo manifestInfo,
            final Set list) {
        if (list.contains(manifestInfo.getId())) {
            return true;
        }
        return list.contains(registry.makeUniqueId(manifestInfo.getId(),
                manifestInfo.getVersion()));
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -