Search in sources :

Example 1 with ResourceInfo

use of gate.Gate.ResourceInfo in project gate-core by GateNLP.

the class Plugin method fillInResInfos.

protected void fillInResInfos(List<ResourceInfo> incompleteResInfos, List<String> allJars) throws IOException {
    // now create a temporary class loader with all the JARs (scanned or
    // not), so we can look up all the referenced classes in the normal
    // way and read their CreoleResource annotations (if any).
    URL[] jarUrls = new URL[allJars.size()];
    for (int i = 0; i < jarUrls.length; i++) {
        jarUrls[i] = new URL(getBaseURL(), allJars.get(i));
    }
    // can then throw away?
    try (URLClassLoader tempClassLoader = new URLClassLoader(jarUrls, Gate.class.getClassLoader())) {
        for (ResourceInfo ri : incompleteResInfos) {
            String classFile = ri.getResourceClassName().replace('.', '/') + ".class";
            InputStream classStream = tempClassLoader.getResourceAsStream(classFile);
            if (classStream != null) {
                ClassReader classReader = new ClassReader(classStream);
                ClassVisitor visitor = new ResourceInfoVisitor(ri);
                classReader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
                classStream.close();
            }
        }
    }
}
Also used : ResourceInfo(gate.Gate.ResourceInfo) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) URLClassLoader(java.net.URLClassLoader) ClassReader(gate.util.asm.ClassReader) Gate(gate.Gate) ClassVisitor(gate.util.asm.ClassVisitor) URL(java.net.URL)

Example 2 with ResourceInfo

use of gate.Gate.ResourceInfo in project gate-core by GateNLP.

the class Plugin method parseCreole.

