Search in sources :

Example 56 with GradleException

use of org.gradle.api.GradleException in project gradle by gradle.

the class DefaultJvmVersionDetector method parseJavaVersionCommandOutput.

private JavaVersion parseJavaVersionCommandOutput(String javaExecutable, BufferedReader reader) {
    try {
        String versionStr = reader.readLine();
        while (versionStr != null) {
            Matcher matcher = Pattern.compile("(?:java|openjdk) version \"(.+?)\"( \\d{4}-\\d{2}-\\d{2}( LTS)?)?").matcher(versionStr);
            if (matcher.matches()) {
                return JavaVersion.toVersion(matcher.group(1));
            }
            versionStr = reader.readLine();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    throw new GradleException(String.format("Could not determine Java version using executable %s.", javaExecutable));
}
Also used : Matcher(java.util.regex.Matcher) GradleException(org.gradle.api.GradleException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException)

Example 57 with GradleException

use of org.gradle.api.GradleException in project gradle by gradle.

the class SimpleStateCache method serialize.

private void serialize(T newValue) {
    try {
        if (!cacheFile.isFile()) {
            cacheFile.createNewFile();
        }
        // read-write-execute for user only
        chmod.chmod(cacheFile.getParentFile(), 0700);
        // read-write for user only
        chmod.chmod(cacheFile, 0600);
        OutputStreamBackedEncoder encoder = new OutputStreamBackedEncoder(new BufferedOutputStream(new FileOutputStream(cacheFile)));
        try {
            serializer.write(encoder, newValue);
        } finally {
            encoder.close();
        }
    } catch (Exception e) {
        throw new GradleException(String.format("Could not write cache value to '%s'.", cacheFile), e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) GradleException(org.gradle.api.GradleException) OutputStreamBackedEncoder(org.gradle.internal.serialize.OutputStreamBackedEncoder) BufferedOutputStream(java.io.BufferedOutputStream) GradleException(org.gradle.api.GradleException)

Example 58 with GradleException

use of org.gradle.api.GradleException in project tomee by apache.

the class TomEEEmbeddedTask method doRun.

private void doRun() {
    final Properties originalSystProp = new Properties();
    originalSystProp.putAll(System.getProperties());
    final Thread thread = Thread.currentThread();
    final ClassLoader loader = thread.getContextClassLoader();
    if (inlinedServerXml != null && !inlinedServerXml.trim().isEmpty()) {
        if (serverXml != null && serverXml.exists()) {
            throw new GradleException("you can't define a server.xml and an inlinedServerXml");
        }
        try {
            GFileUtils.mkdirs(workDir);
            serverXml = new File(workDir, "server.xml_dump");
            GFileUtils.writeFile(inlinedServerXml, serverXml);
        } catch (final Exception e) {
            throw new GradleException(e.getMessage(), e);
        }
    }
    final AtomicBoolean running = new AtomicBoolean();
    AutoCloseable container;
    Thread hook;
    try {
        final Class<?> containerClass = loader.loadClass("org.apache.tomee.embedded.Container");
        final Class<?> configClass = loader.loadClass("org.apache.tomee.embedded.Configuration");
        final Class<?> parentLoaderFinderClass = loader.loadClass("org.apache.openejb.core.ParentClassLoaderFinder");
        final Class<?> loaderFinderClass = loader.loadClass("org.apache.openejb.core.ProvidedClassLoaderFinder");
        final Class<?> systemInstanceClass = loader.loadClass("org.apache.openejb.loader.SystemInstance");
        container = AutoCloseable.class.cast(containerClass.newInstance());
        final Object config = getConfig(configClass);
        containerClass.getMethod("setup", configClass).invoke(container, config);
        if (inlinedTomEEXml != null && inlinedTomEEXml.trim().isEmpty()) {
            try {
                final File conf = new File(dir, "conf");
                GFileUtils.mkdirs(conf);
                GFileUtils.writeFile(inlinedTomEEXml, new File(conf, "tomee.xml"));
            } catch (final Exception e) {
                throw new GradleException(e.getMessage(), e);
            }
        }
        final AutoCloseable finalContainer = container;
        hook = new Thread() {

            @Override
            public void run() {
                if (running.compareAndSet(true, false)) {
                    final Thread thread = Thread.currentThread();
                    final ClassLoader old = thread.getContextClassLoader();
                    thread.setContextClassLoader(loader);
                    try {
                        finalContainer.close();
                    } catch (final NoClassDefFoundError noClassDefFoundError) {
                        // debug cause it is too late to shutdown properly so don't pollute logs
                        getLogger().debug("can't stop TomEE", noClassDefFoundError);
                    } catch (final Exception e) {
                        getLogger().error("can't stop TomEE", e);
                    } finally {
                        thread.setContextClassLoader(old);
                    }
                }
            }
        };
        hook.setName("TomEE-Embedded-ShutdownHook");
        // yes should be done after but we can't help much if we don't do it there for auto shutdown
        running.set(true);
        containerClass.getMethod("start").invoke(container);
        // SystemInstance.get().setComponent(ParentClassLoaderFinder.class, new ProvidedClassLoaderFinder(loader));
        final Object providedLoaderFinder = loaderFinderClass.getConstructor(ClassLoader.class).newInstance(loader);
        final Object systemInstance = systemInstanceClass.getMethod("get").invoke(null);
        systemInstanceClass.getMethod("setComponent", Class.class, Object.class).invoke(systemInstance, parentLoaderFinderClass, providedLoaderFinder);
        Runtime.getRuntime().addShutdownHook(hook);
        containerClass.getMethod("deployClasspathAsWebApp", String.class, File.class, boolean.class).invoke(container, context, docBase, singleClassloader);
        getLogger().info("TomEE embedded started on " + configClass.getMethod("getHost").invoke(config) + ":" + configClass.getMethod("getHttpPort").invoke(config));
    } catch (final Exception e) {
        throw new GradleException(e.getMessage(), e);
    }
    try {
        String line;
        final Scanner scanner = new Scanner(System.in);
        while ((line = scanner.nextLine()) != null) {
            final String cmd = line.trim().toLowerCase(Locale.ENGLISH);
            switch(cmd) {
                case "exit":
                case "quit":
                    running.set(false);
                    Runtime.getRuntime().removeShutdownHook(hook);
                    container.close();
                    return;
                default:
                    getLogger().warn("Unknown: '" + cmd + "', use 'exit' or 'quit'");
            }
        }
    } catch (final Exception e) {
        Thread.interrupted();
    } finally {
        thread.setContextClassLoader(loader);
        System.setProperties(originalSystProp);
    }
}
Also used : Scanner(java.util.Scanner) Properties(java.util.Properties) UnknownConfigurationException(org.gradle.api.artifacts.UnknownConfigurationException) MalformedURLException(java.net.MalformedURLException) GradleException(org.gradle.api.GradleException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GradleException(org.gradle.api.GradleException) FilterGradleClassLoader(org.apache.tomee.gradle.embedded.classloader.FilterGradleClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Example 59 with GradleException

use of org.gradle.api.GradleException in project gradle by gradle.

the class GroovySystemLoaderFactory method forClassLoader.

public GroovySystemLoader forClassLoader(ClassLoader classLoader) {
    try {
        Class<?> groovySystem;
        try {
            groovySystem = classLoader.loadClass("groovy.lang.GroovySystem");
        } catch (ClassNotFoundException e) {
            // Not a Groovy implementation, or not an implementation that we need to deal with
            return NOT_BROKEN;
        }
        if (groovySystem.getClassLoader() != classLoader) {
            // Groovy implementation visible from somewhere else
            return NOT_BROKEN;
        }
        String versionString;
        try {
            Method getVersion = groovySystem.getDeclaredMethod("getVersion");
            versionString = (String) getVersion.invoke(null);
        } catch (NoSuchMethodException ex) {
            return NOT_BROKEN;
        }
        VersionNumber groovyVersion = VersionNumber.parse(versionString);
        boolean isFaultyGroovy = groovyVersion.getMajor() == 2 && groovyVersion.getMinor() == 4;
        return isFaultyGroovy ? new LeakyOnJava7GroovySystemLoader(classLoader) : NOT_BROKEN;
    } catch (Exception e) {
        throw new GradleException("Could not inspect the Groovy system for ClassLoader " + classLoader, e);
    }
}
Also used : GradleException(org.gradle.api.GradleException) Method(java.lang.reflect.Method) VersionNumber(org.gradle.util.VersionNumber) GradleException(org.gradle.api.GradleException)

Example 60 with GradleException

use of org.gradle.api.GradleException in project gradle by gradle.

the class LeakyOnJava7GroovySystemLoader method shutdown.

@Override
public void shutdown() {
    if (leakingLoader == getClass().getClassLoader()) {
        throw new IllegalStateException("Cannot shut down the main Groovy loader.");
    }
    // Remove cached value for every class seen by this ClassLoader
    try {
        Iterator<?> it = globalClassSetIterator();
        while (it.hasNext()) {
            Object classInfo = it.next();
            if (classInfo != null) {
                Class clazz = (Class) clazzField.get(classInfo);
                removeFromGlobalClassValue.invoke(globalClassValue, clazz);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Removed ClassInfo from {} loaded by {}", clazz.getName(), clazz.getClassLoader());
                }
            }
        }
    } catch (Exception e) {
        throw new GradleException("Could not shut down the Groovy system for " + leakingLoader, e);
    }
}
Also used : GradleException(org.gradle.api.GradleException) GradleException(org.gradle.api.GradleException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

GradleException (org.gradle.api.GradleException)114 File (java.io.File)40 IOException (java.io.IOException)32 TaskAction (org.gradle.api.tasks.TaskAction)16 ArrayList (java.util.ArrayList)10 UncheckedIOException (org.gradle.api.UncheckedIOException)10 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)9 AndroidDependencyTree (com.taobao.android.builder.dependency.AndroidDependencyTree)8 HashSet (java.util.HashSet)8 FileOutputStream (java.io.FileOutputStream)6 Method (java.lang.reflect.Method)6 UncheckedException (org.gradle.internal.UncheckedException)6 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)5 URL (java.net.URL)5 List (java.util.List)5 ConsoleRenderer (org.gradle.internal.logging.ConsoleRenderer)5 ProcessOutputHandler (com.android.ide.common.process.ProcessOutputHandler)4 FileInputStream (java.io.FileInputStream)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 MalformedURLException (java.net.MalformedURLException)4