Permalink
Browse files

fixes image resolution so that you can use the <g:img> with normal UR…

…Is in a plugin that then adapt when installed into the application
  • Loading branch information...
1 parent 0022d46 commit 531cb76a0a2f6694c18dd7c0b7bf154636f58872 Graeme Rocher committed Jul 14, 2011
@@ -45,18 +45,18 @@
*
*/
public class DefaultResourceLocator implements ResourceLocator, ResourceLoaderAware, PluginManagerAware{
- private static final Resource NULL_RESOURCE = new ByteArrayResource("null".getBytes());
+ protected static final Resource NULL_RESOURCE = new ByteArrayResource("null".getBytes());
public static final String WILDCARD = "*";
public static final String FILE_SEPARATOR = File.separator;
public static final String CLOSURE_MARKER = "$";
public static final String WEB_APP_DIR = "web-app";
- private PathMatchingResourcePatternResolver patchMatchingResolver;
- private List<String> classSearchDirectories = new ArrayList<String>();
- private List<String> resourceSearchDirectories = new ArrayList<String>();
- private Map<String, Resource> classNameToResourceCache = new ConcurrentHashMap<String, Resource>();
- private Map<String, Resource> uriToResourceCache = new ConcurrentHashMap<String, Resource>();
- private ResourceLoader defaultResourceLoader = new FileSystemResourceLoader();
- private GrailsPluginManager pluginManager;
+ protected PathMatchingResourcePatternResolver patchMatchingResolver;
+ protected List<String> classSearchDirectories = new ArrayList<String>();
+ protected List<String> resourceSearchDirectories = new ArrayList<String>();
+ protected Map<String, Resource> classNameToResourceCache = new ConcurrentHashMap<String, Resource>();
+ protected Map<String, Resource> uriToResourceCache = new ConcurrentHashMap<String, Resource>();
+ protected ResourceLoader defaultResourceLoader = new FileSystemResourceLoader();
+ protected GrailsPluginManager pluginManager;
public void setSearchLocation(String searchLocation) {
ResourceLoader resourceLoader = getDefaultResourceLoader();
@@ -96,7 +96,7 @@ class CoreGrailsPlugin {
if (devMode) {
shutdownHook(DevelopmentShutdownHook)
}
- grailsResourceLocator(DefaultResourceLocator) {
+ abstractGrailsResourceLocator {
if(devMode) {
BuildSettings settings = BuildSettingsHolder.settings
if(settings) {
@@ -107,6 +107,9 @@ class CoreGrailsPlugin {
}
}
+ grailsResourceLocator(DefaultResourceLocator) { bean ->
+ bean.parent = "abstractGrailsResourceLocator"
+ }
customEditors(CustomEditorConfigurer) {
customEditors = [(Class.name): ClassEditor.name]
@@ -43,6 +43,8 @@ import org.codehaus.groovy.grails.web.pages.discovery.CachingGroovyPageLocator
import org.codehaus.groovy.grails.web.pages.discovery.GrailsConventionGroovyPageLocator
import org.codehaus.groovy.grails.web.pages.discovery.CachingGrailsConventionGroovyPageLocator
import org.codehaus.groovy.grails.web.sitemesh.GroovyPageLayoutFinder
+import org.codehaus.groovy.grails.web.pages.discovery.GroovyPageStaticResourceLocator
+import org.codehaus.groovy.grails.web.pages.discovery.CachingGroovyPageStaticResourceLocator
/**
* A Plugin that sets up and configures the GSP and GSP tag library support in Grails.
@@ -148,6 +150,9 @@ class GroovyPagesGrailsPlugin {
}
}
+ grailsResourceLocator(deployed ? CachingGroovyPageStaticResourceLocator : GroovyPageStaticResourceLocator) { bean ->
+ bean.parent = "abstractGrailsResourceLocator"
+ }
// Setup the main templateEngine used to render GSPs
groovyPagesTemplateEngine(GroovyPagesTemplateEngine) { bean ->
classLoader = ref("classLoader")
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2011 SpringSource
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.codehaus.groovy.grails.web.pages.discovery;
+
+import org.springframework.core.io.Resource;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Extends {@link GroovyPageStaticResourceLocator} adding caching of the result of {@link GroovyPageStaticResourceLocator#findResourceForURI(String)}
+ *
+ * @author Graeme Rocher
+ * @since 2.0
+ */
+public class CachingGroovyPageStaticResourceLocator extends GroovyPageStaticResourceLocator{
+
+ private Map<String, Resource> uriResolveCache = new ConcurrentHashMap<String, Resource>();
+
+ @Override
+ public Resource findResourceForURI(String uri) {
+ Resource resource = uriResolveCache.get(uri);
+ if(resource == null) {
+ resource = super.findResourceForURI(uri);
+ if(resource == null) {
+ resource = NULL_RESOURCE;
+ }
+ uriResolveCache.put(uri, resource);
+ }
+ return resource == NULL_RESOURCE ? null : resource;
+ }
+}
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2011 SpringSource
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.codehaus.groovy.grails.web.pages.discovery;
+
+import org.codehaus.groovy.grails.core.io.DefaultResourceLocator;
+import org.codehaus.groovy.grails.io.support.GrailsResourceUtils;
+import org.codehaus.groovy.grails.plugins.GrailsPlugin;
+import org.codehaus.groovy.grails.web.pages.GroovyPageBinding;
+import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes;
+import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest;
+import org.springframework.core.io.Resource;
+import org.springframework.web.context.request.RequestAttributes;
+
+/**
+ * Extends the {@link DefaultResourceLocator} class with extra methods to evaluate static resources relative to the currently executing GSP page.
+ * <p/>
+ * This class is used to resolve references to static resources like CSS, Javascript and images files
+ *
+ * @author Graeme Rocher
+ * @since 2.0
+ */
+public class GroovyPageStaticResourceLocator extends DefaultResourceLocator {
+
+ @Override
+ public Resource findResourceForURI(String uri) {
+ Resource resource = super.findResourceForURI(uri);
+ if (resource == null) {
+ GroovyPageBinding binding = findBindingInWebRequest();
+ if (binding != null) {
+ GrailsPlugin pagePlugin = binding.getPagePlugin();
+ if (pagePlugin != null && pluginManager != null) {
+ resource = findResourceForPlugin(pagePlugin, uri);
+ }
+ }
+ else if(pluginManager != null) {
+ // attempt brut force search of all plugins
+ GrailsPlugin[] allPlugins = pluginManager.getAllPlugins();
+ for (GrailsPlugin plugin : allPlugins) {
+ resource = findResourceForPlugin(plugin, uri);
+ if(resource != null) break;
+ }
+ }
+ }
+ return resource;
+
+ }
+
+ private Resource findResourceForPlugin(GrailsPlugin plugin, String uri) {
+ Resource resource;
+ String pluginPath = pluginManager.getPluginPath(plugin.getName());
+ String pluginUri = GrailsResourceUtils.appendPiecesForUri(pluginPath, uri);
+ resource = super.findResourceForURI(pluginUri);
+ return resource;
+ }
+
+ private GroovyPageBinding findBindingInWebRequest() {
+ GrailsWebRequest webRequest = GrailsWebRequest.lookup();
+ if (webRequest != null) {
+ return (GroovyPageBinding) webRequest.getAttribute(GrailsApplicationAttributes.PAGE_SCOPE, RequestAttributes.SCOPE_REQUEST);
+ }
+ return null;
+ }
+
+}

0 comments on commit 531cb76

Please sign in to comment.