2 * Copyright 2004-2007 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.codehaus
.groovy
.grails
.plugins
;
19 import org
.apache
.commons
.logging
.Log
;
20 import org
.apache
.commons
.logging
.LogFactory
;
21 import org
.codehaus
.groovy
.grails
.commons
.GrailsApplication
;
22 import org
.codehaus
.groovy
.grails
.plugins
.exceptions
.PluginException
;
23 import org
.springframework
.core
.io
.Resource
;
24 import org
.springframework
.core
.io
.support
.PathMatchingResourcePatternResolver
;
26 import java
.io
.IOException
;
27 import java
.util
.HashSet
;
31 * Class with responsibility for loading core plugin classes.
32 * Contains functionality moved in from <code>DefaultGrailsPluginManager</code>
33 * @author Graeme Rocher
36 public class CorePluginFinder
{
38 //This class contains functionality originally found in
39 //DefaultGrailsPluginManager, but moved after 0.5.6
41 private static final Log LOG
= LogFactory
.getLog(CorePluginFinder
.class);
43 private final PathMatchingResourcePatternResolver resolver
;
45 private final GrailsApplication application
;
47 private final Set foundPluginClasses
;
49 public CorePluginFinder(GrailsApplication application
) {
51 this.resolver
= new PathMatchingResourcePatternResolver();
52 this.application
= application
;
53 this.foundPluginClasses
= new HashSet();
56 public Set
getPluginClasses() {
58 // just in case we try to use this twice
59 foundPluginClasses
.clear();
62 Resource
[] resources
= resolver
63 .getResources("classpath*:org/codehaus/groovy/grails/**/plugins/**/*GrailsPlugin.class");
64 if (resources
.length
> 0) {
65 loadCorePluginsFromResources(resources
);
67 LOG
.warn("WARNING: Grails was unable to load core plugins dynamically. This is normally a problem with the container class loader configuration, see troubleshooting and FAQ for more info. ");
68 loadCorePluginsStatically();
70 } catch (IOException e
) {
71 throw new PluginException(
72 "I/O exception configuring core plug-ins: "
75 return foundPluginClasses
;
78 private void loadCorePluginsStatically() {
80 // This is a horrible hard coded hack, but there seems to be no way to
81 // resolve .class files dynamically
82 // on OC4J. If anyones knows how to fix this shout
83 loadCorePlugin("org.codehaus.groovy.grails.plugins.CoreGrailsPlugin");
84 loadCorePlugin("org.codehaus.groovy.grails.plugins.LoggingGrailsPlugin");
85 loadCorePlugin("org.codehaus.groovy.grails.plugins.CodecsGrailsPlugin");
86 loadCorePlugin("org.codehaus.groovy.grails.plugins.i18n.I18nGrailsPlugin");
87 loadCorePlugin("org.codehaus.groovy.grails.plugins.datasource.DataSourceGrailsPlugin");
88 loadCorePlugin("org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin");
89 loadCorePlugin("org.codehaus.groovy.grails.plugins.web.ServletsGrailsPlugin");
90 loadCorePlugin("org.codehaus.groovy.grails.plugins.web.ControllersGrailsPlugin");
91 loadCorePlugin("org.codehaus.groovy.grails.plugins.web.mapping.UrlMappingsGrailsPlugin");
92 loadCorePlugin("org.codehaus.groovy.grails.plugins.web.filters.FiltersGrailsPlugin");
93 loadCorePlugin("org.codehaus.groovy.grails.plugins.web.mimes.MimeTypesGrailsPlugin");
94 loadCorePlugin("org.codehaus.groovy.grails.plugins.webflow.WebFlowGrailsPlugin");
95 loadCorePlugin("org.codehaus.groovy.grails.plugins.orm.hibernate.HibernateGrailsPlugin");
96 loadCorePlugin("org.codehaus.groovy.grails.plugins.services.ServicesGrailsPlugin");
97 loadCorePlugin("org.codehaus.groovy.grails.plugins.converters.ConvertersGrailsPlugin");
98 loadCorePlugin("org.codehaus.groovy.grails.plugins.scaffolding.ScaffoldingGrailsPlugin");
101 private void loadCorePluginsFromResources(Resource
[] resources
)
104 LOG
.debug("Attempting to load [" + resources
.length
+ "] core plugins");
105 for (int i
= 0; i
< resources
.length
; i
++) {
106 Resource resource
= resources
[i
];
107 String url
= resource
.getURL().toString();
108 int packageIndex
= url
.indexOf("org/codehaus/groovy/grails");
109 url
= url
.substring(packageIndex
, url
.length());
110 url
= url
.substring(0, url
.length() - 6);
111 String className
= url
.replace('/', '.');
113 loadCorePlugin(className
);
117 private Class
attemptCorePluginClassLoad(String pluginClassName
) {
119 return application
.getClassLoader().loadClass(pluginClassName
);
120 } catch (ClassNotFoundException e
) {
121 LOG
.warn("[GrailsPluginManager] Core plugin [" + pluginClassName
122 + "] not found, resuming load without..");
123 if (LOG
.isDebugEnabled())
124 LOG
.debug(e
.getMessage(), e
);
129 private void loadCorePlugin(String pluginClassName
) {
130 Class pluginClass
= attemptCorePluginClassLoad(pluginClassName
);
132 if (pluginClass
!= null) {
133 addPlugin(pluginClass
);
137 private void addPlugin(Class plugin
) {
138 foundPluginClasses
.add(plugin
);