Search in sources :

Example 16 with GateException

use of gate.util.GateException in project gate-core by GateNLP.

the class Main method main.

/**
 * Main routine for GATE.
 * Command-line arguments:
 * <UL>
 * <LI>
 * <B>-h</B> display a short help message
 * <LI>
 * <B>-d URL</B> define URL to be a location for CREOLE resoures
 * <LI>
 * <B>-i file</B> additional initialisation file (probably called
 *   <TT>gate.xml</TT>). Used for site-wide initialisation by the
 *   start-up scripts
 * </UL>
 */
public static void main(String[] args) throws GateException {
    // check we have a useable JDK
    if (System.getProperty("java.version").compareTo(Gate.getMinJdkVersion()) < 0) {
        throw new GateException("GATE requires JDK " + Gate.getMinJdkVersion() + " or newer");
    }
    ThreadWarningSystem tws = new ThreadWarningSystem();
    tws.addListener(new ThreadWarningSystem.Listener() {

        final PrintStream out = System.out;

        @Override
        public void deadlockDetected(ThreadInfo inf) {
            out.println("Deadlocked Thread:");
            out.println("------------------");
            out.println(inf);
            for (StackTraceElement ste : inf.getStackTrace()) {
                out.println("\t" + ste);
            }
        }

        @Override
        public void thresholdExceeded(ThreadInfo[] threads) {
        }
    });
    // process command-line options
    processArgs(args);
    runGui();
}
Also used : PrintStream(java.io.PrintStream) ThreadInfo(java.lang.management.ThreadInfo) GateException(gate.util.GateException) ThreadWarningSystem(gate.util.ThreadWarningSystem)

Example 17 with GateException

use of gate.util.GateException in project gate-core by GateNLP.

the class AnnotationDiffGUI method populateGUI.

protected void populateGUI() {
    try {
        documents = Gate.getCreoleRegister().getAllInstances("gate.Document");
    } catch (GateException ge) {
        throw new GateRuntimeException(ge);
    }
    List<String> documentNames = new ArrayList<String>(documents.size());
    for (Resource document : documents) {
        documentNames.add(document.getName());
    }
    Object keyDocSelectedItem = keyDocCombo.getSelectedItem();
    Object resDocSelectedItem = resDocCombo.getSelectedItem();
    keyDocCombo.setModel(new DefaultComboBoxModel<String>(documentNames.toArray(new String[documentNames.size()])));
    resDocCombo.setModel(new DefaultComboBoxModel<String>(documentNames.toArray(new String[documentNames.size()])));
    if (!documents.isEmpty()) {
        keyDocCombo.setSelectedItem(keyDocSelectedItem);
        if (keyDocCombo.getSelectedIndex() == -1) {
            keyDocCombo.setSelectedIndex(0);
        }
        resDocCombo.setSelectedItem(resDocSelectedItem);
        if (resDocCombo.getSelectedIndex() == -1) {
            resDocCombo.setSelectedIndex(0);
        }
        statusLabel.setText(documents.size() + " documents loaded");
        if (annTypeCombo.getSelectedItem() == null) {
            statusLabel.setText(statusLabel.getText() + ". Choose two annotation sets to compare.");
        }
        statusLabel.setForeground(Color.BLACK);
    } else {
        statusLabel.setText("You must load at least one document.");
        statusLabel.setForeground(Color.RED);
    }
}
Also used : GateException(gate.util.GateException) GateRuntimeException(gate.util.GateRuntimeException) ArrayList(java.util.ArrayList) Resource(gate.Resource)

Example 18 with GateException

use of gate.util.GateException in project gate-core by GateNLP.

the class PersistenceManager method loadObjectFromUrl.

