GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / commons / org / codehaus / groovy / grails / plugins / GrailsPluginManagerFactoryBean.java
blob884ccc440bce8d8c29c66f1426d1cc524c9d2e07
1 /*
2 * Copyright 2004-2006 Graeme Rocher
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.codehaus.groovy.grails.plugins;
18 import groovy.lang.GroovyClassLoader;
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.dom4j.Document;
23 import org.dom4j.Node;
24 import org.dom4j.io.SAXReader;
25 import org.springframework.beans.factory.FactoryBean;
26 import org.springframework.beans.factory.InitializingBean;
27 import org.springframework.beans.BeansException;
28 import org.springframework.context.ApplicationContextAware;
29 import org.springframework.context.ApplicationContext;
30 import org.springframework.core.io.Resource;
31 import org.springframework.core.io.ResourceLoader;
33 import java.io.InputStream;
34 import java.util.ArrayList;
35 import java.util.Iterator;
36 import java.util.List;
38 /**
39 * A factory bean for loading the GrailsPluginManager instance
41 * @author Graeme Rocher
42 * @since 0.4
45 public class GrailsPluginManagerFactoryBean implements FactoryBean, InitializingBean, ApplicationContextAware {
48 private GrailsApplication application;
49 private GrailsPluginManager pluginManager;
50 private static final Log LOG = LogFactory.getLog(GrailsPluginManagerFactoryBean.class);
51 private Resource descriptor;
52 private ResourceLoader resourceLoader;
53 private ApplicationContext applicationContext;
56 /**
57 * @param application the application to set
59 public void setApplication(GrailsApplication application) {
60 this.application = application;
64 public Object getObject() throws Exception {
65 return this.pluginManager;
68 public Class getObjectType() {
69 return GrailsPluginManager.class;
72 public boolean isSingleton() {
73 return true;
76 public void afterPropertiesSet() throws Exception {
77 this.pluginManager = PluginManagerHolder.getPluginManager();
79 if(pluginManager == null) {
80 if(descriptor == null) throw new IllegalStateException("Cannot create PluginManager, /WEB-INF/grails.xml not found!");
82 GroovyClassLoader classLoader = application.getClassLoader();
83 List classes = new ArrayList();
84 SAXReader reader = new SAXReader();
85 InputStream inputStream = null;
87 try {
88 inputStream = descriptor.getInputStream();
89 Document doc = reader.read(inputStream);
90 List grailsClasses = doc.selectNodes("/grails/plugins/plugin");
91 for (Iterator i = grailsClasses.iterator(); i.hasNext();) {
92 Node node = (Node) i.next();
93 final String pluginName = node.getText();
94 classes.add(classLoader.loadClass(pluginName));
96 } finally {
97 if(inputStream!=null)
98 inputStream.close();
101 Class[] loadedPlugins = (Class[])classes.toArray(new Class[classes.size()]);
103 pluginManager = new DefaultGrailsPluginManager(loadedPlugins, application);
104 pluginManager.setApplicationContext(applicationContext);
105 PluginManagerHolder.setPluginManager(pluginManager);
106 pluginManager.loadPlugins();
108 this.pluginManager.setApplication(application);
109 this.pluginManager.doArtefactConfiguration();
110 application.initialise();
113 public void setGrailsDescriptor(Resource grailsDescriptor) {
114 this.descriptor = grailsDescriptor;
117 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
118 this.applicationContext = applicationContext;