Search in sources :

Example 1 with DeferredFileOutputStream

use of org.apache.commons.io.output.DeferredFileOutputStream in project jforum2 by rafaelsteil.

the class DiskFileItem method getOutputStream.

/**
     * Returns an {@link java.io.OutputStream OutputStream} that can
     * be used for storing the contents of the file.
     *
     * @return An {@link java.io.OutputStream OutputStream} that can be used
     *         for storing the contensts of the file.
     *
     * @exception IOException if an error occurs.
     */
public OutputStream getOutputStream() throws IOException {
    if (dfos == null) {
        File outputFile = getTempFile();
        dfos = new DeferredFileOutputStream(sizeThreshold, outputFile);
    }
    return dfos;
}
Also used : DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) File(java.io.File)

Example 2 with DeferredFileOutputStream

use of org.apache.commons.io.output.DeferredFileOutputStream in project maven-plugins by apache.

the class ProcessRemoteResourcesMojo method processResourceBundles.

protected void processResourceBundles(RemoteResourcesClassLoader classLoader, VelocityContext context) throws MojoExecutionException {
    try {
        // CHECKSTYLE_OFF: LineLength
        for (Enumeration<URL> e = classLoader.getResources(BundleRemoteResourcesMojo.RESOURCES_MANIFEST); e.hasMoreElements(); ) {
            URL url = e.nextElement();
            InputStream in = null;
            OutputStream out = null;
            Reader reader = null;
            Writer writer = null;
            try {
                reader = new InputStreamReader(url.openStream());
                RemoteResourcesBundleXpp3Reader bundleReader = new RemoteResourcesBundleXpp3Reader();
                RemoteResourcesBundle bundle = bundleReader.read(reader);
                reader.close();
                reader = null;
                for (String bundleResource : bundle.getRemoteResources()) {
                    String projectResource = bundleResource;
                    boolean doVelocity = false;
                    if (projectResource.endsWith(TEMPLATE_SUFFIX)) {
                        projectResource = projectResource.substring(0, projectResource.length() - 3);
                        doVelocity = true;
                    }
                    // Don't overwrite resource that are already being provided.
                    File f = new File(outputDirectory, projectResource);
                    FileUtils.mkdir(f.getParentFile().getAbsolutePath());
                    if (!copyResourceIfExists(f, projectResource, context)) {
                        if (doVelocity) {
                            DeferredFileOutputStream os = new DeferredFileOutputStream(velocityFilterInMemoryThreshold, f);
                            writer = bundle.getSourceEncoding() == null ? new OutputStreamWriter(os) : new OutputStreamWriter(os, bundle.getSourceEncoding());
                            if (bundle.getSourceEncoding() == null) {
                                // TODO: Is this correct? Shouldn't we behave like the rest of maven and fail
                                // down to JVM default instead ISO-8859-1 ?
                                velocity.mergeTemplate(bundleResource, "ISO-8859-1", context, writer);
                            } else {
                                velocity.mergeTemplate(bundleResource, bundle.getSourceEncoding(), context, writer);
                            }
                            writer.close();
                            writer = null;
                            fileWriteIfDiffers(os);
                        } else {
                            URL resUrl = classLoader.getResource(bundleResource);
                            if (resUrl != null) {
                                FileUtils.copyURLToFile(resUrl, f);
                            }
                        }
                        File appendedResourceFile = new File(appendedResourcesDirectory, projectResource);
                        File appendedVmResourceFile = new File(appendedResourcesDirectory, projectResource + ".vm");
                        if (appendedResourceFile.exists()) {
                            in = new FileInputStream(appendedResourceFile);
                            out = new FileOutputStream(f, true);
                            IOUtil.copy(in, out);
                            out.close();
                            out = null;
                            in.close();
                            in = null;
                        } else if (appendedVmResourceFile.exists()) {
                            reader = new FileReader(appendedVmResourceFile);
                            if (bundle.getSourceEncoding() == null) {
                                writer = new PrintWriter(new FileWriter(f, true));
                            } else {
                                writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f, true), bundle.getSourceEncoding()));
                            }
                            Velocity.init();
                            Velocity.evaluate(context, writer, "remote-resources", reader);
                            writer.close();
                            writer = null;
                            reader.close();
                            reader = null;
                        }
                    }
                }
            } finally {
                IOUtil.close(out);
                IOUtil.close(in);
                IOUtil.close(writer);
                IOUtil.close(reader);
            }
        // CHECKSTYLE_ON: LineLength
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Error finding remote resources manifests", e);
    } catch (XmlPullParserException e) {
        throw new MojoExecutionException("Error parsing remote resource bundle descriptor.", e);
    } catch (Exception e) {
        throw new MojoExecutionException("Error rendering velocity resource.", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) FileOutputStream(java.io.FileOutputStream) FileWriter(java.io.FileWriter) RemoteResourcesBundleXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Reader) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) SupplementalDataModelXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.SupplementalDataModelXpp3Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) FileReader(java.io.FileReader) IOException(java.io.IOException) URL(java.net.URL) FileInputStream(java.io.FileInputStream) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) ArtifactFilterException(org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) InvalidDependencyVersionException(org.apache.maven.project.artifact.InvalidDependencyVersionException) InvalidProjectModelException(org.apache.maven.project.InvalidProjectModelException) MalformedURLException(java.net.MalformedURLException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) RemoteResourcesBundleXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Reader) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) File(java.io.File) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter) PrintWriter(java.io.PrintWriter)