public static Object loadObjectFromUrl(URL url) throws PersistenceException, IOException, ResourceInstantiationException {
    if (!Gate.isInitialised())
        throw new ResourceInstantiationException("You must call Gate.init() before you can restore resources");
    ProgressListener pListener = (ProgressListener) Gate.getListeners().get("gate.event.ProgressListener");
    StatusListener sListener = (gate.event.StatusListener) Gate.getListeners().get("gate.event.StatusListener");
    if (pListener != null)
        pListener.progressChanged(0);
    startLoadingFrom(url);
    // the actual stream obtained from the URL. We keep a reference to this
    // so we can ensure it gets closed.
    InputStream rawStream = null;
    try {
        long startTime = System.currentTimeMillis();
        // Determine whether the file contains an application serialized in
        // xml
        // format. Otherwise we will assume that it contains native
        // serializations.
        boolean xmlStream = isXmlApplicationFile(url);
        ObjectInputStream ois = null;
        HierarchicalStreamReader reader = null;
        XStream xstream = null;
        // whether serialization is native or xml.
        if (xmlStream) {
            // we don't want to strip the BOM on XML.
            Reader inputReader = new InputStreamReader(rawStream = url.openStream());
            try {
                XMLInputFactory inputFactory = XMLInputFactory.newInstance();
                inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
                XMLStreamReader xsr = inputFactory.createXMLStreamReader(url.toExternalForm(), inputReader);
                reader = new StaxReader(new QNameMap(), xsr);
            } catch (XMLStreamException xse) {
                // make sure the stream is closed, on error
                inputReader.close();
                throw new PersistenceException("Error creating reader", xse);
            }
            xstream = new XStream(new StaxDriver(new XStream11NameCoder())) {

                @Override
                protected boolean useXStream11XmlFriendlyMapper() {
                    return true;
                }
            };
            // make XStream load classes through the GATE ClassLoader
            xstream.setClassLoader(Gate.getClassLoader());
            // make the XML stream appear as a normal ObjectInputStream
            ois = xstream.createObjectInputStream(reader);
        } else {
            // use GateAwareObjectInputStream to load classes through the
            // GATE ClassLoader if they can't be loaded through the one
            // ObjectInputStream would normally use
            ois = new GateAwareObjectInputStream(url.openStream());
        }
        Object res = null;
        try {
            // first read the list of creole URLs.
            @SuppressWarnings("unchecked") Iterator<?> urlIter = ((Collection<?>) getTransientRepresentation(ois.readObject())).iterator();
            // and re-register them
            while (urlIter.hasNext()) {
                Object anUrl = urlIter.next();
                try {
                    if (anUrl instanceof URL)
                        Gate.getCreoleRegister().registerPlugin(new Plugin.Directory((URL) anUrl), false);
                    else if (anUrl instanceof Plugin)
                        Gate.getCreoleRegister().registerPlugin((Plugin) anUrl, false);
                } catch (GateException ge) {
                    System.out.println("We've hit an error!");
                    ge.printStackTrace();
                    ge.printStackTrace(Err.getPrintWriter());
                    Err.prln("Could not reload creole directory " + anUrl);
                }
            }
            // now we can read the saved object in the presence of all
            // the right plugins
            res = ois.readObject();
            // ensure a fresh start
            clearCurrentTransients();
            res = getTransientRepresentation(res);
            long endTime = System.currentTimeMillis();
            if (sListener != null)
                sListener.statusChanged("Loading completed in " + NumberFormat.getInstance().format((double) (endTime - startTime) / 1000) + " seconds");
            return res;
        } catch (ResourceInstantiationException rie) {
            if (sListener != null)
                sListener.statusChanged("Failure during instantiation of resources.");
            throw rie;
        } catch (PersistenceException pe) {
            if (sListener != null)
                sListener.statusChanged("Failure during persistence operations.");
            throw pe;
        } catch (Exception ex) {
            if (sListener != null)
                sListener.statusChanged("Loading failed!");
            throw new PersistenceException(ex);
        } finally {
            // make sure the stream gets closed
            if (ois != null)
                ois.close();
            if (reader != null)
                reader.close();
        }
    } finally {
        if (rawStream != null)
            rawStream.close();
        finishedLoading();
        if (pListener != null)
            pListener.processFinished();
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) BomStrippingInputStreamReader(gate.util.BomStrippingInputStreamReader) StaxReader(com.thoughtworks.xstream.io.xml.StaxReader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) URL(java.net.URL) StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap) StaxReader(com.thoughtworks.xstream.io.xml.StaxReader) BomStrippingInputStreamReader(gate.util.BomStrippingInputStreamReader) InputStreamReader(java.io.InputStreamReader) GateAwareObjectInputStream(gate.persist.GateAwareObjectInputStream) ObjectInputStream(java.io.ObjectInputStream) GateAwareObjectInputStream(gate.persist.GateAwareObjectInputStream) InputStream(java.io.InputStream) XStream(com.thoughtworks.xstream.XStream) GateException(gate.util.GateException) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) PersistenceException(gate.persist.PersistenceException) GateRuntimeException(gate.util.GateRuntimeException) ResourceInstantiationException(gate.creole.ResourceInstantiationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) GateException(gate.util.GateException) ResourceInstantiationException(gate.creole.ResourceInstantiationException) ProgressListener(gate.event.ProgressListener) XMLStreamException(javax.xml.stream.XMLStreamException) XStream11NameCoder(com.thoughtworks.xstream.io.xml.XStream11NameCoder) PersistenceException(gate.persist.PersistenceException) Collection(java.util.Collection) StatusListener(gate.event.StatusListener) XMLInputFactory(javax.xml.stream.XMLInputFactory) ObjectInputStream(java.io.ObjectInputStream) GateAwareObjectInputStream(gate.persist.GateAwareObjectInputStream) Plugin(gate.creole.Plugin)