protected void parseCreole() {
    valid = true;
    resourceInfoList = new ArrayList<ResourceInfo>();
    requiredPlugins = new LinkedHashSet<Plugin>();
    String relativePathMarker = "$relpath$";
    String gatehomePathMarker = "$gatehome$";
    String gatepluginsPathMarker = "$gateplugins$";
    try {
        org.jdom.Document creoleDoc = getMetadataXML();
        final Map<String, ResourceInfo> resInfos = new LinkedHashMap<String, ResourceInfo>();
        List<Element> jobsList = new ArrayList<Element>();
        List<String> jarsToScan = new ArrayList<String>();
        List<String> allJars = new ArrayList<String>();
        jobsList.add(creoleDoc.getRootElement());
        while (!jobsList.isEmpty()) {
            Element currentElem = jobsList.remove(0);
            if (currentElem.getName().equalsIgnoreCase("JAR")) {
                @SuppressWarnings("unchecked") List<Attribute> attrs = currentElem.getAttributes();
                Iterator<Attribute> attrsIt = attrs.iterator();
                while (attrsIt.hasNext()) {
                    Attribute attr = attrsIt.next();
                    if (attr.getName().equalsIgnoreCase("SCAN") && attr.getBooleanValue()) {
                        jarsToScan.add(currentElem.getTextTrim());
                        break;
                    }
                }
                allJars.add(currentElem.getTextTrim());
            } else if (currentElem.getName().equalsIgnoreCase("RESOURCE")) {
                // we don't go deeper than resources so no recursion here
                String resName = currentElem.getChildTextTrim("NAME");
                String resClass = currentElem.getChildTextTrim("CLASS");
                String resComment = currentElem.getChildTextTrim("COMMENT");
                if (!resInfos.containsKey(resClass)) {
                    // create the handler
                    ResourceInfo rHandler = new ResourceInfo(resName, resClass, resComment);
                    resInfos.put(resClass, rHandler);
                }
            } else if (currentElem.getName().equalsIgnoreCase("REQUIRES")) {
                if (currentElem.getAttribute("GROUP") != null) {
                    // TODO probably need more error checking here
                    requiredPlugins.add(new Plugin.Maven(currentElem.getAttributeValue("GROUP"), currentElem.getAttributeValue("ARTIFACT"), currentElem.getAttributeValue("VERSION")));
                } else {
                    URL url = null;
                    String urlString = currentElem.getTextTrim();
                    if (urlString.startsWith(relativePathMarker)) {
                        url = new URL(getBaseURL(), urlString.substring(relativePathMarker.length()));
                    } else if (urlString.startsWith(gatehomePathMarker)) {
                        throw new IOException("$gatehome$ variable no longer supported in REQUIRES element");
                    } else if (urlString.startsWith(gatepluginsPathMarker)) {
                        throw new IOException("$gateplugins$ variable no longer supported in REQUIRES element");
                    } else {
                        url = new URL(getBaseURL(), urlString);
                    }
                    requiredPlugins.add(new Plugin.Directory(url));
                    Utils.logOnce(log, Level.WARN, "Dependencies on other plugins via URL is deprecated (" + getName() + " plugin)");
                }
            } else {
                // this is some higher level element -> simulate recursion
                // we want Depth-first-search so we need to add at the beginning
                @SuppressWarnings("unchecked") List<Element> newJobsList = new ArrayList<Element>(currentElem.getChildren());
                newJobsList.addAll(jobsList);
                jobsList = newJobsList;
            }
        }
        // CreoleResource annotated classes.
        for (String jarFile : jarsToScan) {
            URL jarUrl = new URL(getBaseURL(), jarFile);
            scanJar(jarUrl, resInfos);
        }
        // see whether any of the ResourceInfo objects are still incomplete
        // (don't have a name)
        List<ResourceInfo> incompleteResInfos = new ArrayList<ResourceInfo>();
        for (ResourceInfo ri : resInfos.values()) {
            if (ri.getResourceName() == null) {
                incompleteResInfos.add(ri);
            }
        }
        if (!incompleteResInfos.isEmpty()) {
            fillInResInfos(incompleteResInfos, allJars);
        }
        // the class name.
        for (ResourceInfo ri : incompleteResInfos) {
            if (ri.getResourceName() == null) {
                ri.setResourceName(ri.getResourceClassName().substring(ri.getResourceClassName().lastIndexOf('.') + 1));
            }
        }
        // finally, we have the complete list of ResourceInfos
        resourceInfoList.addAll(resInfos.values());
    } catch (IOException ioe) {
        valid = false;
        log.error("Problem while parsing plugin " + toString() + "!\n" + ioe.toString() + "\nPlugin not available!");
    } catch (JDOMException jde) {
        valid = false;
        log.error("Problem while parsing plugin " + toString() + "!\n" + jde.toString() + "\nPlugin not available!");
    } catch (Exception e) {
        valid = false;
        log.error("Problem while parsing plugin " + toString() + "!\n" + e.toString() + "\nPlugin not available!");
    }
}
Also used : ResourceInfo(gate.Gate.ResourceInfo) Attribute(org.jdom.Attribute) Element(org.jdom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JDOMException(org.jdom.JDOMException) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) TransferCancelledException(org.eclipse.aether.transfer.TransferCancelledException) JDOMException(org.jdom.JDOMException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) Document(org.jdom.Document)

Example 3 with ResourceInfo

use of gate.Gate.ResourceInfo in project gate-core by GateNLP.

the class GateClassLoader method forgetClassLoader.

public void forgetClassLoader(String id, Plugin dInfo) {
    if (dInfo == null) {
        forgetClassLoader(id);
        return;
    }
    GateClassLoader classloader = null;
    synchronized (childClassLoaders) {
        classloader = childClassLoaders.remove(id);
    }
    if (classloader != null && !classloader.isIsolated()) {
        // classloader was responsible for
        for (ResourceInfo rInfo : dInfo.getResourceInfoList()) {
            try {
                @SuppressWarnings("unchecked") Class<? extends Resource> c = (Class<? extends Resource>) classloader.loadClass(rInfo.getResourceClassName());
                if (c != null) {
                    // in theory this shouldn't be needed as the Introspector
                    // uses soft references if we move to requiring Java 8 it
                    // should be safe to drop this call
                    Introspector.flushFromCaches(c);
                    AbstractResource.forgetBeanInfo(c);
                }
            } catch (ClassNotFoundException e) {
                // hmm not sure what to do now
                e.printStackTrace();
            }
        }
    }
}
Also used : ResourceInfo(gate.Gate.ResourceInfo) Resource(gate.Resource) AbstractResource(gate.creole.AbstractResource)

