Search in sources :

Example 26 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy-core by groovy.

the class PropertyBinding method updateTargetValue.

public void updateTargetValue(final Object newValue) {
    Runnable runnable = new Runnable() {

        public void run() {
            Object sourceValue = getSourceValue();
            // if (isNonChangeCheck()) {
            if ((sourceValue == null && newValue == null) || DefaultTypeTransformation.compareEqual(sourceValue, newValue)) {
                // not a change, don't fire it
                return;
            }
            // }
            setBeanProperty(newValue);
        }
    };
    switch(updateStrategy) {
        case MIXED:
            if (SwingUtilities.isEventDispatchThread()) {
                runnable.run();
            } else {
                SwingUtilities.invokeLater(runnable);
            }
            break;
        case ASYNC:
            SwingUtilities.invokeLater(runnable);
            break;
        case SYNC:
            if (SwingUtilities.isEventDispatchThread()) {
                runnable.run();
            } else {
                try {
                    SwingUtilities.invokeAndWait(runnable);
                } catch (InterruptedException e) {
                    LOG.log(Level.WARNING, "Error notifying propertyChangeListener", e);
                    throw new GroovyRuntimeException(e);
                } catch (InvocationTargetException e) {
                    LOG.log(Level.WARNING, "Error notifying propertyChangeListener", e.getTargetException());
                    throw new GroovyRuntimeException(e.getTargetException());
                }
            }
            break;
        case SAME:
            runnable.run();
            break;
        case OUTSIDE:
            if (SwingUtilities.isEventDispatchThread()) {
                DEFAULT_EXECUTOR_SERVICE.submit(runnable);
            } else {
                runnable.run();
            }
            break;
        case DEFER:
            DEFAULT_EXECUTOR_SERVICE.submit(runnable);
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 27 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy-core by groovy.

the class XmlTemplateEngine method createTemplate.

public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException {
    Node root;
    try {
        root = xmlParser.parse(reader);
    } catch (SAXException e) {
        throw new RuntimeException("Parsing XML source failed.", e);
    }
    if (root == null) {
        throw new IOException("Parsing XML source failed: root node is null.");
    }
    StringWriter writer = new StringWriter(1024);
    writer.write("/* Generated by XmlTemplateEngine */\n");
    new GspPrinter(new PrintWriter(writer), indentation).print(root);
    Script script;
    try {
        script = groovyShell.parse(writer.toString(), "XmlTemplateScript" + counter++ + ".groovy");
    } catch (Exception e) {
        throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
    }
    return new XmlTemplate(script);
}
Also used : Script(groovy.lang.Script) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) StringWriter(java.io.StringWriter) Node(groovy.util.Node) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SAXException(org.xml.sax.SAXException) PrintWriter(java.io.PrintWriter)

Example 28 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy-core by groovy.

the class TestNgRunner method run.

/**
     * Utility method to run a TestNG test.
     *
     * @param scriptClass the class we want to run as a test
     * @param loader the class loader to use
     * @return the result of running the test
     */
public Object run(Class scriptClass, GroovyClassLoader loader) {
    // invoke through reflection to eliminate mandatory TestNG jar dependency
    try {
        Class testNGClass = loader.loadClass("org.testng.TestNG");
        Object testng = InvokerHelper.invokeConstructorOf(testNGClass, new Object[] {});
        InvokerHelper.invokeMethod(testng, "setTestClasses", new Object[] { scriptClass });
        Class listenerClass = loader.loadClass("org.testng.TestListenerAdapter");
        Object listener = InvokerHelper.invokeConstructorOf(listenerClass, new Object[] {});
        InvokerHelper.invokeMethod(testng, "addListener", new Object[] { listener });
        return InvokerHelper.invokeMethod(testng, "run", new Object[] {});
    } catch (ClassNotFoundException e) {
        throw new GroovyRuntimeException("Error running TestNG test.", e);
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException)

Example 29 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy-core by groovy.

the class ExtensionModuleScanner method scanExtensionModuleFromMetaInf.

private void scanExtensionModuleFromMetaInf(final URL metadata) {
    Properties properties = new Properties();
    InputStream inStream = null;
    try {
        inStream = metadata.openStream();
        properties.load(inStream);
    } catch (IOException e) {
        throw new GroovyRuntimeException("Unable to load module META-INF descriptor", e);
    } finally {
        closeQuietly(inStream);
    }
    scanExtensionModuleFromProperties(properties);
}
Also used : InputStream(java.io.InputStream) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException)

Example 30 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy-core by groovy.

the class MetaInfExtensionModule method newModule.

public static MetaInfExtensionModule newModule(final Properties properties, final ClassLoader loader) {
    String name = properties.getProperty(PropertiesModuleFactory.MODULE_NAME_KEY);
    if (name == null)
        throw new GroovyRuntimeException("Module file hasn't set the module name using key [" + PropertiesModuleFactory.MODULE_NAME_KEY + "]");
    String version = properties.getProperty(PropertiesModuleFactory.MODULE_VERSION_KEY);
    if (version == null)
        throw new GroovyRuntimeException("Module file hasn't set the module version using key [" + PropertiesModuleFactory.MODULE_VERSION_KEY + "]");
    String[] extensionClasses = properties.getProperty(MODULE_INSTANCE_CLASSES_KEY, "").trim().split("[,; ]");
    String[] staticExtensionClasses = properties.getProperty(MODULE_STATIC_CLASSES_KEY, "").trim().split("[,; ]");
    List<Class> instanceClasses = new ArrayList<Class>(extensionClasses.length);
    List<Class> staticClasses = new ArrayList<Class>(staticExtensionClasses.length);
    List<String> errors = new LinkedList<String>();
    for (String extensionClass : extensionClasses) {
        try {
            extensionClass = extensionClass.trim();
            if (extensionClass.length() > 0) {
                instanceClasses.add(loader.loadClass(extensionClass));
            }
        } catch (ClassNotFoundException e) {
            errors.add(extensionClass);
        } catch (NoClassDefFoundError e) {
            errors.add(extensionClass);
        } catch (UnsupportedClassVersionError e) {
            errors.add(extensionClass);
        }
    }
    for (String extensionClass : staticExtensionClasses) {
        try {
            extensionClass = extensionClass.trim();
            if (extensionClass.length() > 0) {
                staticClasses.add(loader.loadClass(extensionClass));
            }
        } catch (ClassNotFoundException e) {
            errors.add(extensionClass);
        } catch (NoClassDefFoundError e) {
            errors.add(extensionClass);
        } catch (UnsupportedClassVersionError e) {
            errors.add(extensionClass);
        }
    }
    if (!errors.isEmpty()) {
        for (String error : errors) {
            LOG.warning("Module [" + name + "] - Unable to load extension class [" + error + "]");
        }
    }
    return new MetaInfExtensionModule(name, version, instanceClasses, staticClasses);
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Aggregations

GroovyRuntimeException (groovy.lang.GroovyRuntimeException)59 IOException (java.io.IOException)15 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 GroovyObject (groovy.lang.GroovyObject)8 ArrayList (java.util.ArrayList)6 Closure (groovy.lang.Closure)5 InputStream (java.io.InputStream)5 List (java.util.List)5 GroovyShell (groovy.lang.GroovyShell)4 MetaClass (groovy.lang.MetaClass)4 Map (java.util.Map)4 ClassNode (org.codehaus.groovy.ast.ClassNode)4 MethodNode (org.codehaus.groovy.ast.MethodNode)4 MetaMethod (groovy.lang.MetaMethod)3 Script (groovy.lang.Script)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 URL (java.net.URL)3 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)2 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)2