Search in sources :

Example 96 with UncheckedIOException

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

the class DaemonStartupCommunication method readDiagnostics.

public DaemonStartupInfo readDiagnostics(String message) {
    // Assuming the message has correct format. Not bullet proof, but seems to work ok for now.
    if (!message.startsWith(daemonGreeting())) {
        throw new IllegalArgumentException(String.format("Unexpected daemon startup message: %s", message));
    }
    try {
        String encoded = message.substring(daemonGreeting().length()).trim();
        InputStream inputStream = new EncodedStream.EncodedInput(new ByteArrayInputStream(encoded.getBytes()));
        Decoder decoder = new InputStreamBackedDecoder(inputStream);
        String pidString = decoder.readNullableString();
        String uid = decoder.readString();
        Long pid = pidString == null ? null : Long.valueOf(pidString);
        Address address = new MultiChoiceAddressSerializer().read(decoder);
        File daemonLog = new File(decoder.readString());
        return new DaemonStartupInfo(uid, address, new DaemonDiagnostics(daemonLog, pid));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : InputStreamBackedDecoder(org.gradle.internal.serialize.InputStreamBackedDecoder) Address(org.gradle.internal.remote.Address) MultiChoiceAddress(org.gradle.internal.remote.internal.inet.MultiChoiceAddress) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) Decoder(org.gradle.internal.serialize.Decoder) InputStreamBackedDecoder(org.gradle.internal.serialize.InputStreamBackedDecoder) DaemonStartupInfo(org.gradle.launcher.daemon.diagnostics.DaemonStartupInfo) DaemonDiagnostics(org.gradle.launcher.daemon.diagnostics.DaemonDiagnostics) MultiChoiceAddressSerializer(org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)

Example 97 with UncheckedIOException

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

the class DefaultDaemonStarter method startDaemon.

public DaemonStartupInfo startDaemon(boolean singleUse) {
    String daemonUid = UUID.randomUUID().toString();
    GradleInstallation gradleInstallation = CurrentGradleInstallation.get();
    ModuleRegistry registry = new DefaultModuleRegistry(gradleInstallation);
    ClassPath classpath;
    List<File> searchClassPath;
    if (gradleInstallation == null) {
        // When not running from a Gradle distro, need runtime impl for launcher plus the search path to look for other modules
        classpath = ClassPath.EMPTY;
        for (Module module : registry.getModule("gradle-launcher").getAllRequiredModules()) {
            classpath = classpath.plus(module.getClasspath());
        }
        searchClassPath = registry.getAdditionalClassPath().getAsFiles();
    } else {
        // When running from a Gradle distro, only need launcher jar. The daemon can find everything from there.
        classpath = registry.getModule("gradle-launcher").getImplementationClasspath();
        searchClassPath = Collections.emptyList();
    }
    if (classpath.isEmpty()) {
        throw new IllegalStateException("Unable to construct a bootstrap classpath when starting the daemon");
    }
    versionValidator.validate(daemonParameters);
    List<String> daemonArgs = new ArrayList<String>();
    daemonArgs.add(daemonParameters.getEffectiveJvm().getJavaExecutable().getAbsolutePath());
    List<String> daemonOpts = daemonParameters.getEffectiveJvmArgs();
    daemonArgs.addAll(daemonOpts);
    daemonArgs.add("-cp");
    daemonArgs.add(CollectionUtils.join(File.pathSeparator, classpath.getAsFiles()));
    if (Boolean.getBoolean("org.gradle.daemon.debug")) {
        daemonArgs.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005");
    }
    LOGGER.debug("Using daemon args: {}", daemonArgs);
    daemonArgs.add(GradleDaemon.class.getName());
    // Version isn't used, except by a human looking at the output of jps.
    daemonArgs.add(GradleVersion.current().getVersion());
    // Serialize configuration to daemon via the process' stdin
    StreamByteBuffer buffer = new StreamByteBuffer();
    FlushableEncoder encoder = new KryoBackedEncoder(new EncodedStream.EncodedOutput(buffer.getOutputStream()));
    try {
        encoder.writeString(daemonParameters.getGradleUserHomeDir().getAbsolutePath());
        encoder.writeString(daemonDir.getBaseDir().getAbsolutePath());
        encoder.writeSmallInt(daemonParameters.getIdleTimeout());
        encoder.writeSmallInt(daemonParameters.getPeriodicCheckInterval());
        encoder.writeBoolean(singleUse);
        encoder.writeString(daemonUid);
        encoder.writeSmallInt(daemonOpts.size());
        for (String daemonOpt : daemonOpts) {
            encoder.writeString(daemonOpt);
        }
        encoder.writeSmallInt(searchClassPath.size());
        for (File file : searchClassPath) {
            encoder.writeString(file.getAbsolutePath());
        }
        encoder.flush();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    InputStream stdInput = buffer.getInputStream();
    return startProcess(daemonArgs, daemonDir.getVersionedDir(), stdInput);
}
Also used : ClassPath(org.gradle.internal.classpath.ClassPath) FlushableEncoder(org.gradle.internal.serialize.FlushableEncoder) EncodedStream(org.gradle.process.internal.streams.EncodedStream) InputStream(java.io.InputStream) ModuleRegistry(org.gradle.api.internal.classpath.ModuleRegistry) DefaultModuleRegistry(org.gradle.api.internal.classpath.DefaultModuleRegistry) ArrayList(java.util.ArrayList) StreamByteBuffer(org.gradle.internal.io.StreamByteBuffer) UncheckedIOException(org.gradle.api.UncheckedIOException) KryoBackedEncoder(org.gradle.internal.serialize.kryo.KryoBackedEncoder) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) CurrentGradleInstallation(org.gradle.internal.installation.CurrentGradleInstallation) GradleInstallation(org.gradle.internal.installation.GradleInstallation) DefaultModuleRegistry(org.gradle.api.internal.classpath.DefaultModuleRegistry) GradleDaemon(org.gradle.launcher.daemon.bootstrap.GradleDaemon) Module(org.gradle.api.internal.classpath.Module) File(java.io.File)

Example 98 with UncheckedIOException

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

the class ValidateTaskProperties method validateTaskClasses.

private void validateTaskClasses(final ClassLoader classLoader) throws IOException {
    final Map<String, Boolean> taskValidationProblems = Maps.newTreeMap();
    final Class<?> taskInterface;
    final Method validatorMethod;
    try {
        taskInterface = classLoader.loadClass(Task.class.getName());
        Class<?> validatorClass = classLoader.loadClass("org.gradle.api.internal.tasks.properties.PropertyValidationAccess");
        validatorMethod = validatorClass.getMethod("collectTaskValidationProblems", Class.class, Map.class);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
    getClasses().getAsFileTree().visit(new EmptyFileVisitor() {

        @Override
        public void visitFile(FileVisitDetails fileDetails) {
            if (!fileDetails.getPath().endsWith(".class")) {
                return;
            }
            ClassReader reader;
            try {
                reader = new PatchedClassReader(Files.asByteSource(fileDetails.getFile()).read());
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            List<String> classNames = Lists.newArrayList();
            reader.accept(new TaskNameCollectorVisitor(classNames), ClassReader.SKIP_CODE);
            for (String className : classNames) {
                Class<?> clazz;
                try {
                    clazz = classLoader.loadClass(className);
                } catch (IllegalAccessError e) {
                    throw new GradleException("Could not load class: " + className, e);
                } catch (ClassNotFoundException e) {
                    throw new GradleException("Could not load class: " + className, e);
                } catch (NoClassDefFoundError e) {
                    throw new GradleException("Could not load class: " + className, e);
                }
                if (!Modifier.isPublic(clazz.getModifiers())) {
                    continue;
                }
                if (Modifier.isAbstract(clazz.getModifiers())) {
                    continue;
                }
                if (!taskInterface.isAssignableFrom(clazz)) {
                    continue;
                }
                Class<? extends Task> taskClass = Cast.uncheckedCast(clazz);
                try {
                    validatorMethod.invoke(null, taskClass, taskValidationProblems);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                } catch (InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    });
    List<String> problemMessages = toProblemMessages(taskValidationProblems);
    storeResults(problemMessages);
    communicateResult(problemMessages, taskValidationProblems.values().contains(Boolean.TRUE));
}
Also used : Task(org.gradle.api.Task) VerificationTask(org.gradle.api.tasks.VerificationTask) ConventionTask(org.gradle.api.internal.ConventionTask) CacheableTask(org.gradle.api.tasks.CacheableTask) UncheckedIOException(org.gradle.api.UncheckedIOException) PatchedClassReader(org.gradle.util.internal.PatchedClassReader) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) EmptyFileVisitor(org.gradle.api.file.EmptyFileVisitor) Method(java.lang.reflect.Method) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) FileVisitDetails(org.gradle.api.file.FileVisitDetails) GradleException(org.gradle.api.GradleException) PatchedClassReader(org.gradle.util.internal.PatchedClassReader) ClassReader(org.objectweb.asm.ClassReader) Map(java.util.Map)

Example 99 with UncheckedIOException

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

the class SwiftcMetadataProvider method parseCompilerOutput.

@Override
protected SwiftcMetadata parseCompilerOutput(String stdout, String stderr, File swiftc) {
    BufferedReader reader = new BufferedReader(new StringReader(stdout));
    try {
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.contains("Swift version")) {
                String[] tokens = line.split(" ");
                // Assuming format: 'Swift version 4.0.2 (...)'
                int i = 2;
                if ("Apple".equals(tokens[0])) {
                    // Actual format: 'Apple Swift version 4.0.2 (...)'
                    i++;
                }
                VersionNumber version = VersionNumber.parse(tokens[i]);
                return new DefaultSwiftcMetadata(line, version);
            }
        }
        throw new BrokenResultException(String.format("Could not determine %s metadata: %s produced unexpected output.", getCompilerType().getDescription(), swiftc.getName()));
    } catch (IOException e) {
        // Should not happen when reading from a StringReader
        throw new UncheckedIOException(e);
    }
}
Also used : BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) VersionNumber(org.gradle.util.VersionNumber)

Example 100 with UncheckedIOException

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

the class FileBackedBlockStore method open.

public void open(Runnable runnable, Factory factory) {
    this.factory = factory;
    try {
        cacheFile.getParentFile().mkdirs();
        file = new RandomAccessFile(cacheFile, "rw");
        output = new ByteOutput(file);
        input = new ByteInput(file);
        currentFileSize = file.length();
        nextBlock = currentFileSize;
        if (currentFileSize == 0) {
            runnable.run();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException)

Aggregations

UncheckedIOException (org.gradle.api.UncheckedIOException)101 IOException (java.io.IOException)79 File (java.io.File)32 InputStream (java.io.InputStream)9 FileOutputStream (java.io.FileOutputStream)7 OutputStream (java.io.OutputStream)7 BufferedReader (java.io.BufferedReader)6 StringReader (java.io.StringReader)6 FileInputStream (java.io.FileInputStream)5 Matcher (java.util.regex.Matcher)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 URI (java.net.URI)4 URL (java.net.URL)4 ArrayList (java.util.ArrayList)4 Manifest (java.util.jar.Manifest)4 ZipInputStream (java.util.zip.ZipInputStream)4 FileVisitDetails (org.gradle.api.file.FileVisitDetails)4 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileReader (java.io.FileReader)3