Author: nbelaevski
Date: 2010-07-23 15:04:24 -0400 (Fri, 23 Jul 2010)
New Revision: 18223
Added:
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/naming/
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/resource/MarkerResourcesScanner.java
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/resource/ReflectionsExt.java
Removed:
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/naming/
Modified:
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/Main.java
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/ExternalContextImpl.java
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/FacesContextImpl.java
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/FacesImpl.java
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/naming/FileNameMapperImpl.java
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/resource/ResourcesScannerImpl.java
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/task/ResourceTaskFactoryImpl.java
Log:
Marker resources discovery
Resources-related methods in ExternalContextImpl
Modified:
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/Main.java
===================================================================
---
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/Main.java 2010-07-23
17:49:26 UTC (rev 18222)
+++
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/Main.java 2010-07-23
19:04:24 UTC (rev 18223)
@@ -34,7 +34,7 @@
import javax.faces.application.Resource;
-import org.richfaces.cdk.faces.naming.FileNameMapperImpl;
+import org.richfaces.cdk.naming.FileNameMapperImpl;
import org.richfaces.cdk.resource.ResourceFactoryImpl;
import org.richfaces.cdk.resource.ResourceWriterImpl;
import org.richfaces.cdk.resource.ResourcesScannerImpl;
@@ -64,7 +64,9 @@
transform(resourcesScanner.getClassResources(),
resourceFactory.getClassResourceFactory()),
transform(resourcesScanner.getURLResources(),
resourceFactory.getURLResourceFactory())), notNull());
- ResourceTaskFactory taskFactory = new ResourceTaskFactoryImpl(new
ResourceWriterImpl(dstDir, new FileNameMapperImpl()));
+ ResourceWriterImpl resourceWriter = new ResourceWriterImpl(dstDir, new
FileNameMapperImpl());
+ //TODO set webroot
+ ResourceTaskFactory taskFactory = new ResourceTaskFactoryImpl(resourceWriter,
null);
taskFactory.setResources(resources);
taskFactory.setSkins(Lists.newArrayList("blueSky", "ruby",
"classic"));
Modified:
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/ExternalContextImpl.java
===================================================================
---
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/ExternalContextImpl.java 2010-07-23
17:49:26 UTC (rev 18222)
+++
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/ExternalContextImpl.java 2010-07-23
19:04:24 UTC (rev 18223)
@@ -21,6 +21,9 @@
*/
package org.richfaces.cdk.faces;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
@@ -34,13 +37,21 @@
import javax.faces.context.ExternalContext;
+import com.google.common.collect.Sets;
+
/**
* @author Nick Belaevski
*
*/
public class ExternalContextImpl extends ExternalContext {
+ private String webRoot;
+
private Map<String, String> initParamsMap = new HashMap<String,
String>();
+
+ protected void setWebRoot(String webroot) {
+ this.webRoot = webroot;
+ }
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#dispatch(java.lang.String)
@@ -254,8 +265,7 @@
*/
@Override
public URL getResource(String path) throws MalformedURLException {
- // TODO Auto-generated method stub
- return null;
+ return new URL("file:" + webRoot + path);
}
/* (non-Javadoc)
@@ -263,17 +273,38 @@
*/
@Override
public InputStream getResourceAsStream(String path) {
- // TODO Auto-generated method stub
+ File file = new File(webRoot, path);
+ if (file.exists()) {
+ try {
+ return new FileInputStream(file);
+ } catch (FileNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
return null;
}
- /* (non-Javadoc)
- * @see javax.faces.context.ExternalContext#getResourcePaths(java.lang.String)
- */
@Override
public Set<String> getResourcePaths(String path) {
- // TODO Auto-generated method stub
- return null;
+ File root = new File(webRoot, path);
+
+ Set<String> result = Sets.newHashSet();
+
+ if (root.exists()) {
+ String[] list = root.list();
+ for (String childName : list) {
+ File child = new File(root, childName);
+ if (child.isDirectory()) {
+ childName += '/';
+ }
+
+ result.add(path + '/' + childName);
+ }
+ }
+
+ return result;
}
/* (non-Javadoc)
Modified:
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/FacesContextImpl.java
===================================================================
---
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/FacesContextImpl.java 2010-07-23
17:49:26 UTC (rev 18222)
+++
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/FacesContextImpl.java 2010-07-23
19:04:24 UTC (rev 18223)
@@ -30,7 +30,6 @@
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.component.UIViewRoot;
-import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseStream;
import javax.faces.context.ResponseWriter;
@@ -46,7 +45,7 @@
private ELContext elContext = new ELContextImpl();
- private ExternalContext externalContext = new ExternalContextImpl();
+ private ExternalContextImpl externalContext = new ExternalContextImpl();
public FacesContextImpl() {
setCurrentInstance(this);
@@ -74,7 +73,7 @@
* @see javax.faces.context.FacesContext#getExternalContext()
*/
@Override
- public ExternalContext getExternalContext() {
+ public ExternalContextImpl getExternalContext() {
// TODO Auto-generated method stub
return externalContext;
}
Modified:
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/FacesImpl.java
===================================================================
---
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/FacesImpl.java 2010-07-23
17:49:26 UTC (rev 18222)
+++
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/FacesImpl.java 2010-07-23
19:04:24 UTC (rev 18223)
@@ -41,6 +41,13 @@
*/
public class FacesImpl implements Faces {
+ private String webroot;
+
+ public FacesImpl(String webroot) {
+ super();
+ this.webroot = webroot;
+ }
+
public void start() {
final SkinFactoryImpl skinFactory = new SkinFactoryImpl();
@@ -71,7 +78,8 @@
}
public void startRequest() {
- new FacesContextImpl();
+ FacesContextImpl facesContextImpl = new FacesContextImpl();
+ facesContextImpl.getExternalContext().setWebRoot(webroot);
assert FacesContext.getCurrentInstance() != null;
}
Copied:
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/naming
(from rev 18222,
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/naming)
Modified:
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/naming/FileNameMapperImpl.java
===================================================================
---
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/faces/naming/FileNameMapperImpl.java 2010-07-23
17:49:26 UTC (rev 18222)
+++
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/naming/FileNameMapperImpl.java 2010-07-23
19:04:24 UTC (rev 18223)
@@ -19,7 +19,7 @@
* 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.faces.naming;
+package org.richfaces.cdk.naming;
import org.richfaces.cdk.FileNameMapper;
Added:
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/resource/MarkerResourcesScanner.java
===================================================================
---
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/resource/MarkerResourcesScanner.java
(rev 0)
+++
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/resource/MarkerResourcesScanner.java 2010-07-23
19:04:24 UTC (rev 18223)
@@ -0,0 +1,61 @@
+/*
+ * 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;
+
+import org.reflections.scanners.AbstractScanner;
+import org.reflections.vfs.Vfs.File;
+
+import com.google.common.collect.Multimap;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class MarkerResourcesScanner extends AbstractScanner {
+
+ private static final String RESOURCE_PROPERTIES_EXT =
".resource.properties";
+
+ private static final String META_INF = "META-INF/";
+
+ static final String STORE_KEY = "org.richfaces.cdk.dynamicResourceNames";
+
+ @Override
+ public void scan(Object cls) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void scan(File file) {
+ String relativePath = file.getRelativePath();
+ if (relativePath.startsWith(META_INF) &&
relativePath.endsWith(RESOURCE_PROPERTIES_EXT)) {
+ Multimap<String, String> store = getStore();
+
+ String className = relativePath.substring(META_INF.length(),
relativePath.length() - RESOURCE_PROPERTIES_EXT.length());
+ store.put(STORE_KEY, className);
+ }
+ }
+
+ @Override
+ public boolean acceptsInput(String file) {
+ return true;
+ }
+}
Added:
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/resource/ReflectionsExt.java
===================================================================
---
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/resource/ReflectionsExt.java
(rev 0)
+++
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/resource/ReflectionsExt.java 2010-07-23
19:04:24 UTC (rev 18223)
@@ -0,0 +1,61 @@
+/*
+ * 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;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+
+import org.reflections.Configuration;
+import org.reflections.Reflections;
+import org.reflections.scanners.Scanner;
+import org.reflections.util.Utils;
+
+import com.google.common.collect.Multimap;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class ReflectionsExt extends Reflections {
+
+ ReflectionsExt() {
+ super();
+ }
+
+ ReflectionsExt(Configuration configuration) {
+ super(configuration);
+ }
+
+ ReflectionsExt(String prefix, Scanner... scanners) {
+ super(prefix, scanners);
+ }
+
+ public Collection<Class<?>> getMarkedClasses() {
+ Map<String, Multimap<String, String>> storeMap =
getStore().getStoreMap();
+ Multimap<String, String> scannerMMap =
storeMap.get(MarkerResourcesScanner.class.getName());
+ if (scannerMMap == null) {
+ return Collections.emptySet();
+ }
+ return Utils.forNames(scannerMMap.get(MarkerResourcesScanner.STORE_KEY));
+ }
+}
Modified:
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/resource/ResourcesScannerImpl.java
===================================================================
---
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/resource/ResourcesScannerImpl.java 2010-07-23
17:49:26 UTC (rev 18222)
+++
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/resource/ResourcesScannerImpl.java 2010-07-23
19:04:24 UTC (rev 18223)
@@ -25,7 +25,6 @@
import java.util.Collection;
import java.util.Set;
-import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
@@ -53,13 +52,13 @@
public boolean apply(File input) {
return
input.getRelativePath().endsWith("META-INF/faces-config.xml");
}
-
+
};
-
+
private static boolean containsFacesConfig(Dir dir) {
return Iterators.any(dir.getFiles().iterator(), IS_FACES_CONFIG);
}
-
+
private static final Predicate<URL> FACES_URL = new Predicate<URL>() {
public boolean apply(URL input) {
@@ -71,23 +70,28 @@
}
}
};
-
+
private Collection<Class<?>> classResources = Sets.newHashSet();
-
+
private Collection<URL> urlResources = Sets.newHashSet();
public void scan() {
Collection<URL> urls =
Collections2.filter(ClasspathHelper.getUrlsForCurrentClasspath(), FACES_URL);
-
- Reflections refl = new Reflections(new
ConfigurationBuilder().setUrls(urls).setScanners(new SubTypesScanner(), new
TypeAnnotationsScanner()));
-
+
+ ConfigurationBuilder configurationBuilder = new
ConfigurationBuilder().setUrls(urls);
+ configurationBuilder.setScanners(new SubTypesScanner(), new
TypeAnnotationsScanner(), new MarkerResourcesScanner());
+
+ ReflectionsExt refl = new ReflectionsExt(configurationBuilder);
+
Set<Class<?>> resources =
refl.getTypesAnnotatedWith(DynamicResource.class);
-
- //TODO - reflections doesn't handle @Inherited correctly
+
+ // TODO - reflections doesn't handle @Inherited correctly
for (Class<?> resourceClass : resources) {
classResources.add(resourceClass);
classResources.addAll(refl.getSubTypesOf(resourceClass));
}
+
+ classResources.addAll(refl.getMarkedClasses());
}
public Collection<Class<?>> getClassResources() {
Modified:
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/task/ResourceTaskFactoryImpl.java
===================================================================
---
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/task/ResourceTaskFactoryImpl.java 2010-07-23
17:49:26 UTC (rev 18222)
+++
root/cdk-sandbox/trunk/dynamic-resources-prerenderer/src/main/java/org/richfaces/cdk/task/ResourceTaskFactoryImpl.java 2010-07-23
19:04:24 UTC (rev 18223)
@@ -86,7 +86,7 @@
public Object call() throws Exception {
try {
- faces = new FacesImpl();
+ faces = new FacesImpl(webRoot);
faces.start();
for (Resource resource : resources) {
@@ -104,15 +104,18 @@
}
+ private String webRoot;
+
private ResourceWriter resourceWriter;
private Iterable<Resource> resources = Collections.emptySet();
private Iterable<String> skins = Collections.emptySet();
- public ResourceTaskFactoryImpl(ResourceWriter resourceWriter) {
+ public ResourceTaskFactoryImpl(ResourceWriter resourceWriter, String webRoot) {
super();
this.resourceWriter = resourceWriter;
+ this.webRoot = webRoot;
}
public void setResources(Iterable<Resource> resources) {