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();
}
}
}
}
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!");
}
}
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();
}
}
}
}
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);
}
}
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();
}
Aggregations