Example 3 with DeferredFileOutputStream

use of org.apache.commons.io.output.DeferredFileOutputStream in project maven-plugins by apache.

the class ProcessRemoteResourcesMojo method fileWriteIfDiffers.

/**
     * If the transformation result fits in memory and the destination file already exists
     * then both are compared.
     * <p>If destination file is byte-by-byte equal, then it is not overwritten.
     * This improves subsequent compilation times since upstream plugins property see that
     * the resource was not modified.
     * <p>Note: the method should be called after {@link org.apache.commons.io.output.DeferredFileOutputStream#close}
     *
     * @param outStream Deferred stream
     * @throws IOException
     */
private void fileWriteIfDiffers(DeferredFileOutputStream outStream) throws IOException {
    File file = outStream.getFile();
    if (outStream.isThresholdExceeded()) {
        getLog().info("File " + file + " was overwritten due to content limit threshold " + outStream.getThreshold() + " reached");
        return;
    }
    boolean needOverwrite = true;
    if (file.exists()) {
        InputStream is = null;
        try {
            is = new FileInputStream(file);
            final InputStream newContents = new ByteArrayInputStream(outStream.getData());
            needOverwrite = !IOUtil.contentEquals(is, newContents);
            if (getLog().isDebugEnabled()) {
                getLog().debug("File " + file + " contents " + (needOverwrite ? "differs" : "does not differ"));
            }
            is.close();
            is = null;
        } finally {
            IOUtil.close(is);
        }
    }
    if (!needOverwrite) {
        getLog().info("File " + file + " is up to date");
        return;
    }
    getLog().info("Writing " + file);
    OutputStream os = new FileOutputStream(file);
    try {
        outStream.writeTo(os);
        os.close();
        os = null;
    } finally {
        IOUtil.close(os);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) FileOutputStream(java.io.FileOutputStream) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with DeferredFileOutputStream

use of org.apache.commons.io.output.DeferredFileOutputStream in project maven-plugins by apache.

the class ProcessRemoteResourcesMojo method copyResourceIfExists.

protected boolean copyResourceIfExists(File file, String relFileName, VelocityContext context) throws IOException, MojoExecutionException {
    for (Resource resource : resources) {
        File resourceDirectory = new File(resource.getDirectory());
        if (!resourceDirectory.exists()) {
            continue;
        }
        // TODO - really should use the resource includes/excludes and name mapping
        File source = new File(resourceDirectory, relFileName);
        File templateSource = new File(resourceDirectory, relFileName + TEMPLATE_SUFFIX);
        if (!source.exists() && templateSource.exists()) {
            source = templateSource;
        }
        if (source.exists() && !source.equals(file)) {
            if (source == templateSource) {
                Reader reader = null;
                Writer writer = null;
                DeferredFileOutputStream os = new DeferredFileOutputStream(velocityFilterInMemoryThreshold, file);
                try {
                    if (encoding != null) {
                        reader = new InputStreamReader(new FileInputStream(source), encoding);
                        writer = new OutputStreamWriter(os, encoding);
                    } else {
                        reader = ReaderFactory.newPlatformReader(source);
                        writer = WriterFactory.newPlatformWriter(os);
                    }
                    velocity.evaluate(context, writer, "", reader);
                    writer.close();
                    writer = null;
                    reader.close();
                    reader = null;
                } catch (ParseErrorException e) {
                    throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
                } catch (MethodInvocationException e) {
                    throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
                } catch (ResourceNotFoundException e) {
                    throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
                } finally {
                    IOUtil.close(writer);
                    IOUtil.close(reader);
                }
                fileWriteIfDiffers(os);
            } else if (resource.isFiltering()) {
                MavenFileFilterRequest req = setupRequest(resource, source, file);
                try {
                    fileFilter.copyFile(req);
                } catch (MavenFilteringException e) {
                    throw new MojoExecutionException("Error filtering resource: " + source, e);
                }
            } else {
                FileUtils.copyFile(source, file);
            }
            // exclude the original (so eclipse doesn't complain about duplicate resources)
            resource.addExclude(relFileName);
            return true;
        }
    }
    return false;
}
Also used : InputStreamReader(java.io.InputStreamReader) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) Resource(org.apache.maven.model.Resource) RemoteResourcesBundleXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Reader) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) SupplementalDataModelXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.SupplementalDataModelXpp3Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) FileReader(java.io.FileReader) FileInputStream(java.io.FileInputStream) MavenFileFilterRequest(org.apache.maven.shared.filtering.MavenFileFilterRequest) OutputStreamWriter(java.io.OutputStreamWriter) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter)

Aggregations

File (java.io.File)4 DeferredFileOutputStream (org.apache.commons.io.output.DeferredFileOutputStream)4 FileInputStream (java.io.FileInputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileOutputStream (java.io.FileOutputStream)2 FileReader (java.io.FileReader)2 FileWriter (java.io.FileWriter)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 OutputStream (java.io.OutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 Writer (java.io.Writer)2 MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 RemoteResourcesBundleXpp3Reader (org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Reader)2 SupplementalDataModelXpp3Reader (org.apache.maven.plugin.resources.remote.io.xpp3.SupplementalDataModelXpp3Reader)2 MavenFilteringException (org.apache.maven.shared.filtering.MavenFilteringException)2