1 /* Copyright 2004-2005 the original author or authors.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 package org
.codehaus
.groovy
.grails
.compiler
.support
;
17 import groovy
.lang
.GroovyResourceLoader
;
18 import org
.codehaus
.groovy
.grails
.commons
.GrailsResourceUtils
;
19 import org
.codehaus
.groovy
.grails
.exceptions
.GrailsConfigurationException
;
20 import org
.springframework
.core
.io
.Resource
;
22 import java
.io
.IOException
;
23 import java
.net
.MalformedURLException
;
25 import java
.util
.ArrayList
;
26 import java
.util
.HashMap
;
27 import java
.util
.List
;
31 * A GroovyResourceLoader that loads groovy files using Spring's IO abstraction
33 * @author Graeme Rocher
36 * Created: 22-Feb-2006
38 public class GrailsResourceLoader
implements GroovyResourceLoader
{
39 private Resource
[] resources
;
40 private List loadedResources
= new ArrayList();
41 private Map classToResource
= new HashMap();
42 private Map pathToResource
= new HashMap();
44 public GrailsResourceLoader(Resource
[] resources
) {
45 this.resources
= resources
;
46 createPathToURLMappings(resources
);
49 private void createPathToURLMappings(Resource
[] resources
) {
50 for (int i
= 0; i
< resources
.length
; i
++) {
53 resourceURL
= resources
[i
].getURL().toString();
54 } catch (IOException e
) {
55 throw new GrailsConfigurationException("Unable to load Grails resource: " + e
.getMessage(), e
);
57 String pathWithinRoot
= GrailsResourceUtils
.getPathFromRoot(resourceURL
);
58 pathToResource
.put(pathWithinRoot
, resources
[i
]);
62 public List
getLoadedResources() {
63 return loadedResources
;
66 public void setResources(Resource
[] resources
) {
67 this.resources
= resources
;
68 createPathToURLMappings(resources
);
71 public Resource
[] getResources() {
75 public URL
loadGroovySource(String className
) throws MalformedURLException
{
76 if(className
== null) return null;
77 String groovyFile
= className
.replace('.', '/') + ".groovy";
81 Resource foundResource
= (Resource
)pathToResource
.get(groovyFile
);
82 if (foundResource
!= null) {
83 loadedResources
.add(foundResource
);
84 classToResource
.put(className
, foundResource
);
85 return foundResource
.getURL();
89 } catch (IOException e
) {
90 throw new GrailsConfigurationException("I/O exception loaded resource:" + e
.getMessage(),e
);
95 * Returns the Grails resource for the given class or null if it is not a Grails resource
97 * @param theClass The class
98 * @return The Resource or null
100 public Resource
getResourceForClass(Class theClass
) {
101 return (Resource
)classToResource
.get(theClass
.getName());