Example 4 with ResourceInfo

use of gate.Gate.ResourceInfo in project gate-core by GateNLP.

the class CreoleRegisterImpl method unregisterPlugin.

// put(key, value)
@Override
public void unregisterPlugin(Plugin plugin) {
    if (plugins.remove(plugin)) {
        int prCount = 0;
        for (ResourceInfo rInfo : plugin.getResourceInfoList()) {
            ResourceData rData = get(rInfo.getResourceClassName());
            if (rData != null && rData.getReferenceCount() == 1) {
                // remove the plugin
                try {
                    List<Resource> loaded = getAllInstances(rInfo.getResourceClassName(), true);
                    prCount += loaded.size();
                    for (Resource r : loaded) {
                        // System.out.println(r);
                        Factory.deleteResource(r);
                    }
                } catch (GateException e) {
                    // not much we can do here other than dump the exception
                    e.printStackTrace();
                }
            }
            remove(rInfo.getResourceClassName());
        }
        try {
            Gate.getClassLoader().forgetClassLoader(new URL(plugin.getBaseURL(), "creole.xml").toExternalForm(), plugin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("CREOLE plugin unloaded: " + plugin.getName());
        if (prCount > 0)
            log.warn(prCount + " resources were deleted as they relied on the " + plugin.getName() + " plugin");
        firePluginUnloaded(plugin);
    }
}
Also used : ResourceInfo(gate.Gate.ResourceInfo) GateException(gate.util.GateException) VisualResource(gate.VisualResource) Resource(gate.Resource) LanguageResource(gate.LanguageResource) ProcessingResource(gate.ProcessingResource) URL(java.net.URL) GateRuntimeException(gate.util.GateRuntimeException) JDOMException(org.jdom.JDOMException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) GateException(gate.util.GateException)

Example 5 with ResourceInfo

use of gate.Gate.ResourceInfo in project gate-core by GateNLP.

the class Plugin method scanJar.

protected void scanJar(URL jarUrl, Map<String, ResourceInfo> resInfos) throws IOException {
    JarInputStream jarInput = new JarInputStream(jarUrl.openStream(), false);
    JarEntry entry = null;
    while ((entry = jarInput.getNextJarEntry()) != null) {
        String entryName = entry.getName();
        if (entryName != null && entryName.endsWith(".class")) {
            final String className = entryName.substring(0, entryName.length() - 6).replace('/', '.');
            if (!resInfos.containsKey(className)) {
                ClassReader classReader = new ClassReader(jarInput);
                ResourceInfo resInfo = new ResourceInfo(null, className, null);
                ResourceInfoVisitor visitor = new ResourceInfoVisitor(resInfo);
                classReader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
                if (visitor.isCreoleResource()) {
                    resInfos.put(className, resInfo);
                }
            }
        }
    }
    jarInput.close();
}
Also used : ResourceInfo(gate.Gate.ResourceInfo) JarInputStream(java.util.jar.JarInputStream) ClassReader(gate.util.asm.ClassReader) JarEntry(java.util.jar.JarEntry)

Aggregations

ResourceInfo (gate.Gate.ResourceInfo)6 URL (java.net.URL)3 Resource (gate.Resource)2 ClassReader (gate.util.asm.ClassReader)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 JarInputStream (java.util.jar.JarInputStream)2 Element (org.jdom.Element)2 JDOMException (org.jdom.JDOMException)2 Gate (gate.Gate)1 LanguageResource (gate.LanguageResource)1 ProcessingResource (gate.ProcessingResource)1 VisualResource (gate.VisualResource)1 AbstractResource (gate.creole.AbstractResource)1 GateException (gate.util.GateException)1 GateRuntimeException (gate.util.GateRuntimeException)1 ClassVisitor (gate.util.asm.ClassVisitor)1 InputStream (java.io.InputStream)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 URISyntaxException (java.net.URISyntaxException)1