Author: nbelaevski
Date: 2010-12-14 18:50:50 -0500 (Tue, 14 Dec 2010)
New Revision: 20570
Removed:
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/CharResourceProcessor.java
Modified:
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/ProcessMojo.java
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/scan/impl/DynamicResourcesScanner.java
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/ResourceProcessor.java
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/CSSResourceProcessor.java
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/JavaScriptResourceProcessor.java
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/ResourceWriterImpl.java
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/ThroughputResourceProcessor.java
Log:
Bug fixing for maven-resources-plugin:
- Reflections switched to use single-threaded executor
- Added processing of ${project.build.sourceEncoding} for char-based resources
- Improved IOException handling in ResourceWriterImpl
- Removed classes annotated with @DynamicResource from scanning - only
@DynamicUserResource should be used
- Added warning message when generated output is being overwritten
Modified:
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/ProcessMojo.java
===================================================================
---
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/ProcessMojo.java 2010-12-14
19:48:23 UTC (rev 20569)
+++
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/ProcessMojo.java 2010-12-14
23:50:50 UTC (rev 20570)
@@ -31,6 +31,7 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
+import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -76,6 +77,7 @@
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
+import com.google.common.base.Strings;
import com.google.common.collect.Constraints;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
@@ -161,20 +163,34 @@
*/
private String webRoot;
+ /**
+ * @parameter expression="${encoding}"
default-value="${project.build.sourceEncoding}"
+ */
+ private String encoding;
+
//TODO handle resource locales
private Locale resourceLocales;
private Collection<ResourceKey> foundResources = Sets.newHashSet();
- private Collection<ResourceProcessor> resourceProcessors =
Arrays.<ResourceProcessor>asList(
- new JavaScriptResourceProcessor(getLog()),
- new CSSResourceProcessor());
-
// TODO executor parameters
private static ExecutorService createExecutorService() {
return Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
}
+ private Collection<ResourceProcessor> getDefaultResourceProcessors() {
+ Charset charset = Charset.defaultCharset();
+ if (!Strings.isNullOrEmpty(encoding)) {
+ charset = Charset.forName(encoding);
+ } else {
+ getLog().warn("Encoding is not set explicitly, CDK resources plugin will
use default platform encoding for processing char-based resources");
+ }
+
+ return Arrays.<ResourceProcessor>asList(
+ new JavaScriptResourceProcessor(charset, getLog()),
+ new CSSResourceProcessor(charset));
+ }
+
private Predicate<Resource> createResourcesFilter() {
Predicate<CharSequence> contentTypePredicate =
MorePredicates.compose(includedContentTypes,
excludedContentTypes, REGEX_CONTAINS_BUILDER_FUNCTION);
@@ -287,7 +303,7 @@
faces = new FacesImpl(null, new FileNameMapperImpl(fileNameMappings),
resourceHandler);
faces.start();
- ResourceWriterImpl resourceWriter = new ResourceWriterImpl(resourceOutputDir,
resourceMappingDir, resourceProcessors);
+ ResourceWriterImpl resourceWriter = new ResourceWriterImpl(resourceOutputDir,
resourceMappingDir, getDefaultResourceProcessors(), getLog());
ResourceTaskFactoryImpl taskFactory = new ResourceTaskFactoryImpl(faces);
taskFactory.setResourceWriter(resourceWriter);
Modified:
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/scan/impl/DynamicResourcesScanner.java
===================================================================
---
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/scan/impl/DynamicResourcesScanner.java 2010-12-14
19:48:23 UTC (rev 20569)
+++
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/scan/impl/DynamicResourcesScanner.java 2010-12-14
23:50:50 UTC (rev 20570)
@@ -35,7 +35,6 @@
import org.richfaces.cdk.resource.scan.impl.reflections.ReflectionsExt;
import org.richfaces.cdk.vfs.VFSRoot;
import org.richfaces.cdk.vfs.VFSType;
-import org.richfaces.resource.DynamicResource;
import org.richfaces.resource.DynamicUserResource;
import org.richfaces.resource.ResourceFactory;
import org.richfaces.resource.ResourceKey;
@@ -99,12 +98,11 @@
ConfigurationBuilder configurationBuilder = new
ConfigurationBuilder().setUrls(urls);
configurationBuilder.setScanners(new SubTypesScanner(), new
TypeAnnotationsScanner(),
- new MarkerResourcesScanner()).useParallelExecutor();
+ new MarkerResourcesScanner());
ReflectionsExt refl = new ReflectionsExt(configurationBuilder);
Collection<Class<?>> allClasses = Sets.newHashSet();
- addAnnotatedClasses(DynamicResource.class, refl, allClasses);
addAnnotatedClasses(DynamicUserResource.class, refl, allClasses);
allClasses.addAll(refl.getMarkedClasses());
Modified:
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/ResourceProcessor.java
===================================================================
---
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/ResourceProcessor.java 2010-12-14
19:48:23 UTC (rev 20569)
+++
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/ResourceProcessor.java 2010-12-14
23:50:50 UTC (rev 20570)
@@ -25,6 +25,9 @@
import java.io.InputStream;
import java.io.OutputStream;
+import com.google.common.io.InputSupplier;
+import com.google.common.io.OutputSupplier;
+
/**
* @author Nick Belaevski
*
@@ -33,6 +36,6 @@
public boolean isSupportedFile(String name);
- public void process(String resourceName, InputStream in, OutputStream out) throws
IOException;
+ public void process(String resourceName, InputSupplier<? extends InputStream>
in, OutputSupplier<? extends OutputStream> out) throws IOException;
}
Modified:
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/CSSResourceProcessor.java
===================================================================
---
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/CSSResourceProcessor.java 2010-12-14
19:48:23 UTC (rev 20569)
+++
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/CSSResourceProcessor.java 2010-12-14
23:50:50 UTC (rev 20570)
@@ -22,25 +22,54 @@
package org.richfaces.cdk.resource.writer.impl;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
+import java.nio.charset.Charset;
+import org.richfaces.cdk.resource.writer.ResourceProcessor;
+
+import com.google.common.io.Closeables;
+import com.google.common.io.InputSupplier;
+import com.google.common.io.OutputSupplier;
import com.yahoo.platform.yui.compressor.CssCompressor;
/**
* @author Nick Belaevski
*
*/
-public class CSSResourceProcessor extends CharResourceProcessor {
+public class CSSResourceProcessor implements ResourceProcessor {
+ private Charset charset;
+
+ public CSSResourceProcessor(Charset charset) {
+ this.charset = charset;
+ }
+
@Override
public boolean isSupportedFile(String name) {
return name.endsWith(".css");
}
@Override
- protected void doActualProcess(String resourceName, Reader in, Writer out) throws
IOException {
- new CssCompressor(in).compress(out, 0);
+ public void process(String resourceName, InputSupplier<? extends InputStream>
in,
+ OutputSupplier<? extends OutputStream> out) throws IOException {
+
+ Reader reader = null;
+ Writer writer = null;
+
+ try {
+ reader = new InputStreamReader(in.getInput(), charset);
+ writer = new OutputStreamWriter(out.getOutput(), charset);
+
+ new CssCompressor(reader).compress(writer, 0);
+ } finally {
+ Closeables.closeQuietly(reader);
+ Closeables.closeQuietly(writer);
+ }
}
}
Deleted:
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/CharResourceProcessor.java
===================================================================
---
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/CharResourceProcessor.java 2010-12-14
19:48:23 UTC (rev 20569)
+++
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/CharResourceProcessor.java 2010-12-14
23:50:50 UTC (rev 20570)
@@ -1,62 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.cdk.resource.writer.impl;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.Reader;
-import java.io.Writer;
-
-import org.richfaces.cdk.resource.writer.ResourceProcessor;
-
-import com.google.common.io.Closeables;
-
-/**
- * @author Nick Belaevski
- *
- */
-public abstract class CharResourceProcessor implements ResourceProcessor {
-
- protected abstract void doActualProcess(String resourceName, Reader in, Writer out)
throws IOException;
-
- @Override
- public void process(String resourceName, InputStream in, OutputStream out) throws
IOException {
-
- boolean threw = true;
- Reader reader = new InputStreamReader(in);
- try {
- Writer writer = new OutputStreamWriter(out);
- try {
- doActualProcess(resourceName, reader, writer);
- threw = false;
- } finally {
- Closeables.close(writer, threw);
- }
- } finally {
- Closeables.close(reader, threw);
- }
- }
-
-}
Modified:
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/JavaScriptResourceProcessor.java
===================================================================
---
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/JavaScriptResourceProcessor.java 2010-12-14
19:48:23 UTC (rev 20569)
+++
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/JavaScriptResourceProcessor.java 2010-12-14
23:50:50 UTC (rev 20570)
@@ -22,23 +22,34 @@
package org.richfaces.cdk.resource.writer.impl;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
+import java.nio.charset.Charset;
import org.apache.maven.plugin.logging.Log;
+import org.richfaces.cdk.resource.writer.ResourceProcessor;
+import com.google.common.io.Closeables;
+import com.google.common.io.InputSupplier;
+import com.google.common.io.OutputSupplier;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
/**
* @author Nick Belaevski
*
*/
-public class JavaScriptResourceProcessor extends CharResourceProcessor {
+public class JavaScriptResourceProcessor implements ResourceProcessor {
+ private Charset charset;
+
private Log log;
- public JavaScriptResourceProcessor(Log log) {
- super();
+ public JavaScriptResourceProcessor(Charset charset, Log log) {
+ this.charset = charset;
this.log = log;
}
@@ -48,17 +59,30 @@
}
@Override
- protected void doActualProcess(String resourceName, Reader in, Writer out) throws
IOException {
- MavenLogErrorReporter reporter = new MavenLogErrorReporter(resourceName);
- new JavaScriptCompressor(in, reporter).compress(out, 0, true, true, false,
false);
+ public void process(String resourceName, InputSupplier<? extends InputStream>
in,
+ OutputSupplier<? extends OutputStream> out) throws IOException {
+
+ Reader reader = null;
+ Writer writer = null;
- if (reporter.hasErrors() && log.isErrorEnabled()) {
- log.error(reporter.getErrorsLog());
+ try {
+ reader = new InputStreamReader(in.getInput(), charset);
+ writer = new OutputStreamWriter(out.getOutput(), charset);
+
+ MavenLogErrorReporter reporter = new MavenLogErrorReporter(resourceName);
+ new JavaScriptCompressor(reader, reporter).compress(writer, 0, true, true,
false, false);
+
+ if (reporter.hasErrors() && log.isErrorEnabled()) {
+ log.error(reporter.getErrorsLog());
+ }
+
+ if (reporter.hasWarnings() && log.isDebugEnabled()) {
+ log.debug(reporter.getWarningsLog());
+ }
+ } finally {
+ Closeables.closeQuietly(reader);
+ Closeables.closeQuietly(writer);
}
-
- if (reporter.hasWarnings() && log.isDebugEnabled()) {
- log.debug(reporter.getWarningsLog());
- }
}
}
Modified:
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/ResourceWriterImpl.java
===================================================================
---
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/ResourceWriterImpl.java 2010-12-14
19:48:23 UTC (rev 20569)
+++
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/ResourceWriterImpl.java 2010-12-14
23:50:50 UTC (rev 20570)
@@ -26,19 +26,25 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.text.MessageFormat;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import javax.faces.application.Resource;
+import org.apache.maven.plugin.logging.Log;
import org.richfaces.cdk.ResourceWriter;
import org.richfaces.cdk.resource.writer.ResourceProcessor;
import org.richfaces.cdk.strings.Constants;
import org.richfaces.resource.ResourceFactory;
+import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
+import com.google.common.io.Files;
+import com.google.common.io.InputSupplier;
/**
* @author Nick Belaevski
@@ -46,6 +52,22 @@
*/
public class ResourceWriterImpl implements ResourceWriter {
+ private static final class ResourceInputStreamSupplier implements
InputSupplier<InputStream> {
+
+ private Resource resource;
+
+ public ResourceInputStreamSupplier(Resource resource) {
+ super();
+ this.resource = resource;
+ }
+
+ @Override
+ public InputStream getInput() throws IOException {
+ return resource.getInputStream();
+ }
+
+ }
+
private File resourceContentsDir;
private File resourceMappingDir;
@@ -53,43 +75,65 @@
private Map<String, String> processedResources = Maps.newConcurrentMap();
private Iterable<ResourceProcessor> resourceProcessors;
+
+ private Log log;
- public ResourceWriterImpl(File resourceContentsDir, File resourceMappingDir,
Iterable<ResourceProcessor> resourceProcessors) {
+ private long currentTime;
+
+ public ResourceWriterImpl(File resourceContentsDir, File resourceMappingDir,
Iterable<ResourceProcessor> resourceProcessors, Log log) {
this.resourceContentsDir = resourceContentsDir;
this.resourceMappingDir = resourceMappingDir;
this.resourceProcessors = Iterables.concat(resourceProcessors,
Collections.singleton(ThroughputResourceProcessor.INSTANCE));
+ this.log = log;
+
resourceContentsDir.mkdirs();
+
+ currentTime = System.currentTimeMillis();
}
private String getResourceQualifier(Resource resource) {
return COLON_JOINER.join(resource.getLibraryName(), resource.getResourceName());
}
- private File createOutputFile(String path) throws IOException {
+ private synchronized File createOutputFile(String path) throws IOException {
File outFile = new File(resourceContentsDir, path);
outFile.getParentFile().mkdirs();
- outFile.createNewFile();
+
+ if (outFile.exists()) {
+ if (outFile.lastModified() > currentTime) {
+ log.warn(MessageFormat.format("File {0} already exists and will be
overwritten", outFile.getPath()));
+ }
+ outFile.delete();
+ }
+ if (!outFile.createNewFile()) {
+ log.warn(MessageFormat.format("Could not create {0} file",
outFile.getPath()));
+ }
+
+
return outFile;
}
public void writeResource(String skinName, Resource resource) throws IOException {
- String requestPath = resource.getRequestPath();
+ final String requestPath = resource.getRequestPath();
String requestPathWithSkin = requestPath;
if (requestPath.startsWith(ResourceFactory.SKINNED_RESOURCE_PREFIX)) {
requestPathWithSkin = Constants.SLASH_JOINER.join(skinName,
requestPath.substring(ResourceFactory.SKINNED_RESOURCE_PREFIX.length()));
}
+
+ ResourceProcessor matchingProcessor =
Iterables.get(Iterables.filter(resourceProcessors, new
Predicate<ResourceProcessor>() {
+ @Override
+ public boolean apply(ResourceProcessor input) {
+ return input.isSupportedFile(requestPath);
+ }
+ }), 0);
- for (ResourceProcessor resourceProcessor : resourceProcessors) {
- if (resourceProcessor.isSupportedFile(requestPath)) {
- File outFile = createOutputFile(requestPathWithSkin);
- resourceProcessor.process(requestPathWithSkin, resource.getInputStream(),
new FileOutputStream(outFile));
- processedResources.put(getResourceQualifier(resource), requestPath);
- return;
- }
- }
+ File outFile = createOutputFile(requestPathWithSkin);
+
+ matchingProcessor.process(requestPathWithSkin, new
ResourceInputStreamSupplier(resource), Files.newOutputStreamSupplier(outFile));
+ processedResources.put(getResourceQualifier(resource), requestPath);
}
@Override
Modified:
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/ThroughputResourceProcessor.java
===================================================================
---
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/ThroughputResourceProcessor.java 2010-12-14
19:48:23 UTC (rev 20569)
+++
trunk/cdk/maven-resources-plugin/src/main/java/org/richfaces/cdk/resource/writer/impl/ThroughputResourceProcessor.java 2010-12-14
23:50:50 UTC (rev 20570)
@@ -28,7 +28,8 @@
import org.richfaces.cdk.resource.writer.ResourceProcessor;
import com.google.common.io.ByteStreams;
-import com.google.common.io.Closeables;
+import com.google.common.io.InputSupplier;
+import com.google.common.io.OutputSupplier;
/**
* @author Nick Belaevski
@@ -46,13 +47,10 @@
}
@Override
- public void process(String resourceName, InputStream in, OutputStream out) throws
IOException {
- try {
- ByteStreams.copy(in, out);
- } finally {
- Closeables.close(in, true);
- Closeables.close(out, true);
- }
- }
+ public void process(String resourceName, InputSupplier<? extends InputStream>
in,
+ OutputSupplier<? extends OutputStream> out) throws IOException {
+ ByteStreams.copy(in, out);
+ }
+
}