Example 19 with GateException

use of gate.util.GateException 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 20 with GateException

use of gate.util.GateException in project gate-core by GateNLP.

the class CreoleRegisterImpl method registerPlugin.

@Override
public void registerPlugin(Plugin plugin, boolean loadDependencies) throws GateException {
    if (!plugins.contains(plugin)) {
        Gate.addKnownPlugin(plugin);
        try {
            if (loadDependencies) {
                for (Plugin required : plugin.getRequiredPlugins()) {
                    registerPlugin(required, true);
                }
            }
            Document creoleXML = plugin.getCreoleXML();
            if (plugin.isValid()) {
                parseDirectory(plugin, creoleXML, plugin.getBaseURL(), new URL(plugin.getBaseURL(), "creole.xml"));
                log.info("CREOLE plugin loaded: " + plugin.getName());
            } else {
                throw new GateException("plugin is invalid");
            }
        } catch (Throwable e) {
            // it failed:
            throw (new GateException("couldn't open creole.xml for plugin: " + plugin, e));
        }
        plugins.add(plugin);
        firePluginLoaded(plugin);
    }
}
Also used : GateException(gate.util.GateException) Document(org.jdom.Document) URL(java.net.URL)

Aggregations

GateException (gate.util.GateException)23 IOException (java.io.IOException)11 URL (java.net.URL)10 GateRuntimeException (gate.util.GateRuntimeException)8 MalformedURLException (java.net.MalformedURLException)8 Resource (gate.Resource)5 JDOMException (org.jdom.JDOMException)5 Plugin (gate.creole.Plugin)3 InputStream (java.io.InputStream)3 LanguageResource (gate.LanguageResource)2 ProcessingResource (gate.ProcessingResource)2 VisualResource (gate.VisualResource)2 ResourceInstantiationException (gate.creole.ResourceInstantiationException)2 CreoleResource (gate.creole.metadata.CreoleResource)2 GateClassLoader (gate.util.GateClassLoader)2 BeanInfo (java.beans.BeanInfo)2 IntrospectionException (java.beans.IntrospectionException)2 PropertyDescriptor (java.beans.PropertyDescriptor)2 File (java.io.File)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)2